-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
69 lines (63 loc) · 1.92 KB
/
popup.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
class Popup {
constructor(p5, x, y, font, timer = 2000) { // 2 secs
this.msg = 'N/A';
this._timer = timer;
this.p5 = p5;
this.x = x;
this.y = y;
this.alpha = 0;
this.active = false;
this.fontSize = 25;
this.font = font;
}
reset() {
this.timer = this._timer;
this.alpha = 0;
this.active = false;
}
setMsg(msg) {
this.msg = msg;
let bounds = this.font.textBounds(msg, this.x, this.y, this.fontSize);
this.w = bounds.w + 30;
this.h = bounds.h + 30;
return this;
}
start() {
this.reset(); // if this is called while a timer is already running, turn it off and display the new msg
this.active = true;
}
update() {
if (!this.active) {
return;
}
if (this.timer > 0) {
if (this.timer >= this._timer * 4 / 5) {
this.alpha += this.p5.deltaTime / 1000 * 3;
this.alpha = Math.min(1, this.alpha);
} else if (this.timer <= this._timer / 5) {
this.alpha -= this.p5.deltaTime / 1000 * 3;
this.alpha = Math.max(0, this.alpha);
}
this.timer -= this.p5.deltaTime;
} else {
this.reset();
}
}
draw() {
if (this.active) {
let a = this.p5.map(this.alpha, 0, 1, 0, 255);
this.p5.push();
this.p5.strokeWeight(2);
this.p5.stroke(255, 189, 27, a);
this.p5.rectMode(this.p5.CENTER);
this.p5.fill(231, 84, 128, a * 0.7);
this.p5.rect(this.x, this.y, this.w, this.h, 3);
this.p5.textSize(this.fontSize);
this.p5.noStroke();
this.p5.fill(0, 0, 0, a);
this.p5.textAlign(this.p5.CENTER);
this.p5.text(this.msg, this.x, this.y);
this.p5.pop();
}
}
}