-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathballs-animation.html
More file actions
301 lines (251 loc) · 9.08 KB
/
balls-animation.html
File metadata and controls
301 lines (251 loc) · 9.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Gravity & Moving Gap (Slower)</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #050505;
overflow: hidden;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
touch-action: none;
}
#gameCanvas {
display: block;
}
#ui-layer {
position: absolute;
top: 20px;
left: 20px;
color: white;
pointer-events: none;
z-index: 10;
}
h1 {
font-size: 1rem;
margin: 0;
color: #888;
text-transform: uppercase;
letter-spacing: 2px;
}
#ball-count {
font-size: 2.5rem;
font-weight: bold;
color: #ff3333;
margin: 0;
text-shadow: 0 0 10px rgba(255, 51, 51, 0.4);
}
#instructions {
position: absolute;
bottom: 20px;
width: 100%;
text-align: center;
color: #444;
font-size: 0.9rem;
pointer-events: none;
user-select: none;
}
</style>
</head>
<body>
<div id="ui-layer">
<h1>Balls</h1>
<p id="ball-count">1</p>
</div>
<div id="instructions">Tap screen to restart</div>
<canvas id="gameCanvas"></canvas>
<script>
// --- CONFIGURAÇÕES / SETTINGS ---
const BACKGROUND_COLOR = '#050505';
const BORDER_COLOR = '#ffffff';
const BORDER_THICKNESS = 10;
const BALL_RADIUS = 8;
const INITIAL_BALLS = 1;
const MAX_BALLS = 10000; // Limit 10k
// Physics (Slower & Smoother)
const GRAVITY = 0.15; // Reduzido de 0.25
const BOUNCE_DAMPING = 0.85;
const FRICTION_X = 0.999;
const SPAWN_SPEED_FACTOR = 1.1; // Aceleração menor ao multiplicar
const MAX_SPEED = 10; // Reduzido de 20 para evitar rastros/blur
// Gap (Opening)
let gap = {
width: 150,
x: 0,
speed: 2, // Gap mais lento (era 4)
height: BORDER_THICKNESS
};
const canvas = document.getElementById('gameCanvas');
// alpha: false ajuda a evitar rastros de transparência e melhora performance
const ctx = canvas.getContext('2d', { alpha: false });
const countElement = document.getElementById('ball-count');
let width, height;
let balls = [];
// --- BALL CLASS ---
class Ball {
constructor(x, y, vx, vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = BALL_RADIUS;
// Color optimization
const hue = Math.floor(Math.random() * 40);
this.color = `hsl(${hue}, 100%, 50%)`;
}
update() {
// Physics
this.vy += GRAVITY;
this.vx *= FRICTION_X;
this.x += this.vx;
this.y += this.vy;
// --- COLLISIONS ---
const leftLimit = BORDER_THICKNESS + this.radius;
const rightLimit = width - BORDER_THICKNESS - this.radius;
const topLimit = BORDER_THICKNESS + this.radius;
const bottomWallY = height - BORDER_THICKNESS - this.radius;
// Walls
if (this.x < leftLimit) {
this.x = leftLimit;
this.vx *= -1 * BOUNCE_DAMPING;
} else if (this.x > rightLimit) {
this.x = rightLimit;
this.vx *= -1 * BOUNCE_DAMPING;
}
if (this.y < topLimit) {
this.y = topLimit;
this.vy *= -1 * BOUNCE_DAMPING;
}
// Floor / Gap
if (this.y > bottomWallY) {
// Is inside gap area?
const inGap = (this.x > gap.x && this.x < gap.x + gap.width);
if (!inGap) {
// Hit the floor
this.y = bottomWallY;
// Prevent infinite micro-bouncing
if (Math.abs(this.vy) < GRAVITY * 2) {
this.vy = 0;
} else {
this.vy *= -1 * BOUNCE_DAMPING;
}
}
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
}
isEscaped() {
return (this.y - this.radius > height);
}
}
// --- SYSTEM ---
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
gap.width = Math.min(180, width * 0.4);
if (gap.x + gap.width > width) gap.x = width - gap.width - BORDER_THICKNESS;
}
function updateGap() {
gap.x += gap.speed;
if (gap.x <= 0) {
gap.x = 0;
gap.speed *= -1;
}
if (gap.x + gap.width >= width) {
gap.x = width - gap.width;
gap.speed *= -1;
}
}
function spawnInitialBall() {
balls = [];
// Velocidade inicial reduzida proporcionalmente
balls.push(new Ball(width / 2, height * 0.3, (Math.random() - 0.5) * 3, 0));
}
function multiplyBalls(originBall) {
// CHECK MAX LIMIT 10k
if (balls.length >= MAX_BALLS) return;
let speed = Math.sqrt(originBall.vx**2 + originBall.vy**2);
speed = Math.min(speed * SPAWN_SPEED_FACTOR, MAX_SPEED);
// Velocidade mínima reduzida para 3 (era 5)
speed = Math.max(speed, 3);
// Create 2 balls
for (let i = 0; i < 2; i++) {
if (balls.length >= MAX_BALLS) break;
const newX = Math.random() * (width - 100) + 50;
const newY = height * 0.15;
const angle = Math.random() * Math.PI;
const vx = Math.cos(angle) * speed;
const vy = Math.abs(Math.sin(angle) * speed);
balls.push(new Ball(newX, newY, vx, vy));
}
}
function drawWorld() {
// Clear screen COMPLETE (No trails)
ctx.fillStyle = BACKGROUND_COLOR;
ctx.fillRect(0, 0, width, height);
// Walls
ctx.lineWidth = BORDER_THICKNESS;
ctx.strokeStyle = BORDER_COLOR;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(BORDER_THICKNESS/2, height);
ctx.lineTo(BORDER_THICKNESS/2, 0);
ctx.lineTo(width - BORDER_THICKNESS/2, 0);
ctx.lineTo(width - BORDER_THICKNESS/2, height);
ctx.stroke();
// Floor split
ctx.beginPath();
ctx.moveTo(0, height - BORDER_THICKNESS/2);
ctx.lineTo(gap.x, height - BORDER_THICKNESS/2);
ctx.moveTo(gap.x + gap.width, height - BORDER_THICKNESS/2);
ctx.lineTo(width, height - BORDER_THICKNESS/2);
// Sombra leve apenas no chão
ctx.shadowBlur = 10;
ctx.shadowColor = "#ffffff";
ctx.stroke();
ctx.shadowBlur = 0; // Importante: Resetar sombra para não afetar as bolas
}
function animate() {
updateGap();
drawWorld();
let ballsEscaped = [];
for (let i = 0; i < balls.length; i++) {
const ball = balls[i];
ball.update();
ball.draw();
if (ball.isEscaped()) {
ballsEscaped.push(ball);
}
}
// Remove and multiply
for (let i = 0; i < ballsEscaped.length; i++) {
const ball = ballsEscaped[i];
const index = balls.indexOf(ball);
if (index > -1) {
balls.splice(index, 1);
multiplyBalls(ball);
}
}
countElement.innerText = balls.length;
requestAnimationFrame(animate);
}
window.addEventListener('resize', resize);
window.addEventListener('pointerdown', () => {
// Restart on click/tap
spawnInitialBall();
});
resize();
spawnInitialBall();
animate();
</script>
</body>
</html>