-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 1.41 KB
/
index.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
const canvas = document.querySelector("canvas");
let image = new Image;
image.src = "https://www.bouncingdvdlogo.com/logos/dvdlogo-02.svg";
let cd = canvas.getContext("2d");
let width = window.innerWidth;
let height = window.innerHeight;
let x = 0;
let y = 0;
let turn = "top-left";
canvas.width = width;
canvas.height = height;
function animate() {
requestAnimationFrame(animate);
checkTurn();
cd.clearRect(0,0,canvas.width,canvas.height);
if(y > height - 100 && x > width - 200){
turn = "bottom-right";
random();
}
else if(y < 0 && x === 1080){
turn = "top-right";
random();
}
else if(y > height - 100 && x < 0){
turn = "bottom-left";
random();
}
else if(y < 0 && x === 0){
turn = "top-left";
random();
}
cd.drawImage(image,x,y,200,100);
}
animate();
function checkTurn() {
switch(turn) {
case "top-left":
x ++;
y += 0.5;
break;
case "bottom-right":
x = 1080;
y -= 1;
break;
case "top-right":
x--;
y += 0.5;
break;
case "bottom-left":
x = 0;
y -= 1;
break;
}
}
function random() {
let randomColor = Math.floor(Math.random() * 6) + 1;
let imageSrc = "https://www.bouncingdvdlogo.com/logos/dvdlogo-0" + randomColor + ".svg";
image.src = imageSrc;
}