-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
42 lines (38 loc) · 1.24 KB
/
js.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
let x = 0,
y = 0,
dirX = 1,
dirY = 1;
const speed = 2;
const pallete = ["#ff8800", "#e124ff", "#6a19ff", "#ff2188"];
let dvd = document.getElementById("dvd");
dvd.style.backgroundColor = pallete[0];
let prevColorChoiceIndex = 0;
let black = document.getElementById("black");
const dvdWidth = dvd.clientWidth;
const dvdHeight = dvd.clientHeight;
function getNewRandomColor() {
const currentPallete = [...pallete]
currentPallete.splice(prevColorChoiceIndex,1)
const colorChoiceIndex = Math.floor(Math.random() * currentPallete.length);
prevColorChoiceIndex = colorChoiceIndex<prevColorChoiceIndex?colorChoiceIndex:colorChoiceIndex+1;
const colorChoice = currentPallete[colorChoiceIndex];
return colorChoice;
}
function animate() {
const screenHeight = document.body.clientHeight;
const screenWidth = document.body.clientWidth;
if (y + dvdHeight >= screenHeight || y < 0) {
dirY *= -1;
dvd.style.backgroundColor = getNewRandomColor();
}
if (x + dvdWidth >= screenWidth || x < 0) {
dirX *= -1;
dvd.style.backgroundColor = getNewRandomColor();
}
x += dirX * speed;
y += dirY * speed;
dvd.style.left = x + "px";
dvd.style.top = y + "px";
window.requestAnimationFrame(animate);
}
window.requestAnimationFrame(animate);