-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
511 lines (459 loc) · 16.3 KB
/
main.js
File metadata and controls
511 lines (459 loc) · 16.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
const eyes = document.querySelectorAll('.eye');
const cursor = document.createElement('div');
cursor.className = 'cursor';
document.body.appendChild(cursor);
const cursorTarget = { x: window.innerWidth / 2, y: window.innerHeight / 2 };
const cursorCurrent = { ...cursorTarget };
let cursorRafId = null;
let cursorVisible = true;
const activeEffects = new Set();
const MAX_EFFECTS = 140;
let lastClickTime = 0;
const MIN_CLICK_INTERVAL = 120;
const baseTypes = [
{ name: 'burst', className: 'effect-burst', multiplier: 1, count: 1 },
{ name: 'ripple', className: 'effect-ripple', multiplier: 1.4, count: 1 },
{ name: 'spark', className: 'effect-spark', multiplier: 0.25, count: 10 },
{ name: 'spiral', className: 'effect-spiral', multiplier: 1.2, count: 1 },
{ name: 'pulse', className: 'effect-pulse', multiplier: 1.6, count: 1 },
{ name: 'comet', className: 'effect-comet', multiplier: 0.4, count: 4 },
];
const palettes = [
['#ff5f6d', '#ffc371'],
['#42e695', '#3bb2b8'],
['#f953c6', '#b91d73'],
['#30cfd0', '#330867'],
['#f6d365', '#fda085'],
['#a18cd1', '#fbc2eb'],
['#fda085', '#f6d365'],
['#5ee7df', '#b490ca'],
['#c6ffdd', '#fbd786'],
['#f7797d', '#c471ed'],
['#f8ffae', '#43c6ac'],
['#ff9a9e', '#fad0c4'],
];
const shapes = [
{ name: 'circle', radius: '50%', rotate: 0 },
{ name: 'pill', radius: '50% / 35%', rotate: 0 },
{ name: 'diamond', radius: '15%', rotate: 45 },
{ name: 'square', radius: '12%', rotate: 0 },
];
const effectVariants = [];
baseTypes.forEach((base) => {
palettes.forEach((palette, paletteIndex) => {
shapes.forEach((shape, shapeIndex) => {
const duration = 900 + paletteIndex * 80 + shapeIndex * 45;
const size = 70 + paletteIndex * 8;
effectVariants.push({
key: `${base.name}-${paletteIndex}-${shape.name}`,
...base,
palette,
shape,
duration: duration * base.multiplier,
size: size * base.multiplier,
});
});
});
});
const rand = (min, max) => Math.random() * (max - min) + min;
const pick = (list) => list[Math.floor(Math.random() * list.length)];
function registerEffect(effect) {
activeEffects.add(effect);
document.body.appendChild(effect);
effect.addEventListener(
'animationend',
() => {
effect.remove();
activeEffects.delete(effect);
},
{ once: true }
);
}
function getEyeCenters() {
return Array.from(eyes).map((eye) => {
const rect = eye.getBoundingClientRect();
return {
eye,
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2,
};
});
}
function updateEyes(event) {
eyes.forEach((eye) => {
const pupil = eye.querySelector('.pupil');
const rect = eye.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const dx = event.clientX - centerX;
const dy = event.clientY - centerY;
const angle = Math.atan2(dy, dx);
const distance = Math.min(rect.width * 0.25, Math.hypot(dx, dy) * 0.2);
const offsetX = Math.cos(angle) * distance;
const offsetY = Math.sin(angle) * distance;
pupil.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
});
}
function syncCursor(event) {
cursorTarget.x = event.clientX;
cursorTarget.y = event.clientY;
if (!cursorVisible) {
cursorVisible = true;
cursor.style.opacity = '1';
}
if (!cursorRafId) {
cursorRafId = requestAnimationFrame(cursorLoop);
}
}
function cursorLoop() {
const dx = cursorTarget.x - cursorCurrent.x;
const dy = cursorTarget.y - cursorCurrent.y;
const distance = Math.hypot(dx, dy);
const stiffness = 0.4;
if (distance < 0.5) {
cursorCurrent.x = cursorTarget.x;
cursorCurrent.y = cursorTarget.y;
cursorRafId = null;
} else {
cursorCurrent.x += dx * stiffness;
cursorCurrent.y += dy * stiffness;
cursorRafId = requestAnimationFrame(cursorLoop);
}
cursor.style.left = `${cursorCurrent.x}px`;
cursor.style.top = `${cursorCurrent.y}px`;
}
function cleanupOldEffects() {
if (activeEffects.size <= MAX_EFFECTS) return;
const excess = activeEffects.size - MAX_EFFECTS;
const iterator = activeEffects.values();
for (let i = 0; i < excess; i++) {
const el = iterator.next().value;
if (el) {
el.remove();
activeEffects.delete(el);
}
}
}
function spawnVariantEffects(event) {
const variant = pick(effectVariants);
const total = variant.count ?? 1;
for (let i = 0; i < total; i++) {
const effect = document.createElement('div');
effect.className = `effect ${variant.className}`;
effect.style.left = `${event.clientX}px`;
effect.style.top = `${event.clientY}px`;
effect.style.translate = '-50% -50%';
const duration = rand(variant.duration * 0.8, variant.duration * 1.2);
const size = rand(variant.size * 0.8, variant.size * 1.2);
effect.style.setProperty('--color-main', variant.palette[0]);
effect.style.setProperty('--color-secondary', variant.palette[1 % variant.palette.length]);
effect.style.setProperty('--duration', `${duration}ms`);
effect.style.setProperty('--size', `${size}px`);
effect.style.setProperty('--shape-radius', variant.shape.radius);
if (variant.className === 'effect-spark' || variant.className === 'effect-comet') {
const angle = rand(0, Math.PI * 2);
const distance = rand(60, 200);
const offsetX = Math.cos(angle) * distance;
const offsetY = Math.sin(angle) * distance * -1;
effect.style.setProperty('--spark-x', `${offsetX}px`);
effect.style.setProperty('--spark-y', `${offsetY}px`);
effect.style.setProperty('--comet-x', `${offsetX}px`);
effect.style.setProperty('--comet-y', `${offsetY}px`);
}
if (variant.shape.rotate) {
effect.style.rotate = `${variant.shape.rotate}deg`;
}
registerEffect(effect);
}
}
function shootEyeBeams(pointer) {
const centers = getEyeCenters();
const palette = pick(palettes);
centers.forEach((center, index) => {
const beam = document.createElement('div');
beam.className = 'effect effect-laser';
beam.style.left = `${center.x}px`;
beam.style.top = `${center.y}px`;
const dx = pointer.x - center.x;
const dy = pointer.y - center.y;
const length = Math.hypot(dx, dy);
const angle = (Math.atan2(dy, dx) * 180) / Math.PI;
beam.style.rotate = `${angle}deg`;
beam.style.setProperty('--beam-length', `${length}px`);
beam.style.setProperty('--duration', `${rand(420, 680)}ms`);
beam.style.setProperty('--color-main', palette[index % palette.length]);
registerEffect(beam);
const burst = document.createElement('div');
burst.className = 'effect effect-gaze-burst';
burst.style.left = `${center.x}px`;
burst.style.top = `${center.y}px`;
burst.style.translate = '-50% -50%';
burst.style.setProperty('--color-main', palette[(index + 1) % palette.length]);
burst.style.setProperty('--duration', `${rand(500, 900)}ms`);
registerEffect(burst);
});
}
function drawPointerEcho(pointer) {
const echo = document.createElement('div');
echo.className = 'effect effect-pointer-echo';
echo.style.left = `${pointer.x}px`;
echo.style.top = `${pointer.y}px`;
echo.style.translate = '-50% -50%';
echo.style.setProperty('--duration', `${rand(600, 1100)}ms`);
registerEffect(echo);
}
function fireBulletBurst(pointer) {
const centers = getEyeCenters();
centers.forEach((center) => {
for (let i = 0; i < 3; i++) {
const bullet = document.createElement('div');
bullet.className = 'effect effect-bullet';
bullet.style.left = `${center.x}px`;
bullet.style.top = `${center.y}px`;
const dx = pointer.x - center.x;
const dy = pointer.y - center.y;
const jitter = rand(-0.15, 0.15);
const multiplier = 1 + rand(0.05, 0.3);
bullet.style.setProperty('--travel-x', `${dx * multiplier + jitter * 80}px`);
bullet.style.setProperty('--travel-y', `${dy * multiplier + jitter * 80}px`);
bullet.style.setProperty('--duration', `${rand(320, 520)}ms`);
bullet.style.setProperty('--bullet-angle', `${(Math.atan2(dy, dx) * 180) / Math.PI}deg`);
registerEffect(bullet);
}
});
}
function launchMissiles(pointer) {
const missiles = Math.floor(rand(2, 4));
for (let i = 0; i < missiles; i++) {
const radius = rand(220, 420);
const angle = rand(Math.PI * -0.25, Math.PI * 1.25);
const startX = pointer.x + Math.cos(angle) * radius;
const startY = pointer.y + Math.sin(angle) * radius;
const missile = document.createElement('div');
missile.className = 'effect effect-missile';
missile.style.left = `${pointer.x}px`;
missile.style.top = `${pointer.y}px`;
missile.style.translate = '-50% -50%';
missile.style.setProperty('--from-x', `${startX - pointer.x}px`);
missile.style.setProperty('--from-y', `${startY - pointer.y}px`);
missile.style.setProperty('--duration', `${rand(900, 1400)}ms`);
missile.style.rotate = `${(Math.atan2(pointer.y - startY, pointer.x - startX) * 180) / Math.PI}deg`;
registerEffect(missile);
}
}
function spawnInkTrail(pointer) {
const blobs = 14;
for (let i = 0; i < blobs; i++) {
const blob = document.createElement('div');
blob.className = 'effect effect-ink-trail';
const angle = rand(-Math.PI, Math.PI);
const radius = rand(6, 120);
blob.style.left = `${pointer.x - Math.cos(angle) * radius}px`;
blob.style.top = `${pointer.y - Math.sin(angle) * radius}px`;
blob.style.translate = '-50% -50%';
blob.style.setProperty('--duration', `${rand(400, 900)}ms`);
blob.style.setProperty('--blob-scale', rand(0.6, 1.4).toFixed(2));
blob.style.setProperty('--blob-rotate', `${rand(-25, 25)}deg`);
registerEffect(blob);
}
}
function dropTears() {
const centers = getEyeCenters();
centers.forEach((center) => {
const tear = document.createElement('div');
tear.className = 'effect effect-tear';
tear.style.left = `${center.x}px`;
tear.style.top = `${center.y}px`;
tear.style.translate = '-50% -50%';
tear.style.setProperty('--drop-distance', `${rand(140, 240)}px`);
tear.style.setProperty('--duration', `${rand(900, 1200)}ms`);
registerEffect(tear);
});
}
function chainLightning(pointer) {
const centers = getEyeCenters();
centers.forEach((center) => {
const bolt = document.createElement('div');
bolt.className = 'effect effect-lightning';
bolt.style.left = `${center.x}px`;
bolt.style.top = `${center.y}px`;
const dx = pointer.x - center.x;
const dy = pointer.y - center.y;
const length = Math.hypot(dx, dy);
bolt.style.rotate = `${(Math.atan2(dy, dx) * 180) / Math.PI}deg`;
bolt.style.setProperty('--beam-length', `${length}px`);
bolt.style.setProperty('--duration', `${rand(260, 420)}ms`);
registerEffect(bolt);
});
}
function bubbleBurst(pointer) {
const bubbles = 8;
for (let i = 0; i < bubbles; i++) {
const bubble = document.createElement('div');
bubble.className = 'effect effect-bubble';
bubble.style.left = `${pointer.x}px`;
bubble.style.top = `${pointer.y}px`;
bubble.style.translate = '-50% -50%';
const angle = rand(Math.PI * -0.3, Math.PI * 0.3);
const distance = rand(80, 200);
bubble.style.setProperty('--bubble-x', `${Math.cos(angle) * distance}px`);
bubble.style.setProperty('--bubble-y', `${-Math.abs(Math.sin(angle) * distance) - rand(40, 120)}px`);
bubble.style.setProperty('--duration', `${rand(1200, 2000)}ms`);
bubble.style.setProperty('--bubble-size', `${rand(40, 90)}px`);
registerEffect(bubble);
}
}
function portalSummon(pointer) {
const portal = document.createElement('div');
portal.className = 'effect effect-portal';
portal.style.left = `${pointer.x}px`;
portal.style.top = `${pointer.y}px`;
portal.style.translate = '-50% -50%';
portal.style.setProperty('--duration', `${rand(900, 1500)}ms`);
registerEffect(portal);
const stars = 4;
for (let i = 0; i < stars; i++) {
const star = document.createElement('div');
star.className = 'effect effect-mini-star';
star.style.left = `${pointer.x}px`;
star.style.top = `${pointer.y}px`;
star.style.translate = '-50% -50%';
const angle = rand(0, Math.PI * 2);
const distance = rand(80, 200);
star.style.setProperty('--star-x', `${Math.cos(angle) * distance}px`);
star.style.setProperty('--star-y', `${Math.sin(angle) * distance}px`);
star.style.setProperty('--duration', `${rand(800, 1400)}ms`);
registerEffect(star);
}
}
function audioPulse(pointer) {
const rings = 3;
for (let i = 0; i < rings; i++) {
const ring = document.createElement('div');
ring.className = 'effect effect-audio-ring';
ring.style.left = `${pointer.x}px`;
ring.style.top = `${pointer.y}px`;
ring.style.translate = '-50% -50%';
ring.style.setProperty('--duration', `${600 + i * 200}ms`);
ring.style.setProperty('--ring-delay', `${i * 60}ms`);
registerEffect(ring);
}
}
function droneOrbit(pointer) {
const drone = document.createElement('div');
drone.className = 'effect effect-drone';
drone.style.left = `${pointer.x}px`;
drone.style.top = `${pointer.y}px`;
drone.style.translate = '-50% -50%';
drone.style.setProperty('--orbit-radius', `${rand(60, 140)}px`);
drone.style.setProperty('--dash-x', `${rand(-40, 40)}px`);
drone.style.setProperty('--dash-y', `${rand(-40, 40)}px`);
drone.style.setProperty('--duration', `${rand(1200, 1700)}ms`);
registerEffect(drone);
}
function coinMagnet(pointer) {
const coins = 6;
for (let i = 0; i < coins; i++) {
const coin = document.createElement('div');
coin.className = 'effect effect-coin';
coin.style.left = `${pointer.x}px`;
coin.style.top = `${pointer.y}px`;
coin.style.translate = '-50% -50%';
const startAngle = rand(0, Math.PI * 2);
const startRadius = rand(120, 260);
coin.style.setProperty('--coin-from-x', `${Math.cos(startAngle) * startRadius}px`);
coin.style.setProperty('--coin-from-y', `${Math.sin(startAngle) * startRadius}px`);
coin.style.setProperty('--duration', `${rand(700, 1100)}ms`);
registerEffect(coin);
}
}
function pixelDisperse(pointer) {
const pixels = 24;
for (let i = 0; i < pixels; i++) {
const pixel = document.createElement('div');
pixel.className = 'effect effect-pixel';
pixel.style.left = `${pointer.x}px`;
pixel.style.top = `${pointer.y}px`;
pixel.style.translate = '-50% -50%';
const angle = rand(0, Math.PI * 2);
const distance = rand(20, 140);
pixel.style.setProperty('--pixel-x', `${Math.cos(angle) * distance}px`);
pixel.style.setProperty('--pixel-y', `${Math.sin(angle) * distance}px`);
pixel.style.setProperty('--duration', `${rand(500, 900)}ms`);
registerEffect(pixel);
}
}
const specialAttacks = [
shootEyeBeams,
fireBulletBurst,
launchMissiles,
spawnInkTrail,
dropTears,
chainLightning,
bubbleBurst,
portalSummon,
audioPulse,
droneOrbit,
coinMagnet,
pixelDisperse,
];
function spawnEffect(event) {
if (event.button !== 0) return;
const now = performance.now();
if (now - lastClickTime < MIN_CLICK_INTERVAL) return;
lastClickTime = now;
const pointer = { x: event.clientX, y: event.clientY };
spawnVariantEffects(event);
const attack = pick(specialAttacks);
attack(pointer);
drawPointerEcho(pointer);
cleanupOldEffects();
}
let pendingPointer = null;
let rafId = null;
function handlePointerFrame() {
if (!pendingPointer) return;
updateEyes(pendingPointer);
pendingPointer = null;
rafId = null;
}
window.addEventListener('pointermove', (event) => {
pendingPointer = {
clientX: event.clientX,
clientY: event.clientY,
};
if (!rafId) {
rafId = requestAnimationFrame(handlePointerFrame);
}
syncCursor(event);
});
window.addEventListener('click', spawnEffect);
window.addEventListener(
'mousedown',
(event) => {
if (event.button === 0) {
event.preventDefault();
}
},
{ passive: false }
);
window.addEventListener('resize', () => {
cursor.style.display = window.innerWidth < 600 ? 'none' : 'block';
if (window.innerWidth < 600) {
cursorVisible = false;
cursor.style.opacity = '0';
}
});
window.addEventListener('pointerleave', () => {
cursorVisible = false;
cursor.style.opacity = '0';
});
window.addEventListener('pointerenter', (event) => {
cursorVisible = true;
cursor.style.opacity = '1';
cursorTarget.x = event.clientX;
cursorTarget.y = event.clientY;
if (!cursorRafId) {
cursorRafId = requestAnimationFrame(cursorLoop);
}
});