-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbles.html
More file actions
268 lines (234 loc) · 7.24 KB
/
bubbles.html
File metadata and controls
268 lines (234 loc) · 7.24 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bubble Canvas</title>
<link rel="icon" type="image/png" href="./nano.png" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Quicksand:wght@400;700&display=swap"
rel="stylesheet"
/>
<style>
:root {
color-scheme: light;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
background: #000000;
font-family: "Quicksand", sans-serif;
}
#canvas {
position: relative;
width: 100%;
height: 100%;
}
.bubble {
position: absolute;
width: 500px;
height: 180px;
border-radius: 999px;
border: 2px solid #d783a9;
background: #efacc8;
color: #6f425b;
text-decoration: none;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
padding: 0 16px;
font-size: 2.6rem;
font-weight: 600;
line-height: 1.2;
backdrop-filter: blur(2px);
cursor: pointer;
user-select: none;
}
.bubble:hover,
.bubble:focus-visible {
background: #f4b7d0;
outline: none;
}
</style>
</head>
<body>
<main id="canvas" aria-label="Animated bubble links"></main>
<script>
const BUBBLES = [
{
text: "Brookline Town Meeting Votes",
url: "https://millxing.github.io/TM/TMMvotes.html",
},
{
text: "Extra Pass Anaytics",
url: "https://extrapass.onrender.com/",
},
{
text: "Washington Street Journal",
url: "https://millxing.github.io/BF/",
},
];
const BUBBLE_WIDTH = 500;
const BUBBLE_HEIGHT = 180;
const MIN_SPEED = 24; // px per second
const MAX_SPEED = 44; // px per second
const canvas = document.getElementById("canvas");
const bubbles = [];
let lastTime = 0;
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function overlapsAtPosition(x, y, other) {
return (
x < other.x + BUBBLE_WIDTH &&
x + BUBBLE_WIDTH > other.x &&
y < other.y + BUBBLE_HEIGHT &&
y + BUBBLE_HEIGHT > other.y
);
}
function createBubble(bubbleData) {
const link = document.createElement("a");
link.className = "bubble";
link.href = bubbleData.url;
link.target = "_blank";
link.rel = "noopener noreferrer";
link.textContent = bubbleData.text;
link.setAttribute("aria-label", bubbleData.text);
link.addEventListener("click", (event) => {
event.preventDefault();
window.open(bubbleData.url, "_blank", "noopener,noreferrer");
});
canvas.appendChild(link);
const maxX = Math.max(0, window.innerWidth - BUBBLE_WIDTH);
const maxY = Math.max(0, window.innerHeight - BUBBLE_HEIGHT);
const angle = randomInRange(0, Math.PI * 2);
const speed = randomInRange(MIN_SPEED, MAX_SPEED);
let x = 0;
let y = 0;
let placed = false;
for (let attempts = 0; attempts < 150; attempts += 1) {
const candidateX = randomInRange(0, maxX);
const candidateY = randomInRange(0, maxY);
const collision = bubbles.some((bubble) =>
overlapsAtPosition(candidateX, candidateY, bubble)
);
if (!collision) {
x = candidateX;
y = candidateY;
placed = true;
break;
}
}
if (!placed) {
x = randomInRange(0, maxX);
y = randomInRange(0, maxY);
}
bubbles.push({
el: link,
x,
y,
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed,
});
}
function resolveBubbleCollisions() {
const maxX = Math.max(0, window.innerWidth - BUBBLE_WIDTH);
const maxY = Math.max(0, window.innerHeight - BUBBLE_HEIGHT);
const epsilon = 0.1;
for (let i = 0; i < bubbles.length; i += 1) {
for (let j = i + 1; j < bubbles.length; j += 1) {
const a = bubbles[i];
const b = bubbles[j];
const centerDx = (a.x + BUBBLE_WIDTH / 2) - (b.x + BUBBLE_WIDTH / 2);
const centerDy = (a.y + BUBBLE_HEIGHT / 2) - (b.y + BUBBLE_HEIGHT / 2);
const overlapX = BUBBLE_WIDTH - Math.abs(centerDx);
const overlapY = BUBBLE_HEIGHT - Math.abs(centerDy);
if (overlapX <= 0 || overlapY <= 0) {
continue;
}
if (overlapX < overlapY) {
const direction = centerDx === 0 ? (Math.random() < 0.5 ? -1 : 1) : Math.sign(centerDx);
const shift = overlapX / 2 + epsilon;
a.x += shift * direction;
b.x -= shift * direction;
const swap = a.vx;
a.vx = b.vx;
b.vx = swap;
} else {
const direction = centerDy === 0 ? (Math.random() < 0.5 ? -1 : 1) : Math.sign(centerDy);
const shift = overlapY / 2 + epsilon;
a.y += shift * direction;
b.y -= shift * direction;
const swap = a.vy;
a.vy = b.vy;
b.vy = swap;
}
a.x = clamp(a.x, 0, maxX);
a.y = clamp(a.y, 0, maxY);
b.x = clamp(b.x, 0, maxX);
b.y = clamp(b.y, 0, maxY);
}
}
}
function updateBubbles(timestamp) {
if (!lastTime) {
lastTime = timestamp;
}
const dt = (timestamp - lastTime) / 1000;
lastTime = timestamp;
const maxX = Math.max(0, window.innerWidth - BUBBLE_WIDTH);
const maxY = Math.max(0, window.innerHeight - BUBBLE_HEIGHT);
for (const bubble of bubbles) {
bubble.x += bubble.vx * dt;
bubble.y += bubble.vy * dt;
if (bubble.x <= 0) {
bubble.x = 0;
bubble.vx = Math.abs(bubble.vx);
} else if (bubble.x >= maxX) {
bubble.x = maxX;
bubble.vx = -Math.abs(bubble.vx);
}
if (bubble.y <= 0) {
bubble.y = 0;
bubble.vy = Math.abs(bubble.vy);
} else if (bubble.y >= maxY) {
bubble.y = maxY;
bubble.vy = -Math.abs(bubble.vy);
}
}
// Multiple passes reduce lingering overlap on multi-bubble contacts.
for (let pass = 0; pass < 3; pass += 1) {
resolveBubbleCollisions();
}
for (const bubble of bubbles) {
bubble.el.style.transform = `translate(${bubble.x}px, ${bubble.y}px)`;
}
requestAnimationFrame(updateBubbles);
}
function handleResize() {
const maxX = Math.max(0, window.innerWidth - BUBBLE_WIDTH);
const maxY = Math.max(0, window.innerHeight - BUBBLE_HEIGHT);
for (const bubble of bubbles) {
bubble.x = clamp(bubble.x, 0, maxX);
bubble.y = clamp(bubble.y, 0, maxY);
}
}
for (const bubbleData of BUBBLES) {
createBubble(bubbleData);
}
window.addEventListener("resize", handleResize);
requestAnimationFrame(updateBubbles);
</script>
</body>
</html>