Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
467 lines (397 loc) · 17.1 KB
/
Copy pathindex.html
File metadata and controls
467 lines (397 loc) · 17.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>问号秘境</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js">
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
overflow: hidden;
background: #000000;
cursor: default;
-webkit-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
touch-action: none;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="mainCanvas"></canvas>
<script>
(function() {
// ============ 获取元素与上下文 ============
const canvas = document.getElementById('mainCanvas');
const ctx = canvas.getContext('2d');
// ============ Matter.js 解构 ============
const {
Engine,
World,
Bodies,
Body,
Composite,
Mouse,
MouseConstraint,
Events,
Query,
Vector,
Runner
} = Matter;
// ============ 创建物理引擎 ============
const engine = Engine.create();
const world = engine.world;
engine.gravity.y = 1.2;
// ============ Canvas 尺寸设置 ============
function resizeCanvas() {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = window.innerWidth * dpr;
canvas.height = window.innerHeight * dpr;
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(dpr, dpr);
}
resizeCanvas();
// ============ 存储墙壁引用 ============
let wallTop, wallBottom, wallLeft, wallRight;
// ============ 创建边界墙壁 ============
function createWalls() {
const w = window.innerWidth;
const h = window.innerHeight;
const thickness = 60;
const offset = thickness / 2;
const wallOptions = {
isStatic: true,
friction: 0.4,
restitution: 0.5,
render: { visible: false },
label: 'wall'
};
wallTop = Bodies.rectangle(w / 2, -offset, w + thickness * 2, thickness, wallOptions);
wallBottom = Bodies.rectangle(w / 2, h + offset, w + thickness * 2, thickness, wallOptions);
wallLeft = Bodies.rectangle(-offset, h / 2, thickness, h + thickness * 2, wallOptions);
wallRight = Bodies.rectangle(w + offset, h / 2, thickness, h + thickness * 2, wallOptions);
Composite.add(world, [wallTop, wallBottom, wallLeft, wallRight]);
}
function rebuildWalls() {
if (wallTop) Composite.remove(world, wallTop);
if (wallBottom) Composite.remove(world, wallBottom);
if (wallLeft) Composite.remove(world, wallLeft);
if (wallRight) Composite.remove(world, wallRight);
createWalls();
}
createWalls();
// ============ 存储body元数据的Map ============
const bodyMetaMap = new Map(); // body.id -> { type, char, fontSize, color, createdAt }
// ============ 存储分类引用 ============
const bigChars = [];
const catChars = [];
// ============ 粒子系统 ============
const particles = [];
function spawnParticles(x, y, color = '#b0b0b0', count = 10) {
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const speed = 1.5 + Math.random() * 2.5;
particles.push({
x: x,
y: y,
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed,
life: 0.4 + Math.random() * 0.3,
maxLife: 0.4 + Math.random() * 0.3,
size: 2 + Math.random() * 3,
color: color
});
}
}
function updateParticles(deltaTime) {
// deltaTime 单位为秒
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.life -= deltaTime;
if (p.life <= 0) {
particles.splice(i, 1);
continue;
}
p.x += p.vx;
p.y += p.vy;
p.vx *= 0.96;
p.vy *= 0.96;
}
}
function drawParticles(ctx) {
for (const p of particles) {
const alpha = Math.max(0, p.life / p.maxLife);
ctx.save();
ctx.globalAlpha = alpha;
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
// ============ 创建"问号秘境"四个大字 ============
function createBigChars() {
const w = window.innerWidth;
const h = window.innerHeight;
const centerX = w / 2;
const centerY = h / 2;
const charSize = 85;
const gap = 20;
const halfTotal = charSize + gap / 2;
const positions = [
{ char: '问', x: centerX - halfTotal, y: centerY - halfTotal },
{ char: '号', x: centerX + halfTotal, y: centerY - halfTotal },
{ char: '秘', x: centerX - halfTotal, y: centerY + halfTotal },
{ char: '境', x: centerX + halfTotal, y: centerY + halfTotal },
];
const bodyOptions = {
restitution: 0.55,
friction: 0.35,
frictionAir: 0.001,
density: 0.002,
render: { visible: false },
label: 'big-char'
};
positions.forEach(({ char, x, y }) => {
const body = Bodies.rectangle(x, y, charSize, charSize, bodyOptions);
// 【修改】去除了初始旋转和初始线速度,保持静止
bodyMetaMap.set(body.id, {
type: 'big',
char: char,
fontSize: 62,
color: '#ffffff',
createdAt: 0 // 大字无延迟需求
});
bigChars.push(body);
Composite.add(world, body);
});
}
createBigChars();
// ============ 创建"猫"字 ============
function createCat(x, y) {
const catSize = 42;
const bodyOptions = {
restitution: 0.5,
friction: 0.4,
frictionAir: 0.001,
density: 0.0015,
render: { visible: false },
label: 'cat'
};
const body = Bodies.rectangle(x, y, catSize, catSize, bodyOptions);
// 微小的初始随机速度,但保留一些活力
Body.setAngularVelocity(body, (Math.random() - 0.5) * 0.2);
Body.setVelocity(body, { x: (Math.random() - 0.5) * 0.3, y: (Math.random() - 0.5) * 0.3 });
// 【新增】记录创建时间,用于延迟可点击
bodyMetaMap.set(body.id, {
type: 'cat',
char: '猫',
fontSize: 28,
color: '#b0b0b0',
createdAt: Date.now()
});
catChars.push(body);
Composite.add(world, body);
// 【新增】生成粒子特效
spawnParticles(x, y, '#b0b0b0', 10);
return body;
}
// ============ 删除"猫"字 ============
function removeCat(body) {
const index = catChars.indexOf(body);
if (index > -1) {
catChars.splice(index, 1);
}
// 【新增】删除时获取位置并生成粒子
const pos = body.position;
spawnParticles(pos.x, pos.y, '#b0b0b0', 12);
bodyMetaMap.delete(body.id);
Composite.remove(world, body);
}
// ============ 鼠标交互 ============
const mouse = Mouse.create(canvas);
mouse.pixelRatio = Math.min(window.devicePixelRatio || 1, 2);
const mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.3,
damping: 0.08,
angularStiffness: 0.2,
}
});
mouseConstraint.collisionFilter.mask = 0xFFFFFFFF;
Events.on(mouseConstraint, 'mousedown', function(event) {
const draggedBody = event.source.body;
if (draggedBody && draggedBody.isStatic) {
mouseConstraint.body = null;
}
});
Composite.add(world, mouseConstraint);
// ============ 点击检测 ============
let mouseDownPos = null;
let mouseDownBody = null;
const CLICK_THRESHOLD = 6;
const CAT_DELAY_MS = 300; // “猫”不可点击的延迟时间
function handleClickAt(pos) {
const bodiesUnder = Query.point(Composite.allBodies(world), pos);
const nonStaticBodies = bodiesUnder.filter(b => !b.isStatic);
let hitBigChar = null;
let hitCat = null;
for (const body of nonStaticBodies) {
const meta = bodyMetaMap.get(body.id);
if (meta && meta.type === 'big') {
hitBigChar = body;
break;
}
if (meta && meta.type === 'cat' && !hitCat) {
hitCat = body;
}
}
if (hitBigChar) {
// 点击大字不操作
return;
}
if (hitCat) {
const meta = bodyMetaMap.get(hitCat.id);
// 【修改】检查延迟:创建时间超过300ms才能删除
if (meta && Date.now() - meta.createdAt > CAT_DELAY_MS) {
removeCat(hitCat);
}
return;
}
// 空白处生成猫
if (nonStaticBodies.length === 0) {
createCat(pos.x, pos.y);
}
}
canvas.addEventListener('mousedown', function(e) {
const rect = canvas.getBoundingClientRect();
const x = (e.clientX - rect.left);
const y = (e.clientY - rect.top);
mouseDownPos = { x, y };
const bodiesUnder = Query.point(Composite.allBodies(world), { x, y });
const nonStaticBodies = bodiesUnder.filter(b => !b.isStatic);
mouseDownBody = nonStaticBodies.length > 0 ? nonStaticBodies[0] : null;
});
canvas.addEventListener('mouseup', function(e) {
if (!mouseDownPos) return;
const rect = canvas.getBoundingClientRect();
const x = (e.clientX - rect.left);
const y = (e.clientY - rect.top);
const mouseUpPos = { x, y };
const dx = mouseUpPos.x - mouseDownPos.x;
const dy = mouseUpPos.y - mouseDownPos.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < CLICK_THRESHOLD) {
handleClickAt(mouseUpPos);
}
mouseDownPos = null;
mouseDownBody = null;
});
// 触摸事件
canvas.addEventListener('touchstart', function(e) {
if (e.touches.length === 1) {
const rect = canvas.getBoundingClientRect();
const x = (e.touches[0].clientX - rect.left);
const y = (e.touches[0].clientY - rect.top);
mouseDownPos = { x, y };
const bodiesUnder = Query.point(Composite.allBodies(world), { x, y });
const nonStaticBodies = bodiesUnder.filter(b => !b.isStatic);
mouseDownBody = nonStaticBodies.length > 0 ? nonStaticBodies[0] : null;
}
}, { passive: false });
canvas.addEventListener('touchend', function(e) {
if (!mouseDownPos) return;
const rect = canvas.getBoundingClientRect();
const lastTouch = e.changedTouches[0];
const x = lastTouch ? (lastTouch.clientX - rect.left) : mouseDownPos.x;
const y = lastTouch ? (lastTouch.clientY - rect.top) : mouseDownPos.y;
const mouseUpPos = { x, y };
const dx = mouseUpPos.x - mouseDownPos.x;
const dy = mouseUpPos.y - mouseDownPos.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < CLICK_THRESHOLD * 1.5) {
handleClickAt(mouseUpPos);
}
mouseDownPos = null;
mouseDownBody = null;
}, { passive: false });
canvas.addEventListener('contextmenu', function(e) {
e.preventDefault();
});
// ============ 窗口大小调整 ============
window.addEventListener('resize', function() {
resizeCanvas();
rebuildWalls();
mouse.pixelRatio = Math.min(window.devicePixelRatio || 1, 2);
});
// ============ 渲染循环 ============
let lastTime = performance.now();
function render() {
const now = performance.now();
const deltaTime = (now - lastTime) / 1000; // 秒
lastTime = now;
// 清屏
ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
// 更新粒子
updateParticles(deltaTime);
// 收集渲染body
const allRenderBodies = [...bigChars, ...catChars];
allRenderBodies.sort((a, b) => a.position.y - b.position.y);
// 绘制文字
for (const body of allRenderBodies) {
const meta = bodyMetaMap.get(body.id);
if (!meta) continue;
ctx.save();
ctx.translate(body.position.x, body.position.y);
ctx.rotate(body.angle);
ctx.shadowColor = meta.type === 'big' ?
'rgba(255, 255, 255, 0.25)' :
'rgba(180, 180, 180, 0.2)';
ctx.shadowBlur = meta.type === 'big' ? 8 : 4;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.fillStyle = meta.color;
ctx.font = `bold ${meta.fontSize}px "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(meta.char, 0, 0);
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.restore();
}
// 绘制粒子(在最上层)
drawParticles(ctx);
}
function gameLoop() {
Engine.update(engine, 1000 / 60);
render();
requestAnimationFrame(gameLoop);
}
gameLoop();
console.log('✨ 问号秘境 · 猫字延迟消失 + 粒子特效已启用');
})();
</script>
</body>
</html>