-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial_sim.html
More file actions
1661 lines (1501 loc) · 75.3 KB
/
initial_sim.html
File metadata and controls
1661 lines (1501 loc) · 75.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chaotic Pen Studio</title>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/face_mesh.js" crossorigin="anonymous"></script>
<style>
:root {
--bg: #050505;
--panel: rgba(15, 15, 18, 0.92);
--text: #e0e0e0;
--dim: #777;
--accent: #fff;
--font: 'Inter', -apple-system, sans-serif;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
user-select: none;
outline: none;
}
body {
background: var(--bg);
color: var(--text);
font-family: var(--font);
height: 100vh;
overflow: hidden;
display: flex;
flex-direction: column;
}
.view {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transition: opacity 0.5s;
opacity: 0;
pointer-events: none;
z-index: 10;
}
.view.active {
opacity: 1;
pointer-events: auto;
z-index: 20;
}
.top-bar {
position: absolute;
top: 0;
left: 0;
right: 0;
padding: 1.2rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
z-index: 100;
pointer-events: none;
}
.top-bar>* {
pointer-events: auto;
}
.logo {
font-weight: 300;
letter-spacing: 0.3em;
text-transform: uppercase;
font-size: 0.7rem;
color: var(--dim);
}
.phase-indicator {
font-size: 0.7rem;
letter-spacing: 0.15em;
text-transform: uppercase;
color: var(--accent);
}
.floating-panel {
position: absolute;
bottom: 1.5rem;
left: 50%;
transform: translateX(-50%);
background: var(--panel);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.08);
padding: 0.8rem 1.5rem;
border-radius: 50px;
display: flex;
gap: 1.2rem;
align-items: center;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8);
flex-wrap: wrap;
justify-content: center;
max-width: 95vw;
}
.floating-panel.collapsed {
display: none !important;
}
.control-group {
display: flex;
flex-direction: column;
gap: 0.3rem;
}
.control-group label {
font-size: 0.55rem;
text-transform: uppercase;
letter-spacing: 0.08em;
color: var(--dim);
}
input[type="range"] {
-webkit-appearance: none;
width: 90px;
height: 2px;
background: #333;
border-radius: 1px;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 10px;
height: 10px;
background: var(--accent);
border-radius: 50%;
cursor: pointer;
}
.btn {
background: transparent;
border: 1px solid #444;
color: var(--text);
padding: 0.5rem 1rem;
border-radius: 25px;
font-size: 0.65rem;
text-transform: uppercase;
letter-spacing: 0.08em;
cursor: pointer;
transition: all 0.2s;
}
.btn:hover {
border-color: var(--accent);
color: var(--accent);
}
.btn-primary {
background: var(--accent);
color: #000;
border: none;
font-weight: 600;
}
.btn-primary:hover {
background: #e0e0e0;
}
.canvas-wrapper {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
canvas {
max-width: 90vw;
max-height: 80vh;
box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
border-radius: 4px;
}
#editCanvas {
cursor: crosshair;
background: #000;
}
#simCanvas {
background: #f5f0eb;
}
.eraser-cursor {
position: fixed;
border: 1px solid rgba(255, 255, 255, 0.5);
border-radius: 50%;
pointer-events: none;
transform: translate(-50%, -50%);
z-index: 9999;
display: none;
}
.splash-content {
text-align: center;
display: flex;
flex-direction: column;
gap: 1.5rem;
align-items: center;
}
h1 {
font-size: 2rem;
font-weight: 200;
letter-spacing: 0.4em;
text-transform: uppercase;
}
.hidden-input {
display: none;
}
.sep {
width: 1px;
height: 24px;
background: #333;
}
/* Status bar (top center) */
.status-bar {
position: absolute;
top: 1.2rem;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.6);
border: 1px solid rgba(255, 255, 255, 0.08);
backdrop-filter: blur(10px);
padding: 0.4rem 1.2rem;
border-radius: 30px;
display: flex;
align-items: center;
gap: 1.2rem;
pointer-events: none;
z-index: 100;
font-size: 0.75rem;
}
.status-bar .clock {
font-family: monospace;
font-size: 1rem;
font-weight: bold;
color: var(--accent);
}
.status-bar .phase {
color: var(--dim);
text-transform: uppercase;
letter-spacing: 0.1em;
font-size: 0.65rem;
}
.status-bar .coverage {
color: #f0c040;
font-family: monospace;
font-size: 0.75rem;
}
/* Collapse toggle */
.collapse-toggle {
position: absolute;
bottom: 1.5rem;
right: 1.5rem;
z-index: 110;
background: var(--panel);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.08);
color: var(--dim);
width: 32px;
height: 32px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.2s;
}
.collapse-toggle:hover {
color: var(--accent);
border-color: var(--accent);
}
/* Right Dev Panel */
.dev-panel {
position: absolute;
top: 50px;
right: 0;
bottom: 70px;
width: 270px;
background: rgba(10, 10, 14, 0.94);
backdrop-filter: blur(12px);
border-left: 1px solid rgba(255, 255, 255, 0.06);
z-index: 90;
overflow-y: auto;
padding: 0.8rem 1rem 2rem;
display: none;
scrollbar-width: thin;
scrollbar-color: #333 transparent;
}
.dev-panel.visible {
display: block;
}
.dev-panel::-webkit-scrollbar {
width: 4px;
}
.dev-panel::-webkit-scrollbar-thumb {
background: #333;
border-radius: 2px;
}
.dev-panel h3 {
font-size: 0.6rem;
text-transform: uppercase;
letter-spacing: 0.12em;
color: var(--accent);
margin: 0.8rem 0 0.4rem;
padding-bottom: 0.25rem;
border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.dev-panel h3:first-child {
margin-top: 0;
}
.dev-row {
display: flex;
align-items: center;
gap: 6px;
margin-bottom: 0.5rem;
}
.dev-row label {
font-size: 0.5rem;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--dim);
flex: 1;
min-width: 0;
}
.dev-row input[type="range"] {
width: 75px;
}
.dev-row .dev-val {
font-family: monospace;
font-size: 0.55rem;
color: var(--accent);
width: 36px;
text-align: right;
flex-shrink: 0;
}
.dev-row .tip {
width: 14px;
height: 14px;
border-radius: 50%;
border: 1px solid #444;
color: #555;
font-size: 8px;
display: flex;
align-items: center;
justify-content: center;
cursor: help;
flex-shrink: 0;
position: relative;
}
.dev-row .tip:hover {
border-color: var(--accent);
color: var(--accent);
}
.dev-row .tip .tiptext {
display: none;
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
background: #1a1a1e;
border: 1px solid #333;
color: var(--text);
padding: 6px 10px;
border-radius: 6px;
font-size: 9px;
width: 160px;
line-height: 1.3;
text-transform: none;
letter-spacing: 0;
z-index: 200;
white-space: normal;
}
.dev-row .tip:hover .tiptext {
display: block;
}
</style>
</head>
<body>
<div class="top-bar">
<div class="logo">Chaotic Pen</div>
<div class="phase-indicator" id="phaseIndicator">Upload</div>
<button class="btn" style="opacity:0;pointer-events:none;" id="restartBtn">Restart</button>
</div>
<!-- VIEW 1: SPLASH -->
<div class="view active" id="view-splash">
<div class="splash-content">
<h1>Chaotic Pen</h1>
<p style="font-size:0.8rem;color:var(--dim);letter-spacing:0.1em;max-width:400px;line-height:1.5;">
Upload a portrait. The system will detect the face, build a density map, and deploy a single chaotic pen
to sketch it — one continuous stroke.
</p>
<button class="btn btn-primary" onclick="document.getElementById('fileInput').click()">Load
Portrait</button>
</div>
</div>
<!-- VIEW 2: EDGE EDITOR -->
<div class="view" id="view-edit">
<div class="canvas-wrapper">
<canvas id="editCanvas"></canvas>
<div class="eraser-cursor" id="eraserCursor"></div>
</div>
<div class="floating-panel">
<div class="control-group"><label>Edge Detail</label><input type="range" id="edgeThreshold" min="10"
max="150" value="45"></div>
<div class="control-group"><label>Eraser</label><input type="range" id="eraserSize" min="5" max="100"
value="30"></div>
<div class="sep"></div>
<button class="btn" id="resetEdgesBtn">Reset</button>
<button class="btn btn-primary" id="startPenBtn">Start Pen ▸</button>
</div>
</div>
<!-- VIEW 3: PEN SIMULATION -->
<div class="view" id="view-sim">
<div class="canvas-wrapper">
<canvas id="simCanvas"></canvas>
</div>
<div class="status-bar" id="statusBar">
<span class="clock" id="clockDisplay">00:00</span>
<span class="phase" id="phaseDisplay">Outline</span>
<span class="coverage" id="coverageDisplay">0%</span>
</div>
<div class="floating-panel" id="bottomBar">
<button class="btn" id="pauseBtn">Pause</button>
<button class="btn" id="autoDrawBtn">Auto Draw</button>
<button class="btn" id="wipeBtn">Wipe</button>
<button class="btn" id="toggleErrorBtn">Error Map</button>
<div class="sep"></div>
<button class="btn btn-primary" id="exportBtn">Export PNG</button>
</div>
<button class="collapse-toggle" id="collapseBtn" title="Toggle bar">▼</button>
<!-- DEV PANEL -->
<div class="dev-panel visible" id="devPanel">
<h3>⚡ Speed & Motion</h3>
<div class="dev-row"><label>Max Speed</label><input type="range" id="p-maxSpeed" min="0.5" max="25"
step="0.5" value="3"><span class="dev-val" id="v-maxSpeed">3</span><span class="tip">?<span
class="tiptext">Maximum pen velocity. Higher = faster coverage, less detail.</span></span></div>
<div class="dev-row"><label>Steps/Frame</label><input type="range" id="p-stepsPerFrame" min="1" max="200"
step="1" value="100"><span class="dev-val" id="v-stepsPerFrame">100</span><span class="tip">?<span
class="tiptext">How many pen steps per animation frame. Higher = faster drawing.</span></span>
</div>
<div class="dev-row"><label>Wander Rate</label><input type="range" id="p-wanderRate" min="0.01" max="1.5"
step="0.01" value="0.4"><span class="dev-val" id="v-wanderRate">0.4</span><span class="tip">?<span
class="tiptext">How chaotically the pen direction changes. Higher = more scribble.</span></span>
</div>
<div class="dev-row"><label>Wander Force</label><input type="range" id="p-wanderForce" min="0.001"
max="0.15" step="0.001" value="0.02"><span class="dev-val" id="v-wanderForce">0.02</span><span
class="tip">?<span class="tiptext">Strength of the random drift force applied to the
pen.</span></span></div>
<h3>🧲 Error Attraction</h3>
<div class="dev-row"><label>Pull Strength</label><input type="range" id="p-errorPull" min="0.005" max="0.3"
step="0.005" value="0.08"><span class="dev-val" id="v-errorPull">0.08</span><span class="tip">?<span
class="tiptext">How strongly the pen is pulled toward high-error (under-drawn)
areas.</span></span></div>
<div class="dev-row"><label>Search Radius</label><input type="range" id="p-searchRadius" min="5" max="100"
step="1" value="40"><span class="dev-val" id="v-searchRadius">40</span><span class="tip">?<span
class="tiptext">How far the pen looks to find under-drawn areas. Larger = smoother
paths.</span></span></div>
<div class="dev-row"><label>Search Step</label><input type="range" id="p-searchStep" min="1" max="10"
step="1" value="4"><span class="dev-val" id="v-searchStep">4</span><span class="tip">?<span
class="tiptext">Pixel step size when scanning for error. Lower = more accurate,
slower.</span></span></div>
<h3>🌊 Flow Alignment</h3>
<div class="dev-row"><label>Flow Strength</label><input type="range" id="p-flowStrength" min="0" max="0.15"
step="0.005" value="0.04"><span class="dev-val" id="v-flowStrength">0.04</span><span
class="tip">?<span class="tiptext">How strongly the pen aligns to facial contour
directions.</span></span></div>
<h3>🔄 Dwell Looping</h3>
<div class="dev-row"><label>Dwell Factor</label><input type="range" id="p-dwellFactor" min="0" max="3"
step="0.1" value="1.2"><span class="dev-val" id="v-dwellFactor">1.2</span><span class="tip">?<span
class="tiptext">In dark areas the pen's wander tightens into loops. Higher = tighter circles in
shadows.</span></span></div>
<div class="dev-row"><label>Repulsion</label><input type="range" id="p-repulsion" min="0" max="0.1"
step="0.005" value="0.02"><span class="dev-val" id="v-repulsion">0.02</span><span class="tip">?<span
class="tiptext">Push pen away from already-saturated areas (error near zero).</span></span>
</div>
<h3>✏️ Line Style</h3>
<div class="dev-row"><label>Width Min</label><input type="range" id="p-lineWidthMin" min="0.05" max="1"
step="0.05" value="0.2"><span class="dev-val" id="v-lineWidthMin">0.2</span><span class="tip">?<span
class="tiptext">Line width in light areas.</span></span></div>
<div class="dev-row"><label>Width Max</label><input type="range" id="p-lineWidthMax" min="0.5" max="4"
step="0.1" value="1.8"><span class="dev-val" id="v-lineWidthMax">1.8</span><span class="tip">?<span
class="tiptext">Line width in dark areas.</span></span></div>
<div class="dev-row"><label>Alpha Min</label><input type="range" id="p-alphaMin" min="0.005" max="0.3"
step="0.005" value="0.03"><span class="dev-val" id="v-alphaMin">0.03</span><span class="tip">?<span
class="tiptext">Ink opacity in light areas.</span></span></div>
<div class="dev-row"><label>Alpha Max</label><input type="range" id="p-alphaMax" min="0.1" max="1"
step="0.05" value="0.7"><span class="dev-val" id="v-alphaMax">0.7</span><span class="tip">?<span
class="tiptext">Ink opacity in dark areas.</span></span></div>
<h3>🧱 Border</h3>
<div class="dev-row"><label>Margin</label><input type="range" id="p-borderMargin" min="5" max="80" step="5"
value="25"><span class="dev-val" id="v-borderMargin">25</span><span class="tip">?<span
class="tiptext">Distance from edge where border push activates.</span></span></div>
<div class="dev-row"><label>Push Force</label><input type="range" id="p-borderPush" min="0.005" max="0.15"
step="0.005" value="0.04"><span class="dev-val" id="v-borderPush">0.04</span><span
class="tip">?<span class="tiptext">How hard the pen is pushed away from canvas
borders.</span></span></div>
<h3>⏱ Error Decay</h3>
<div class="dev-row"><label>Decay Radius</label><input type="range" id="p-decayRadius" min="1" max="15"
step="1" value="5"><span class="dev-val" id="v-decayRadius">5</span><span class="tip">?<span
class="tiptext">Radius around each drawn point where error is reduced.</span></span></div>
<div class="dev-row"><label>Decay Amount</label><input type="range" id="p-decayAmount" min="0.001"
max="0.05" step="0.001" value="0.008"><span class="dev-val" id="v-decayAmount">0.008</span><span
class="tip">?<span class="tiptext">How much error is reduced per stroke. Lower = pen revisits areas
more.</span></span></div>
</div>
</div>
<input type="file" id="fileInput" class="hidden-input" accept="image/*">
<script>
// ============================================
// CORE ELEMENTS
// ============================================
const views = { splash: document.getElementById('view-splash'), edit: document.getElementById('view-edit'), sim: document.getElementById('view-sim') };
const editCanvas = document.getElementById('editCanvas');
const editCtx = editCanvas.getContext('2d', { willReadFrequently: true });
const simCanvas = document.getElementById('simCanvas');
const simCtx = simCanvas.getContext('2d', { willReadFrequently: true });
const phaseIndicator = document.getElementById('phaseIndicator');
const restartBtn = document.getElementById('restartBtn');
let sourceImgData = null;
let originalImg = null;
let w, h;
let simulationRaf;
let faceWeight = null; // Float32Array: 0.0 (outside) to 1.0 (inside face) — soft feathered
let isPaused = false;
let simStartTime = 0;
let savedCanvasData = null;
let isAutoDrawing = true;
// === 3 PRE-COMPUTED MAPS ===
let darknessMap = null; // Float32Array: 0.0 (white) to 1.0 (black)
let flowFieldX = null; // Float32Array: flow direction X component
let flowFieldY = null; // Float32Array: flow direction Y component
let errorMap = null; // Float32Array: remaining "ink needed" per pixel
let edgeDataArray = null; // Uint8Array: 1 = edge pixel (for edge editor display)
// ============================================
// PEN CONFIG — all driven from dev panel
// ============================================
const pen = {
maxSpeed: 3, stepsPerFrame: 100,
wanderRate: 0.4, wanderForce: 0.02,
errorPull: 0.08, searchRadius: 40, searchStep: 4,
flowStrength: 0.04,
dwellFactor: 1.2, repulsion: 0.02,
lineWidthMin: 0.2, lineWidthMax: 1.8,
alphaMin: 0.03, alphaMax: 0.7,
borderMargin: 25, borderPush: 0.04,
decayRadius: 5, decayAmount: 0.008
};
// ============================================
// MEDIAPIPE FACE MESH
// ============================================
const faceMeshInstance = new FaceMesh({ locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${file}` });
faceMeshInstance.setOptions({ maxNumFaces: 1, refineLandmarks: true, minDetectionConfidence: 0.2, minTrackingConfidence: 0.4 });
let storedLandmarks = null;
faceMeshInstance.onResults((results) => {
if (results.multiFaceLandmarks && results.multiFaceLandmarks.length > 0) {
storedLandmarks = results.multiFaceLandmarks[0];
buildSoftFaceMask(storedLandmarks);
console.log("Face detected.");
} else {
console.warn("No face detected. Using full image.");
faceWeight = new Float32Array(w * h).fill(1.0);
}
generateEdges();
setView('edit');
});
function buildSoftFaceMask(landmarks) {
// Step 1: Draw sharp face polygon
const mc = document.createElement('canvas'); mc.width = w; mc.height = h;
const mCtx = mc.getContext('2d');
let ovalIndices = [10, 338, 297, 332, 284, 251, 389, 356, 454, 323, 361, 288, 397, 365, 379, 378, 400, 377, 152, 148, 176, 149, 150, 136, 172, 58, 132, 93, 234, 127, 162, 21, 54, 103, 67, 109];
if (window.FACEMESH_FACE_OVAL) {
const pairs = [...window.FACEMESH_FACE_OVAL];
ovalIndices = [pairs[0][0]];
for (let i = 0; i < pairs.length; i++) ovalIndices.push(pairs[i][1]);
}
let cx = 0, cy = 0;
ovalIndices.forEach(idx => { cx += landmarks[idx].x * w; cy += landmarks[idx].y * h; });
cx /= ovalIndices.length; cy /= ovalIndices.length;
const padding = 1.3; // Large to ensure no clipping
mCtx.fillStyle = '#fff'; mCtx.beginPath();
ovalIndices.forEach((idx, i) => {
let px = cx + (landmarks[idx].x * w - cx) * padding;
let py = cy + (landmarks[idx].y * h - cy) * padding;
if (i === 0) mCtx.moveTo(px, py); else mCtx.lineTo(px, py);
});
mCtx.closePath(); mCtx.fill();
// Step 2: Gaussian blur the mask for soft feathering
const blurCanvas = document.createElement('canvas'); blurCanvas.width = w; blurCanvas.height = h;
const bCtx = blurCanvas.getContext('2d');
bCtx.filter = 'blur(80px)'; // Wide feather for invisible boundary
bCtx.drawImage(mc, 0, 0);
// Step 3: Read blurred values as float weights
const blurData = bCtx.getImageData(0, 0, w, h).data;
faceWeight = new Float32Array(w * h);
for (let i = 0; i < blurData.length; i += 4) {
faceWeight[i / 4] = blurData[i] / 255.0; // 0.0 to 1.0 smooth gradient
}
console.log('Soft face mask built with 80px feather.');
}
// ============================================
// NAVIGATION
// ============================================
function setView(v) {
Object.values(views).forEach(el => el.classList.remove('active'));
views[v].classList.add('active');
if (v === 'splash') { phaseIndicator.innerText = "Upload"; restartBtn.style.opacity = "0"; restartBtn.style.pointerEvents = "none"; cancelAnimationFrame(simulationRaf); }
else if (v === 'edit') { phaseIndicator.innerText = "Isolate Edges"; restartBtn.style.opacity = "1"; restartBtn.style.pointerEvents = "auto"; }
else if (v === 'sim') { phaseIndicator.innerText = "Drawing"; }
}
restartBtn.onclick = () => setView('splash');
// ============================================
// PHASE 1: UPLOAD
// ============================================
document.getElementById('fileInput').onchange = (e) => {
const f = e.target.files[0]; if (!f) return;
const reader = new FileReader();
reader.onload = (ev) => { originalImg = new Image(); originalImg.onload = () => initEditor(originalImg); originalImg.src = ev.target.result; };
reader.readAsDataURL(f);
};
// ============================================
// PHASE 2: EDGE EDITOR
// ============================================
const config = { eraserSize: 30, edgeThreshold: 45 };
function initEditor(img) {
const maxW = window.innerWidth * 0.9, maxH = window.innerHeight * 0.8;
const scale = Math.min(maxW / img.width, maxH / img.height);
w = editCanvas.width = simCanvas.width = Math.max(1, Math.floor(img.width * scale));
h = editCanvas.height = simCanvas.height = Math.max(1, Math.floor(img.height * scale));
const tc = document.createElement('canvas'); tc.width = w; tc.height = h;
const tCtx = tc.getContext('2d'); tCtx.drawImage(img, 0, 0, w, h);
sourceImgData = tCtx.getImageData(0, 0, w, h).data;
phaseIndicator.innerText = "Detecting face...";
faceMeshInstance.send({ image: tc });
}
function generateEdges() {
const blurC = document.createElement('canvas'); blurC.width = w; blurC.height = h;
const bCtx = blurC.getContext('2d'); bCtx.filter = 'blur(2px)';
const srcC = document.createElement('canvas'); srcC.width = w; srcC.height = h;
srcC.getContext('2d').putImageData(new ImageData(new Uint8ClampedArray(sourceImgData), w, h), 0, 0);
bCtx.drawImage(srcC, 0, 0);
const blurData = bCtx.getImageData(0, 0, w, h).data;
const edges = new Uint8ClampedArray(w * h * 4);
const luma = new Uint8Array(w * h);
for (let i = 0; i < blurData.length; i += 4) luma[i / 4] = blurData[i] * 0.21 + blurData[i + 1] * 0.72 + blurData[i + 2] * 0.07;
for (let y = 1; y < h - 1; y++) {
for (let x = 1; x < w - 1; x++) {
const idx = y * w + x;
const hg = -luma[(y - 1) * w + (x - 1)] + luma[(y - 1) * w + (x + 1)] - 2 * luma[y * w + (x - 1)] + 2 * luma[y * w + (x + 1)] - luma[(y + 1) * w + (x - 1)] + luma[(y + 1) * w + (x + 1)];
const vg = -luma[(y - 1) * w + (x - 1)] - 2 * luma[(y - 1) * w + x] - luma[(y - 1) * w + (x + 1)] + luma[(y + 1) * w + (x - 1)] + 2 * luma[(y + 1) * w + x] + luma[(y + 1) * w + (x + 1)];
const val = Math.sqrt(hg * hg + vg * vg);
const pi = idx * 4;
const fw = faceWeight ? faceWeight[idx] : 1.0;
// Smooth threshold: lower inside face (more detail), higher outside
const thresh = config.edgeThreshold * (0.7 + (1.0 - fw) * 1.3);
if (val > thresh) {
const b = Math.floor(80 + fw * 175); // Smooth brightness fade
edges[pi] = b; edges[pi + 1] = b; edges[pi + 2] = b; edges[pi + 3] = 255;
} else { edges[pi] = 0; edges[pi + 1] = 0; edges[pi + 2] = 0; edges[pi + 3] = 255; }
}
}
editCtx.putImageData(new ImageData(edges, w, h), 0, 0);
}
document.getElementById('edgeThreshold').oninput = (e) => { config.edgeThreshold = parseInt(e.target.value); generateEdges(); };
document.getElementById('eraserSize').oninput = (e) => { config.eraserSize = parseInt(e.target.value); document.getElementById('eraserCursor').style.width = `${config.eraserSize * 2}px`; document.getElementById('eraserCursor').style.height = `${config.eraserSize * 2}px`; };
document.getElementById('resetEdgesBtn').onclick = generateEdges;
let isErasing = false; let elX, elY;
const cursor = document.getElementById('eraserCursor');
function getMousePos(c, e) { const r = c.getBoundingClientRect(); return { x: (e.clientX - r.left) * c.width / r.width, y: (e.clientY - r.top) * c.height / r.height }; }
editCanvas.addEventListener('mousedown', (e) => { isErasing = true; const p = getMousePos(editCanvas, e); elX = p.x; elY = p.y; erase(p.x, p.y); });
editCanvas.addEventListener('mousemove', (e) => { cursor.style.display = 'block'; cursor.style.left = e.clientX + 'px'; cursor.style.top = e.clientY + 'px'; if (isErasing) { const p = getMousePos(editCanvas, e); erase(p.x, p.y, true); elX = p.x; elY = p.y; } });
editCanvas.addEventListener('mouseup', () => isErasing = false);
editCanvas.addEventListener('mouseleave', () => { isErasing = false; cursor.style.display = 'none'; });
function erase(x, y, line = false) {
editCtx.fillStyle = '#000'; editCtx.strokeStyle = '#000'; editCtx.lineWidth = config.eraserSize * 2; editCtx.lineCap = 'round';
if (line) { editCtx.beginPath(); editCtx.moveTo(elX, elY); editCtx.lineTo(x, y); editCtx.stroke(); }
else { editCtx.beginPath(); editCtx.arc(x, y, config.eraserSize, 0, Math.PI * 2); editCtx.fill(); }
}
// ============================================
// TRANSITION: BUILD 3 MAPS & START PEN
// ============================================
document.getElementById('startPenBtn').onclick = () => {
buildMaps();
startPen();
setView('sim');
};
function buildMaps() {
const n = w * h;
// 1) DARKNESS MAP from grayscale — soft-blended with face weight
darknessMap = new Float32Array(n);
for (let i = 0; i < n; i++) {
const pi = i * 4;
const lum = sourceImgData[pi] * 0.21 + sourceImgData[pi + 1] * 0.72 + sourceImgData[pi + 2] * 0.07;
let darkness = 1.0 - (lum / 255.0);
// Soft blend: inside face = full darkness, outside = fades smoothly
const fw = faceWeight ? faceWeight[i] : 1.0;
darkness *= (0.25 + fw * 0.75); // Smooth: center=100%, far outside=25%
darknessMap[i] = darkness;
}
// Second pass: Gaussian-smooth the darkness map to eliminate any residual transitions
// Use a fast box blur approximation (2 passes of horizontal+vertical)
const tempMap = new Float32Array(n);
const blurR = 8; // 8px blur radius
for (let pass = 0; pass < 2; pass++) {
// Horizontal
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
let sum = 0, cnt = 0;
for (let dx = -blurR; dx <= blurR; dx++) {
const nx = x + dx;
if (nx >= 0 && nx < w) { sum += darknessMap[y * w + nx]; cnt++; }
}
tempMap[y * w + x] = sum / cnt;
}
}
// Vertical
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
let sum = 0, cnt = 0;
for (let dy = -blurR; dy <= blurR; dy++) {
const ny = y + dy;
if (ny >= 0 && ny < h) { sum += tempMap[ny * w + x]; cnt++; }
}
darknessMap[y * w + x] = sum / cnt;
}
}
}
// 2) FLOW FIELD from Sobel gradient (rotated 90° = along contours)
flowFieldX = new Float32Array(n);
flowFieldY = new Float32Array(n);
// Compute luma for gradient
const luma = new Uint8Array(n);
for (let i = 0; i < sourceImgData.length; i += 4) {
luma[i / 4] = sourceImgData[i] * 0.21 + sourceImgData[i + 1] * 0.72 + sourceImgData[i + 2] * 0.07;
}
for (let y = 1; y < h - 1; y++) {
for (let x = 1; x < w - 1; x++) {
const idx = y * w + x;
const gx = -luma[(y - 1) * w + (x - 1)] + luma[(y - 1) * w + (x + 1)] - 2 * luma[y * w + (x - 1)] + 2 * luma[y * w + (x + 1)] - luma[(y + 1) * w + (x - 1)] + luma[(y + 1) * w + (x + 1)];
const gy = -luma[(y - 1) * w + (x - 1)] - 2 * luma[(y - 1) * w + x] - luma[(y - 1) * w + (x + 1)] + luma[(y + 1) * w + (x - 1)] + 2 * luma[(y + 1) * w + x] + luma[(y + 1) * w + (x + 1)];
// Rotate 90°: flow along contour (perpendicular to gradient)
const mag = Math.sqrt(gx * gx + gy * gy) || 1;
flowFieldX[idx] = -gy / mag; // perpendicular
flowFieldY[idx] = gx / mag;
}
}
// 3) ERROR MAP = copy of darkness map (gets reduced as we draw)
errorMap = new Float32Array(n);
for (let i = 0; i < n; i++) errorMap[i] = darknessMap[i];
// Also store edge data for display
const ed = editCtx.getImageData(0, 0, w, h).data;
edgeDataArray = new Uint8Array(n);
for (let i = 0; i < ed.length; i += 4) edgeDataArray[i / 4] = ed[i] > 60 ? 1 : 0;
console.log(`Maps built: ${w}x${h}, ${n} pixels`);
}
// ============================================
// LAYER 1: THE CHAOTIC PEN (with command execution)
// ============================================
class ChaoticPen {
constructor() {
this.x = w / 2; this.y = h / 2;
let bestIdx = 0, bestVal = 0;
for (let i = 0; i < darknessMap.length; i += 50) {
if (darknessMap[i] > bestVal) { bestVal = darknessMap[i]; bestIdx = i; }
}
this.x = bestIdx % w; this.y = Math.floor(bestIdx / w);
this.vx = (Math.random() - 0.5) * 2;
this.vy = (Math.random() - 0.5) * 2;
this.lx = this.x; this.ly = this.y;
this.wanderAngle = Math.random() * Math.PI * 2;
this.totalSteps = 0;
this.smoothDarkness = 0;
// Command state
this.command = null; // Current command object
this.cmdStep = 0; // Steps into current command
this.scribbleAngle = 0; // For SCRIBBLE
this.cmdLineWidth = 0.4;
this.cmdAlpha = 0.1;
}
// === MANUAL MODE: original force-based update ===
update() {
this.totalSteps++;
let steerX = 0, steerY = 0;
const ix = Math.floor(this.x), iy = Math.floor(this.y);
const pixIdx = iy * w + ix;
const localDarkness = (ix >= 0 && ix < w && iy >= 0 && iy < h) ? darknessMap[pixIdx] : 0;
const localError = (ix >= 0 && ix < w && iy >= 0 && iy < h) ? errorMap[pixIdx] : 0;
this.smoothDarkness = this.smoothDarkness * 0.92 + localDarkness * 0.08;
// Force 1: Error Attraction
let errDx = 0, errDy = 0, errTotal = 0;
const sr = pen.searchRadius, ss = pen.searchStep;
for (let ox = -sr; ox <= sr; ox += ss) {
for (let oy = -sr; oy <= sr; oy += ss) {
const cx = ix + ox, cy = iy + oy;
if (cx >= 0 && cx < w && cy >= 0 && cy < h) {
const e = errorMap[cy * w + cx];
if (e > 0.05) {
const dist = Math.sqrt(ox * ox + oy * oy) || 1;
const weight = e / dist;
errDx += (ox / dist) * weight; errDy += (oy / dist) * weight; errTotal += weight;
}
}
}
}
if (errTotal > 0) {
const em = Math.sqrt(errDx * errDx + errDy * errDy) || 1;
steerX += (errDx / em) * pen.errorPull; steerY += (errDy / em) * pen.errorPull;
}
// Force 2: Flow Alignment
if (ix >= 0 && ix < w && iy >= 0 && iy < h && pen.flowStrength > 0) {
steerX += flowFieldX[pixIdx] * pen.flowStrength * localDarkness;
steerY += flowFieldY[pixIdx] * pen.flowStrength * localDarkness;
}
// Force 3: Dwell Looping
const dwellMod = 1.0 + localDarkness * pen.dwellFactor;
// Force 4: Repulsion
if (localError < 0.02 && pen.repulsion > 0) {
steerX += (Math.random() - 0.5) * pen.repulsion * 3;
steerY += (Math.random() - 0.5) * pen.repulsion * 3;
}
// Force 5: Wander
this.wanderAngle += (Math.random() - 0.5) * pen.wanderRate * dwellMod;
steerX += Math.cos(this.wanderAngle) * pen.wanderForce;
steerY += Math.sin(this.wanderAngle) * pen.wanderForce;
// Force 6: Border Avoidance
const m = pen.borderMargin, bp = pen.borderPush;
if (this.x < m) steerX += bp * (1 - this.x / m);
if (this.x > w - m) steerX -= bp * (1 - (w - this.x) / m);
if (this.y < m) steerY += bp * (1 - this.y / m);
if (this.y > h - m) steerY -= bp * (1 - (h - this.y) / m);
this.vx += steerX; this.vy += steerY;
const speedMod = 1.0 - localDarkness * 0.4;
const maxSpd = pen.maxSpeed * Math.max(0.3, speedMod);
const spd = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
if (spd > maxSpd) { this.vx = (this.vx / spd) * maxSpd; this.vy = (this.vy / spd) * maxSpd; }
this.lx = this.x; this.ly = this.y;
this.x += this.vx; this.y += this.vy;
this.x = Math.max(1, Math.min(w - 1, this.x));
this.y = Math.max(1, Math.min(h - 1, this.y));
this.decayError();
}
// === AUTO MODE: execute pen commands from Director ===
executeCommand() {
this.totalSteps++;
if (!this.command) {
// Zero out drawing params when no command active
this.cmdLineWidth = 0;
this.cmdAlpha = 0;
return;
}
const cmd = this.command;
this.cmdStep++;
switch (cmd.type) {
case 'GOTO': {
const dx = cmd.tx - this.x, dy = cmd.ty - this.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 8) { this.command = null; break; }
const speed = cmd.speed || 7;
const drift = Math.sin(this.cmdStep * 0.12) * 0.5;
const nx = dx / dist, ny = dy / dist;
// Smooth velocity transition to prevent dark spots at junctions
const blend = Math.min(1, this.cmdStep / 5); // ramp up over 5 steps
this.vx = this.vx * (1 - blend) + (nx * speed + (-ny) * drift) * blend;
this.vy = this.vy * (1 - blend) + (ny * speed + nx * drift) * blend;
this.lx = this.x; this.ly = this.y;
this.x += this.vx; this.y += this.vy;
this.x = Math.max(1, Math.min(w - 1, this.x));
this.y = Math.max(1, Math.min(h - 1, this.y));
// Suppress drawing for first 3 steps (prevents dark spots at transitions)
this.cmdLineWidth = this.cmdStep < 3 ? 0 : 0.12;
this.cmdAlpha = this.cmdStep < 3 ? 0 : 0.01;
this.decayError();
break;
}
case 'SCRIBBLE': {
if (this.cmdStep >= cmd.steps) { this.command = null; break; }
// CHAOTIC LOCAL MOVEMENT — force-based, not circular orbits
// This mimics the manual mode's behavior but contained to a region
const ix = Math.floor(this.x), iy = Math.floor(this.y);
const pidx = iy * w + ix;
const ld = (ix >= 0 && ix < w && iy >= 0 && iy < h) ? darknessMap[pidx] : 0;
const le = (ix >= 0 && ix < w && iy >= 0 && iy < h) ? errorMap[pidx] : 0;
this.smoothDarkness = this.smoothDarkness * 0.9 + ld * 0.1;
let sx = 0, sy = 0;
// Local error attraction (pull toward nearby high-error pixels)
const sr = 20;
for (let ox = -sr; ox <= sr; ox += 4) {
for (let oy = -sr; oy <= sr; oy += 4) {
const cx = ix + ox, cy = iy + oy;
if (cx >= 0 && cx < w && cy >= 0 && cy < h) {
const e = errorMap[cy * w + cx];
if (e > 0.04) {
const d2 = Math.sqrt(ox * ox + oy * oy) || 1;
sx += (ox / d2) * e / d2 * 0.08;
sy += (oy / d2) * e / d2 * 0.08;
}
}
}
}
// Flow field alignment (follow contours)
if (ix >= 1 && ix < w - 1 && iy >= 1 && iy < h - 1) {
sx += flowFieldX[pidx] * 0.04 * ld * 2;
sy += flowFieldY[pidx] * 0.04 * ld * 2;
}