Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1554 lines (1366 loc) · 52 KB
/
Copy pathscript.js
File metadata and controls
1554 lines (1366 loc) · 52 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TouchTexture class
class TouchTexture {
constructor() {
this.size = 64;
this.width = this.height = this.size;
this.maxAge = 64;
this.radius = 0.25 * this.size;
this.speed = 1 / this.maxAge;
this.trail = [];
this.last = null;
this.initTexture();
}
initTexture() {
this.canvas = document.createElement("canvas");
this.canvas.width = this.width;
this.canvas.height = this.height;
this.ctx = this.canvas.getContext("2d");
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.texture = new THREE.Texture(this.canvas);
}
update() {
this.clear();
let speed = this.speed;
for (let i = this.trail.length - 1; i >= 0; i--) {
const point = this.trail[i];
let f = point.force * speed * (1 - point.age / this.maxAge);
point.x += point.vx * f;
point.y += point.vy * f;
point.age++;
if (point.age > this.maxAge) {
this.trail.splice(i, 1);
} else {
this.drawPoint(point);
}
}
this.texture.needsUpdate = true;
}
clear() {
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
}
addTouch(point) {
let force = 0;
let vx = 0;
let vy = 0;
const last = this.last;
if (last) {
const dx = point.x - last.x;
const dy = point.y - last.y;
if (dx === 0 && dy === 0) return;
const dd = dx * dx + dy * dy;
let d = Math.sqrt(dd);
vx = dx / d;
vy = dy / d;
force = Math.min(dd * 20000, 2.0);
}
this.last = { x: point.x, y: point.y };
this.trail.push({ x: point.x, y: point.y, age: 0, force, vx, vy });
}
drawPoint(point) {
const pos = {
x: point.x * this.width,
y: (1 - point.y) * this.height
};
let intensity = 1;
if (point.age < this.maxAge * 0.3) {
intensity = Math.sin((point.age / (this.maxAge * 0.3)) * (Math.PI / 2));
} else {
const t = 1 - (point.age - this.maxAge * 0.3) / (this.maxAge * 0.7);
intensity = -t * (t - 2);
}
intensity *= point.force;
const radius = this.radius;
let color = `${((point.vx + 1) / 2) * 255}, ${
((point.vy + 1) / 2) * 255
}, ${intensity * 255}`;
let offset = this.size * 5;
this.ctx.shadowOffsetX = offset;
this.ctx.shadowOffsetY = offset;
this.ctx.shadowBlur = radius * 1;
this.ctx.shadowColor = `rgba(${color},${0.2 * intensity})`;
this.ctx.beginPath();
this.ctx.fillStyle = "rgba(255,0,0,1)";
this.ctx.arc(pos.x - offset, pos.y - offset, radius, 0, Math.PI * 2);
this.ctx.fill();
}
}
// GradientBackground class
class GradientBackground {
constructor(sceneManager) {
this.sceneManager = sceneManager;
this.mesh = null;
this.uniforms = {
uTime: { value: 0 },
uResolution: {
value: new THREE.Vector2(window.innerWidth, window.innerHeight)
},
uColor1: { value: new THREE.Vector3(0.945, 0.353, 0.133) },
uColor2: { value: new THREE.Vector3(0.039, 0.055, 0.153) },
uColor3: { value: new THREE.Vector3(0.945, 0.353, 0.133) },
uColor4: { value: new THREE.Vector3(0.039, 0.055, 0.153) },
uColor5: { value: new THREE.Vector3(0.945, 0.353, 0.133) },
uColor6: { value: new THREE.Vector3(0.039, 0.055, 0.153) },
uSpeed: { value: 1.2 },
uIntensity: { value: 1.8 },
uTouchTexture: { value: null },
uGrainIntensity: { value: 0.08 },
uZoom: { value: 1.0 },
uDarkNavy: { value: new THREE.Vector3(0.039, 0.055, 0.153) },
uGradientSize: { value: 1.0 },
uGradientCount: { value: 6.0 },
uColor1Weight: { value: 1.0 },
uColor2Weight: { value: 1.0 }
};
}
init() {
const viewSize = this.sceneManager.getViewSize();
const geometry = new THREE.PlaneGeometry(
viewSize.width,
viewSize.height,
1,
1
);
const material = new THREE.ShaderMaterial({
uniforms: this.uniforms,
vertexShader: `
varying vec2 vUv;
void main() {
vec3 pos = position.xyz;
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.);
vUv = uv;
}
`,
fragmentShader: `
uniform float uTime;
uniform vec2 uResolution;
uniform vec3 uColor1;
uniform vec3 uColor2;
uniform vec3 uColor3;
uniform vec3 uColor4;
uniform vec3 uColor5;
uniform vec3 uColor6;
uniform float uSpeed;
uniform float uIntensity;
uniform sampler2D uTouchTexture;
uniform float uGrainIntensity;
uniform float uZoom;
uniform vec3 uDarkNavy;
uniform float uGradientSize;
uniform float uGradientCount;
uniform float uColor1Weight;
uniform float uColor2Weight;
varying vec2 vUv;
#define PI 3.14159265359
float grain(vec2 uv, float time) {
vec2 grainUv = uv * uResolution * 0.5;
float grainValue = fract(sin(dot(grainUv + time, vec2(12.9898, 78.233))) * 43758.5453);
return grainValue * 2.0 - 1.0;
}
vec3 getGradientColor(vec2 uv, float time) {
float gradientRadius = uGradientSize;
vec2 center1 = vec2(
0.5 + sin(time * uSpeed * 0.4) * 0.4,
0.5 + cos(time * uSpeed * 0.5) * 0.4
);
vec2 center2 = vec2(
0.5 + cos(time * uSpeed * 0.6) * 0.5,
0.5 + sin(time * uSpeed * 0.45) * 0.5
);
vec2 center3 = vec2(
0.5 + sin(time * uSpeed * 0.35) * 0.45,
0.5 + cos(time * uSpeed * 0.55) * 0.45
);
vec2 center4 = vec2(
0.5 + cos(time * uSpeed * 0.5) * 0.4,
0.5 + sin(time * uSpeed * 0.4) * 0.4
);
vec2 center5 = vec2(
0.5 + sin(time * uSpeed * 0.7) * 0.35,
0.5 + cos(time * uSpeed * 0.6) * 0.35
);
vec2 center6 = vec2(
0.5 + cos(time * uSpeed * 0.45) * 0.5,
0.5 + sin(time * uSpeed * 0.65) * 0.5
);
vec2 center7 = vec2(
0.5 + sin(time * uSpeed * 0.55) * 0.38,
0.5 + cos(time * uSpeed * 0.48) * 0.42
);
vec2 center8 = vec2(
0.5 + cos(time * uSpeed * 0.65) * 0.36,
0.5 + sin(time * uSpeed * 0.52) * 0.44
);
vec2 center9 = vec2(
0.5 + sin(time * uSpeed * 0.42) * 0.41,
0.5 + cos(time * uSpeed * 0.58) * 0.39
);
vec2 center10 = vec2(
0.5 + cos(time * uSpeed * 0.48) * 0.37,
0.5 + sin(time * uSpeed * 0.62) * 0.43
);
vec2 center11 = vec2(
0.5 + sin(time * uSpeed * 0.68) * 0.33,
0.5 + cos(time * uSpeed * 0.44) * 0.46
);
vec2 center12 = vec2(
0.5 + cos(time * uSpeed * 0.38) * 0.39,
0.5 + sin(time * uSpeed * 0.56) * 0.41
);
float dist1 = length(uv - center1);
float dist2 = length(uv - center2);
float dist3 = length(uv - center3);
float dist4 = length(uv - center4);
float dist5 = length(uv - center5);
float dist6 = length(uv - center6);
float dist7 = length(uv - center7);
float dist8 = length(uv - center8);
float dist9 = length(uv - center9);
float dist10 = length(uv - center10);
float dist11 = length(uv - center11);
float dist12 = length(uv - center12);
float influence1 = 1.0 - smoothstep(0.0, gradientRadius, dist1);
float influence2 = 1.0 - smoothstep(0.0, gradientRadius, dist2);
float influence3 = 1.0 - smoothstep(0.0, gradientRadius, dist3);
float influence4 = 1.0 - smoothstep(0.0, gradientRadius, dist4);
float influence5 = 1.0 - smoothstep(0.0, gradientRadius, dist5);
float influence6 = 1.0 - smoothstep(0.0, gradientRadius, dist6);
float influence7 = 1.0 - smoothstep(0.0, gradientRadius, dist7);
float influence8 = 1.0 - smoothstep(0.0, gradientRadius, dist8);
float influence9 = 1.0 - smoothstep(0.0, gradientRadius, dist9);
float influence10 = 1.0 - smoothstep(0.0, gradientRadius, dist10);
float influence11 = 1.0 - smoothstep(0.0, gradientRadius, dist11);
float influence12 = 1.0 - smoothstep(0.0, gradientRadius, dist12);
vec2 rotatedUv1 = uv - 0.5;
float angle1 = time * uSpeed * 0.15;
rotatedUv1 = vec2(
rotatedUv1.x * cos(angle1) - rotatedUv1.y * sin(angle1),
rotatedUv1.x * sin(angle1) + rotatedUv1.y * cos(angle1)
);
rotatedUv1 += 0.5;
vec2 rotatedUv2 = uv - 0.5;
float angle2 = -time * uSpeed * 0.12;
rotatedUv2 = vec2(
rotatedUv2.x * cos(angle2) - rotatedUv2.y * sin(angle2),
rotatedUv2.x * sin(angle2) + rotatedUv2.y * cos(angle2)
);
rotatedUv2 += 0.5;
float radialGradient1 = length(rotatedUv1 - 0.5);
float radialGradient2 = length(rotatedUv2 - 0.5);
float radialInfluence1 = 1.0 - smoothstep(0.0, 0.8, radialGradient1);
float radialInfluence2 = 1.0 - smoothstep(0.0, 0.8, radialGradient2);
vec3 color = vec3(0.0);
color += uColor1 * influence1 * (0.55 + 0.45 * sin(time * uSpeed)) * uColor1Weight;
color += uColor2 * influence2 * (0.55 + 0.45 * cos(time * uSpeed * 1.2)) * uColor2Weight;
color += uColor3 * influence3 * (0.55 + 0.45 * sin(time * uSpeed * 0.8)) * uColor1Weight;
color += uColor4 * influence4 * (0.55 + 0.45 * cos(time * uSpeed * 1.3)) * uColor2Weight;
color += uColor5 * influence5 * (0.55 + 0.45 * sin(time * uSpeed * 1.1)) * uColor1Weight;
color += uColor6 * influence6 * (0.55 + 0.45 * cos(time * uSpeed * 0.9)) * uColor2Weight;
if (uGradientCount > 6.0) {
color += uColor1 * influence7 * (0.55 + 0.45 * sin(time * uSpeed * 1.4)) * uColor1Weight;
color += uColor2 * influence8 * (0.55 + 0.45 * cos(time * uSpeed * 1.5)) * uColor2Weight;
color += uColor3 * influence9 * (0.55 + 0.45 * sin(time * uSpeed * 1.6)) * uColor1Weight;
color += uColor4 * influence10 * (0.55 + 0.45 * cos(time * uSpeed * 1.7)) * uColor2Weight;
}
if (uGradientCount > 10.0) {
color += uColor5 * influence11 * (0.55 + 0.45 * sin(time * uSpeed * 1.8)) * uColor1Weight;
color += uColor6 * influence12 * (0.55 + 0.45 * cos(time * uSpeed * 1.9)) * uColor2Weight;
}
color += mix(uColor1, uColor3, radialInfluence1) * 0.45 * uColor1Weight;
color += mix(uColor2, uColor4, radialInfluence2) * 0.4 * uColor2Weight;
color = clamp(color, vec3(0.0), vec3(1.0)) * uIntensity;
float luminance = dot(color, vec3(0.299, 0.587, 0.114));
color = mix(vec3(luminance), color, 1.35);
color = pow(color, vec3(0.92));
float brightness1 = length(color);
float mixFactor1 = max(brightness1 * 1.2, 0.15);
color = mix(uDarkNavy, color, mixFactor1);
float maxBrightness = 1.0;
float brightness = length(color);
if (brightness > maxBrightness) {
color = color * (maxBrightness / brightness);
}
return color;
}
void main() {
vec2 uv = vUv;
vec4 touchTex = texture2D(uTouchTexture, uv);
float vx = -(touchTex.r * 2.0 - 1.0);
float vy = -(touchTex.g * 2.0 - 1.0);
float intensity = touchTex.b;
uv.x += vx * 0.8 * intensity;
uv.y += vy * 0.8 * intensity;
vec2 center = vec2(0.5);
float dist = length(uv - center);
float ripple = sin(dist * 20.0 - uTime * 3.0) * 0.04 * intensity;
float wave = sin(dist * 15.0 - uTime * 2.0) * 0.03 * intensity;
uv += vec2(ripple + wave);
vec3 color = getGradientColor(uv, uTime);
float grainValue = grain(uv, uTime);
color += grainValue * uGrainIntensity;
float timeShift = uTime * 0.5;
color.r += sin(timeShift) * 0.02;
color.g += cos(timeShift * 1.4) * 0.02;
color.b += sin(timeShift * 1.2) * 0.02;
float brightness2 = length(color);
float mixFactor2 = max(brightness2 * 1.2, 0.15);
color = mix(uDarkNavy, color, mixFactor2);
color = clamp(color, vec3(0.0), vec3(1.0));
float maxBrightness = 1.0;
float brightness = length(color);
if (brightness > maxBrightness) {
color = color * (maxBrightness / brightness);
}
gl_FragColor = vec4(color, 1.0);
}
`
});
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.position.z = 0;
this.sceneManager.scene.add(this.mesh);
}
update(delta) {
if (this.uniforms.uTime) {
this.uniforms.uTime.value += delta;
}
}
onResize(width, height) {
const viewSize = this.sceneManager.getViewSize();
if (this.mesh) {
this.mesh.geometry.dispose();
this.mesh.geometry = new THREE.PlaneGeometry(
viewSize.width,
viewSize.height,
1,
1
);
}
if (this.uniforms.uResolution) {
this.uniforms.uResolution.value.set(width, height);
}
}
}
// App class
class App {
constructor() {
this.renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance",
alpha: false,
stencil: false,
depth: false
});
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
this.renderer.setAnimationLoop(null);
document.body.appendChild(this.renderer.domElement);
this.renderer.domElement.id = "webGLApp";
this.renderer.domElement.style.pointerEvents = "none";
this.camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
10000
);
this.camera.position.z = 50;
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0x0a0e27);
this.clock = new THREE.Clock();
this.touchTexture = new TouchTexture();
this.gradientBackground = new GradientBackground(this);
this.gradientBackground.uniforms.uTouchTexture.value = this.touchTexture.texture;
this.colorSchemes = {
1: {
color1: new THREE.Vector3(0.945, 0.353, 0.133),
color2: new THREE.Vector3(0.039, 0.055, 0.153)
},
2: {
color1: new THREE.Vector3(1.0, 0.424, 0.314),
color2: new THREE.Vector3(0.251, 0.878, 0.816)
},
3: {
color1: new THREE.Vector3(0.945, 0.353, 0.133),
color2: new THREE.Vector3(0.039, 0.055, 0.153),
color3: new THREE.Vector3(0.251, 0.878, 0.816)
},
4: {
color1: new THREE.Vector3(0.949, 0.4, 0.2),
color2: new THREE.Vector3(0.176, 0.42, 0.427),
color3: new THREE.Vector3(0.82, 0.686, 0.612)
},
5: {
color1: new THREE.Vector3(0.945, 0.353, 0.133),
color2: new THREE.Vector3(0.0, 0.259, 0.22),
color3: new THREE.Vector3(0.945, 0.353, 0.133),
color4: new THREE.Vector3(0.0, 0.0, 0.0),
color5: new THREE.Vector3(0.945, 0.353, 0.133),
color6: new THREE.Vector3(0.0, 0.0, 0.0)
}
};
this.currentScheme = 1;
this.init();
}
setColorScheme(scheme) {
if (!this.colorSchemes[scheme]) return;
this.currentScheme = scheme;
const colors = this.colorSchemes[scheme];
const uniforms = this.gradientBackground.uniforms;
if (scheme === 3) {
uniforms.uColor1.value.copy(colors.color1);
uniforms.uColor2.value.copy(colors.color2);
uniforms.uColor3.value.copy(colors.color3);
uniforms.uColor4.value.copy(colors.color1);
uniforms.uColor5.value.copy(colors.color2);
uniforms.uColor6.value.copy(colors.color3);
} else if (scheme === 4) {
uniforms.uColor1.value.copy(colors.color1);
uniforms.uColor2.value.copy(colors.color2);
uniforms.uColor3.value.copy(colors.color3);
uniforms.uColor4.value.copy(colors.color1);
uniforms.uColor5.value.copy(colors.color2);
uniforms.uColor6.value.copy(colors.color3);
} else if (scheme === 5) {
uniforms.uColor1.value.copy(colors.color1);
uniforms.uColor2.value.copy(colors.color2);
uniforms.uColor3.value.copy(colors.color3);
uniforms.uColor4.value.copy(colors.color4);
uniforms.uColor5.value.copy(colors.color5);
uniforms.uColor6.value.copy(colors.color6);
} else {
uniforms.uColor1.value.copy(colors.color1);
uniforms.uColor2.value.copy(colors.color2);
uniforms.uColor3.value.copy(colors.color1);
uniforms.uColor4.value.copy(colors.color2);
uniforms.uColor5.value.copy(colors.color1);
uniforms.uColor6.value.copy(colors.color2);
}
if (scheme === 1) {
this.scene.background = new THREE.Color(0x0a0e27);
uniforms.uDarkNavy.value.set(0.039, 0.055, 0.153);
uniforms.uGradientSize.value = 0.45;
uniforms.uGradientCount.value = 12.0;
uniforms.uSpeed.value = 1.5;
uniforms.uColor1Weight.value = 0.5;
uniforms.uColor2Weight.value = 1.8;
} else if (scheme === 2) {
this.scene.background = new THREE.Color(0x0a0e27);
uniforms.uDarkNavy.value.set(0.039, 0.055, 0.153);
uniforms.uGradientSize.value = 1.0;
uniforms.uGradientCount.value = 6.0;
uniforms.uSpeed.value = 1.2;
uniforms.uColor1Weight.value = 1.0;
uniforms.uColor2Weight.value = 1.0;
} else {
this.scene.background = new THREE.Color(0x0a0e27);
uniforms.uDarkNavy.value.set(0.039, 0.055, 0.153);
uniforms.uGradientSize.value = 1.0;
uniforms.uGradientCount.value = 6.0;
uniforms.uSpeed.value = 1.2;
uniforms.uColor1Weight.value = 1.0;
uniforms.uColor2Weight.value = 1.0;
}
}
init() {
this.gradientBackground.init();
this.setColorScheme(1);
this.render();
this.tick();
window.addEventListener("resize", () => this.onResize());
window.addEventListener("mousemove", (ev) => this.onMouseMove(ev));
window.addEventListener("touchmove", (ev) => this.onTouchMove(ev));
document.addEventListener("visibilitychange", () => {
if (!document.hidden) {
this.render();
}
});
const wakeUpAnimation = () => {
this.render();
window.removeEventListener("click", wakeUpAnimation);
window.removeEventListener("touchstart", wakeUpAnimation);
window.removeEventListener("mousemove", wakeUpAnimation);
};
window.addEventListener("click", wakeUpAnimation, { once: true });
window.addEventListener("touchstart", wakeUpAnimation, { once: true });
window.addEventListener("mousemove", wakeUpAnimation, { once: true });
}
onTouchMove(ev) {
const touch = ev.touches[0];
this.onMouseMove({ clientX: touch.clientX, clientY: touch.clientY });
}
onMouseMove(ev) {
this.mouse = {
x: ev.clientX / window.innerWidth,
y: 1 - ev.clientY / window.innerHeight
};
this.touchTexture.addTouch(this.mouse);
}
getViewSize() {
const fovInRadians = (this.camera.fov * Math.PI) / 180;
const height = Math.abs(
this.camera.position.z * Math.tan(fovInRadians / 2) * 2
);
return { width: height * this.camera.aspect, height };
}
update(delta) {
this.touchTexture.update();
this.gradientBackground.update(delta);
}
render() {
const delta = this.clock.getDelta();
const clampedDelta = Math.min(delta, 0.1);
this.renderer.render(this.scene, this.camera);
this.update(clampedDelta);
}
tick() {
this.render();
requestAnimationFrame(() => this.tick());
}
onResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.gradientBackground.onResize(window.innerWidth, window.innerHeight);
}
}
// Start the app
const app = new App();
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => {
app.render();
});
} else {
setTimeout(() => app.render(), 0);
}
// GameShelf nav link: animated navigation on plain left-click only
const gameshelfLink = document.getElementById("gameshelfLink");
const gameshelfTransition = document.getElementById("gameshelfTransition");
if (gameshelfLink && gameshelfTransition) {
gameshelfLink.addEventListener("click", (e) => {
// Let modified clicks / middle-click / etc. behave like a normal <a>
if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
return;
}
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (prefersReducedMotion) {
return;
}
e.preventDefault();
const destination = gameshelfLink.href;
// Iris grows from wherever the link actually sits (varies by page/line)
const rect = gameshelfLink.getBoundingClientRect();
const originX = ((rect.left + rect.width / 2) / window.innerWidth) * 100;
const originY = ((rect.top + rect.height / 2) / window.innerHeight) * 100;
gameshelfTransition.style.setProperty("--iris-x", `${originX.toFixed(2)}%`);
gameshelfTransition.style.setProperty("--iris-y", `${originY.toFixed(2)}%`);
let navigated = false;
const goToGameshelf = () => {
if (navigated) return;
navigated = true;
window.location.href = destination;
};
gameshelfLink.classList.add("is-leaving");
gameshelfTransition.classList.add("is-active");
document.body.classList.add("is-navigating");
gameshelfTransition.addEventListener("transitionend", goToGameshelf, {
once: true,
});
// Fallback in case the transition never fires
setTimeout(goToGameshelf, 600);
});
}
// OS shell: desktop icons open a single floating window
const osDesktopIcons = document.querySelectorAll(".os-icon[data-window]");
const osWindow = document.getElementById("osWindow");
const osWindowTitle = document.getElementById("osWindowTitle");
const osWindowClose = document.getElementById("osWindowClose");
const osWindowMinimize = document.getElementById("osWindowMinimize");
const osWindowExpand = document.getElementById("osWindowExpand");
const osTaskbarChip = document.getElementById("osTaskbarChip");
const osViews = document.querySelectorAll(".os-view");
const osWindowTitles = {
home: "ABOUT.TXT",
projects: "PROJECTS.SYS",
resume: "RESUME.SYS",
pong: "PONG.EXE",
thewall: "THE WALL.EXE",
};
function openOsWindow(name) {
osViews.forEach((view) =>
view.classList.toggle("is-active", view.id === `view-${name}`)
);
osDesktopIcons.forEach((i) =>
i.classList.toggle("is-active", i.getAttribute("data-window") === name)
);
osWindowTitle.textContent = `BAILEY // ${osWindowTitles[name] || "OS 2.0"}`;
osWindow.classList.add("is-open");
if (osTaskbarChip) {
osTaskbarChip.textContent = osWindowTitles[name] || name.toUpperCase();
osTaskbarChip.hidden = false;
}
}
function closeOsWindow() {
osWindow.classList.remove("is-open");
osDesktopIcons.forEach((i) => i.classList.remove("is-active"));
if (osTaskbarChip) osTaskbarChip.hidden = true;
}
// real minimize, now gated behind the genie's "Yes, Mr. Genie." dialogue choice
function minimizeOsWindow() {
if (!osWindow.classList.contains("is-open")) return;
const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReducedMotion) {
closeOsWindow();
return;
}
osWindow.classList.add("is-minimizing");
osWindow.addEventListener(
"animationend",
() => {
osWindow.classList.remove("is-open", "is-minimizing");
osDesktopIcons.forEach((i) => i.classList.remove("is-active"));
if (osTaskbarChip) osTaskbarChip.hidden = true;
},
{ once: true }
);
}
if (osDesktopIcons.length && osWindow) {
osDesktopIcons.forEach((icon) => {
icon.addEventListener("click", () => {
const target = icon.getAttribute("data-window");
const alreadyOpenOnThis =
osWindow.classList.contains("is-open") &&
icon.classList.contains("is-active");
if (alreadyOpenOnThis) {
closeOsWindow();
} else {
openOsWindow(target);
}
});
});
if (osWindowClose) {
osWindowClose.addEventListener("click", closeOsWindow);
}
if (osWindowMinimize) {
osWindowMinimize.addEventListener("click", triggerGenieEasterEgg);
}
if (osWindowExpand) {
osWindowExpand.addEventListener("click", () => triggerGlitchEasterEgg(osWindowExpand));
}
}
// titlebar easter egg: minimize summons a genie who immediately has second thoughts
const osGenieOverlay = document.getElementById("osGenieOverlay");
const osGenieBottle = document.getElementById("osGenieBottle");
const osGenieFigure = document.getElementById("osGenieFigure");
const osGenieDialogue = document.getElementById("osGenieDialogue");
const osGenieLine = document.getElementById("osGenieLine");
const osGenieChoiceYes = document.getElementById("osGenieChoiceYes");
const osGenieChoiceNo = document.getElementById("osGenieChoiceNo");
let genieRunning = false;
let genieDismissTimeout = null;
let genieStage = 0;
const GENIE_LINE_ASK = "you sure about that?";
const GENIE_LINE_INTRO =
"oh! i'm the genie from barbie magic genie bottle, circa 2000. we use the LATEST tech and graphics technologies. quite ahead of it's time. thank you for asking.";
const GENIE_LINE_BYE = "BACK TO THE BOTTLE";
function spawnGenieSparkles(x, y) {
const layer = document.getElementById("cursorTrailLayer");
if (!layer) return;
for (let i = 0; i < 14; i++) {
const el = document.createElement("span");
el.textContent = Math.random() < 0.5 ? "✦" : "•";
el.style.position = "absolute";
el.style.left = "0";
el.style.top = "0";
el.style.color = "#7ee8ff";
el.style.fontSize = `${10 + Math.random() * 10}px`;
el.style.textShadow = "0 0 8px rgba(126,232,255,0.9)";
layer.appendChild(el);
const angle = Math.random() * Math.PI * 2;
const dist = 40 + Math.random() * 60;
const dx = Math.cos(angle) * dist;
const dy = Math.sin(angle) * dist;
const anim = el.animate(
[
{ transform: `translate(${x}px, ${y}px) scale(0.6)`, opacity: 1 },
{ transform: `translate(${x + dx}px, ${y + dy}px) scale(1.1)`, opacity: 0 },
],
{ duration: 700 + Math.random() * 300, easing: "ease-out", fill: "forwards" }
);
anim.onfinish = () => el.remove();
}
}
function triggerGenieEasterEgg() {
if (!osGenieOverlay || genieRunning) return;
genieRunning = true;
genieStage = 0;
const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReducedMotion) {
showOsModal(
"THE GENIE",
"you sure about that?\n\n> yes, mr. genie. (minimizes the window)\n> wait.. who are you? (nevermind)"
);
genieRunning = false;
return;
}
// matches .os-genie-bottle's fixed right/bottom/size in index_style.css
const cornerX = window.innerWidth - 94;
const cornerY = window.innerHeight - 126;
osGenieFigure.style.setProperty("--gx", `${cornerX - window.innerWidth / 2}px`);
osGenieFigure.style.setProperty("--gy", `${cornerY - window.innerHeight / 2}px`);
osGenieOverlay.classList.add("is-active");
osGenieBottle.classList.add("is-visible");
osGenieFigure.classList.add("is-visible");
osGenieLine.textContent = "";
osGenieDialogue.classList.remove("is-visible");
osGenieChoiceYes.style.display = "";
osGenieChoiceYes.textContent = "yes, mr. genie.";
osGenieChoiceNo.style.display = "";
osGenieChoiceNo.textContent = "wait.. who are you?";
spawnGenieSparkles(cornerX, cornerY);
setTimeout(() => osGenieFigure.classList.add("is-centered"), 350);
setTimeout(() => {
osGenieLine.textContent = GENIE_LINE_ASK;
osGenieDialogue.classList.add("is-visible");
}, 1100);
// safety net: if she's ignored entirely, quietly dismiss with no side effects
genieDismissTimeout = setTimeout(() => resolveGenie(false), 12000);
}
function advanceGenieToIntro() {
clearTimeout(genieDismissTimeout);
genieStage = 1;
osGenieDialogue.classList.remove("is-visible");
setTimeout(() => {
osGenieLine.textContent = GENIE_LINE_INTRO;
osGenieChoiceYes.style.display = "none";
osGenieChoiceNo.textContent = "..oh?";
osGenieDialogue.classList.add("is-visible");
}, 200);
genieDismissTimeout = setTimeout(() => resolveGenieBye(), 12000);
}
function resolveGenie(shouldMinimize) {
if (!genieRunning) return;
clearTimeout(genieDismissTimeout);
osGenieDialogue.classList.remove("is-visible");
osGenieFigure.classList.remove("is-centered", "is-visible");
setTimeout(() => {
osGenieBottle.classList.remove("is-visible");
osGenieOverlay.classList.remove("is-active");
genieRunning = false;
}, 500);
if (shouldMinimize) {
minimizeOsWindow();
}
}
function resolveGenieBye() {
if (!genieRunning) return;
clearTimeout(genieDismissTimeout);
osGenieChoiceNo.style.display = "none";
osGenieLine.textContent = GENIE_LINE_BYE;
setTimeout(() => {
osGenieDialogue.classList.remove("is-visible");
osGenieFigure.classList.remove("is-centered", "is-visible");
}, 1200);
setTimeout(() => {
osGenieBottle.classList.remove("is-visible");
osGenieOverlay.classList.remove("is-active");
genieRunning = false;
}, 1700);
}
if (osGenieChoiceYes) {
osGenieChoiceYes.addEventListener("click", () => {
if (genieStage === 0) resolveGenie(true);
});
}
if (osGenieChoiceNo) {
osGenieChoiceNo.addEventListener("click", () => {
if (genieStage === 0) advanceGenieToIntro();
else if (genieStage === 1) resolveGenieBye();
});
}
// titlebar easter egg: expand triggers a glitchy square burst and a reality check
const osGlitchMessage = document.getElementById("osGlitchMessage");
let glitchRunning = false;
function triggerGlitchEasterEgg(originEl) {
if (glitchRunning) return;
glitchRunning = true;
const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReducedMotion) {
showOsModal("SYSTEM", "whoa there buddy, you're in a WEB BROWSER. relax.");
glitchRunning = false;
return;
}
const rect = originEl.getBoundingClientRect();
const originX = rect.left + rect.width / 2;
const originY = rect.top + rect.height / 2;
const palette = ["#ff5f87", "#5eead4", "#ffd166", "#9d8cff", "#7cf29c", "#ff8b5e"];
for (let i = 0; i < 18; i++) {
const el = document.createElement("div");
el.className = "os-glitch-square";
const size = 10 + Math.random() * 14;
el.style.width = `${size}px`;
el.style.height = `${size}px`;
el.style.left = `${originX}px`;
el.style.top = `${originY}px`;
el.style.background = palette[Math.floor(Math.random() * palette.length)];
document.body.appendChild(el);
const angle = Math.random() * Math.PI * 2;
const dist = 120 + Math.random() * 420;
const dx = Math.cos(angle) * dist;
const dy = Math.sin(angle) * dist;
const spin = 180 + Math.random() * 180;
const scale = 3 + Math.random() * 7;
const duration = 420 + Math.random() * 260;
const anim = el.animate(
[
{ transform: "translate(-50%, -50%) scale(0.4) rotate(0deg)", opacity: 1 },
{
transform: `translate(calc(-50% + ${dx * 0.5}px), calc(-50% + ${dy * 0.5}px)) scale(${scale * 0.6}) rotate(${spin}deg)`,
opacity: 1,
offset: 0.55,
},
{
transform: `translate(calc(-50% + ${dx}px), calc(-50% + ${dy}px)) scale(${scale}) rotate(${spin * 2}deg)`,
opacity: 0,
},
],
{ duration, easing: "steps(5, end)", fill: "forwards" }
);
anim.onfinish = () => el.remove();
}
setTimeout(() => {
if (osGlitchMessage) osGlitchMessage.classList.add("is-visible");
}, 180);
setTimeout(() => {
if (osGlitchMessage) osGlitchMessage.classList.remove("is-visible");
}, 2756);
setTimeout(() => {
glitchRunning = false;
}, 3006);
}
// OS shell: cursor-tracked 3d tilt on every icon (layers on top of the CSS orbit)
const osTiltIcons = document.querySelectorAll(".os-icon");
const osReduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
osTiltIcons.forEach((icon) => {
const inner = icon.querySelector(".os-icon-inner");
if (!inner) return;
icon.addEventListener("mousemove", (e) => {
if (osReduceMotion.matches) return;
const rect = icon.getBoundingClientRect();
const px = (e.clientX - rect.left) / rect.width - 0.5;
const py = (e.clientY - rect.top) / rect.height - 0.5;
const maxTilt = 18;
inner.style.transform = `rotateX(${(-py * maxTilt).toFixed(2)}deg) rotateY(${(px * maxTilt).toFixed(2)}deg)`;
});
icon.addEventListener("mouseleave", () => {
inner.style.transform = "rotateX(0deg) rotateY(0deg)";
});
});
// OS shell: taskbar clock
const osClock = document.getElementById("osClock");
if (osClock) {
const updateClock = () => {
const now = new Date();
let hours = now.getHours();
const minutes = now.getMinutes().toString().padStart(2, "0");
const ampm = hours >= 12 ? "PM" : "AM";
hours = hours % 12 || 12;
osClock.textContent = `${hours}:${minutes} ${ampm}`;
};
updateClock();
setInterval(updateClock, 15000);
}
// ---------- taskbar: live "N ONLINE" presence tracker (Upstash Redis via /api/presence) ----------
const osOnlineCount = document.getElementById("osOnlineCount");
if (osOnlineCount) {