-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathface-warps-esf.html
More file actions
851 lines (778 loc) · 34.1 KB
/
face-warps-esf.html
File metadata and controls
851 lines (778 loc) · 34.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0"/>
<title>Dark Universe Detectives - Face Warps</title>
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:ital,wght@1,800&family=Source+Sans+3:wght@400;600&display=swap" rel="stylesheet"/>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
width: 100%; height: 100%;
background: #000;
overflow: hidden;
font-family: 'Source Sans 3', 'Source Sans Pro', sans-serif;
color: #fff;
}
#canvas {
display: block;
width: 100vw;
height: 100vh;
cursor: none; /* replaced by SVG cursor below */
touch-action: none; /* hand all touch events to JS; prevents browser pinch-zoom stealing the gesture */
}
/* ── Info toggle button (top-left) ── */
#info-toggle {
position: fixed;
top: 20px;
left: 20px;
width: 34px;
height: 34px;
border-radius: 50%;
background: rgba(0,0,0,0.55);
border: 1px solid rgba(255,255,255,0.2);
backdrop-filter: blur(10px);
color: #fff;
font-family: Georgia, 'Times New Roman', serif;
font-size: 16px;
font-style: italic;
font-weight: 700;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
transition: background 0.15s, border-color 0.15s;
user-select: none;
}
#info-toggle:hover { background: rgba(255,255,255,0.15); border-color: rgba(255,255,255,0.4); }
#info-toggle.open { background: rgba(255,255,255,0.18); border-color: rgba(255,255,255,0.45); }
/* ── Collapsible info panel (top-left, below the toggle) ── */
#info-panel {
position: fixed;
top: 62px;
left: 20px;
background: rgba(0,0,0,0.55);
border: 1px solid rgba(255,255,255,0.12);
border-radius: 16px;
backdrop-filter: blur(10px);
user-select: none;
overflow: hidden;
/* collapsed state */
max-height: 0;
opacity: 0;
padding: 0 20px;
pointer-events: none;
transition: max-height 0.28s ease, opacity 0.2s ease, padding 0.28s ease;
}
#info-panel.open {
max-height: 160px;
opacity: 1;
padding: 16px 20px;
pointer-events: auto;
}
.ctrl {
display: flex;
align-items: center;
gap: 12px;
font-size: 11px;
letter-spacing: 0.08em;
color: rgba(255,255,255,0.6);
}
.ctrl label {
white-space: nowrap;
text-transform: uppercase;
font-weight: 600;
font-size: 13px;
color: #fff;
min-width: 52px;
}
input[type=range] {
-webkit-appearance: none;
width: 110px; height: 3px;
background: rgba(255,255,255,0.2);
border-radius: 2px;
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 14px; height: 14px;
border-radius: 50%;
background: #fff;
cursor: pointer;
}
/* ── Mini pill toggle (used for grid option in info panel) ── */
.pill-toggle {
position: relative;
width: 36px; height: 20px;
background: rgba(255,255,255,0.15);
border: 1px solid rgba(255,255,255,0.2);
border-radius: 10px;
cursor: pointer;
flex-shrink: 0;
transition: background 0.2s, border-color 0.2s;
}
.pill-toggle::after {
content: '';
position: absolute;
top: 2px; left: 2px;
width: 14px; height: 14px;
background: #fff;
border-radius: 50%;
transition: transform 0.2s;
}
.pill-toggle.on { background: rgba(255,255,255,0.45); border-color: rgba(255,255,255,0.6); }
.pill-toggle.on::after { transform: translateX(16px); }
/* ── Bottom buttons ── */
#ui {
position: fixed;
bottom: 28px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 14px;
user-select: none;
}
#snapshot {
background: rgba(0,0,0,0.55);
border: 1px solid rgba(255,255,255,0.25);
color: #fff;
width: 48px;
height: 48px;
border-radius: 50%;
cursor: pointer;
backdrop-filter: blur(10px);
transition: background 0.15s, border-color 0.15s;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
#snapshot:hover { background: rgba(255,255,255,0.18); }
#snapshot:active { background: rgba(255,255,255,0.32); }
#upload-btn {
background: rgba(0,0,0,0.55);
border: 1px solid rgba(255,255,255,0.25);
color: #fff;
width: 48px;
height: 48px;
border-radius: 50%;
cursor: pointer;
backdrop-filter: blur(10px);
transition: background 0.15s, border-color 0.15s;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
}
#upload-btn:hover { background: rgba(255,255,255,0.18); }
#upload-btn:active { background: rgba(255,255,255,0.32); }
#upload-btn.active { border-color: rgba(255,255,255,0.6); background: rgba(255,255,255,0.15); }
#title {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
font-family: 'Barlow Condensed', sans-serif;
font-weight: 800;
font-style: italic;
font-size: clamp(22px, 5.5vw, 36px);
line-height: 1.02;
color: #fff;
text-shadow: 1px 1px 4px rgba(0,0,0,0.5);
pointer-events: none;
white-space: nowrap;
}
#lock-indicator {
position: fixed;
top: 20px;
right: 28px;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.15em;
text-transform: uppercase;
color: rgba(255,255,255,0.4);
pointer-events: none;
transition: color 0.25s, background 0.25s, padding 0.25s, border-radius 0.25s, border-color 0.25s;
}
#lock-indicator.visible {
color: #fff;
background: rgba(255,255,255,0.15);
border: 1px solid rgba(255,255,255,0.3);
backdrop-filter: blur(8px);
padding: 4px 10px;
border-radius: 20px;
}
#ec-logo {
position: fixed;
bottom: 20px;
left: 20px;
display: flex;
align-items: center;
gap: clamp(8px, 2vw, 16px);
pointer-events: none;
}
#ec-logo a {
pointer-events: auto;
line-height: 0;
display: block;
}
#ec-logo img {
height: clamp(39px, 10.9vw, 83px);
width: auto;
display: block;
}
#corner-branding {
position: fixed;
bottom: 20px;
right: 20px;
display: flex;
align-items: center;
gap: clamp(8px, 2vw, 16px);
pointer-events: none;
}
#corner-branding a {
pointer-events: auto;
line-height: 0;
}
#corner-logo {
height: clamp(59px, 16.4vw, 125px);
width: auto;
display: block;
pointer-events: none;
}
#msg {
position: fixed;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
font-size: 13px;
letter-spacing: 0.12em;
color: rgba(255,255,255,0.5);
pointer-events: none;
text-align: center;
}
/* ── Mobile layout ────────────────────────────────────────────────────
Logos, text and buttons are sized with clamp() above so they scale
smoothly with viewport width. This block handles spacing/positioning
on narrow screens where the buttons would otherwise sit on top of the
logo strip. */
@media (max-width: 600px) {
#title { top: 16px; }
#info-toggle { top: 16px; left: 14px; }
#info-panel { top: 58px; left: 14px; }
#lock-indicator { top: 16px; right: 14px; }
#ec-logo { bottom: 14px; left: 14px; }
#corner-branding { bottom: 14px; right: 14px; }
/* Lift buttons above the logo strip.
Logo top edge ≈ 14px margin + ~52px logo = ~66px from bottom. */
#ui { bottom: 84px; gap: 12px; }
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="title">#FaceWarps</div>
<div id="lock-indicator">Tap to lock</div>
<div id="msg">Requesting camera access…</div>
<div id="ec-logo">
<a href="https://www.euclid-ec.org" target="_blank" rel="noopener">
<img src="images/logos/EC_logos_official_white_with_text.png" alt="Euclid Consortium"/>
</a>
<a href="https://www.ed.ac.uk" target="_blank" rel="noopener">
<img src="images/UoE_Centred Logo_white_v1_160215.png" alt="University of Edinburgh"/>
</a>
</div>
<div id="corner-branding">
<a href="https://eucliduk.net/rssse.html" target="_blank" rel="noopener" style="line-height:0;pointer-events:auto;">
<img id="corner-logo" src="images/DarkUniverseDetectives_Logo.png" alt="Dark Universe Detectives"/>
</a>
</div>
<button id="info-toggle" aria-label="Show controls" aria-expanded="false">i</button>
<div id="info-panel" role="region" aria-label="Lens controls">
<div class="ctrl" style="margin-bottom:12px;">
<label for="mass">Mass</label>
<input type="range" id="mass" min="0.01" max="1.0" step="0.01" value="0.35"/>
</div>
<div class="ctrl">
<label for="radius">Radius</label>
<input type="range" id="radius" min="0.05" max="0.6" step="0.01" value="0.22"/>
</div>
<div class="ctrl" style="margin-top:12px;">
<label>Grid</label>
<div class="pill-toggle" id="grid-pill" role="switch" aria-checked="false" aria-label="Grid overlay"></div>
</div>
</div>
<input type="file" id="img-upload" accept="image/*" style="display:none"/>
<div id="ui">
<button id="upload-btn" aria-label="Upload image">
<!-- Image upload icon (shown when using camera) -->
<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="3" y="3" width="18" height="18" rx="2"/>
<circle cx="8.5" cy="8.5" r="1.5"/>
<polyline points="21 15 16 10 5 21"/>
</svg>
</button>
<button id="snapshot" aria-label="Capture">
<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/>
<circle cx="12" cy="13" r="4"/>
</svg>
</button>
</div>
<script>
// ── Euclid space telescope cursor ─────────────────────────────────────
// Hand-drawn SVG icon of the Euclid telescope (aperture top, solar panels
// left/right, sunshield bottom). Hotspot centred on the service module.
(function() {
// Euclid cursor — barrel, aperture, octagonal service module, legs. No solar panel.
// Hotspot at aperture centre.
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="35" viewBox="0 0 36 52">
<!-- BARREL: dominant white cylinder -->
<rect x="7" y="5" width="22" height="24" rx="4" fill="#dfe0e1"/>
<!-- Left highlight -->
<rect x="8" y="6" width="6" height="22" rx="2.5" fill="white" opacity=".5"/>
<!-- Right shadow -->
<rect x="21" y="6" width="5" height="22" rx="2" fill="#a8a9aa" opacity=".45"/>
<!-- Horizontal panel groove lines -->
<line x1="7.5" y1="10" x2="28.5" y2="10" stroke="#b0b1b2" stroke-width=".9"/>
<line x1="7.5" y1="14" x2="28.5" y2="14" stroke="#b0b1b2" stroke-width=".9"/>
<line x1="7.5" y1="18" x2="28.5" y2="18" stroke="#b0b1b2" stroke-width=".9"/>
<line x1="7.5" y1="22" x2="28.5" y2="22" stroke="#b0b1b2" stroke-width=".9"/>
<!-- Barrel-to-service-module junction ring -->
<rect x="7" y="26.5" width="22" height="2.5" rx=".5" fill="#888"/>
<!-- APERTURE: very large, nearly full barrel width -->
<!-- Collar rim -->
<ellipse cx="18" cy="6" rx="11" ry="4" fill="#c0c1c2"/>
<!-- Main aperture (large, very dark) -->
<ellipse cx="18" cy="5.5" rx="9.5" ry="3.4" fill="#0d0d0d"/>
<!-- Secondary mirror -->
<ellipse cx="18" cy="5.5" rx="5.5" ry="2" fill="#050505"/>
<!-- Spider vanes -->
<line x1="18" y1="2.5" x2="18" y2="8.5" stroke="#222" stroke-width=".8"/>
<line x1="12" y1="5.5" x2="24" y2="5.5" stroke="#222" stroke-width=".8"/>
<!-- Aperture rim -->
<ellipse cx="18" cy="5.5" rx="9.5" ry="3.4" fill="none" stroke="#3a3a3a" stroke-width=".4"/>
<!-- SERVICE MODULE: wide octagonal gold foil body -->
<polygon points="1,31 5,27 31,27 35,31 33,47 3,47" fill="#9a7010"/>
<!-- Top bright face -->
<polygon points="5,27 31,27 29,33 7,33" fill="#d0a822" opacity=".9"/>
<!-- Left diagonal face -->
<polygon points="1,31 5,27 7,33 3,37" fill="#b89018" opacity=".8"/>
<!-- Right diagonal face -->
<polygon points="31,27 35,31 33,37 29,33" fill="#a07810" opacity=".75"/>
<!-- Main front face -->
<rect x="3" y="33" width="30" height="10" fill="#966e0e"/>
<!-- Foil crinkle texture lines -->
<line x1="7" y1="27" x2="6" y2="46" stroke="#6a4e08" stroke-width=".6" opacity=".65"/>
<line x1="11" y1="27" x2="10" y2="46" stroke="#6a4e08" stroke-width=".6" opacity=".65"/>
<line x1="15" y1="27" x2="14" y2="46" stroke="#6a4e08" stroke-width=".6" opacity=".65"/>
<line x1="19" y1="27" x2="18" y2="46" stroke="#6a4e08" stroke-width=".6" opacity=".65"/>
<line x1="23" y1="27" x2="22" y2="46" stroke="#6a4e08" stroke-width=".6" opacity=".65"/>
<line x1="27" y1="27" x2="26" y2="46" stroke="#6a4e08" stroke-width=".6" opacity=".65"/>
<line x1="1" y1="38" x2="35" y2="38" stroke="#6a4e08" stroke-width=".5" opacity=".5"/>
<line x1="2" y1="43" x2="34" y2="43" stroke="#6a4e08" stroke-width=".5" opacity=".5"/>
<!-- Bottom dark band -->
<polygon points="3,44 33,44 33,47 3,47" fill="#4a2e04"/>
<!-- White instrument box (lower-right) -->
<rect x="27" y="34" width="6" height="7" rx=".5" fill="#dde0e2"/>
<rect x="28" y="35" width="4" height="5" rx=".3" fill="#c8cbcd" opacity=".8"/>
<!-- DARK STRUCTURAL LEGS at base -->
<rect x="5" y="46" width="4" height="5" rx=".5" fill="#252525"/>
<rect x="27" y="46" width="4" height="5" rx=".5" fill="#252525"/>
<rect x="15" y="47" width="5" height="4" rx=".5" fill="#1a1a1a"/>
</svg>`;
// Hotspot at the aperture centre — where the telescope is "looking"
document.getElementById('canvas').style.cursor =
`url("data:image/svg+xml;base64,${btoa(svg)}") 12 3, crosshair`;
})();
const canvas = document.getElementById('canvas');
const msg = document.getElementById('msg');
const gl = canvas.getContext('webgl', { preserveDrawingBuffer: true }) || canvas.getContext('experimental-webgl', { preserveDrawingBuffer: true });
if (!gl) {
msg.textContent = 'WebGL not supported in this browser.';
throw new Error('no webgl');
}
// ── Shaders ─────────────────────────────────────────────────────────────
const VS = `
attribute vec2 a_pos;
varying vec2 v_uv;
void main() {
v_uv = a_pos * 0.5 + 0.5;
gl_Position = vec4(a_pos, 0.0, 1.0);
}
`;
// Gravitational lensing via the point-mass lens equation:
// β = θ - θE² / |θ| * θ̂
// where θ is the angular position relative to lens centre,
// θE is the Einstein radius.
// We also add chromatic aberration (wavelength-dependent bending).
const FS = `
precision highp float;
varying vec2 v_uv;
uniform sampler2D u_tex;
uniform vec2 u_lensPos; // lens centre in UV space [0,1]
uniform float u_einsteinR; // Einstein radius in UV units
uniform float u_mass; // lensing strength multiplier
uniform float u_chroma; // chromatic split amount
uniform float u_aspect; // canvas width / height
uniform bool u_mirror; // flip x for selfie cam
uniform bool u_grid; // show distorted grid overlay
vec2 lensDeflect(vec2 uv, float wavelengthFactor) {
// aspect-correct the UV offsets so circles look round
vec2 d = uv - u_lensPos;
d.x *= u_aspect;
float r2 = dot(d, d);
float eR = u_einsteinR * wavelengthFactor;
float eR2 = eR * eR * u_mass;
// lens equation deflection (avoids singularity at r=0)
vec2 deflect = (eR2 / max(r2, 0.0001)) * d;
// undo aspect correction on deflection
deflect.x /= u_aspect;
return uv - deflect;
}
void main() {
vec2 uv = v_uv;
if (u_mirror) uv.x = 1.0 - uv.x;
// chromatic aberration: R/G/B channels bend slightly differently
float chromaScale = u_chroma * 0.012;
vec2 uvR = lensDeflect(uv, 1.0 + chromaScale);
vec2 srcG = lensDeflect(uv, 1.0); // source-plane coords (used for grid)
vec2 uvG = srcG;
vec2 uvB = lensDeflect(uv, 1.0 - chromaScale);
// clamp to edge to avoid black borders
uvR = clamp(uvR, 0.001, 0.999);
uvG = clamp(uvG, 0.001, 0.999);
uvB = clamp(uvB, 0.001, 0.999);
// flip v for WebGL (origin bottom-left vs video top-left)
uvR.y = 1.0 - uvR.y;
uvG.y = 1.0 - uvG.y;
uvB.y = 1.0 - uvB.y;
float r = texture2D(u_tex, uvR).r;
float g = texture2D(u_tex, uvG).g;
float b = texture2D(u_tex, uvB).b;
// vignette-darken the strong-lensing core
vec2 dc = (uv - u_lensPos);
dc.x *= u_aspect;
float core = length(dc);
float eR = u_einsteinR * u_mass;
float vign = smoothstep(0.0, eR * 0.4, core - eR * 0.05);
vign = mix(0.0, 1.0, vign);
gl_FragColor = vec4(r * vign, g * vign, b * vign, 1.0);
// Grid overlay — lines live in the source plane so they bend with the
// lens exactly as a real background grid would.
if (u_grid) {
// spX / spY scaled by aspect so each cell appears square on screen.
float sp = 0.05; // 20 columns across the image
float spX = sp;
float spY = sp * u_aspect;
float thresh = 0.016; // line width as a fraction of cell size
float fx = fract(srcG.x / spX);
float fy = fract(srcG.y / spY);
float onX = step(fx, thresh) + step(1.0 - thresh, fx);
float onY = step(fy, thresh) + step(1.0 - thresh, fy);
if (onX + onY > 0.0) {
gl_FragColor = vec4(vign, vign, vign, 1.0);
}
}
}
`;
// ── GL setup ─────────────────────────────────────────────────────────────
function compile(type, src) {
const s = gl.createShader(type);
gl.shaderSource(s, src);
gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(s));
return s;
}
const prog = gl.createProgram();
gl.attachShader(prog, compile(gl.VERTEX_SHADER, VS));
gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, FS));
gl.linkProgram(prog);
gl.useProgram(prog);
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, 1,1]), gl.STATIC_DRAW);
const aPos = gl.getAttribLocation(prog, 'a_pos');
gl.enableVertexAttribArray(aPos);
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
const uTex = gl.getUniformLocation(prog, 'u_tex');
const uLensPos = gl.getUniformLocation(prog, 'u_lensPos');
const uEinstein = gl.getUniformLocation(prog, 'u_einsteinR');
const uMass = gl.getUniformLocation(prog, 'u_mass');
const uChroma = gl.getUniformLocation(prog, 'u_chroma');
const uAspect = gl.getUniformLocation(prog, 'u_aspect');
const uMirror = gl.getUniformLocation(prog, 'u_mirror');
const uGrid = gl.getUniformLocation(prog, 'u_grid');
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// ── Lens position (mouse / touch) ─────────────────────────────────────
// Start off-screen so lens only appears once the user moves/touches
let lensX = -2, lensY = -2;
let targetX = -2, targetY = -2;
let locked = false;
const lockIndicator = document.getElementById('lock-indicator');
function setLocked(val) {
locked = val;
lockIndicator.classList.toggle('visible', locked);
lockIndicator.textContent = locked ? 'Locked — tap to unlock' : 'Tap to lock';
}
function setLens(clientX, clientY) {
if (locked) return;
// Flip X for front-facing (mirrored) camera, flip Y because WebGL UV
// has Y=0 at the bottom while the browser has Y=0 at the top.
targetX = (!usingUpload && isFrontCam) ? 1 - clientX / canvas.clientWidth
: clientX / canvas.clientWidth;
targetY = 1 - clientY / canvas.clientHeight;
// Snap immediately on first interaction so it doesn't drift in from off-screen
if (lensX < 0) { lensX = targetX; lensY = targetY; }
}
canvas.addEventListener('mousemove', e => setLens(e.clientX, e.clientY));
canvas.addEventListener('mouseenter', e => setLens(e.clientX, e.clientY));
canvas.addEventListener('click', () => setLocked(!locked));
// ── Touch: single finger moves lens, two fingers pinch-zooms radius ──
let pinchStartDist = null;
let pinchStartRadius = null;
function touchDist(touches) {
const dx = touches[0].clientX - touches[1].clientX;
const dy = touches[0].clientY - touches[1].clientY;
return Math.hypot(dx, dy);
}
let touchMoved = false;
function onTouch(e) {
e.preventDefault();
if (e.touches.length === 1) {
// Single finger — move the lens (if it moves); tap-in-place toggles lock
if (e.type === 'touchstart') touchMoved = false;
if (e.type === 'touchmove') touchMoved = true;
pinchStartDist = null;
setLens(e.touches[0].clientX, e.touches[0].clientY);
} else if (e.touches.length === 2) {
// Two fingers — pinch to set radius
const dist = touchDist(e.touches);
if (pinchStartDist === null) {
// Record baseline on first two-finger frame
pinchStartDist = dist;
pinchStartRadius = parseFloat(sliderRadius.value);
} else {
const scale = dist / pinchStartDist;
const min = parseFloat(sliderRadius.min);
const max = parseFloat(sliderRadius.max);
sliderRadius.value = Math.min(max, Math.max(min, pinchStartRadius * scale));
}
}
}
canvas.addEventListener('touchstart', onTouch, { passive: false });
canvas.addEventListener('touchmove', onTouch, { passive: false });
canvas.addEventListener('touchend', e => {
if (e.touches.length < 2) pinchStartDist = null;
// A tap (no move) on a single finger toggles the lock
if (e.touches.length === 0 && !touchMoved) setLocked(!locked);
}, { passive: false });
// Prevent the browser's own pinch-zoom on the whole page
document.addEventListener('gesturestart', e => e.preventDefault(), { passive: false });
document.addEventListener('gesturechange', e => e.preventDefault(), { passive: false });
// ── Controls ──────────────────────────────────────────────────────────
const sliderMass = document.getElementById('mass');
const sliderRadius = document.getElementById('radius');
// ── Image upload ───────────────────────────────────────────────────────
let uploadedImage = null;
let usingUpload = false;
let uploadDirty = false;
const uploadBtn = document.getElementById('upload-btn');
const imgUpload = document.getElementById('img-upload');
const SVG_IMAGE = `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>`;
const SVG_CAMERA = `<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2"/></svg>`;
function setUsingUpload(val) {
usingUpload = val;
uploadBtn.classList.toggle('active', val);
if (val) {
uploadBtn.innerHTML = SVG_CAMERA;
uploadBtn.setAttribute('aria-label', 'Switch to camera');
} else {
uploadedImage = null;
uploadBtn.innerHTML = SVG_IMAGE;
uploadBtn.setAttribute('aria-label', 'Upload image');
}
}
uploadBtn.addEventListener('click', () => {
if (usingUpload) {
setUsingUpload(false);
} else {
imgUpload.click();
}
});
imgUpload.addEventListener('change', e => {
const file = e.target.files[0];
if (!file) return;
imgUpload.value = ''; // allow re-selecting same file
const load = src => { uploadedImage = src; uploadDirty = true; setUsingUpload(true); };
if (window.createImageBitmap) {
createImageBitmap(file).then(load).catch(() => {
const img = new Image();
img.onload = () => load(img);
img.src = URL.createObjectURL(file);
});
} else {
const img = new Image();
img.onload = () => load(img);
img.src = URL.createObjectURL(file);
}
});
// ── Info panel toggle ──────────────────────────────────────────────────
const infoToggle = document.getElementById('info-toggle');
const infoPanel = document.getElementById('info-panel');
infoToggle.addEventListener('click', () => {
const open = infoPanel.classList.toggle('open');
infoToggle.classList.toggle('open', open);
infoToggle.setAttribute('aria-expanded', open);
});
// ── Grid overlay toggle ────────────────────────────────────────────────
let gridOn = false;
const gridPill = document.getElementById('grid-pill');
gridPill.addEventListener('click', () => {
gridOn = !gridOn;
gridPill.classList.toggle('on', gridOn);
gridPill.setAttribute('aria-checked', gridOn);
});
// ── Snapshot ──────────────────────────────────────────────────────────
// Composites the WebGL frame with all visible overlays (title, logos,
// corner branding) onto an offscreen 2D canvas before export.
function buildComposite() {
const out = document.createElement('canvas');
out.width = canvas.width;
out.height = canvas.height;
const ctx = out.getContext('2d');
const dpr = window.devicePixelRatio || 1;
// 1. WebGL frame
ctx.drawImage(canvas, 0, 0);
// Read actual rendered sizes from the DOM so the composite matches the
// current display (clamp() values vary with viewport width on mobile).
const cornerLogoImg = document.getElementById('corner-logo');
const titleEl = document.getElementById('title');
const firstEcImg = document.querySelector('#ec-logo img');
// Heights: getBoundingClientRect gives CSS px; multiply by dpr for canvas px
const ecLogoH = Math.round((firstEcImg ? firstEcImg.getBoundingClientRect().height : 83) * dpr);
const cornerLogoH = Math.round((cornerLogoImg ? cornerLogoImg.getBoundingClientRect().height : 104) * dpr);
const titlePx = Math.round((titleEl ? parseFloat(getComputedStyle(titleEl).fontSize) : 36) * dpr);
// Margins / gaps also scale with dpr
const margin = Math.round(20 * dpr);
const gap = Math.round(16 * dpr);
// Helper: draw Barlow Condensed italic 800 text at a given size
function barlowText(text, x, y, align, baseline, sizePx) {
ctx.save();
ctx.font = `italic 800 ${sizePx}px 'Barlow Condensed', sans-serif`;
ctx.fillStyle = '#ffffff';
ctx.textAlign = align;
ctx.textBaseline = baseline;
ctx.shadowColor = 'rgba(0,0,0,0.5)';
ctx.shadowBlur = Math.round(4 * dpr);
ctx.shadowOffsetX = Math.round(1 * dpr);
ctx.shadowOffsetY = Math.round(1 * dpr);
ctx.fillText(text, x, y);
ctx.restore();
}
// 2. Title "#FaceWarps" — centred at top
barlowText('#FaceWarps', out.width / 2, margin, 'center', 'top', titlePx);
// 3. EC logo + UoE logo — bottom-left, side by side
const logoImgs = document.querySelectorAll('#ec-logo img');
let logoX = margin;
logoImgs.forEach(img => {
if (img && img.complete && img.naturalWidth) {
const w = Math.round(img.naturalWidth * ecLogoH / img.naturalHeight);
ctx.drawImage(img, logoX, out.height - margin - ecLogoH, w, ecLogoH);
logoX += w + gap;
}
});
// 4. Dark Universe Detectives logo — bottom-right
if (cornerLogoImg && cornerLogoImg.complete && cornerLogoImg.naturalWidth) {
const h = cornerLogoH;
const w = Math.round(cornerLogoImg.naturalWidth * h / cornerLogoImg.naturalHeight);
const x = out.width - margin - w;
const y = out.height - margin - h;
ctx.drawImage(cornerLogoImg, x, y, w, h);
}
return out;
}
document.getElementById('snapshot').addEventListener('click', () => {
render(); // fresh WebGL frame
const filename = `gravitational-lens-${Date.now()}.png`;
buildComposite().toBlob(blob => {
const file = new File([blob], filename, { type: 'image/png' });
if (navigator.canShare && navigator.canShare({ files: [file] })) {
navigator.share({ files: [file], title: '#FaceWarps' })
.catch(() => { /* user cancelled */ });
} else {
const a = document.createElement('a');
a.download = filename;
a.href = URL.createObjectURL(blob);
a.click();
URL.revokeObjectURL(a.href);
}
}, 'image/png');
});
// ── Camera ───────────────────────────────────────────────────────────
const video = document.createElement('video');
video.autoplay = true;
video.playsInline = true;
video.muted = true;
let isFrontCam = true;
navigator.mediaDevices.getUserMedia({
video: { facingMode: 'user', width: { ideal: 1280 }, height: { ideal: 720 } },
audio: false
}).then(stream => {
video.srcObject = stream;
msg.style.display = 'none';
// detect front-facing camera for mirror flip
const track = stream.getVideoTracks()[0];
const settings = track.getSettings ? track.getSettings() : {};
isFrontCam = settings.facingMode !== 'environment';
video.play();
}).catch(err => {
msg.textContent = 'Camera access denied.\n(' + err.message + ')';
});
// ── Resize ────────────────────────────────────────────────────────────
function resize() {
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
gl.viewport(0, 0, canvas.width, canvas.height);
}
window.addEventListener('resize', resize);
resize();
// ── Render loop ────────────────────────────────────────────────────────
function render() {
requestAnimationFrame(render);
gl.bindTexture(gl.TEXTURE_2D, tex);
if (usingUpload && uploadedImage) {
if (uploadDirty) {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, uploadedImage);
uploadDirty = false;
}
} else {
if (video.readyState < 2) return;
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, video);
}
// Smooth lens position
lensX += (targetX - lensX) * 0.12;
lensY += (targetY - lensY) * 0.12;
gl.uniform1i(uTex, 0);
gl.uniform2f(uLensPos, lensX, lensY);
gl.uniform1f(uEinstein, parseFloat(sliderRadius.value));
gl.uniform1f(uMass, parseFloat(sliderMass.value));
gl.uniform1f(uChroma, 0.4);
gl.uniform1f(uAspect, canvas.width / canvas.height);
gl.uniform1i(uMirror, !usingUpload && isFrontCam ? 1 : 0);
gl.uniform1i(uGrid, gridOn ? 1 : 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
render();
</script>
</body>
</html>