-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.js
43 lines (34 loc) · 1.04 KB
/
magic.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
document.addEventListener("DOMContentLoaded", function() {
var balls = [];
var numberOfBalls = 20;
function Ball() {
var ball = create();
this.element = ball;
this.dx = Math.random() * 4 + 1; // Random speed between 1 and 5
this.dy = Math.random() * 4 + 1; // Random speed between 1 and 5
}
Ball.prototype.updatePosition = function() {
var x = this.element.x;
var y = this.element.y;
x += this.dx;
y += this.dy;
if (x <= 0 || x >= window.innerWidth - 50) {
this.dx = -this.dx;
}
if (y <= 0 || y >= window.innerHeight - 50) {
this.dy = -this.dy;
}
move(this.element, this.dx, this.dy);
this.element.x = x;
this.element.y = y;
}
for (var i = 0; i < numberOfBalls; i++) {
balls.push(new Ball());
}
function updateAllPositions() {
balls.forEach(function(ball) {
ball.updatePosition();
});
}
setInterval(updateAllPositions, 10);
});