-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimatedBoxes.html
86 lines (74 loc) · 2.42 KB
/
animatedBoxes.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
<!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");
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;
// 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>