-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.js
70 lines (70 loc) · 2.4 KB
/
sample.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
class MyClass {
constructor() {
this.enabled = true;
this.paused = false;
//box variables
this.boxX = 20;
this.boxY = 20;
this.colorChangeCounter = 0;
this.currentColor = 'lightgreen';
this.canvas = document.getElementById('my-canvas');
this.ctx = this.canvas.getContext('2d');
}
runAnimation() {
if (this.paused)
return;
this.boxX += 1;
if (this.boxX > 300)
this.boxX = 0;
this.ctx.clearRect(0, 0, 300, 300);
this.ctx.fillStyle = this.currentColor;
this.ctx.fillRect(this.boxX, this.boxY, 50, 50);
this.drawText('COLOR: ' + this.currentColor, 20, 230, 'purple');
if (this.enabled)
window.requestAnimationFrame(window["myCanvasApp"].runAnimation.bind(this));
}
drawText(text, x, y, color) {
// this.ctx.globalAlpha = 0.5;
this.ctx.font = "16px Comic Sans MS";
this.ctx.fillStyle = color;
this.ctx.fillText(text, x, y);
this.ctx.globalAlpha = 1;
}
pause() {
if (this.paused) {
this.paused = false;
document.getElementById('btnPause').innerText = 'Pause';
this.runAnimation();
}
else {
this.paused = true;
document.getElementById('btnPause').innerText = 'Resume';
}
}
}
//clear out old canvases
document.getElementById('divOutput').innerHTML = '';
if (window["myCanvasApp"])
window["myCanvasApp"].enabled = false;
//create canvas
const canvas = document.createElement("canvas");
canvas.id = "my-canvas";
canvas.style.backgroundColor = "#eeeedd";
canvas.style.width = "300px";
canvas.width = 300;
canvas.height = 300;
canvas.style.imageRendering = 'pixelated';
document.getElementById('divOutput').appendChild(canvas);
const br = document.createElement("br");
document.getElementById('divOutput').appendChild(br);
const button = document.createElement("button");
button.classList.add("btn", "btn-primary");
button.id = "btnPause";
button.innerText = "Pause";
button.style.marginLeft = '20px';
button.style.marginTop = '30px';
button.onclick = () => { window["myCanvasApp"].pause(); };
document.getElementById('divOutput').appendChild(button);
window["myCanvasApp"] = new MyClass();
window["myCanvasApp"].runAnimation();
//# sourceMappingURL=sample.js.map