-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
25 lines (19 loc) · 805 Bytes
/
Copy pathscript.js
File metadata and controls
25 lines (19 loc) · 805 Bytes
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
const VELOCITY = 5;
let direction = { x: 1, y: 1 };
const dvd = document.getElementById("dvd");
const getRandomInt = (max) => {
return Math.floor(Math.random() * max);
}
dvd.style.left = getRandomInt(window.innerWidth) + "px";
dvd.style.top = getRandomInt(window.innerHeight) + "px";
setInterval(() => {
const dvdBounding = dvd.getBoundingClientRect();
if (direction.x > 0 && dvdBounding.right > window.innerWidth || direction.x < 0 && dvdBounding.left < 0) {
direction.x *= -1;
}
if (direction.y > 0 && dvdBounding.bottom > window.innerHeight || direction.y < 0 && dvdBounding.top < 0) {
direction.y *= -1;
}
dvd.style.left = parseInt(dvd.style.left) + direction.x * VELOCITY + "px";
dvd.style.top = parseInt(dvd.style.top) + direction.y * VELOCITY + "px";
}, 1000 / 60);