-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
59 lines (53 loc) · 1.44 KB
/
view.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
import { Audio } from "./audio.js";
export class View {
constructor(canvas) {
this.canvas = canvas;
this.clicks = [];
this.frameRate = 1000 / 30;
this.loopRate = 8000;
this.maxRadius = 80;
}
handleClick(event) {
const view = this;
let x = event.offsetX;
let y = event.offsetY;
let pos = view.clicks.push({ x: x, y: y, radius: 0 });
Audio.play(x % 10);
setInterval(() => {
view.clicks[pos - 1].radius = 0;
Audio.play(x % 10);
}, view.loopRate);
}
updateDisplay() {
const view = this;
const context = view.canvas.getContext("2d");
context.clearRect(0, 0, view.canvas.width, view.canvas.height);
context.fillStyle = "black";
context.fillRect(0, 0, view.canvas.width, view.canvas.height);
for (let i = 0; i < view.clicks.length; i++) {
let circle = view.clicks[i];
if (circle.radius > view.maxRadius) continue;
circle.radius += 1;
let alpha = 0.7;
if (circle.radius > view.maxRadius - 15) {
alpha = (view.maxRadius - circle.radius) / 15;
}
view.drawCircle(context, circle.x, circle.y, circle.radius, alpha);
}
}
drawCircle(context, x, y, radius, alpha) {
context.beginPath();
context.arc(x, y, radius, 0, 2 * Math.PI);
context.fillStyle =
"rgba(" +
(x % 256) +
", " +
(y % 256) +
", " +
((x * y) % 256) +
" ," +
alpha +
")";
context.fill();
}
}