-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloatingCharacter.js
132 lines (115 loc) · 4.56 KB
/
floatingCharacter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import FunnyFound from "./assets/funny-found.svg"
import FunnyReadingSVG from "./assets/funny-reading.svg"
import HappyFound from "./assets/happy-found.svg"
import HappyReadingSVG from "./assets/happy-reading.svg"
import LoveFound from "./assets/love-found.svg"
import LoveReadingSVG from "./assets/love-reading.svg"
import SadFound from "./assets/sad-found.svg"
import SadReadingSVG from "./assets/sad-reading.svg"
import ScaredFound from "./assets/scared-found.svg"
import ScaredReadingSVG from "./assets/scared-reading.svg"
import SurprisedFound from "./assets/surprised-found.svg"
import SurprisedReadingSVG from "./assets/surprised-reading.svg"
import VHappyFound from "./assets/vhappy-found.svg"
import VHappyReadingSVG from "./assets/vhappy-reading.svg"
import WaitingRead from "./assets/waiting-read.svg"
import { SpeechBubble } from "./speechBubble.js"
export class FloatingCharacter {
constructor() {
this.element = document.createElement("div")
this.element.id = "floating-circle"
this.element.style.position = "absolute"
this.element.style.width = "80px"
this.element.style.height = "80px"
this.element.style.borderRadius = "50%"
this.element.style.zIndex = "9999999999999"
this.element.style.transformOrigin = "center top"
this.element.style.top = "10px"
this.element.style.opacity = "0"
this.element.style.transform = "scale(.5)"
this.element.style.transition =
"transform .5s ease-in-out, opacity .5s ease-in-out"
document.body.appendChild(this.element)
const avatar = document.createElement("img")
avatar.src = HappyFound // Default to happy face
avatar.style.width = "100%"
avatar.style.height = "100%"
avatar.style.userSelect = "none"
this.element.appendChild(avatar)
this.avatar = avatar
this.bubble = new SpeechBubble(this) // Initialize the speech bubble
// Mood-to-image mapping
this.moods = {
funny: { image: FunnyReadingSVG, found: FunnyFound },
happy: { image: VHappyReadingSVG, found: VHappyFound },
love: { image: LoveReadingSVG, found: LoveFound },
sad: { image: SadReadingSVG, found: SadFound },
scared: { image: ScaredReadingSVG, found: ScaredFound },
surprised: { image: SurprisedReadingSVG, found: SurprisedFound },
neutral: { image: HappyReadingSVG, found: HappyFound },
waiting: { image: WaitingRead, found: WaitingRead }
}
}
async moveToElement(element) {
const rect = element.getBoundingClientRect()
const scrollTop = window.scrollY || document.documentElement.scrollTop
const scrollLeft = window.scrollX || document.documentElement.scrollLeft
this.element.style.left = `${rect.left + rect.width + scrollLeft}px`
this.element.style.top = `${rect.top + scrollTop - 10}px`
// Wait for the transition to complete
await new Promise((resolve) => setTimeout(resolve, 1000))
}
async showInCorner() {
setTimeout(() => {
this.element.style.transform = "scale(1)"
this.element.style.right = "350px"
this.element.style.top = "30px"
this.element.style.opacity = "1"
this.element.style.position = "fixed"
//this.element.style.transition = "all 1s ease-in-out"
}, 100)
await new Promise((resolve) => setTimeout(resolve, 400))
}
remove() {
if (this.element) {
this.element.remove() // Remove the DOM element
}
}
updateMood(mood, found = false, surprise = false) {
const moodData = this.moods[mood]
if (!moodData) {
console.warn(`Mood "${mood}" not found.`)
return
}
// Update the avatar image
this.avatar.src = found ? moodData.found : moodData.image
// Add "jump" animation if the mood is surprise
if (surprise) {
this.element.style.transition = "transform 0.2s ease-in-out"
this.element.style.transform = "scale(1.2)"
// Reset to normal scale after the jump
setTimeout(() => {
this.element.style.transform = "scale(1)"
this.element.style.transition = "all 1s ease-in-out"
}, 200)
}
}
ready() {
this.updateMood("happy", false)
this.element.style.position = "absolute"
this.element.style.right = "auto"
this.element.style.left = "auto"
this.element.style.transition = "all 1s ease-in-out"
}
async speak(message, action = "OK") {
await this.bubble.show(message, action)
}
async showWithChoices(message, choices) {
const result = await this.bubble.showWithChoices(message, choices)
return result
}
async showWithInput(message, placeholder) {
const result = await this.bubble.showWithInput(message, placeholder)
return result
}
}