-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
78 lines (65 loc) · 2.16 KB
/
Copy pathindex.html
File metadata and controls
78 lines (65 loc) · 2.16 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Prank App</title>
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: black;
}
.floaty {
position: absolute;
width: 150px;
user-select: none;
cursor: pointer;
}
</style>
</head>
<body>
<script>
const imgSrc = "prank.png"; // replace with your image
function spawnImage(x, y, size) {
const img = document.createElement("img");
img.src = imgSrc;
img.className = "floaty";
img.style.width = size + "px";
document.body.appendChild(img);
// Randomly set initial velocity (speed and direction)
let vx = (Math.random() * 2 + 1) * (Math.random() < 0.5 ? -1 : 1);
let vy = (Math.random() * 2 + 1) * (Math.random() < 0.5 ? -1 : 1);
img.style.left = x + "px";
img.style.top = y + "px";
function move() {
const rect = img.getBoundingClientRect();
// **Bounce Logic:** Reverses direction (velocity) upon hitting a boundary
if (rect.left <= 0 || rect.right >= window.innerWidth) vx = -vx;
if (rect.top <= 0 || rect.bottom >= window.innerHeight) vy = -vy;
// Apply movement
img.style.left = rect.left + vx + "px";
img.style.top = rect.top + vy + "px";
requestAnimationFrame(move);
}
move();
img.addEventListener("click", () => {
const rect = img.getBoundingClientRect();
const newSize = size * 0.9;
img.remove();
// When split, the new images inherit the current position but get NEW random directions (via the call to spawnImage)
spawnImage(rect.left, rect.top, newSize);
spawnImage(rect.left + 20, rect.top + 20, newSize);
});
}
window.onload = () => {
const centerX = window.innerWidth / 2;
const centerY = window.innerHeight / 2;
spawnImage(centerX - 75, centerY - 75, 150);
};
</script>
</body>
</html>