-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhero-animation.js
More file actions
613 lines (525 loc) · 24.3 KB
/
Copy pathhero-animation.js
File metadata and controls
613 lines (525 loc) · 24.3 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
window.startHeroAnimation = () => {
const canvas = document.getElementById('hero-canvas');
if (!canvas) return;
const ctx = canvas.getContext('2d');
if (window.heroAnimationFrame) {
cancelAnimationFrame(window.heroAnimationFrame);
}
const name = "DANIEL HAN";
const initialCountdown = (Math.random() * 1.5 + 3.5).toFixed(2);
let countdown = parseFloat(initialCountdown);
let exploded = false;
let particles = [];
let splatters = [];
let timeSinceStop = 0;
let isCrawling = false;
let crawlStartTime = 0;
let isWaving = false;
// --- OPTIMIZATION 1: DYNAMIC PERFORMANCE & DELTA TIME MONITOR ---
let frameTimes = [];
let lowPowerMode = false;
let lastFrameTime = Date.now();
window.isCountingDown = false;
window.triggerDetonation = () => {
window.isCountingDown = true;
lastFrameTime = Date.now(); // Reset clock right when clicked
};
let glitchState = 0;
let bodyOffsetX = 0;
let bodyOffsetY = 0;
let walkCycle = 0;
const explosionForce = Math.min(window.innerWidth, window.innerHeight) * 0.05;
const friction = 0.94;
const bloodDark = "#4a0000";
const bloodBright = "#8b0000";
class BloodSplatter {
constructor(x, y, vx, vy, isTrail = false) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = isTrail ? Math.random() * 3 + 1 : Math.random() * 8 + 2;
this.stopped = false;
this.friction = 0.85;
this.gravity = 0.5;
this.alpha = 1;
this.color = Math.random() > 0.8 ? bloodBright : bloodDark;
}
update(startFading) {
if (!this.stopped) {
this.vy += this.gravity;
this.x += this.vx;
this.y += this.vy;
this.vx *= this.friction;
this.vy *= this.friction;
if (this.y > canvas.height) {
this.y = canvas.height;
this.stopped = true;
}
if (Math.abs(this.vx) < 0.1 && Math.abs(this.vy) < 0.1) this.stopped = true;
} else if (startFading) {
this.alpha -= 0.005;
}
}
draw() {
ctx.save();
ctx.shadowBlur = 0; // Prevent leak on particle draw
ctx.globalAlpha = Math.max(0, this.alpha);
ctx.fillStyle = this.color;
if (!this.stopped) {
const angle = Math.atan2(this.vy, this.vx);
const speed = Math.hypot(this.vx, this.vy);
const stretch = Math.max(1, speed * 0.4);
ctx.translate(this.x, this.y);
ctx.rotate(angle);
ctx.beginPath();
ctx.ellipse(0, 0, this.radius * stretch, this.radius, 0, 0, Math.PI * 2);
ctx.fill();
} else {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
}
ctx.restore();
}
}
class LetterShard {
constructor(char, x, y) {
this.char = char;
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.rotation = 0;
this.spin = 0;
this.stopped = false;
this.startX = 0;
this.startY = 0;
this.startRot = 0;
this.targetX = 0;
this.targetY = 0;
this.targetRot = 0;
this.id = '';
this.inPosition = false;
this.flashGreen = false;
}
explode() {
const randomAngle = Math.random() * Math.PI * 2;
const speed = (Math.random() * 0.6 + 0.4) * explosionForce;
this.vx = Math.cos(randomAngle) * speed;
this.vy = Math.sin(randomAngle) * speed;
this.spin = (Math.random() - 0.5) * 0.6;
}
update() {
if (!this.stopped && !isCrawling && !isWaving) {
this.x += this.vx;
this.y += this.vy;
this.rotation += this.spin;
this.vx *= friction;
this.vy *= friction;
this.spin *= friction;
const margin = 50;
if (this.x < margin) { this.x = margin; this.vx *= -0.6; this.spin *= -0.5; }
else if (this.x > canvas.width - margin) { this.x = canvas.width - margin; this.vx *= -0.6; this.spin *= -0.5; }
if (this.y < margin) { this.y = margin; this.vy *= -0.6; this.spin *= -0.5; }
else if (this.y > canvas.height - margin) { this.y = canvas.height - margin; this.vy *= -0.6; this.spin *= -0.5; }
// Lowered trailing probability to save frame rate
if (!lowPowerMode && Math.random() > 0.7 && (Math.abs(this.vx) > 1 || Math.abs(this.vy) > 1)) {
splatters.push(new BloodSplatter(this.x, this.y, this.vx * 0.1, this.vy * 0.1, true));
}
if (Math.abs(this.vx) < 0.05 && Math.abs(this.vy) < 0.05 && Math.abs(this.spin) < 0.01) {
this.stopped = true;
}
} else if (isCrawling && !this.inPosition) {
const elapsed = Date.now() - crawlStartTime;
const crawlDuration = 4500;
const progress = Math.min(elapsed / crawlDuration, 1);
if (progress === 1) {
this.x = this.targetX;
this.y = this.targetY;
this.rotation = this.targetRot;
this.inPosition = true;
} else if (Math.random() > 0.7) {
this.x = this.startX + (this.targetX - this.startX) * progress + (Math.random() - 0.5) * 4;
this.y = this.startY + (this.targetY - this.startY) * progress + (Math.random() - 0.5) * 4;
let rotDiff = this.targetRot - this.startRot;
rotDiff = Math.atan2(Math.sin(rotDiff), Math.cos(rotDiff));
this.rotation = this.startRot + (rotDiff * progress);
if (Math.random() > 0.8) splatters.push(new BloodSplatter(this.x, this.y, 0, 0, true));
}
} else if (isWaving) {
let cTargetX = this.targetX;
let cTargetY = this.targetY;
let cTargetRot = this.targetRot;
if (this.id !== 'head' || glitchState >= 5) {
cTargetX += bodyOffsetX;
cTargetY += bodyOffsetY;
}
if (glitchState === 1) {
this.x = cTargetX + (Math.random() - 0.5) * 20;
this.y = cTargetY + (Math.random() - 0.5) * 20;
this.rotation = cTargetRot + (Math.random() - 0.5) * 0.8;
} else if (glitchState >= 2) {
if (this.id === 'head' && glitchState <= 4) {
this.vy += 0.6;
this.x += this.vx;
this.y += this.vy;
this.rotation += this.spin;
if (this.x < 50) { this.x = 50; this.vx *= -0.7; }
else if (this.x > canvas.width - 50) { this.x = canvas.width - 50; this.vx *= -0.7; }
const floorY = canvas.height / 2 + 150;
if (this.y > floorY) {
this.y = floorY;
this.vy *= -0.5;
this.vx *= 0.8;
this.spin *= 0.8;
}
}
const isMoving = (glitchState === 3 || glitchState === 5) &&
(Math.abs(this.targetX + bodyOffsetX - this.x) > 10 || Math.abs(bodyOffsetX) > 10);
if (isMoving && this.id !== 'head') {
if (this.id === 'l_leg') {
cTargetX += Math.sin(walkCycle) * (35 + Math.random() * 15);
cTargetY -= Math.max(0, Math.cos(walkCycle)) * (20 + Math.random() * 10);
cTargetRot += Math.sin(walkCycle) * 0.2;
}
if (this.id === 'r_leg') {
cTargetX += Math.sin(walkCycle + Math.PI) * 15;
cTargetY -= Math.max(0, Math.cos(walkCycle + Math.PI)) * 5;
cTargetRot += Math.sin(walkCycle + Math.PI) * 0.1;
}
if (this.id.includes('torso') || this.id.includes('arm')) {
cTargetY += Math.abs(Math.sin(walkCycle)) * 18 + (Math.random() * 5);
cTargetX += (Math.random() - 0.5) * 8;
cTargetRot += (Math.random() - 0.5) * 0.15;
}
if (this.id === 'r_arm_1' || this.id === 'r_arm_2') {
cTargetX += Math.sin(walkCycle + Math.PI) * 20;
cTargetRot += Math.sin(walkCycle + Math.PI) * 0.3;
}
}
if (glitchState === 4 && (this.id === 'l_arm_1' || this.id === 'l_arm_2')) {
const headParticle = particles.find(p => p.id === 'head');
if (this.id === 'l_arm_2') {
cTargetX = headParticle.x;
cTargetY = headParticle.y - 15;
cTargetRot = 0;
} else {
cTargetX = (this.targetX + bodyOffsetX + headParticle.x) / 2 - 10;
cTargetY = (this.targetY + bodyOffsetY + headParticle.y) / 2;
cTargetRot = -1.0;
}
}
if (this.id === 'head' && glitchState === 5) {
const lHand = particles.find(p => p.id === 'l_arm_2');
this.x += (lHand.x - this.x) * 0.4;
this.y += (lHand.y - 15 - this.y) * 0.4;
this.rotation += (0 - this.rotation) * 0.1;
}
if (glitchState === 5 && (this.id === 'l_arm_1' || this.id === 'l_arm_2')) {
const neckX = canvas.width / 2 + bodyOffsetX;
const neckY = canvas.height / 2 - 110 + bodyOffsetY;
const bobbingOffset = isMoving ? Math.abs(Math.sin(walkCycle)) * 18 : 0;
if (this.id === 'l_arm_2') {
cTargetX = neckX - 35;
cTargetY = neckY - 10 + bobbingOffset;
cTargetRot = 0.5;
} else {
cTargetX = (this.targetX + bodyOffsetX + neckX) / 2 - 30;
cTargetY = (this.targetY + bodyOffsetY + neckY) / 2 + bobbingOffset;
cTargetRot = 0.2;
}
}
let trackingSpeed = 0.15;
if (isMoving) {
if (Math.random() > 0.8) trackingSpeed = 0.02;
else if (Math.random() > 0.9) trackingSpeed = 0.4;
}
if (this.id !== 'head' || glitchState >= 6) {
this.x += (cTargetX - this.x) * trackingSpeed;
this.y += (cTargetY - this.y) * trackingSpeed;
this.rotation += (cTargetRot - this.rotation) * trackingSpeed;
}
} else {
if (this.id === 'r_arm_2') {
const waveAngle = -0.85 + Math.sin(Date.now() * 0.003) * 0.6;
const pivotX = canvas.width / 2 + 45;
const pivotY = canvas.height / 2 - 30;
const radius = 53;
cTargetX = pivotX + Math.cos(waveAngle) * radius;
cTargetY = pivotY + Math.sin(waveAngle) * radius;
cTargetRot = waveAngle + Math.PI/2;
}
this.x += (cTargetX - this.x) * 0.15;
this.y += (cTargetY - this.y) * 0.15;
this.rotation += (cTargetRot - this.rotation) * 0.15;
}
}
}
draw() {
ctx.save();
let renderColor = "#ff003c";
let renderShadow = bloodDark;
let renderX = this.x;
let renderY = this.y;
let glowFlicker = 10;
if (this.flashGreen) {
renderColor = "#00ff41";
renderShadow = "#00ff41";
glowFlicker = 15 + Math.random() * 10;
} else if (isWaving) {
if (glitchState === 1) {
renderColor = "#ff003c";
renderShadow = bloodDark;
renderX += (Math.random() - 0.5) * 10;
renderY += (Math.random() - 0.5) * 10;
glowFlicker = 20 + Math.random() * 15;
} else if (glitchState >= 2) {
renderColor = "#ff003c";
renderShadow = bloodDark;
glowFlicker = 5;
} else {
renderColor = "#00ff41";
renderShadow = "#00ff41";
glowFlicker = 10 + Math.random() * 2;
}
}
ctx.translate(renderX, renderY);
ctx.rotate(this.rotation);
ctx.fillStyle = renderColor;
// OPTIMIZATION 2: Only apply expensive shadowBlur if completely stopped or waving and NOT in low power mode
if (!lowPowerMode && (this.stopped || isWaving)) {
ctx.shadowColor = renderShadow;
ctx.shadowBlur = glowFlicker;
} else {
ctx.shadowBlur = 0;
}
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(this.char, 0, 0);
ctx.restore();
}
}
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function createShrapnel() {
ctx.font = 'bold 80px "JetBrains Mono"';
const totalWidth = ctx.measureText(name).width;
const startX = (canvas.width - totalWidth) / 2;
const startY = canvas.height / 2;
const splatterCount = lowPowerMode ? 12 : 45;
for (let i = 0; i < splatterCount; i++) {
const angle = Math.random() * Math.PI * 2;
const speed = Math.random() * 30 + 10;
splatters.push(new BloodSplatter(canvas.width / 2, canvas.height / 2, Math.cos(angle) * speed, Math.sin(angle) * speed));
}
for (let i = 0; i < name.length; i++) {
if (name[i] === ' ') continue;
const prevWidth = ctx.measureText(name.substring(0, i)).width;
const charWidth = ctx.measureText(name[i]).width;
const charCenterX = startX + prevWidth + (charWidth / 2);
const shard = new LetterShard(name[i], charCenterX, startY);
shard.explode();
particles.push(shard);
}
}
function assignStickFigureTargets() {
const cx = canvas.width / 2;
const cy = canvas.height / 2 + 30;
const targets = [
{ x: cx, y: cy - 110, rot: (Math.random()-0.5)*0.3, id: 'head' },
{ x: cx - 85, y: cy - 5, rot: 0.8, id: 'l_arm_2' },
{ x: cx - 45, y: cy - 40, rot: 0.8, id: 'l_arm_1' },
{ x: cx, y: cy - 40, rot: 0, id: 'torso_1' },
{ x: cx + 45, y: cy - 60, rot: -0.4, id: 'r_arm_1' },
{ x: cx + 80, y: cy - 100, rot: -0.85, id: 'r_arm_2' },
{ x: cx - 35, y: cy + 80, rot: 0, id: 'l_leg' },
{ x: cx, y: cy + 10, rot: 0, id: 'torso_2' },
{ x: cx + 35, y: cy + 80, rot: 0, id: 'r_leg' }
];
particles.forEach((p, index) => {
p.targetX = targets[index].x;
p.targetY = targets[index].y;
p.targetRot = targets[index].rot;
p.id = targets[index].id;
p.startX = p.x;
p.startY = p.y;
p.startRot = p.rotation;
});
}
function drawIdleName() {
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.font = 'bold 80px "JetBrains Mono"';
ctx.fillStyle = "#00ff41";
if (!lowPowerMode) {
ctx.shadowColor = "#00ff41";
ctx.shadowBlur = 10 + Math.sin(Date.now() / 400) * 5;
} else {
ctx.shadowBlur = 0;
}
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(name, 0, 0);
ctx.restore();
}
function drawPulsingName() {
const progress = 1 - (countdown / initialCountdown);
const intensity = Math.pow(progress, 4);
const freq = 5 + (intensity * 60);
const amp = 0.005 + (intensity * 0.15);
const scale = 1 + Math.sin(Date.now() / 1000 * freq) * amp;
ctx.save();
// OPTIMIZATION 3: FORCE SHADOW BLUR OFF to prevent neon leaking
ctx.shadowBlur = 0;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(scale, scale);
ctx.font = 'bold 80px "JetBrains Mono"';
ctx.fillStyle = "#00ff41";
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(name, 0, 0);
if (countdown < 0.6 && Math.random() > 0.4) {
ctx.fillStyle = "rgba(255, 0, 60, 0.7)";
ctx.fillText(name, (Math.random()-0.5)*20, (Math.random()-0.5)*20);
ctx.fillStyle = "rgba(0, 212, 255, 0.7)";
ctx.fillText(name, (Math.random()-0.5)*20, (Math.random()-0.5)*20);
}
ctx.restore();
}
function animate() {
// CALCULATE DELTA TIME TO PREVENT SLOW MOTION
const now = Date.now();
const dt = (now - lastFrameTime) / 1000;
lastFrameTime = now;
// Dynamic performance monitoring (e.g. for low battery/low power mode throttling)
if (!lowPowerMode) {
frameTimes.push(dt);
if (frameTimes.length > 25) {
frameTimes.shift();
// If more than 35% of recent frames take > 28ms (~35 FPS)
const slowFrames = frameTimes.filter(t => t > 0.028).length;
if (slowFrames > 8) {
lowPowerMode = true;
console.log("[Performance Monitor] Frame rate dropped or device throttled. Activating high-performance/low-power mode.");
}
}
}
const cycleTime = Date.now() % 17000;
let newPhase = 0;
if (cycleTime > 5000 && cycleTime < 5200) newPhase = 1;
else if (cycleTime >= 5200 && cycleTime < 6200) newPhase = 0;
else if (cycleTime >= 6200 && cycleTime < 6400) newPhase = 1;
else if (cycleTime >= 6400 && cycleTime < 7800) newPhase = 2;
else if (cycleTime >= 7800 && cycleTime < 11000) newPhase = 3;
else if (cycleTime >= 11000 && cycleTime < 12200) newPhase = 4;
else if (cycleTime >= 12200 && cycleTime < 15500) newPhase = 5;
else if (cycleTime >= 15500 && cycleTime < 17000) newPhase = 6;
if (newPhase === 3) {
const headParticle = particles.find(p => p.id === 'head');
if (headParticle) {
let targetOffsetX = headParticle.x - (canvas.width / 2) + 35;
let dist = targetOffsetX - bodyOffsetX;
if (Math.abs(dist) > 30) {
if (Math.random() > 0.92) {
bodyOffsetX += dist * (Math.random() * 0.1 + 0.02);
walkCycle += Math.random() * 0.6 + 0.2;
}
}
}
} else if (newPhase === 4) {
bodyOffsetY += (50 - bodyOffsetY) * 0.04;
} else if (newPhase === 5) {
let dist = 0 - bodyOffsetX;
if (Math.abs(dist) > 30) {
if (Math.random() > 0.92) {
bodyOffsetX += dist * (Math.random() * 0.1 + 0.02);
walkCycle += Math.random() * 0.6 + 0.2;
}
} else {
bodyOffsetX = 0;
}
bodyOffsetY += (0 - bodyOffsetY) * 0.04;
} else if (newPhase === 6) {
bodyOffsetX += (0 - bodyOffsetX) * 0.05;
bodyOffsetY += (0 - bodyOffsetY) * 0.05;
walkCycle = 0;
} else {
bodyOffsetX = 0;
bodyOffsetY = 0;
walkCycle = 0;
}
if (newPhase === 2 && glitchState !== 2 && isWaving) {
const head = particles.find(p => p.id === 'head');
if (head) {
head.vy = -12 - Math.random() * 4;
head.vx = -Math.abs(Math.random() * 5 + 4);
head.spin = (Math.random() - 0.5) * 0.8;
const popSplatters = lowPowerMode ? 8 : 25;
for(let i = 0; i < popSplatters; i++) {
splatters.push(new BloodSplatter(head.targetX, head.targetY, (Math.random()-0.5)*15, Math.random()*-15));
}
}
}
glitchState = newPhase;
const allLettersStopped = exploded && particles.length > 0 && particles.every(p => p.stopped);
if (allLettersStopped && !isCrawling && !isWaving) {
if (timeSinceStop === 0) timeSinceStop = Date.now();
let elapsedIdle = Date.now() - timeSinceStop;
if (elapsedIdle > 1500 && elapsedIdle < 1700) {
if (Math.random() > 0.1) {
particles[0].x += (Math.random() - 0.5) * 14;
particles[0].y += (Math.random() - 0.5) * 14;
particles[0].rotation += (Math.random() - 0.5) * 0.8;
if (Math.random() > 0.6) {
splatters.push(new BloodSplatter(particles[0].x, particles[0].y, (Math.random()-0.5)*6, (Math.random()-0.5)*6, true));
}
}
}
if (elapsedIdle > 2700) {
isCrawling = true;
crawlStartTime = Date.now();
assignStickFigureTargets();
}
}
if (isCrawling && particles.every(p => p.inPosition)) {
isCrawling = false;
isWaving = true;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!isCountingDown) {
drawIdleName();
} else if (!exploded) {
// SUBTRACT EXACT TIME PASSED INSTEAD OF FIXED FRAME COUNT
countdown = (parseFloat(countdown) - dt).toFixed(2);
if (countdown <= 0) {
countdown = 0;
exploded = true;
createShrapnel();
// REVERTED GLITCH FIX: Shake body, not Canvas, to prevent layout thrashing lag
document.body.style.animation = "glitch 0.3s 2";
}
drawPulsingName();
} else {
// OPTIMIZATION 4: Keep blood on the floor, but hard-cap the array to prevent GPU memory leak
splatters = splatters.filter(s => s.alpha > 0);
const maxSplatters = lowPowerMode ? 40 : 200;
if (splatters.length > maxSplatters) splatters.splice(0, splatters.length - maxSplatters);
splatters.forEach(s => {
s.update(allLettersStopped);
s.draw();
});
ctx.font = 'bold 80px "JetBrains Mono"';
particles.forEach(p => {
p.update();
p.draw();
});
}
window.heroAnimationFrame = requestAnimationFrame(animate);
}
resize();
animate();
window.addEventListener('resize', resize);
};