-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-gravity-field-202603242202.html
More file actions
297 lines (257 loc) · 9.21 KB
/
Copy pathai-gravity-field-202603242202.html
File metadata and controls
297 lines (257 loc) · 9.21 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>引力场 - Gravity Field</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow: hidden;
background: #000;
font-family: 'Arial', sans-serif;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
}
.info {
position: fixed;
bottom: 20px;
left: 20px;
color: rgba(255, 150, 50, 0.8);
font-size: 14px;
z-index: 100;
pointer-events: none;
text-shadow: 0 0 15px rgba(255, 100, 0, 0.6);
}
.title {
position: fixed;
top: 30px;
left: 50%;
transform: translateX(-50%);
color: rgba(255, 150, 50, 0.9);
font-size: 32px;
font-weight: bold;
z-index: 100;
text-shadow: 0 0 30px rgba(255, 100, 0, 0.8);
letter-spacing: 5px;
}
.fps {
position: fixed;
top: 20px;
right: 20px;
color: rgba(255, 150, 50, 0.6);
font-size: 12px;
font-family: monospace;
z-index: 100;
}
@media (max-width: 768px) {
.info {
font-size: 11px;
bottom: 10px;
left: 10px;
}
.title {
font-size: 20px;
top: 15px;
}
}
</style>
</head>
<body>
<div class="title">🌀 引力场</div>
<canvas id="gravity"></canvas>
<div class="info">
<div>🖱️ 点击 - 添加引力源</div>
<div>👆 右键 - 移除引力源</div>
</div>
<div class="fps" id="fps">FPS: 60</div>
<script>
// 性能监控
const fpsElement = document.getElementById('fps');
let lastTime = performance.now();
let frameCount = 0;
// Canvas 设置
const canvas = document.getElementById('gravity');
const ctx = canvas.getContext('2d');
let width, height;
// 粒子系统
let particles = [];
const PARTICLE_COUNT = 500;
// 引力源系统
let gravitySources = [];
const MAX_SOURCES = 5;
// 颜色配置
const COLORS = [
'#ff6b6b', '#ffa500', '#ffff00', '#00ff00',
'#00ffff', '#0080ff', '#8000ff', '#ff00ff'
];
// 响应式调整
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
initParticles();
}
// 初始化粒子
function initParticles() {
particles = [];
for (let i = 0; i < PARTICLE_COUNT; i++) {
particles.push({
x: Math.random() * width,
y: Math.random() * height,
vx: (Math.random() - 0.5) * 2,
vy: (Math.random() - 0.5) * 2,
size: Math.random() * 2 + 1,
color: COLORS[Math.floor(Math.random() * COLORS.length)],
alpha: 0.5 + Math.random() * 0.5,
});
}
}
// 创建引力源
function createGravitySource(x, y) {
if (gravitySources.length >= MAX_SOURCES) {
gravitySources.shift();
}
gravitySources.push({
x: x,
y: y,
mass: 5000 + Math.random() * 5000,
color: COLORS[gravitySources.length % COLORS.length],
});
}
// 更新粒子
function updateParticles() {
particles.forEach(particle => {
// 应用所有引力源的影响
gravitySources.forEach(source => {
const dx = source.x - particle.x;
const dy = source.y - particle.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 10) { // 避免除以0
const force = source.mass / (dist * dist);
const angle = Math.atan2(dy, dx);
particle.vx += Math.cos(angle) * force * 0.001;
particle.vy += Math.sin(angle) * force * 0.001;
}
});
// 应用速度
particle.x += particle.vx;
particle.y += particle.vy;
// 速度限制
const speed = Math.sqrt(particle.vx * particle.vx + particle.vy * particle.vy);
if (speed > 10) {
particle.vx = (particle.vx / speed) * 10;
particle.vy = (particle.vy / speed) * 10;
}
// 边界循环
if (particle.x < 0) particle.x = width;
if (particle.x > width) particle.x = 0;
if (particle.y < 0) particle.y = height;
if (particle.y > height) particle.y = 0;
});
}
// 绘制粒子
function drawParticles() {
particles.forEach(particle => {
ctx.save();
ctx.fillStyle = particle.color;
ctx.globalAlpha = particle.alpha;
ctx.shadowBlur = 5;
ctx.shadowColor = particle.color;
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
});
}
// 绘制引力源
function drawGravitySources() {
gravitySources.forEach(source => {
// 引力场范围
const gradient = ctx.createRadialGradient(
source.x, source.y, 0,
source.x, source.y, 150
);
gradient.addColorStop(0, source.color + '40');
gradient.addColorStop(1, source.color + '00');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(source.x, source.y, 150, 0, Math.PI * 2);
ctx.fill();
// 引力源核心
ctx.save();
ctx.fillStyle = source.color;
ctx.shadowBlur = 20;
ctx.shadowColor = source.color;
ctx.beginPath();
ctx.arc(source.x, source.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
});
}
// 绘制引力线
function drawGravityLines() {
if (gravitySources.length < 2) return;
ctx.save();
ctx.strokeStyle = 'rgba(255, 100, 0, 0.2)';
ctx.lineWidth = 1;
for (let i = 0; i < gravitySources.length; i++) {
for (let j = i + 1; j < gravitySources.length; j++) {
ctx.beginPath();
ctx.moveTo(gravitySources[i].x, gravitySources[i].y);
ctx.lineTo(gravitySources[j].x, gravitySources[j].y);
ctx.stroke();
}
}
ctx.restore();
}
// 主动画循环
function animate(currentTime) {
// 清除画布
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, width, height);
// 更新和绘制
updateParticles();
drawGravityLines();
drawGravitySources();
drawParticles();
// FPS 计算
frameCount++;
if (frameCount >= 30) {
const fps = Math.round(30 / ((currentTime - lastTime) / 1000 * 30));
fpsElement.textContent = `FPS: ${Math.min(fps, 60)}`;
frameCount = 0;
}
lastTime = currentTime;
requestAnimationFrame(animate);
}
// 事件监听
window.addEventListener('resize', resize);
canvas.addEventListener('click', (e) => {
createGravitySource(e.clientX, e.clientY);
});
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
if (gravitySources.length > 0) {
gravitySources.pop();
}
});
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
createGravitySource(e.touches[0].clientX, e.touches[0].clientY);
});
// 初始化
resize();
// 添加初始引力源
createGravitySource(width / 2, height / 2);
requestAnimationFrame(animate);
</script>
</body>
</html>