-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.js
More file actions
333 lines (284 loc) · 9.16 KB
/
ball.js
File metadata and controls
333 lines (284 loc) · 9.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
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
// Ball Playground Code
const ballCanvas = document.getElementById('ballCanvas');
const ballCtx = ballCanvas.getContext('2d');
const balls = [];
let ballsVisible = false;
let ballsDragging = false;
let dragBall = null;
let ballMouseX = 0;
let ballMouseY = 0;
let ballAnimationId;
// Resize ball canvas to full window
function resizeBallCanvas() {
ballCanvas.width = window.innerWidth;
ballCanvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeBallCanvas);
resizeBallCanvas();
// Ball class with retro styling
class RetloBall {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * 3;
this.vy = (Math.random() - 0.5) * 3;
this.radius = radius;
this.color = color;
this.isDragging = false;
this.gravity = 0.15;
this.friction = 0.98;
this.bounce = 0.7;
}
update() {
if (!this.isDragging) {
// Apply gravity
this.vy += this.gravity;
// Apply velocity
this.x += this.vx;
this.y += this.vy;
// Apply friction
this.vx *= this.friction;
this.vy *= this.friction;
// Bounce off walls
if (this.x + this.radius > ballCanvas.width) {
this.x = ballCanvas.width - this.radius;
this.vx *= -this.bounce;
}
if (this.x - this.radius < 0) {
this.x = this.radius;
this.vx *= -this.bounce;
}
if (this.y + this.radius > ballCanvas.height) {
this.y = ballCanvas.height - this.radius;
this.vy *= -this.bounce;
}
if (this.y - this.radius < 0) {
this.y = this.radius;
this.vy *= -this.bounce;
}
}
// Check collisions with other balls
this.checkCollisions();
}
checkCollisions() {
for (let other of balls) {
if (other === this) continue;
const dx = other.x - this.x;
const dy = other.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
const minDistance = this.radius + other.radius;
if (distance < minDistance) {
// Simple collision response
const angle = Math.atan2(dy, dx);
const targetX = this.x + Math.cos(angle) * minDistance;
const targetY = this.y + Math.sin(angle) * minDistance;
const ax = (targetX - other.x) * 0.03;
const ay = (targetY - other.y) * 0.03;
this.vx -= ax;
this.vy -= ay;
other.vx += ax;
other.vy += ay;
}
}
}
draw() {
ballCtx.save();
// Draw retro-style shadow (offset black circle)
ballCtx.fillStyle = '#808080';
ballCtx.beginPath();
ballCtx.arc(this.x + 2, this.y + 2, this.radius, 0, Math.PI * 2);
ballCtx.fill();
// Draw main ball with retro border effect
ballCtx.fillStyle = this.color;
ballCtx.beginPath();
ballCtx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ballCtx.fill();
// Draw highlight (top-left)
ballCtx.fillStyle = '#ffffff';
ballCtx.beginPath();
ballCtx.arc(this.x - this.radius * 0.3, this.y - this.radius * 0.3, this.radius * 0.2, 0, Math.PI * 2);
ballCtx.fill();
ballCtx.restore();
}
isPointInside(x, y) {
const dx = x - this.x;
const dy = y - this.y;
return Math.sqrt(dx * dx + dy * dy) <= this.radius;
}
}
// Ball configurations with retro colors
const retroBallConfigs = [
{ color: '#ff0000', radius: 25 }, // Red
{ color: '#00ff00', radius: 28 }, // Green
{ color: '#0000ff', radius: 26 }, // Blue
{ color: '#ffff00', radius: 30 }, // Yellow
{ color: '#ff00ff', radius: 27 }, // Magenta
{ color: '#00ffff', radius: 29 }, // Cyan
{ color: '#800080', radius: 24 }, // Purple
{ color: '#ffa500', radius: 31 } // Orange
];
function spawnRetroBall() {
const config = retroBallConfigs[Math.floor(Math.random() * retroBallConfigs.length)];
const x = Math.random() * (ballCanvas.width - config.radius * 2) + config.radius;
const y = Math.random() * (ballCanvas.height - config.radius * 2) + config.radius;
balls.push(new RetloBall(x, y, config.radius, config.color));
updateBallCounter();
}
function clearAllBalls() {
balls.length = 0;
updateBallCounter();
}
function updateBallCounter() {
document.getElementById('ballCounter').textContent = balls.length;
}
function showBallControls() {
console.log('Showing ball controls');
ballsVisible = true;
document.getElementById('ballControls').style.display = 'flex';
ballCanvas.classList.add('interactive');
const ballsBtn = document.getElementById('ballsButton');
ballsBtn.setAttribute('data-active', 'true');
// Debug canvas properties
console.log('Canvas dimensions:', ballCanvas.width, 'x', ballCanvas.height);
console.log('Canvas classes:', ballCanvas.className);
console.log('Canvas z-index:', getComputedStyle(ballCanvas).zIndex);
// Spawn 8 random balls
for (let i = 0; i < 8; i++) {
addBall();
}
console.log('Spawned balls:', balls.length);
if (!ballAnimationId) {
animateBalls();
}
}
function hideBallControls() {
console.log('Hiding ball controls');
ballsVisible = false;
document.getElementById('ballControls').style.display = 'none';
ballCanvas.classList.remove('interactive');
const ballsBtn = document.getElementById('ballsButton');
ballsBtn.setAttribute('data-active', 'false');
// Clear all balls
clearAllBalls();
// Immediately clear the canvas
ballCtx.clearRect(0, 0, ballCanvas.width, ballCanvas.height);
// Stop any dragging
if (ballsDragging && dragBall) {
dragBall.isDragging = false;
dragBall = null;
ballsDragging = false;
}
// Reset animation ID so it can restart properly
ballAnimationId = null;
}
function addBall() {
const config = retroBallConfigs[Math.floor(Math.random() * retroBallConfigs.length)];
const x = Math.random() * (ballCanvas.width - config.radius * 2) + config.radius;
const y = Math.random() * (ballCanvas.height - config.radius * 2) + config.radius;
balls.push(new RetloBall(x, y, config.radius, config.color));
updateBallCounter();
}
function removeBall() {
if (balls.length > 0) {
balls.pop();
updateBallCounter();
}
}
// Mouse/touch event handlers for balls
function getBallMousePos(e) {
const rect = ballCanvas.getBoundingClientRect();
return {
x: (e.clientX || e.touches[0].clientX) - rect.left,
y: (e.clientY || e.touches[0].clientY) - rect.top
};
}
// Test if canvas is receiving events at all
ballCanvas.addEventListener('click', (e) => {
console.log('Canvas clicked! Event received');
});
ballCanvas.addEventListener('mousedown', (e) => {
console.log('Mouse down event fired, ballsVisible:', ballsVisible, 'balls.length:', balls.length);
if (!ballsVisible) return;
const pos = getBallMousePos(e);
ballMouseX = pos.x;
ballMouseY = pos.y;
console.log('Mouse position:', pos.x, pos.y);
// Check if clicking on a ball
for (let i = balls.length - 1; i >= 0; i--) {
console.log('Checking ball', i, 'at position:', balls[i].x, balls[i].y, 'radius:', balls[i].radius);
if (balls[i].isPointInside(ballMouseX, ballMouseY)) {
console.log('Ball clicked, starting drag');
ballsDragging = true;
dragBall = balls[i];
dragBall.isDragging = true;
ballCanvas.style.cursor = 'grabbing';
// Move ball to end of array (draw on top)
balls.splice(i, 1);
balls.push(dragBall);
break;
}
}
});
ballCanvas.addEventListener('mousemove', (e) => {
if (!ballsVisible) return;
const pos = getBallMousePos(e);
if (ballsDragging && dragBall) {
console.log('Dragging ball to:', pos.x, pos.y);
dragBall.x = pos.x;
dragBall.y = pos.y;
dragBall.vx = pos.x - ballMouseX;
dragBall.vy = pos.y - ballMouseY;
} else {
// Check if hovering over a ball for cursor feedback
let hoveringOverBall = false;
for (let ball of balls) {
if (ball.isPointInside(pos.x, pos.y)) {
hoveringOverBall = true;
break;
}
}
ballCanvas.style.cursor = hoveringOverBall ? 'grab' : 'default';
}
ballMouseX = pos.x;
ballMouseY = pos.y;
});
ballCanvas.addEventListener('mouseup', () => {
if (ballsDragging && dragBall) {
console.log('Mouse up, ending drag');
dragBall.isDragging = false;
dragBall = null;
ballsDragging = false;
ballCanvas.style.cursor = 'default';
}
});
// Ball animation loop
function animateBalls() {
ballCtx.clearRect(0, 0, ballCanvas.width, ballCanvas.height);
if (ballsVisible && balls.length > 0) {
for (let ball of balls) {
ball.update();
ball.draw();
}
ballAnimationId = requestAnimationFrame(animateBalls);
} else {
// Clear canvas when balls are hidden
ballCtx.clearRect(0, 0, ballCanvas.width, ballCanvas.height);
if (ballsVisible) {
ballAnimationId = requestAnimationFrame(animateBalls);
} else {
ballAnimationId = null;
}
}
}
// Event listeners for ball controls
document.getElementById('ballPlus').addEventListener('click', addBall);
document.getElementById('ballMinus').addEventListener('click', removeBall);
document.getElementById('ballsButton').addEventListener('click', () => {
if (ballsVisible) {
hideBallControls();
} else {
showBallControls();
}
});
// Initialize (no balls by default)
updateBallCounter();