-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (111 loc) · 3.51 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const NUM_BALLS = 12;
window.onload = function () {
for (let i = 0; i < NUM_BALLS; i++) {
var newDomBall = document.createElement("div");
newDomBall.classList.add("ball");
document.body.appendChild(newDomBall);
}
moveBalls(document.getElementsByClassName("ball"));
};
var balls = [];
const Xmin = 0;
const Xmax = screen.availWidth - 100;
const Ymin = 0;
const Ymax = screen.availHeight - 150;
var cursor = { x: -1000, y: -1000 };
document.onmousemove = (e) => {
cursor.x = e.pageX;
cursor.y = e.pageY;
};
function moveBalls(ballElements) {
for (let i = 0; i < ballElements.length; i++) {
balls.push({
x: Math.random() * (Xmax - 200),
y: Math.random() * (Ymax - 200),
reverse: {
x: false,
y: false
},
velocity: {
x: 2 ^ (Math.random() * 1 + 5),
y: Math.random() * 1 + 5
}
});
setInterval(() => {
document.getElementsByClassName("ball")[i].style.background = `rgb(${
255 - (Xmax - balls[i].x < 125 ? Xmax - balls[i].x : balls[i].x)
}, ${255 - balls[i].y}, ${255 - (Ymax - balls[i].y)})`;
balls[i].reverse.x =
balls[i].x >= Xmax || balls[i].x <= Xmin
? !balls[i].reverse.x
: balls[i].reverse.x;
balls[i].reverse.y =
balls[i].y >= Ymax || balls[i].y <= Ymin
? !balls[i].reverse.y
: balls[i].reverse.y;
const original = { x: balls[i].x, y: balls[i].y };
balls[i].x = balls[i].reverse.x
? balls[i].x + balls[i].velocity.x
: balls[i].x - balls[i].velocity.x;
balls[i].y = balls[i].reverse.y
? balls[i].y + balls[i].velocity.y
: balls[i].y - balls[i].velocity.y;
balls.map((otherBall, index) => {
if (
index !== i &&
balls[i].y >= otherBall.y &&
balls[i].y <= otherBall.y + 100 &&
balls[i].x >= otherBall.x &&
balls[i].x <= otherBall.x + 100
) {
balls[i].reverse.x = !otherBall.reverse.x;
balls[i].reverse.y = !otherBall.reverse.y;
otherBall.reverse.x = !balls[i].reverse.x;
otherBall.reverse.y = !balls[i].reverse.y;
balls[i].x = original.x;
balls[i].y = original.y;
}
if (
balls[i].y >= cursor.y - 100 &&
balls[i].y <= cursor.y + 100 &&
balls[i].x >= cursor.x - 100 &&
balls[i].x <= cursor.x + 100
) {
if (
(balls[i].x >= cursor.x && !balls[i].reverse.x) ||
(balls[i].x <= cursor.x && balls[i].reverse.x)
) {
balls[i].reverse.x = !balls[i].reverse.x;
}
if (
(balls[i].y >= cursor.y && !balls[i].reverse.y) ||
(balls[i].y <= cursor.y && balls[i].reverse.y)
) {
balls[i].reverse.y = !balls[i].reverse.y;
}
}
});
if (
balls[i].y >= Ymax - 50 ||
balls[i].y <= Ymin ||
balls[i].x >= Xmax ||
balls[i].x <= Xmin
) {
balls[i].velocity.y = Math.random() * 1 + 5;
balls[i].velocity.x = Math.random() * 1 + 5;
}
if (balls[i].x >= Xmax) {
balls[i].x = Xmax;
} else if (balls[i].x <= Xmin) {
balls[i].x = Xmin;
}
if (balls[i].y >= Ymax) {
balls[i].y = Ymax;
} else if (balls[i].y <= Ymin) {
balls[i].y = Ymin;
}
document.getElementsByClassName("ball")[i].style.left = balls[i].x + "px";
document.getElementsByClassName("ball")[i].style.top = balls[i].y + "px";
}, 10);
}
}