-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdvd.js
More file actions
62 lines (40 loc) · 1.47 KB
/
dvd.js
File metadata and controls
62 lines (40 loc) · 1.47 KB
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
const dvdFieldDOM = document.getElementById("dvd-field");
class DVD {
constructor(dvdSvgDOM) {
this.dom = dvdSvgDOM
this.coord = { x: 0, y: 0 }
this.speed = 0.26
this.rgbVec3 = [0, 0, 0]
this.previousTime = 0
this.dom.onclick = () => this.changeColor()
this.dom.ontouchstart = () => this.changeColor()
this.changeColor()
}
update(t) {
const dt = t - this.previousTime
const areaWidth = dvdFieldDOM.clientWidth - this.dom.clientWidth
const areaHeight = dvdFieldDOM.clientHeight - this.dom.clientHeight
const x = (this.coord.x + this.speed * dt) % (areaWidth * 2)
const y = (this.coord.y + this.speed * dt) % (areaHeight * 2)
if ((Math.floor(this.coord.x / areaWidth) != Math.floor(x / areaWidth))
|| (Math.floor(this.coord.y / areaHeight) != Math.floor(y / areaHeight)))
this.changeColor()
this.coord = {x, y}
this.dom.style.left = areaWidth - Math.abs(this.coord.x - areaWidth) + "px";
this.dom.style.top = areaHeight - Math.abs(this.coord.y - areaHeight) + "px";
this.previousTime = t
}
changeColor() {
this.rgbVec3 = Array(3).fill(null).map(_ => parseInt(Math.floor(Math.random() * 256), 10))
this.dom.style.fill = `rgb(${this.rgbVec3.map(int => int.toString()).join(',')})`;
}
}
function runDVD() {
const dvd = new DVD(document.getElementById("dvd"));
function loop(dt) {
dvd.update(dt)
requestAnimationFrame(loop)
}
requestAnimationFrame(loop)
}
runDVD()