-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1114 lines (968 loc) · 41.3 KB
/
app.js
File metadata and controls
1114 lines (968 loc) · 41.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
/**
* app.js – TutuDVD Old-TV Bouncing-Text Screensaver
*
* Key exported / notable functions (described in JSDoc below):
* loadTextsFromFile(file)
* loadTextsFromDefaultCsv(seed)
* parseCSV(text)
* parseJSON(text)
* stepSimulation(dt)
* handleCollisions()
* exportVideoWebM()
* exportFramesZip()
*/
'use strict';
/* ═══════════════════════════════════════════════════════════════════════
1. CONSTANTS & DEFAULTS
═══════════════════════════════════════════════════════════════════════ */
/** Target number of scanlines at export/full resolution. */
const TARGET_SCANLINE_COUNT = 300;
/** Video bitrate for WebM export (bits per second). */
const VIDEO_BITRATE = 8_000_000;
const TOP_TAGLINE = 'NASONE TI CHIAMA PI NOMI';
const BOTTOM_TAGLINE = 'XYZ 🌣 VIAGGIO INTORNO AL SOLE';
const MOVING_TEXT_MIN_SIZE = 8;
const MOVING_TEXT_MAX_WIDTH_RATIO = 0.92;
const TRAIL_LIFE_FRAMES = 140;
const TRAIL_MAX_ALPHA = 0.35;
const DEFAULT_TRAIL_GAP = 42;
const TRAIL_SAMPLE_DISTANCE_RATIO = 0.75;
const TRAIL_ECHO_DELAY_FRAMES = 14;
const DEFAULT_NAMES_CSV = 'soprannomi.csv';
const DEFAULT_PICK_MIN = 20;
const DEFAULT_PICK_MAX = 30;
const DEFAULT_TEXTS = [
'tutudvd',
'ciao bella',
'la scuola open source',
'festa di tutu',
'BOUNCE',
'¡holá!',
'dvd screensaver',
'projection mapping',
'loop forever',
'open source',
'creative coding',
'canvas 2d',
'deterministic',
'video mapping',
'seedable rng',
'WebM export',
'scanlines on',
'la festa continua',
'∞',
'press start',
];
/* ═══════════════════════════════════════════════════════════════════════
2. SEEDED RNG (Mulberry32)
═══════════════════════════════════════════════════════════════════════ */
/**
* Hash a string into a 32-bit integer (djb2 variant).
* @param {string} s
* @returns {number}
*/
function hashString(s) {
let h = 0;
for (let i = 0; i < s.length; i++) {
h = Math.imul(31, h) + s.charCodeAt(i) | 0;
}
return h >>> 0;
}
/**
* Create a seeded pseudo-random number generator (Mulberry32).
* Returns a function () => float in [0, 1).
* @param {number|string} seed
* @returns {() => number}
*/
function createRng(seed) {
let s = (typeof seed === 'string') ? hashString(seed) : (seed >>> 0);
// Mulberry32
return function () {
s |= 0; s = s + 0x6D2B79F5 | 0;
let t = Math.imul(s ^ s >>> 15, 1 | s);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
/* ═══════════════════════════════════════════════════════════════════════
3. TEXT PARSING
═══════════════════════════════════════════════════════════════════════ */
/**
* Parse a CSV-like file: one string per line, empty lines ignored.
* @param {string} text Raw file contents.
* @returns {string[]}
*/
function parseCSV(text) {
return text
.split(/\r?\n/)
.map(l => l.trim())
.filter(l => l.length > 0);
}
/**
* Parse a JSON file. Accepts either an array or { items: [...] }.
* @param {string} text Raw file contents.
* @returns {string[]}
*/
function parseJSON(text) {
const data = JSON.parse(text);
if (Array.isArray(data)) return data.map(String);
if (data && Array.isArray(data.items)) return data.items.map(String);
throw new Error('JSON must be an array or { "items": [...] }');
}
/**
* Load texts from a File object (JSON or CSV/TXT).
* Updates the global `state.texts` list and resets the index.
* @param {File} file
* @returns {Promise<void>}
*/
function loadTextsFromFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
try {
const raw = e.target.result;
const isJSON = file.name.toLowerCase().endsWith('.json');
state.texts = isJSON ? parseJSON(raw) : parseCSV(raw);
if (state.texts.length === 0) throw new Error('No texts found in file');
state.textIndex = 0;
console.info(`Loaded ${state.texts.length} text(s) from ${file.name}`);
resolve();
} catch (err) {
reject(err);
}
};
reader.onerror = () => reject(new Error('Could not read file'));
reader.readAsText(file);
});
}
/**
* Pick a subset of entries that is random but spread across the whole source list.
* @param {string[]} items
* @param {() => number} rng
* @param {number} minCount
* @param {number} maxCount
* @returns {string[]}
*/
function pickDistributedSubset(items, rng, minCount, maxCount) {
if (!Array.isArray(items) || items.length === 0) return [];
const upper = Math.max(1, Math.min(items.length, maxCount));
const lower = Math.max(1, Math.min(upper, minCount));
const count = lower + Math.floor(rng() * (upper - lower + 1));
const bucketSize = items.length / count;
const picks = [];
for (let i = 0; i < count; i++) {
const start = Math.floor(i * bucketSize);
const end = Math.max(start, Math.floor((i + 1) * bucketSize) - 1);
const idx = start + Math.floor(rng() * (end - start + 1));
picks.push(items[idx]);
}
// Shuffle picked entries for better perceived randomness.
for (let i = picks.length - 1; i > 0; i--) {
const j = Math.floor(rng() * (i + 1));
[picks[i], picks[j]] = [picks[j], picks[i]];
}
return picks;
}
/**
* Load names from the bundled CSV and choose a distributed random subset.
* @param {string|number} seed
* @returns {Promise<void>}
*/
async function loadTextsFromDefaultCsv(seed) {
const response = await fetch(DEFAULT_NAMES_CSV, { cache: 'no-store' });
if (!response.ok) {
throw new Error(`Failed to load ${DEFAULT_NAMES_CSV} (${response.status})`);
}
const raw = await response.text();
const allNames = parseCSV(raw);
if (allNames.length === 0) {
throw new Error(`${DEFAULT_NAMES_CSV} is empty`);
}
const rng = createRng(seed || 'tutudvd');
state.texts = pickDistributedSubset(allNames, rng, DEFAULT_PICK_MIN, DEFAULT_PICK_MAX);
state.textIndex = 0;
}
/* ═══════════════════════════════════════════════════════════════════════
4. SIMULATION STATE
═══════════════════════════════════════════════════════════════════════ */
const state = {
// texts
texts: [...DEFAULT_TEXTS],
textIndex: 0,
// position & velocity (in preview-canvas coordinates)
x: 0,
y: 0,
vx: 0,
vy: 0,
// colours cycling
hue: 0,
// counters
bounces: 0,
// running flag
running: false,
// rng instance (reset on each seed change)
rng: createRng('tutudvd'),
// for rAF
_rafId: null,
_lastTs: null,
// export cancellation
_exportCancelled: false,
// fading trail history
trailPoints: [],
trailEchoDelay: 0,
};
/* ═══════════════════════════════════════════════════════════════════════
5. CANVAS & DOM REFERENCES
═══════════════════════════════════════════════════════════════════════ */
const canvas = document.getElementById('screen');
const ctx = canvas.getContext('2d');
const wrap = document.getElementById('canvas-wrap');
const inpSeed = document.getElementById('inp-seed');
const inpSpeed = document.getElementById('inp-speed');
const inpPadding = document.getElementById('inp-padding');
const inpFontSize = document.getElementById('inp-fontsize');
const inpTrailGap = document.getElementById('inp-trailgap');
const selFps = document.getElementById('sel-fps');
const selRes = document.getElementById('sel-res');
const inpDuration = document.getElementById('inp-duration');
const selFormat = document.getElementById('sel-format');
const chkScanlines = document.getElementById('chk-scanlines');
const chkVignette = document.getElementById('chk-vignette');
const chkDebug = document.getElementById('chk-debug');
const btnStartStop = document.getElementById('btn-startstop');
const btnReset = document.getElementById('btn-reset');
const btnExport = document.getElementById('btn-export');
const btnCancelExp = document.getElementById('btn-cancel-export');
const btnLoad = document.getElementById('btn-load');
const fileInput = document.getElementById('file-input');
const debugEl = document.getElementById('debug');
const dbgBounces = document.getElementById('dbg-bounces');
const dbgIndex = document.getElementById('dbg-index');
const dbgText = document.getElementById('dbg-text');
const dbgPos = document.getElementById('dbg-pos');
const dbgVel = document.getElementById('dbg-vel');
const progressWrap = document.getElementById('progress-wrap');
const progressLabel = document.getElementById('progress-label');
const progressFill = document.getElementById('progress-bar-fill');
/* ═══════════════════════════════════════════════════════════════════════
6. LAYOUT
═══════════════════════════════════════════════════════════════════════ */
/** Resize preview canvas to fill the wrap div while keeping 16:9. */
function resizeCanvas() {
const { clientWidth: W, clientHeight: H } = wrap;
// Try to fill available space with 16:9
let cw = W, ch = Math.round(W * 9 / 16);
if (ch > H) { ch = H; cw = Math.round(H * 16 / 9); }
canvas.width = cw;
canvas.height = ch;
// If simulation was initialised, keep position in bounds
clampPosition();
}
/* ═══════════════════════════════════════════════════════════════════════
7. HELPERS
═══════════════════════════════════════════════════════════════════════ */
function getSettings() {
return {
seed: inpSeed.value || 'tutudvd',
speed: Math.max(10, parseFloat(inpSpeed.value) || 180),
padding: Math.max(0, parseFloat(inpPadding.value) || 20),
fontSize: Math.max(8, parseFloat(inpFontSize.value) || 48),
trailGap: Math.max(1, parseFloat(inpTrailGap.value) || DEFAULT_TRAIL_GAP),
fps: parseInt(selFps.value, 10) || 60,
duration: Math.max(1, parseFloat(inpDuration.value) || 30),
exportRes: selRes.value,
format: selFormat.value,
scanlines: chkScanlines.checked,
vignette: chkVignette.checked,
};
}
/** Compute the safe inner screen rect (accounting for bezel + padding). */
function getScreenRect(cw, ch, padding) {
const bezel = Math.min(cw, ch) * 0.04; // ~4 % of min dimension
return {
x: bezel + padding,
y: bezel + padding,
w: cw - 2 * bezel - 2 * padding,
h: ch - 2 * bezel - 2 * padding,
};
}
/**
* Measure text bounding box in the current canvas context.
* Returns { w, h } where h is approximated from the font metrics.
*/
function measureText(ctx, text, fontSize, fontWeight = 700) {
ctx.font = `${fontWeight} ${fontSize}px 'Savate', 'Courier New', Courier, sans-serif`;
const m = ctx.measureText(text);
const w = m.width;
// Use actual bounding box if available, otherwise approximate
const h = (m.actualBoundingBoxAscent !== undefined && m.actualBoundingBoxDescent !== undefined)
? (m.actualBoundingBoxAscent + m.actualBoundingBoxDescent)
: fontSize * 0.8;
return { w, h };
}
/**
* Fit moving text width inside the usable screen rect by reducing size if needed.
* Returns the effective font size plus measured width/height.
*/
function getMovingTextMetrics(ctx2d, text, requestedFontSize, sr) {
const maxWidth = Math.max(1, sr.w * MOVING_TEXT_MAX_WIDTH_RATIO);
let fontSize = Math.max(MOVING_TEXT_MIN_SIZE, requestedFontSize);
let metrics = measureText(ctx2d, text, fontSize, 700);
while (fontSize > MOVING_TEXT_MIN_SIZE && metrics.w > maxWidth) {
fontSize--;
metrics = measureText(ctx2d, text, fontSize, 700);
}
return { fontSize, w: metrics.w, h: metrics.h };
}
/** Clamp position so the text stays inside the screen rect. */
function clampPosition() {
const s = getSettings();
const sr = getScreenRect(canvas.width, canvas.height, s.padding);
const { w: tw, h: th } = getMovingTextMetrics(ctx, state.texts[state.textIndex], s.fontSize, sr);
const hw = tw / 2, hh = th / 2;
const minX = sr.x + hw, maxX = sr.x + sr.w - hw;
const minY = sr.y + hh, maxY = sr.y + sr.h - hh;
// Only clamp if the text actually fits; otherwise center
if (maxX > minX) state.x = Math.min(maxX, Math.max(minX, state.x));
else state.x = sr.x + sr.w / 2;
if (maxY > minY) state.y = Math.min(maxY, Math.max(minY, state.y));
else state.y = sr.y + sr.h / 2;
}
/* ═══════════════════════════════════════════════════════════════════════
8. INITIALISATION / RESET
═══════════════════════════════════════════════════════════════════════ */
/** Initialise / reset simulation with current settings. */
function resetSimulation() {
const s = getSettings();
state.rng = createRng(s.seed);
const sr = getScreenRect(canvas.width, canvas.height, s.padding);
const { w: tw, h: th } = getMovingTextMetrics(ctx, state.texts[0], s.fontSize, sr);
const minX = sr.x + tw / 2;
const maxX = sr.x + sr.w - tw / 2;
const minY = sr.y + th / 2;
const maxY = sr.y + sr.h - th / 2;
// Start at a random position inside the screen rect
state.x = maxX > minX ? minX + state.rng() * (maxX - minX) : sr.x + sr.w / 2;
state.y = maxY > minY ? minY + state.rng() * (maxY - minY) : sr.y + sr.h / 2;
// Random initial direction (diagonal-ish, avoid axis-aligned)
const angle = (0.3 + state.rng() * 0.9) * Math.PI / 2; // 17°–69° ish
const quadrant = Math.floor(state.rng() * 4);
const signX = (quadrant & 1) ? 1 : -1;
const signY = (quadrant & 2) ? 1 : -1;
state.vx = signX * Math.cos(angle) * s.speed;
state.vy = signY * Math.sin(angle) * s.speed;
state.hue = Math.floor(state.rng() * 360);
state.bounces = 0;
state.textIndex = 0;
state._lastTs = null;
state.trailPoints = [];
state.trailEchoDelay = 0;
}
/* ═══════════════════════════════════════════════════════════════════════
9. SIMULATION STEP
═══════════════════════════════════════════════════════════════════════ */
const MAX_SUBSTEPS = 8;
/**
* Advance the simulation by dt seconds.
* Uses multiple sub-steps to prevent tunnelling at high speeds.
* @param {number} dt Delta-time in seconds.
* @param {object} ctx2d Canvas 2D context (used only for text measurement).
* @param {number} cw Canvas width.
* @param {number} ch Canvas height.
* @param {object} [settings] Optional settings override (used by export).
*/
function stepSimulation(dt, ctx2d, cw, ch, settings) {
const s = settings || getSettings();
const sr = getScreenRect(cw, ch, s.padding);
const maxTravel = Math.max(Math.abs(state.vx * dt), Math.abs(state.vy * dt));
const substeps = Math.min(MAX_SUBSTEPS, Math.ceil(maxTravel / (Math.min(sr.w, sr.h) * 0.3)) + 1);
const subDt = dt / substeps;
for (let i = 0; i < substeps; i++) {
state.x += state.vx * subDt;
state.y += state.vy * subDt;
handleCollisions(ctx2d, cw, ch, s, sr);
}
}
/**
* Handle collisions with the inner screen bounds.
* On a bounce: reflect velocity axis, clamp position, advance text, shift hue.
* If both axes collide in the same step (corner), both axes reflect but only
* ONE bounce event is counted (see inline note).
* @param {CanvasRenderingContext2D} ctx2d
* @param {number} cw Canvas width.
* @param {number} ch Canvas height.
* @param {object} s Settings object.
* @param {object} sr Screen rect { x, y, w, h }.
*/
function handleCollisions(ctx2d, cw, ch, s, sr) {
const { w: tw, h: th } = getMovingTextMetrics(ctx2d, state.texts[state.textIndex], s.fontSize, sr);
const hw = tw / 2, hh = th / 2;
const minX = sr.x + hw, maxX = sr.x + sr.w - hw;
const minY = sr.y + hh, maxY = sr.y + sr.h - hh;
let hit = false;
// Guard: if text doesn't fit, just center and stop
if (maxX <= minX) { state.x = sr.x + sr.w / 2; state.vx = 0; }
else if (state.x < minX) { state.x = minX; state.vx = Math.abs(state.vx); hit = true; }
else if (state.x > maxX) { state.x = maxX; state.vx = -Math.abs(state.vx); hit = true; }
if (maxY <= minY) { state.y = sr.y + sr.h / 2; state.vy = 0; }
else if (state.y < minY) { state.y = minY; state.vy = Math.abs(state.vy); hit = true; }
else if (state.y > maxY) { state.y = maxY; state.vy = -Math.abs(state.vy); hit = true; }
// ONE bounce event even if both axes reflected (corner case)
if (hit) {
state.bounces++;
// Advance text
state.textIndex = (state.textIndex + 1) % state.texts.length;
// Shift hue by a random amount (20–80 degrees)
state.hue = (state.hue + 20 + Math.floor(state.rng() * 60)) % 360;
}
}
/* ═══════════════════════════════════════════════════════════════════════
10. RENDERING
═══════════════════════════════════════════════════════════════════════ */
/**
* Draw one frame onto the given context.
* Separated from the global `canvas` so the same function works during export.
*/
function drawFrame(ctx2d, cw, ch, settings) {
const s = settings || getSettings();
const sr = getScreenRect(cw, ch, s.padding);
const bezel = Math.min(cw, ch) * 0.04;
// ── Background ──────────────────────────────────────────────────────
ctx2d.clearRect(0, 0, cw, ch);
// Outer bezel gradient
const grad = ctx2d.createLinearGradient(0, 0, cw, ch);
grad.addColorStop(0, '#2e2e2e');
grad.addColorStop(0.5, '#1a1a1a');
grad.addColorStop(1, '#111');
ctx2d.fillStyle = grad;
ctx2d.beginPath();
const br = bezel * 1.5;
roundRect(ctx2d, 0, 0, cw, ch, br);
ctx2d.fill();
// Inner screen
ctx2d.fillStyle = '#fff';
ctx2d.beginPath();
const ir = bezel * 0.8;
roundRect(ctx2d, bezel * 0.5, bezel * 0.5, cw - bezel, ch - bezel, ir);
ctx2d.fill();
// ── Moving text ──────────────────────────────────────────────────────
const text = state.texts[state.textIndex];
const movingMetrics = getMovingTextMetrics(ctx2d, text, s.fontSize, sr);
const movingFontSize = movingMetrics.fontSize;
ctx2d.font = `700 ${movingFontSize}px 'Savate', 'Courier New', Courier, sans-serif`;
ctx2d.textAlign = 'center';
ctx2d.textBaseline = 'middle';
// Colour from hue
const colour = `hsl(${state.hue}, 90%, 60%)`;
ageTrailPoints();
recordTrailPoint(text, movingFontSize, s);
drawMotionTrails(ctx2d);
ctx2d.shadowBlur = 0;
ctx2d.fillStyle = colour;
ctx2d.fillText(text, state.x, state.y);
// ── Static top/bottom taglines ───────────────────────────────────────
drawTaglines(ctx2d, sr, s.fontSize);
// ── Scanlines overlay ────────────────────────────────────────────────
if (s.scanlines) {
drawScanlines(ctx2d, cw, ch);
}
// ── Vignette overlay ─────────────────────────────────────────────────
if (s.vignette) {
drawVignette(ctx2d, cw, ch);
}
}
/** Increase trail sample age and discard samples that are fully faded out. */
function ageTrailPoints() {
for (const point of state.trailPoints) point.age++;
state.trailPoints = state.trailPoints.filter(point => point.age <= TRAIL_LIFE_FRAMES);
if (state.trailEchoDelay > 0) state.trailEchoDelay--;
}
/** Record the current logo position as a trail sample. */
function recordTrailPoint(text, fontSize, settings) {
const last = state.trailPoints[state.trailPoints.length - 1];
const minGap = settings && Number.isFinite(settings.trailGap)
? settings.trailGap
: DEFAULT_TRAIL_GAP;
const sampleDistance = Math.max(minGap, fontSize * TRAIL_SAMPLE_DISTANCE_RATIO);
if (state.trailEchoDelay > 0) return;
if (last && last.text === text && Math.hypot(state.x - last.x, state.y - last.y) < sampleDistance) {
return;
}
state.trailPoints.push({
x: state.x,
y: state.y,
text,
fontSize,
hue: state.hue,
age: 0,
});
state.trailEchoDelay = TRAIL_ECHO_DELAY_FRAMES;
}
/** Draw fading trajectory trails from stored historical samples. */
function drawMotionTrails(ctx2d) {
if (state.trailPoints.length === 0) return;
ctx2d.save();
ctx2d.shadowBlur = 0;
ctx2d.textAlign = 'center';
ctx2d.textBaseline = 'middle';
for (const point of state.trailPoints) {
if (point.age <= 0) continue; // current position is drawn as the main text
const t = 1 - (point.age / TRAIL_LIFE_FRAMES);
if (t <= 0) continue;
const alpha = TRAIL_MAX_ALPHA * t * t;
if (alpha <= 0.005) continue;
ctx2d.font = `700 ${point.fontSize}px 'Savate', 'Courier New', Courier, sans-serif`;
ctx2d.fillStyle = `hsla(${point.hue}, 90%, 45%, ${alpha.toFixed(3)})`;
ctx2d.fillText(point.text, point.x, point.y);
}
ctx2d.restore();
}
/** Draw fixed taglines at the top and bottom of the inner screen area. */
function drawTaglines(ctx2d, screenRect, baseFontSize) {
const topText = TOP_TAGLINE.toUpperCase();
const bottomText = BOTTOM_TAGLINE.toUpperCase();
const maxWidth = screenRect.w * 0.94;
const minSize = 10;
let size = Math.max(16, Math.round(baseFontSize * 0.34));
// Fit both strings on one line using the same font size.
while (size > minSize) {
ctx2d.font = `500 ${size}px 'Savate', 'Courier New', Courier, sans-serif`;
if (
ctx2d.measureText(topText).width <= maxWidth &&
ctx2d.measureText(bottomText).width <= maxWidth
) {
break;
}
size--;
}
const margin = Math.max(8, Math.round(size * 0.4));
ctx2d.save();
ctx2d.font = `500 ${size}px 'Savate', 'Courier New', Courier, sans-serif`;
ctx2d.fillStyle = '#000';
ctx2d.shadowBlur = 0;
ctx2d.textAlign = 'center';
ctx2d.textBaseline = 'top';
ctx2d.fillText(topText, screenRect.x + screenRect.w / 2, screenRect.y + margin);
ctx2d.textBaseline = 'bottom';
ctx2d.fillText(
bottomText,
screenRect.x + screenRect.w / 2,
screenRect.y + screenRect.h - margin
);
ctx2d.restore();
}
/** Draw horizontal scanlines over the full canvas. */
function drawScanlines(ctx2d, cw, ch) {
const lineSpacing = Math.max(2, Math.round(ch / TARGET_SCANLINE_COUNT));
ctx2d.save();
ctx2d.globalAlpha = 0.08;
ctx2d.fillStyle = '#000';
for (let y = 0; y < ch; y += lineSpacing * 2) {
ctx2d.fillRect(0, y, cw, lineSpacing);
}
ctx2d.restore();
}
/** Draw a radial vignette (dark edges). */
function drawVignette(ctx2d, cw, ch) {
const cx = cw / 2, cy = ch / 2;
const r = Math.sqrt(cx * cx + cy * cy);
const vg = ctx2d.createRadialGradient(cx, cy, r * 0.3, cx, cy, r);
vg.addColorStop(0, 'rgba(0,0,0,0)');
vg.addColorStop(1, 'rgba(0,0,0,0.65)');
ctx2d.save();
ctx2d.fillStyle = vg;
ctx2d.fillRect(0, 0, cw, ch);
ctx2d.restore();
}
/**
* Polyfill for roundRect path (supported natively in modern browsers,
* but we inline for safety).
*/
function roundRect(ctx2d, x, y, w, h, r) {
if (ctx2d.roundRect) {
ctx2d.roundRect(x, y, w, h, r);
} else {
r = Math.min(r, w / 2, h / 2);
ctx2d.moveTo(x + r, y);
ctx2d.arcTo(x + w, y, x + w, y + h, r);
ctx2d.arcTo(x + w, y + h, x, y + h, r);
ctx2d.arcTo(x, y + h, x, y, r);
ctx2d.arcTo(x, y, x + w, y, r);
ctx2d.closePath();
}
}
/* ═══════════════════════════════════════════════════════════════════════
11. ANIMATION LOOP
═══════════════════════════════════════════════════════════════════════ */
function tick(ts) {
if (!state.running) return;
if (state._lastTs === null) state._lastTs = ts;
const rawDt = Math.min((ts - state._lastTs) / 1000, 0.1); // cap at 100 ms
state._lastTs = ts;
const s = getSettings();
stepSimulation(rawDt, ctx, canvas.width, canvas.height, s);
drawFrame(ctx, canvas.width, canvas.height, s);
updateDebug();
state._rafId = requestAnimationFrame(tick);
}
function startAnimation() {
if (state.running) return;
state.running = true;
state._lastTs = null;
btnStartStop.textContent = '⏸ Pause';
state._rafId = requestAnimationFrame(tick);
}
function pauseAnimation() {
state.running = false;
if (state._rafId) { cancelAnimationFrame(state._rafId); state._rafId = null; }
btnStartStop.textContent = '▶ Start';
}
/* ═══════════════════════════════════════════════════════════════════════
12. DEBUG OVERLAY
═══════════════════════════════════════════════════════════════════════ */
function updateDebug() {
if (debugEl.classList.contains('hidden')) return;
dbgBounces.textContent = state.bounces;
dbgIndex.textContent = state.textIndex;
dbgText.textContent = state.texts[state.textIndex];
dbgPos.textContent = `${state.x.toFixed(1)}, ${state.y.toFixed(1)}`;
dbgVel.textContent = `${state.vx.toFixed(1)}, ${state.vy.toFixed(1)}`;
}
/* ═══════════════════════════════════════════════════════════════════════
13. EXPORT HELPERS
═══════════════════════════════════════════════════════════════════════ */
/** Parse an export resolution string like "1920x1080" → { w, h }. */
function parseResolution(resStr, previewW, previewH) {
if (resStr === 'preview') return { w: previewW, h: previewH };
const parts = resStr.split('x');
return { w: parseInt(parts[0], 10), h: parseInt(parts[1], 10) };
}
/** Scale simulation position/velocity from preview canvas to export canvas. */
function scaleStateToCanvas(exportW, exportH) {
const scaleX = exportW / canvas.width;
const scaleY = exportH / canvas.height;
return {
x: state.x * scaleX,
y: state.y * scaleY,
vx: state.vx * scaleX,
vy: state.vy * scaleY,
};
}
/** Show / hide the progress bar UI. */
function setExportProgress(current, total) {
const pct = total > 0 ? (current / total) * 100 : 0;
progressWrap.classList.add('active');
progressLabel.textContent = `Exporting… ${current} / ${total} frames`;
progressFill.style.width = `${pct.toFixed(1)}%`;
}
function hideExportProgress() {
progressWrap.classList.remove('active');
}
/**
* Build a filename prefix with timestamp + seed + resolution.
* @param {string} seed
* @param {number} w
* @param {number} h
* @returns {string}
*/
function exportFilename(seed, w, h) {
const d = new Date();
// Format: YYYY-MM-DD_HH-MM-SS
const ts = [
d.getFullYear(),
String(d.getMonth() + 1).padStart(2, '0'),
String(d.getDate()).padStart(2, '0'),
].join('-') + '_' + [
String(d.getHours()).padStart(2, '0'),
String(d.getMinutes()).padStart(2, '0'),
String(d.getSeconds()).padStart(2, '0'),
].join('-');
const safeSeed = seed.replace(/[^a-zA-Z0-9_-]/g, '_');
return `tutudvd_${ts}_seed-${safeSeed}_${w}x${h}`;
}
/* ═══════════════════════════════════════════════════════════════════════
14. EXPORT – WebM via MediaRecorder
═══════════════════════════════════════════════════════════════════════ */
/**
* Export the animation as a WebM video using canvas.captureStream() + MediaRecorder.
* Runs deterministically: advances simulation by exact 1/fps timestep per frame,
* then manually draws each frame onto a dedicated off-screen canvas whose stream
* is fed to MediaRecorder.
*
* Note: MediaRecorder / captureStream are not available in all browsers.
* Falls back to exportFramesZip() automatically.
*/
async function exportVideoWebM() {
if (!canvas.captureStream || !window.MediaRecorder) {
console.warn('MediaRecorder not available – falling back to PNG sequence');
return exportFramesZip();
}
const s = getSettings();
const { w: expW, h: expH } = parseResolution(s.exportRes, canvas.width, canvas.height);
const fps = s.fps;
const totalFrames = Math.round(s.duration * fps);
const dt = 1 / fps;
// Scale font size and speed to export canvas
const scaleX = expW / canvas.width;
const expFontSize = Math.round(s.fontSize * scaleX);
const expSpeed = s.speed * scaleX;
// Off-screen canvas for export
const expCanvas = document.createElement('canvas');
expCanvas.width = expW;
expCanvas.height = expH;
const expCtx = expCanvas.getContext('2d');
// Capture stream
const stream = expCanvas.captureStream(fps);
const mimeType = MediaRecorder.isTypeSupported('video/webm;codecs=vp9')
? 'video/webm;codecs=vp9'
: 'video/webm';
const recorder = new MediaRecorder(stream, { mimeType, videoBitsPerSecond: VIDEO_BITRATE });
const chunks = [];
recorder.ondataavailable = (e) => { if (e.data.size > 0) chunks.push(e.data); };
recorder.start();
// Snapshot of current simulation state (so we don't mutate live state)
const savedState = {
...state,
trailPoints: state.trailPoints.map(point => ({ ...point })),
};
// Re-initialise simulation deterministically for export
state.rng = createRng(s.seed);
const sr0 = getScreenRect(expW, expH, s.padding);
const { w: tw0, h: th0 } = getMovingTextMetrics(expCtx, state.texts[0], expFontSize, sr0);
const minX0 = sr0.x + tw0 / 2;
const maxX0 = sr0.x + sr0.w - tw0 / 2;
const minY0 = sr0.y + th0 / 2;
const maxY0 = sr0.y + sr0.h - th0 / 2;
state.x = maxX0 > minX0 ? minX0 + state.rng() * (maxX0 - minX0) : sr0.x + sr0.w / 2;
state.y = maxY0 > minY0 ? minY0 + state.rng() * (maxY0 - minY0) : sr0.y + sr0.h / 2;
const angle0 = (0.3 + state.rng() * 0.9) * Math.PI / 2;
const q0 = Math.floor(state.rng() * 4);
state.vx = ((q0 & 1) ? 1 : -1) * Math.cos(angle0) * expSpeed;
state.vy = ((q0 & 2) ? 1 : -1) * Math.sin(angle0) * expSpeed;
state.hue = Math.floor(state.rng() * 360);
state.bounces = 0;
state.textIndex = 0;
state.trailPoints = [];
state.trailEchoDelay = 0;
// Override font size and speed in settings for export frames
const expSettings = { ...s, fontSize: expFontSize, speed: expSpeed, trailGap: s.trailGap * scaleX };
state._exportCancelled = false;
btnCancelExp.style.display = '';
btnExport.disabled = true;
try {
for (let f = 0; f < totalFrames; f++) {
if (state._exportCancelled) break;
// Advance simulation
stepSimulation(dt, expCtx, expW, expH, expSettings);
// Draw frame
drawFrame(expCtx, expW, expH, expSettings);
// Update progress every 10 frames to stay responsive
if (f % 10 === 0) {
setExportProgress(f, totalFrames);
// Yield to browser
await new Promise(r => setTimeout(r, 0));
}
}
} finally {
// Restore live simulation state
Object.assign(state, savedState);
state.trailPoints = savedState.trailPoints.map(point => ({ ...point }));
}
recorder.stop();
await new Promise(r => { recorder.onstop = r; });
const blob = new Blob(chunks, { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${exportFilename(s.seed, expW, expH)}.webm`;
a.click();
setTimeout(() => URL.revokeObjectURL(url), 10000);
setExportProgress(totalFrames, totalFrames);
setTimeout(hideExportProgress, 1500);
btnCancelExp.style.display = 'none';
btnExport.disabled = false;
}
/* ═══════════════════════════════════════════════════════════════════════
15. EXPORT – PNG sequence in ZIP (JSZip)
═══════════════════════════════════════════════════════════════════════ */
/**
* Load JSZip dynamically from CDN.
* @returns {Promise<JSZip constructor>}
*/
function loadJSZip() {
if (window.JSZip) return Promise.resolve(window.JSZip);
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = window._JSZIP_CDN;
script.onload = () => resolve(window.JSZip);
script.onerror = () => reject(new Error('Failed to load JSZip'));
document.head.appendChild(script);
});
}
/**
* Export the animation as a ZIP of PNG frames.
* Used when MediaRecorder is unavailable or explicitly requested.
* Same deterministic approach as exportVideoWebM.
*/
async function exportFramesZip() {
let JSZip;
try {
JSZip = await loadJSZip();
} catch (err) {
alert('Could not load JSZip: ' + err.message);
return;
}
const s = getSettings();
const { w: expW, h: expH } = parseResolution(s.exportRes, canvas.width, canvas.height);
const fps = s.fps;
const totalFrames = Math.round(s.duration * fps);
const dt = 1 / fps;
const scaleX = expW / canvas.width;
const expFontSize = Math.round(s.fontSize * scaleX);
const expSpeed = s.speed * scaleX;
const expCanvas = document.createElement('canvas');
expCanvas.width = expW;
expCanvas.height = expH;
const expCtx = expCanvas.getContext('2d');
// Snapshot + deterministic re-init (same logic as exportVideoWebM)
const savedState = {
...state,
trailPoints: state.trailPoints.map(point => ({ ...point })),
};
state.rng = createRng(s.seed);
const sr0 = getScreenRect(expW, expH, s.padding);
const { w: tw0, h: th0 } = getMovingTextMetrics(expCtx, state.texts[0], expFontSize, sr0);
const minX0 = sr0.x + tw0 / 2;
const maxX0 = sr0.x + sr0.w - tw0 / 2;
const minY0 = sr0.y + th0 / 2;
const maxY0 = sr0.y + sr0.h - th0 / 2;
state.x = maxX0 > minX0 ? minX0 + state.rng() * (maxX0 - minX0) : sr0.x + sr0.w / 2;
state.y = maxY0 > minY0 ? minY0 + state.rng() * (maxY0 - minY0) : sr0.y + sr0.h / 2;
const angle0 = (0.3 + state.rng() * 0.9) * Math.PI / 2;
const q0 = Math.floor(state.rng() * 4);
state.vx = ((q0 & 1) ? 1 : -1) * Math.cos(angle0) * expSpeed;
state.vy = ((q0 & 2) ? 1 : -1) * Math.sin(angle0) * expSpeed;
state.hue = Math.floor(state.rng() * 360);
state.bounces = 0;
state.textIndex = 0;
state.trailPoints = [];
state.trailEchoDelay = 0;
const expSettings = { ...s, fontSize: expFontSize, speed: expSpeed, trailGap: s.trailGap * scaleX };
const zip = new JSZip();
const folder = zip.folder(exportFilename(s.seed, expW, expH));
state._exportCancelled = false;
btnCancelExp.style.display = '';
btnExport.disabled = true;
try {
for (let f = 0; f < totalFrames; f++) {
if (state._exportCancelled) break;
stepSimulation(dt, expCtx, expW, expH, expSettings);
drawFrame(expCtx, expW, expH, expSettings);
// Add PNG to zip
const frameNum = String(f).padStart(6, '0');
const dataUrl = expCanvas.toDataURL('image/png');
const base64 = dataUrl.split(',')[1];
folder.file(`frame_${frameNum}.png`, base64, { base64: true });
if (f % 5 === 0) {
setExportProgress(f, totalFrames);
await new Promise(r => setTimeout(r, 0));
}
}