-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouncing-balls.html
More file actions
323 lines (272 loc) · 10.8 KB
/
bouncing-balls.html
File metadata and controls
323 lines (272 loc) · 10.8 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>弹跳球动画</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: 'Arial', sans-serif;
}
.container {
background: white;
padding: 20px;
border-radius: 15px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
h1 {
text-align: center;
color: #333;
margin-bottom: 20px;
font-size: 28px;
}
canvas {
border: 3px solid #333;
border-radius: 10px;
display: block;
background: #f0f0f0;
}
.controls {
text-align: center;
margin-top: 15px;
}
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
background: #667eea;
color: white;
transition: background 0.3s;
}
button:hover {
background: #5568d3;
}
button:active {
transform: scale(0.95);
}
</style>
</head>
<body>
<div class="container">
<h1>🎾 弹跳球动画</h1>
<canvas id="canvas"></canvas>
<div class="controls">
<button onclick="addBall()">添加球</button>
<button onclick="resetBalls()">重置</button>
</div>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// 设置画布大小
canvas.width = 800;
canvas.height = 600;
// 边界设置(留出边距)
const borderWidth = 3;
const padding = 10;
const minX = padding + borderWidth;
const minY = padding + borderWidth;
const maxX = canvas.width - padding - borderWidth;
const maxY = canvas.height - padding - borderWidth;
// 球的颜色数组
const colors = ['#FF4444', '#4444FF', '#44FF44', '#FF8844', '#FFFFFF'];
const colorNames = ['红色', '蓝色', '绿色', '橙色', '白色'];
// 球类
class Ball {
constructor(x, y, radius, color, vx, vy) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.vx = vx; // x方向速度
this.vy = vy; // y方向速度
this.mass = radius * radius; // 质量与半径的平方成正比
}
// 更新球的位置
update() {
this.x += this.vx;
this.y += this.vy;
// 左右边界碰撞
if (this.x - this.radius <= minX || this.x + this.radius >= maxX) {
this.vx = -this.vx;
this.x = Math.max(minX + this.radius, Math.min(maxX - this.radius, this.x));
}
// 上下边界碰撞
if (this.y - this.radius <= minY || this.y + this.radius >= maxY) {
this.vy = -this.vy;
this.y = Math.max(minY + this.radius, Math.min(maxY - this.radius, this.y));
}
}
// 检查与另一个球是否碰撞
checkCollision(other) {
const dx = other.x - this.x;
const dy = other.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance < this.radius + other.radius;
}
// 处理与另一个球的碰撞
resolveCollision(other) {
const dx = other.x - this.x;
const dy = other.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
// 如果球重叠,先分离它们
if (distance < this.radius + other.radius) {
const overlap = (this.radius + other.radius) - distance;
const separationX = (dx / distance) * overlap * 0.5;
const separationY = (dy / distance) * overlap * 0.5;
this.x -= separationX;
this.y -= separationY;
other.x += separationX;
other.y += separationY;
}
// 计算碰撞法向量(归一化)
const nx = dx / distance;
const ny = dy / distance;
// 计算相对速度
const relativeVx = other.vx - this.vx;
const relativeVy = other.vy - this.vy;
// 计算相对速度在法向量上的投影
const relativeSpeed = relativeVx * nx + relativeVy * ny;
// 如果球正在分离,不需要处理碰撞
if (relativeSpeed > 0) return;
// 弹性碰撞系数(1.0为完全弹性碰撞)
const restitution = 0.9;
// 计算碰撞冲量
const impulse = (2 * relativeSpeed * restitution) / (this.mass + other.mass);
// 更新速度
this.vx += impulse * other.mass * nx;
this.vy += impulse * other.mass * ny;
other.vx -= impulse * this.mass * nx;
other.vy -= impulse * this.mass * ny;
}
// 绘制球
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
// 添加阴影效果
ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.fill();
// 重置阴影
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
// 添加高光效果
ctx.beginPath();
ctx.arc(this.x - this.radius * 0.3, this.y - this.radius * 0.3, this.radius * 0.3, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
ctx.fill();
}
}
// 球数组
let balls = [];
// 创建随机球
function createRandomBall() {
const radius = 15 + Math.random() * 20; // 半径15-35
const x = minX + radius + Math.random() * (maxX - minX - 2 * radius);
const y = minY + radius + Math.random() * (maxY - minY - 2 * radius);
const colorIndex = Math.floor(Math.random() * colors.length);
const color = colors[colorIndex];
const speed = 2 + Math.random() * 4; // 速度2-6
const angle = Math.random() * Math.PI * 2;
const vx = Math.cos(angle) * speed;
const vy = Math.sin(angle) * speed;
return new Ball(x, y, radius, color, vx, vy);
}
// 初始化球
function initBalls() {
balls = [];
// 创建5个不同颜色的球
for (let i = 0; i < 5; i++) {
const radius = 20 + Math.random() * 15;
const x = minX + radius + Math.random() * (maxX - minX - 2 * radius);
const y = minY + radius + Math.random() * (maxY - minY - 2 * radius);
const color = colors[i];
const speed = 2 + Math.random() * 4;
const angle = Math.random() * Math.PI * 2;
const vx = Math.cos(angle) * speed;
const vy = Math.sin(angle) * speed;
balls.push(new Ball(x, y, radius, color, vx, vy));
}
// 添加4个红色的球
const redColor = colors[0]; // 红色
for (let i = 0; i < 4; i++) {
const radius = 20 + Math.random() * 15;
const x = minX + radius + Math.random() * (maxX - minX - 2 * radius);
const y = minY + radius + Math.random() * (maxY - minY - 2 * radius);
const speed = 2 + Math.random() * 4;
const angle = Math.random() * Math.PI * 2;
const vx = Math.cos(angle) * speed;
const vy = Math.sin(angle) * speed;
balls.push(new Ball(x, y, radius, redColor, vx, vy));
}
}
// 添加新球
function addBall() {
balls.push(createRandomBall());
}
// 重置所有球
function resetBalls() {
initBalls();
}
// 绘制边界
function drawBorder() {
ctx.strokeStyle = '#333';
ctx.lineWidth = borderWidth;
ctx.strokeRect(padding, padding, canvas.width - 2 * padding, canvas.height - 2 * padding);
}
// 检测并处理所有球之间的碰撞
function checkBallCollisions() {
for (let i = 0; i < balls.length; i++) {
for (let j = i + 1; j < balls.length; j++) {
if (balls[i].checkCollision(balls[j])) {
balls[i].resolveCollision(balls[j]);
}
}
}
}
// 动画循环
function animate() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制背景
ctx.fillStyle = '#f0f0f0';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制边界
drawBorder();
// 更新所有球的位置
balls.forEach(ball => {
ball.update();
});
// 检测并处理球之间的碰撞
checkBallCollisions();
// 绘制所有球
balls.forEach(ball => {
ball.draw();
});
requestAnimationFrame(animate);
}
// 启动动画
initBalls();
animate();
</script>
</body>
</html>