-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounce.html
123 lines (112 loc) · 4.09 KB
/
bounce.html
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
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
<style>
.panel {
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="panel-container">
<div class="panel"></div>
<div class="panel"></div>
<div class="panel"></div>
<div class="panel"></div>
</div>
<script>
const panelContainer = document.getElementById("panel-container");
const panels = document.querySelectorAll(".panel");
for (let i = 0; i < 100; i++) {
const panel = document.createElement("div");
panel.classList.add("panel");
// Set the panel's background color to a random color
panel.style.backgroundColor = `rgb(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255})`;
// Create a function and object to generate a unique random movement for the panel
function generateRandomMovement(panel)
{
const movement = {
x: Math.random() * 4 - 2,
y: Math.random() * 4 - 2,
};
// Update the panel's position every few seconds
setInterval(() => {
let x = panel.offsetLeft + movement.x;
let y = panel.offsetTop + movement.y;
// Check if the panel is colliding with any other panels
for (const otherPanel of panels) {
if (panel !== otherPanel) {
const isOverlapping = !(
panel.offsetLeft + panel.offsetWidth < otherPanel.offsetLeft ||
panel.offsetTop + panel.offsetHeight < otherPanel.offsetTop ||
panel.offsetLeft > otherPanel.offsetLeft + otherPanel.offsetWidth ||
panel.offsetTop > otherPanel.offsetTop + otherPanel.offsetHeight
);
if (isOverlapping) {
// Check if the panel is already colliding with the other panel
let isAlreadyColliding = false;
for (const collider of panels) {
if (collider !== panel && collider !== otherPanel) {
if (
!(
collider.offsetLeft + collider.offsetWidth < panel.offsetLeft ||
collider.offsetTop + collider.offsetHeight < panel.offsetTop ||
collider.offsetLeft > panel.offsetLeft + panel.offsetWidth ||
collider.offsetTop > panel.offsetTop + panel.offsetHeight
)
) {
isAlreadyColliding = true;
break;
}
}
}
if (!isAlreadyColliding) {
// Reverse the movement of the panel
movement.x = -movement.x;
movement.y = -movement.y;
break;
}
}
}
}
// Ensure that the panel stays within the bounds of the visible screen
if (x < 0) {
x = 0;
movement.x = Math.abs(movement.x);
}
if (x > window.innerWidth - 100) {
x = window.innerWidth - 100;
movement.x = -Math.abs(movement.x);
}
if (y < 0) {
y = 0;
movement.y = Math.abs(movement.y);
}
if (y > window.innerHeight - 100) {
y = window.innerHeight - 100;
movement.y = -Math.abs(movement.y);
}
panel.style.top = y + "px";
panel.style.left = x + "px";
}, 30);
}
generateRandomMovement(panel);
// Add randomness and organic movement to the panels using the sin function
let x = Math.sin(i / 5) * 100 + Math.random() * 200;
let y = Math.cos(i / 5) * 100 + Math.random() * 200;
// Ensure that the panel stays within the bounds of the visible screen
if (x < 0) x = 0;
if (x > window.innerWidth - 100) x = window.innerWidth - 100;
if (y < 0) y = 0;
if (y > window.innerHeight - 100) y = window.innerHeight - 100;
panel.style.top = y + "px";
panel.style.left = x + "px";
panelContainer.appendChild(panel);
}
</script>
</body>
</html>