-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble.js
53 lines (46 loc) · 1.63 KB
/
Bubble.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
var x = 0;
var y = 0;
class Bubble {
constructor() {
let element = document.createElement("div");
element.id = "bubble";
element.style.display = "none";
document.body.appendChild(element);
this.msgdone = true;
document.getElementById("bubble").style.display = "none";
}
update() {
let element = document.getElementById("bubble");
if (garfield.x > window.innerWidth - 500) {
element.className = "xreverse";
element.style = "left: "+(garfield.x+25-500)+"px; top: "+Math.max(0, garfield.y-290)+"px;";
} else {
element.className = "";
element.style = "left: "+(garfield.x+25)+"px; top: "+Math.max(0, garfield.y-290)+"px;";
}
if (this.msgdone) bubble.hide();
}
hide() {
this.msgdone = true;
document.getElementById("bubble").style.display = "none";
}
show(text) {
if (!this.msgdone) return;
this.msgdone = false;
let size = 100/(Math.log(text.length) + 1) + "px";
document.getElementById("bubble").innerHTML = "<span class='inner' style='font-size:"+size+"'>"+text+"</span>";
document.getElementById("bubble").hidden = false;
var msg = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(msg);
if(window.speechSynthesis.getVoices().length > 0) {
msg.onend = () => {
this.msgdone = true;
bubble.hide();
}
} else {
setTimeout(function() { bubble.hide(); }, text.length * 100);
}
}
}
let bubble = new Bubble();
bubble.hide();