-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
344 lines (303 loc) · 12.9 KB
/
index.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html lang="en">
<head>
<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=Bebas+Neue&display=swap" rel="stylesheet">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Bouncing Balls with Flinging and Header Text</title>
<style>
body, html {
margin: 0;
height: 100%;
overflow: hidden;
}
canvas {
display: block;
background-color: transparent;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d', { willReadFrequently: true });
function resizeCanvas() {
// Resize the canvas to fit the window
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
textCanvas.width = canvas.width;
textCanvas.height = canvas.height;
textPosition.x = 10;
textPosition.y = textCanvas.height - 50;
adjustFontSize();
drawText();
}
function adjustFontSize() {
// Adjust the font size of the text to fit the canvas width
const maxFontSize = 100;
let fontSize = maxFontSize;
textCtx.font = `${fontSize}px "Bebas Neue", sans-serif`;
let textWidth = textCtx.measureText(text).width;
while (textWidth > textCanvas.width - 20 && fontSize > 10) {
fontSize -= 1;
textCtx.font = `${fontSize}px "Bebas Neue"`;
textWidth = textCtx.measureText(text).width;
}
}
window.addEventListener('resize', resizeCanvas);
let balls = [];
const numSmallBalls = 10; // Number of balls
let isDragging = false;
let dragBall = null;
let lastMouseX, lastMouseY;
const friction = 0.99; // Friction coefficient
const text = "pbritton.dev"; // Text to display
const textCanvas = document.createElement('canvas');
const textCtx = textCanvas.getContext('2d', { willReadFrequently: true });
textCanvas.width = canvas.width;
textCanvas.height = canvas.height;
let textPosition = {
x: 10,
y: canvas.height - 50
};
function drawText() {
// Draw the text on the textCanvas
textCtx.clearRect(0, 0, textCanvas.width, textCanvas.height);
textCtx.textAlign = 'left';
textCtx.textBaseline = 'bottom';
textCtx.fillStyle = '#003865';
textCtx.font = '6.25rem "Bebas Neue"';
textCtx.fillText(text, textPosition.x, textPosition.y);
}
resizeCanvas();
const colors = ['#003865', '#78BE21', '#5D295F', '#000', '#002469', '#007336'];
for (let i = 0; i < numSmallBalls; i++) {
// Initialize balls with random positions and velocities
const color = colors[i % colors.length];
balls.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: 10 + Math.random() * 10,
color: color,
vx: (Math.random() - 0.5) * 4,
vy: (Math.random() - 0.5) * 4,
isMoving: true
});
}
function handleMouseDown(event) {
event.preventDefault();
const mouseX = event.clientX || event.touches[0].clientX;
const mouseY = event.clientY || event.touches[0].clientY;
// Check if a ball is clicked and enable dragging
balls.forEach(ball => {
if (Math.hypot(ball.x - mouseX, ball.y - mouseY) <= ball.radius) {
isDragging = true;
dragBall = ball;
lastMouseX = mouseX;
lastMouseY = mouseY;
}
});
}
function handleMouseMove(event) {
event.preventDefault();
const mouseX = event.clientX || event.touches[0].clientX;
const mouseY = event.clientY || event.touches[0].clientY;
// Update ball position while dragging
if (isDragging) {
dragBall.x = mouseX;
dragBall.y = mouseY;
dragBall.vx = mouseX - lastMouseX;
dragBall.vy = mouseY - lastMouseY;
dragBall.isMoving = true;
lastMouseX = mouseX;
lastMouseY = mouseY;
}
}
function handleMouseUp(event) {
event.preventDefault();
isDragging = false;
dragBall = null;
}
canvas.addEventListener('mousedown', handleMouseDown);
canvas.addEventListener('mousemove', handleMouseMove);
canvas.addEventListener('mouseup', handleMouseUp);
canvas.addEventListener('touchstart', handleMouseDown, { passive: false });
canvas.addEventListener('touchmove', handleMouseMove, { passive: false });
canvas.addEventListener('touchend', handleMouseUp);
function drawBalls() {
// Clear the canvas and draw all balls
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(textCanvas, 0, 0);
balls.forEach(ball => {
ctx.fillStyle = ball.color;
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fill();
});
}
function detectCollisions() {
// Detect collisions between balls
for (let i = 0; i < balls.length; i++) {
for (let j = i + 1; j < balls.length; j++) {
const ball1 = balls[i];
const ball2 = balls[j];
const dx = ball2.x - ball1.x;
const dy = ball2.y - ball1.y;
const distance = Math.hypot(dx, dy);
const minDistance = ball1.radius + ball2.radius;
if (distance < minDistance) {
const angle = Math.atan2(dy, dx);
const totalMass = ball1.radius + ball2.radius;
const v1 = Math.sqrt(ball1.vx * ball1.vx + ball1.vy * ball1.vy);
const v2 = Math.sqrt(ball2.vx * ball2.vx + ball2.vy * ball2.vy);
ball1.vx = (v1 * Math.cos(angle) * (ball1.radius - ball2.radius) + (2 * ball2.radius * v2 * Math.cos(angle))) / totalMass;
ball1.vy = (v1 * Math.sin(angle) * (ball1.radius - ball2.radius) + (2 * ball2.radius * v2 * Math.sin(angle))) / totalMass;
ball2.vx = (v2 * Math.cos(angle) * (ball2.radius - ball1.radius) + (2 * ball1.radius * v1 * Math.cos(angle))) / totalMass;
ball2.vy = (v2 * Math.sin(angle) * (ball2.radius - ball1.radius) + (2 * ball1.radius * v1 * Math.sin(angle))) / totalMass;
const overlap = minDistance - distance;
const correction = overlap / 2;
ball1.x -= correction * Math.cos(angle);
ball1.y -= correction * Math.sin(angle);
ball2.x += correction * Math.cos(angle);
ball2.y += correction * Math.sin(angle);
}
}
}
}
function textCollision(ball) {
// Detect collision between balls and text
const radius = ball.radius;
const imageData = textCtx.getImageData(ball.x - radius, ball.y - radius, radius * 2, radius * 2);
const pixels = imageData.data;
for (let y = 0; y < radius * 2; y++) {
for (let x = 0; x < radius * 2; x++) {
const index = (y * radius * 2 + x) * 4;
if (pixels[index + 3] > 128) {
const dx = x - radius;
const dy = y - radius;
const distance = Math.hypot(dx, dy);
if (distance < radius) {
ball.vx = -ball.vx * 0.7;
ball.vy = -ball.vy * 0.7;
ball.x += ball.vx;
ball.y += ball.vy;
return;
}
}
}
}
}
function update() {
// Update ball positions and detect collisions
balls.forEach(ball => {
if (!isDragging || ball !== dragBall) {
if (ball.isMoving) {
ball.vy += 0.2;
ball.vx += 0;
ball.vy *= friction;
ball.vx *= friction;
ball.y += ball.vy;
ball.x += ball.vx;
if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
ball.vy *= -0.7;
ball.y = ball.y > canvas.height / 2 ? canvas.height - ball.radius : ball.radius;
}
if (ball.x + ball.radius > canvas.width || ball.x - ball.radius < 0) {
ball.vx *= -0.7;
ball.x = ball.x > canvas.width / 2 ? canvas.width - ball.radius : ball.radius;
}
textCollision(ball);
if (Math.abs(ball.vx) < 0.01 && Math.abs(ball.vy) < 0.01) {
ball.vx = 0;
ball.vy = 0;
ball.isMoving = false;
}
}
}
});
detectCollisions();
drawBalls();
requestAnimationFrame(update);
}
update();
canvas.addEventListener('mousedown', function(event) {
if (event.button === 1) {
explodeBalls();
}
});
function explodeBalls() {
// Apply a random high velocity to each ball to simulate an explosion
balls.forEach(ball => {
ball.vx = (Math.random() - 0.5) * 20;
ball.vy = (Math.random() - 0.5) * 20;
ball.isMoving = true;
});
}
for (let i = 0; i < numSmallBalls; i++) {
// Initialize smaller balls with random positions and velocities
const radius = 10 + Math.random() * 10;
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const color = colors[i % colors.length];
balls.push({
x: x,
y: y,
radius: radius,
color: color,
vx: (Math.random() - 0.5) * 4,
vy: (Math.random() - 0.5) * 4,
isMoving: true
});
}
function drawBalls() {
// Clear the canvas and draw all balls
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(textCanvas, 0, 0);
balls.forEach(ball => {
ctx.fillStyle = ball.color;
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fill();
});
}
function update() {
// Update ball positions and detect collisions
balls.forEach(ball => {
if (ball.isMoving) {
ball.vy += 0.2;
ball.vx += 0;
ball.vy *= friction;
ball.vx *= friction;
ball.y += ball.vy;
ball.x += ball.vx;
if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
ball.vy *= -0.7;
ball.y = ball.y > canvas.height / 2 ? canvas.height - ball.radius : ball.radius;
}
if (ball.x + ball.radius > canvas.width || ball.x - ball.radius < 0) {
ball.vx *= -0.7;
ball.x = ball.x > canvas.width / 2 ? canvas.width - ball.radius : ball.radius;
}
textCollision(ball);
if (Math.abs(ball.vx) < 0.01 && Math.abs(ball.vy) < 0.01) {
ball.vx = 0;
ball.vy = 0;
ball.isMoving = false;
}
}
});
detectCollisions();
drawBalls();
requestAnimationFrame(update);
}
update();
});
</script>
</body>
</html>