-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1845 lines (1540 loc) · 99.8 KB
/
Copy pathindex.html
File metadata and controls
1845 lines (1540 loc) · 99.8 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>Phosphor — Reference Manual</title>
<style>
:root {
--bg: #080808;
--bg2: #111111;
--bg3: #1a1a1a;
--border: #2a2a2a;
--text: #d0d0c8;
--dim: #888880;
--green: #3ec878;
--amber: #c8a030;
--blue: #5090d0;
--red: #d05050;
--code-bg: #141414;
--code-text: #a8c890;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg);
color: var(--text);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
font-size: 15px;
line-height: 1.65;
max-width: 900px;
margin: 0 auto;
padding: 2rem 1.5rem 6rem;
}
/* ── Typography ── */
h1 { color: var(--green); font-size: 2rem; margin: 2rem 0 0.4rem; letter-spacing: 0.04em; }
h2 { color: var(--amber); font-size: 1.25rem; margin: 2.5rem 0 0.8rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; }
h3 { color: var(--text); font-size: 1rem; margin: 1.4rem 0 0.4rem; }
p { margin: 0.6rem 0; }
ul, ol { margin: 0.5rem 0 0.5rem 1.4rem; }
li { margin: 0.25rem 0; }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
/* ── Code ── */
code {
font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
font-size: 0.88em;
background: var(--code-bg);
color: var(--code-text);
padding: 0.1em 0.35em;
border-radius: 3px;
}
pre {
background: var(--code-bg);
color: var(--code-text);
font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
font-size: 0.85em;
line-height: 1.55;
padding: 1rem 1.2rem;
border-radius: 5px;
border-left: 3px solid var(--border);
overflow-x: auto;
margin: 0.8rem 0;
}
pre code { background: none; padding: 0; color: inherit; font-size: inherit; }
/* ── Collapsible sections ── */
details { margin: 0.5rem 0; }
details > summary {
cursor: pointer;
color: var(--amber);
font-weight: 600;
font-size: 1.2rem;
padding: 0.5rem 0;
border-bottom: 1px solid var(--border);
list-style: none;
display: flex;
align-items: center;
gap: 0.5rem;
user-select: none;
}
details > summary::-webkit-details-marker { display: none; }
details > summary::before {
content: "▶";
font-size: 0.7em;
color: var(--dim);
transition: transform 0.15s;
display: inline-block;
}
details[open] > summary::before { transform: rotate(90deg); }
details > .section-body { padding: 0.8rem 0 1rem; }
/* ── Tables ── */
table {
width: 100%;
border-collapse: collapse;
margin: 0.8rem 0;
font-size: 0.9em;
}
th {
background: var(--bg3);
color: var(--amber);
text-align: left;
padding: 0.45rem 0.7rem;
font-weight: 600;
border-bottom: 1px solid var(--border);
}
td {
padding: 0.4rem 0.7rem;
border-bottom: 1px solid var(--border);
vertical-align: top;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg2); }
td code { font-size: 0.85em; }
/* ── Callout boxes ── */
.note, .warn, .tip {
border-radius: 4px;
padding: 0.7rem 1rem;
margin: 1rem 0;
font-size: 0.93em;
border-left: 3px solid;
}
.note { background: #0d1a10; border-color: var(--green); color: #a8d8b0; }
.warn { background: #1a100d; border-color: var(--red); color: #d8a090; }
.tip { background: #13120a; border-color: var(--amber); color: #c8b888; }
.note strong, .warn strong, .tip strong { display: block; margin-bottom: 0.25rem; }
/* ── Nav ── */
nav {
background: var(--bg2);
border: 1px solid var(--border);
border-radius: 5px;
padding: 1rem 1.2rem;
margin: 1.5rem 0 2.5rem;
font-size: 0.88em;
column-count: 2;
column-gap: 2rem;
}
nav a { color: var(--dim); display: block; padding: 0.15rem 0; }
nav a:hover { color: var(--green); }
/* ── Sub-heading inside section ── */
.api-entry { margin: 1.2rem 0 0.2rem; }
.sig {
font-family: "SF Mono", "Fira Code", monospace;
font-size: 0.92em;
color: var(--green);
background: var(--bg3);
padding: 0.4rem 0.8rem;
border-radius: 4px;
display: block;
margin-bottom: 0.4rem;
}
/* ── Header ── */
.site-header { border-bottom: 1px solid var(--border); padding-bottom: 1.2rem; margin-bottom: 0.5rem; }
.site-header p { color: var(--dim); font-size: 0.93em; margin-top: 0.3rem; }
.version { font-size: 0.78em; color: var(--dim); font-family: monospace; }
</style>
</head>
<body>
<div class="site-header">
<h1>Phosphor</h1>
<p>A Lua-scriptable real-time graphics engine for live performance and audiovisual work.</p>
<span class="version">reference manual</span>
</div>
<nav>
<a href="#quickstart">Quick Start</a>
<a href="#keys">Keyboard</a>
<a href="#anatomy">Scene Anatomy</a>
<a href="#drawing">Drawing API</a>
<a href="#text">Bitmap Text</a>
<a href="#transforms">Transform Stack</a>
<a href="#feedback">Feedback</a>
<a href="#shaders">Shader Pipeline</a>
<a href="#custom-shaders">Custom Shaders</a>
<a href="#noise">Noise Functions</a>
<a href="#easing">Easing & Math</a>
<a href="#slew">Slewed Values</a>
<a href="#vectors">Vectors</a>
<a href="#wolfram">Wolfram 1D CA</a>
<a href="#conway">Conway's Game of Life</a>
<a href="#images">Images & Sprites</a>
<a href="#canvas">Canvas</a>
<a href="#waveforms">Waveforms</a>
<a href="#wire3d">3D Wireframe</a>
<a href="#fractals">Fractal Shaders</a>
<a href="#clock">Musical Clock</a>
<a href="#osc">OSC</a>
<a href="#scenes">Example Scenes</a>
</nav>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="quickstart">
<summary>Quick Start</summary>
<div class="section-body">
<h3>Build</h3>
<pre><code>cmake -B build -DCMAKE_PREFIX_PATH=$(brew --prefix)
cmake --build build</code></pre>
<h3>Run</h3>
<pre><code>./build/phosphor -s scenes/test.lua</code></pre>
<p>Once running, the engine is driven by <a href="#keys">keyboard</a> — <code>F</code> fullscreen, <code>Space</code> pause, <code>B</code> blackout, <code>T</code> alignment grid, <code>P</code> parameters.</p>
<h3>Command-line flags</h3>
<table>
<tr><th>Flag</th><th>Description</th><th>Default</th></tr>
<tr><td><code>-s <path></code></td><td>Scene file to load</td><td>—</td></tr>
<tr><td><code>-d <n></code></td><td>Display index (0 = primary)</td><td>0</td></tr>
<tr><td><code>-f</code></td><td>Start fullscreen</td><td>off</td></tr>
<tr><td><code>-p <n></code></td><td>UDP port to listen for OSC on. If something else already holds the port (an OSC monitor such as Protokol, or a stray phosphor), the bind fails loudly at startup rather than silently sharing it — the kernel would deliver each datagram to only one listener.</td><td>9000</td></tr>
<tr><td><code>-h</code></td><td>Print help and exit</td><td>—</td></tr>
</table>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="keys">
<summary>Keyboard</summary>
<div class="section-body">
<p>Everything the engine does by hand. Grouped by why you would reach for it — the output group is the one to know before a show.</p>
<h3>Output — live safety</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td><code>B</code></td><td><strong>Blackout.</strong> Output goes to black instantly. The scene keeps running and the feedback buffer keeps filling underneath, so restoring brings back a live image rather than a frozen one. This is the “something is wrong, kill the projector” key.</td></tr>
<tr><td><code>-</code> / <code>=</code></td><td>Master output level down / up, in 10% steps. For venue brightness, and for not blinding a front row.</td></tr>
<tr><td><code>F</code></td><td>Toggle fullscreen on the current display. Viewport and Lua globals update immediately.</td></tr>
</table>
<h3>Transport — controlling time</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td><code>Space</code></td><td>Pause. The scene keeps redrawing with <code>dt = 0</code>, so feedback trails hold still rather than the screen going black, and anything driven by <code>elapsed()</code> freezes in place.</td></tr>
<tr><td><code>[</code> / <code>]</code></td><td>Halve / double the rate time passes. Affects <code>dt</code>, <code>elapsed()</code> and <code>u_time</code> together. Clamped to ×1/64 … ×8.</td></tr>
<tr><td><code>\</code></td><td>Back to ×1 speed, unpaused.</td></tr>
</table>
<p>None of these touch the <a href="#clock">musical clock</a>, which runs on the wall clock — pausing the visuals does not distort tempo or beat phase.</p>
<h3>Scene and parameters</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td><code>R</code></td><td>Reload the scene now, without waiting for a file save. Useful for re-running <code>on_load</code> to reseed an automaton. State in <code>persist</code> still survives.</td></tr>
<tr><td><code>P</code></td><td>Overlay the declared parameters and their current values. A star marks those under live OSC control rather than following the scene file.</td></tr>
<tr><td><code>0</code></td><td>Reset every parameter to the default written in the scene file, abandoning live OSC values.</td></tr>
</table>
<h3>Setup</h3>
<table>
<tr><th>Key</th><th>Action</th></tr>
<tr><td><code>T</code></td><td>Projector alignment grid, drawn over the running scene: 16-column grid, bright border, centre cross, and a circle that reveals any aspect-ratio stretch — it only stays circular if the pixels are square. The drawable size is printed in the corner.</td></tr>
<tr><td><code>Esc</code></td><td>Quit.</td></tr>
</table>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="anatomy">
<summary>Scene Anatomy</summary>
<div class="section-body">
<p>A scene is a plain Lua file. The engine calls these optional global functions:</p>
<table>
<tr><th>Hook</th><th>When called</th><th>Typical use</th></tr>
<tr><td><code>on_load()</code></td><td>Once, after the file is executed</td><td>Allocate canvases, load images, set initial shader</td></tr>
<tr><td><code>on_frame(dt)</code></td><td>Every frame — <code>dt</code> is seconds since last frame</td><td>Clear, draw geometry, advance animation state</td></tr>
<tr><td><code>on_osc(addr, ...)</code></td><td>Once per incoming OSC message</td><td>React to live control from SuperCollider / PD</td></tr>
<tr><td><code>on_beat(phase)</code></td><td>On each <code>/beat</code> OSC message</td><td>Trigger something musically — advance a pattern, fire a flash, step an automaton</td></tr>
</table>
<p>All three are optional. A scene that defines none of them is valid (the window shows a “no scene loaded” message).</p>
<h3>Built-in globals</h3>
<table>
<tr><th>Name</th><th>Type</th><th>Description</th></tr>
<tr><td><code>screen_width</code></td><td>integer</td><td>Drawable width in pixels (Retina-aware). Updated on resize and fullscreen toggle.</td></tr>
<tr><td><code>screen_height</code></td><td>integer</td><td>Drawable height in pixels.</td></tr>
<tr><td><code>persist</code></td><td>table</td><td>A table whose contents survive a hot reload — see <a href="#anatomy">Hot reload</a> below. Empty on first run.</td></tr>
<tr><td><code>elapsed()</code></td><td>function → number</td><td>Seconds since startup — the same clock the engine sends to shaders as <code>u_time</code>, so Lua and shader animation stay in step. Responds to pause and the time-scale keys, which a scene’s own <code>t = t + dt</code> accumulator does not.</td></tr>
</table>
<h3>Minimal scene template</h3>
<pre><code>-- Copy-paste starting point
local t = 0
function on_load()
shader_set("scanlines")
end
function on_frame(dt)
t = t + dt
clear(0, 0, 0, 1)
set_color(0, 1, 0.4, 1)
draw_circle(screen_width / 2, screen_height / 2, 60 + math.sin(t * 2) * 20)
end</code></pre>
<h3>Hot reload</h3>
<p>Save the scene file while Phosphor is running — changes appear within ~200 ms. The Lua VM is torn down and rebuilt; GPU state (renderer and FBOs) is untouched. <code>on_load()</code> is called again after reload. The post-process shader chain is reset on reload, so a scene always starts from the pipeline its own <code>on_load</code> asks for rather than inheriting the previous scene’s.</p>
<p>Fragment shaders hot-reload too: save a file in <code>shaders/</code> and the loaded programs recompile in place within ~250 ms, including the ones canvases own. If the edited source fails to compile, the previous program keeps running and the GLSL error is printed — a typo mid-set doesn’t drop the pass.</p>
<h3>Keeping state across a reload</h3>
<p>Reloading destroys the whole Lua VM, which is what makes it reliable — no stale globals, no half-updated closures. It also means a scene normally restarts from nothing on every save: a clock accumulating <code>t = t + dt</code> returns to zero, a particle field is rebuilt, a Game of Life grid that had been running for ten minutes is gone.</p>
<p>Anything stored in the global <code>persist</code> table survives instead. The idiom is to create state only if it is not already there:</p>
<pre><code>function on_load()
-- Runs on the first load; skipped on every reload after it, because
-- persist.particles already exists.
if not persist.particles then
persist.particles = {}
for i = 1, 300 do
persist.particles[i] = { pos = vec(0, 0), vel = vec.random(50) }
end
end
persist.t = persist.t or 0
end
function on_frame(dt)
persist.t = persist.t + dt -- keeps counting across saves
end</code></pre>
<p>Now you can edit the drawing code, the forces, the colours — and the simulation carries on mid-flight instead of starting over. <code>scenes/vector_test.lua</code> is built around this; edit any constant in it while it runs.</p>
<h4>What can and cannot cross</h4>
<table>
<tr><th>Crosses</th><th>Does not</th></tr>
<tr>
<td>Numbers (integer and float subtypes preserved), strings, booleans, vectors, and tables of those, nested up to 16 deep.</td>
<td>Functions, coroutines, and every userdata except <code>vec</code> — canvases, images, sprite sheets, <code>conway</code> and <code>wolfram</code> objects. These own GPU textures, framebuffers or C buffers that are freed when the VM closes, so there is nothing honest to carry across.</td>
</tr>
</table>
<p>Anything that cannot cross is dropped and reported on stderr rather than disappearing silently. Table cycles (<code>persist.a.self = persist.a</code>) are detected and dropped too. If you want an automaton’s grid to survive, copy it into a plain table with the <code>get</code>/<code>set</code> methods those types already provide.</p>
<h3>When a scene errors</h3>
<p>A runtime error in <code>on_frame</code> does not black out the window. The half-drawn frame is discarded, the last complete frame is put back up, and the error message is drawn across the top of the screen. Logging is throttled to once a second so the terminal stays readable. Fix the file, save, and the next good frame clears the banner. The same applies to a syntax error saved into the scene: the visuals hold on the last good frame while you sort it out.</p>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="drawing">
<summary>Drawing API</summary>
<div class="section-body">
<p>All coordinates are in pixel space: <strong>(0, 0) is the top-left corner</strong>, +X rightward, +Y downward. All geometry goes through the current transform matrix (see <a href="#transforms">Transform Stack</a>).</p>
<h3>Clear</h3>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>clear(r, g, b, a)</code></td><td>Fill the frame buffer with a solid colour. Call this at the start of <code>on_frame</code> unless you want feedback trails.</td></tr>
</table>
<h3>Colour state</h3>
<p>Two independent colour registers. State persists across frames until changed.</p>
<table>
<tr><th>Signature</th><th>Applies to</th><th>Default</th></tr>
<tr><td><code>set_color(r, g, b, a)</code></td><td><code>draw_rect</code>, <code>draw_circle</code></td><td>1, 1, 1, 1</td></tr>
<tr><td><code>set_stroke(r, g, b, a)</code></td><td><code>draw_line</code>, <code>draw_point</code></td><td>1, 1, 1, 1</td></tr>
<tr><td><code>set_stroke_weight(w)</code></td><td>Line thickness / point size in pixels</td><td>1.0</td></tr>
<tr><td><code>set_circle_segments(n)</code></td><td>Tessellation for <code>draw_circle</code></td><td>32</td></tr>
</table>
<h3>HSV colour</h3>
<p>RGB is how the hardware thinks; hue/saturation/value is how visuals think. “Rotate the hue over time” is one number going up in HSV, and an awkward three-channel dance in RGB.</p>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>hsv(h, s, v [, a])</code></td><td>Returns <code>r, g, b, a</code> — four values, so it drops straight into anything that takes a colour.</td></tr>
<tr><td><code>set_color_hsv(h, s, v [, a])</code></td><td>Shorthand for <code>set_color(hsv(...))</code>.</td></tr>
<tr><td><code>set_stroke_hsv(h, s, v [, a])</code></td><td>Shorthand for <code>set_stroke(hsv(...))</code>.</td></tr>
</table>
<p>All components are <strong>0..1</strong>, matching every other colour call in the engine — hue is not in degrees. Hue <strong>wraps</strong>, so <code>h = 1.25</code> and <code>h = 0.25</code> are the same colour and you can feed an ever-increasing number straight in without wrapping it yourself. Saturation 0 gives grey at brightness <code>v</code>.</p>
<pre><code>-- Hue cycling on the engine clock
set_color(hsv(elapsed() * 0.1, 0.9, 1))
draw_circle(cx, cy, 60)
-- A whole palette from one loop
for i = 0, 7 do
set_color_hsv(i / 8, 0.85, 1)
draw_rect(i * 60, 20, 50, 50)
end
-- hsv() composes with clear() too
clear(hsv(0.6, 0.4, 0.12))</code></pre>
<div class="note">
<strong>Expansion rule</strong>
<code>hsv()</code> returns four values, and Lua keeps all of them only in the
last argument position. <code>set_color(hsv(h, 1, 1))</code> is fine.
<code>set_color(hsv(h, 1, 1), 0.5)</code> is not — that passes only the
red channel and then 0.5. Pass alpha to <code>hsv</code> itself:
<code>hsv(h, 1, 1, 0.5)</code>.
</div>
<h3>Blend mode</h3>
<p><code>set_blend(mode)</code> controls how new pixels combine with what is already on screen. It resets to <code>"alpha"</code> at the start of every frame, like the colour registers, so a scene that wants another mode asks for it each frame.</p>
<table>
<tr><th>Mode</th><th>Result</th><th>Use for</th></tr>
<tr><td><code>"alpha"</code></td><td><code>src×a + dst×(1-a)</code></td><td>Normal layering. The default.</td></tr>
<tr><td><code>"add"</code></td><td><code>src×a + dst</code></td><td>Light accumulates instead of replacing — overlapping strokes brighten toward white. This is how phosphor, neon and CRT beams actually behave, and it is the mode to reach for with <code>draw_feedback</code>.</td></tr>
<tr><td><code>"multiply"</code></td><td><code>src×dst</code></td><td>Darkening and tinting. White leaves the destination alone, black knocks it out.</td></tr>
<tr><td><code>"screen"</code></td><td><code>src + dst×(1-src)</code></td><td>Lightens like <code>add</code>, but approaches white gently rather than clipping hard.</td></tr>
</table>
<pre><code>function on_frame(dt)
draw_feedback(0.92) -- fade the previous frame
set_blend("add") -- strokes now glow where they cross
set_stroke(0.1, 0.9, 0.5, 1)
for i = 1, 40 do
local a = elapsed() + i * 0.15
draw_line(cx, cy, cx + math.cos(a) * 300, cy + math.sin(a) * 300)
end
end</code></pre>
<h3>Primitives</h3>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>draw_rect(x, y, w, h)</code></td><td>Filled rectangle. <code>(x, y)</code> is the top-left corner.</td></tr>
<tr><td><code>draw_circle(cx, cy, r)</code></td><td>Filled circle centred at <code>(cx, cy)</code> with radius <code>r</code> pixels.</td></tr>
<tr><td><code>draw_line(x1, y1, x2, y2)</code></td><td>Expanded quad line (not <code>glLineWidth</code>). Uses stroke colour + weight.</td></tr>
<tr><td><code>draw_point(x, y)</code></td><td>Square dot centred at <code>(x, y)</code>. Size = stroke weight.</td></tr>
</table>
<pre><code>function on_frame(dt)
clear(0.05, 0.05, 0.05, 1)
set_color(0.2, 0.8, 0.4, 1)
draw_rect(100, 100, 200, 80)
set_color(1, 0.3, 0.1, 1)
set_circle_segments(64)
draw_circle(400, 300, 50)
set_stroke(1, 1, 1, 1)
set_stroke_weight(2)
draw_line(0, 0, screen_width, screen_height)
set_stroke_weight(6)
draw_point(screen_width / 2, screen_height / 2)
end</code></pre>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="text">
<summary>Bitmap Text</summary>
<div class="section-body">
<p>An 8×8 monochrome bitmap font built into the binary — no font file to load, no dependency. Glyphs are drawn as filled rectangles through the normal geometry path, which means text obeys <code>set_color</code> and the transform stack exactly like a rectangle does: it rotates, scales and nests inside <code>push</code>/<code>pop</code>.</p>
<h3>API</h3>
<span class="sig">draw_text(x, y, str [, scale])</span>
<table>
<tr><th>Parameter</th><th>Default</th><th>Description</th></tr>
<tr><td><code>x, y</code></td><td>—</td><td>Top-left corner of the first glyph, in pixel space.</td></tr>
<tr><td><code>str</code></td><td>—</td><td>The string to draw. <code>\n</code> returns to the starting <code>x</code> and drops one line.</td></tr>
<tr><td><code>scale</code></td><td>1.0</td><td>Multiplies both axes. A glyph occupies <code>8 × scale</code> pixels square, so <code>scale = 3</code> gives 24 px text.</td></tr>
</table>
<span class="sig">text_width(str [, scale]) → number</span>
<p>Pixel width of the <em>longest line</em> in <code>str</code> — every character is <code>8 × scale</code> wide, and <code>\n</code> starts the measurement again. Use it to centre or right-align, since the font is fixed-width and there is nothing else to measure.</p>
<h3>Character set</h3>
<p>Printable ASCII, <code>0x20</code> (space) to <code>0x7E</code> (<code>~</code>). Bytes outside that range advance the cursor by one glyph without drawing anything, so a stray control character or a UTF-8 multi-byte sequence leaves gaps rather than garbage — there are no accented or non-Latin glyphs in the font.</p>
<h3>Colour and transforms</h3>
<p>Text uses the <strong>fill</strong> colour from <code>set_color</code>, not the stroke colour. Because it goes through the transform stack, this works as you would expect:</p>
<pre><code>function on_frame(dt)
clear(0, 0, 0, 1)
-- Plain text, top-left
set_color(0, 1, 0.4, 1)
draw_text(20, 20, "PHOSPHOR", 3)
-- Horizontally centred
local label = "SIGNAL LOST"
local w = text_width(label, 2)
set_color(1, 0.3, 0.2, 1)
draw_text(screen_width / 2 - w / 2, 100, label, 2)
-- Multi-line: \n returns to the starting x
set_color(0.6, 0.6, 0.6, 1)
draw_text(20, 160, "CH 01 -12.4 dB\nCH 02 -09.1 dB\nCH 03 -- ", 2)
-- Rotating, because it is geometry like anything else
push()
translate(screen_width / 2, screen_height / 2)
rotate(elapsed())
local t = "ROTATE"
draw_text(-text_width(t, 4) / 2, -16, t, 4)
pop()
end</code></pre>
<h3>Cost</h3>
<p>Each glyph is emitted as one rectangle per horizontal run of lit pixels rather than one per pixel — a typical glyph row is two or three runs, so a character costs roughly 12–18 vertices instead of up to 384. Text-heavy scenes such as <code>matrix.lua</code> and <code>datafield.lua</code> are perfectly happy putting hundreds of characters on screen per frame, but a full screen of tiny text is still real geometry, not a texture blit.</p>
<p>The engine uses this same font itself for the “no scene loaded” message and the on-screen error banner.</p>
<p>See <code>scenes/text_test.lua</code> for a scene exercising every one of these features.</p>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="transforms">
<summary>Transform Stack</summary>
<div class="section-body">
<p>The transform stack works like Processing's <code>pushMatrix/popMatrix</code>. Transforms are post-multiplied — they apply in the order you call them.</p>
<table>
<tr><th>Function</th><th>Description</th></tr>
<tr><td><code>push()</code></td><td>Save the current matrix. Pair every <code>push()</code> with a <code>pop()</code>.</td></tr>
<tr><td><code>pop()</code></td><td>Restore the matrix saved by the matching <code>push()</code>.</td></tr>
<tr><td><code>translate(x, y)</code></td><td>Move the origin by <code>(x, y)</code> pixels.</td></tr>
<tr><td><code>rotate(radians)</code></td><td>Rotate counter-clockwise around the current origin.</td></tr>
<tr><td><code>scale(sx, sy)</code></td><td>Scale around the current origin. Pass one value to scale uniformly.</td></tr>
</table>
<pre><code>-- Rotate a rectangle around its own centre
push()
translate(cx, cy) -- move origin to rect centre
rotate(angle) -- rotate around that point
draw_rect(-hw, -hh, hw * 2, hh * 2) -- draw centred at origin
pop() -- restore transform</code></pre>
<div class="warn">
<strong>Transform stack does not affect canvas or image draws</strong>
<code>canvas:draw()</code>, <code>img:draw()</code>, and <code>sheet:draw()</code> use a separate GPU shader
that bypasses the CPU transform matrix entirely.
<code>push/translate/rotate/scale</code> have <strong>no effect</strong> on these calls.
Use the <code>angle</code> parameter on those functions instead — see
<a href="#canvas">Canvas</a> and <a href="#images">Images & Sprites</a>.
</div>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="feedback">
<summary>Feedback</summary>
<div class="section-body">
<p>Feedback blends the previous frame's final composited image over the current frame buffer. Call it <strong>before drawing geometry</strong> in <code>on_frame</code> — it acts as a persistent background that geometry is drawn on top of.</p>
<span class="sig">draw_feedback(alpha [, scale [, angle]])</span>
<table>
<tr><th>Parameter</th><th>Default</th><th>Effect</th></tr>
<tr><td><code>alpha</code></td><td>—</td><td>Blend weight 0–1. Lower = faster fade. Required.</td></tr>
<tr><td><code>scale</code></td><td>1.0</td><td>Scale the previous frame around the screen centre. >1 = infinite zoom outward; <1 = shrinking spiral.</td></tr>
<tr><td><code>angle</code></td><td>0.0</td><td>Rotate the previous frame around the screen centre (radians). Non-zero = rotating trail.</td></tr>
</table>
<h3>Alpha decay reference (at 60 fps)</h3>
<table>
<tr><th>alpha</th><th>Half-life</th><th>Character</th></tr>
<tr><td>0.90</td><td>~0.4 s</td><td>Fast phosphor decay</td></tr>
<tr><td>0.95</td><td>~1.0 s</td><td>Medium trail</td></tr>
<tr><td>0.97</td><td>~1.6 s</td><td>Long smear</td></tr>
<tr><td>0.99</td><td>~5.0 s</td><td>Ghost persistence</td></tr>
</table>
<pre><code>function on_frame(dt)
-- No clear() — feedback IS the background
draw_feedback(0.93, 1.002, 0.002) -- slow zoom + slow rotate = spiral trail
set_color(1, 1, 1, 1)
draw_circle(math.random() * screen_width,
math.random() * screen_height, 8)
end</code></pre>
<div class="tip">
<strong>Tip</strong>
Omitting <code>clear()</code> entirely and relying on feedback gives the classic CRT phosphor
look. Adding a very dark <code>clear(0,0,0,0.03)</code> as an FBO blend is not directly supported —
use <code>alpha < 1.0</code> on <code>draw_feedback</code> instead.
</div>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="shaders">
<summary>Post-Process Shader Pipeline</summary>
<div class="section-body">
<p>Post-process shaders run on the full frame after geometry is drawn. Shaders are chained: the output of each feeds the next. The pipeline is applied once per frame, in <code>end_frame()</code>.</p>
<table>
<tr><th>Function</th><th>Description</th></tr>
<tr><td><code>shader_set(name, ...)</code></td><td>Replace the entire pipeline. Pass multiple names to chain them left-to-right.</td></tr>
<tr><td><code>shader_add(name)</code></td><td>Append one shader to the current pipeline.</td></tr>
<tr><td><code>shader_clear()</code></td><td>Remove all post-process shaders.</td></tr>
<tr><td><code>shader_set_uniform(name, value)</code></td><td>Set a named <code>float</code> uniform on all active shaders.</td></tr>
</table>
<div class="warn">
<strong>Common mistake</strong>
Calling <code>shader_set</code> twice sets only the <em>last</em> pipeline.
To chain two shaders, pass both names to a single call:<br>
<code>shader_set("scanlines", "chromatic_ab")</code>
</div>
<h3>Built-in shaders</h3>
<table>
<tr><th>Name</th><th>Effect</th><th>Uniforms</th></tr>
<tr><td><code>scanlines</code></td><td>Dims alternating pixel rows — CRT scanline look.</td><td>none</td></tr>
<tr><td><code>chromatic_ab</code></td><td>Samples R, G, B channels at slightly offset UVs — colour fringing on bright edges.</td><td><code>u_chrom_amount</code> (default 0.002)</td></tr>
</table>
<pre><code>function on_load()
shader_set("scanlines", "chromatic_ab")
shader_set_uniform("u_chrom_amount", 0.004)
end
-- Change aberration amount live
function on_osc(addr, ...)
local args = {...}
if addr == "/chrom" then
shader_set_uniform("u_chrom_amount", args[1])
end
end</code></pre>
<p>All shaders receive these uniforms automatically:</p>
<table>
<tr><th>Uniform</th><th>Type</th><th>Value</th></tr>
<tr><td><code>u_texture</code></td><td>sampler2D</td><td>Input frame texture</td></tr>
<tr><td><code>u_resolution</code></td><td>vec2</td><td>Drawable size in pixels</td></tr>
<tr><td><code>u_time</code></td><td>float</td><td>Elapsed seconds since startup</td></tr>
<tr><td><code>u_beat_phase</code></td><td><code>float</code></td><td>Continuous position within the current beat, [0,1) — see <a href="#clock">Musical Clock</a>.</td></tr>
<tr><td><code>u_beat</code></td><td>float</td><td>Beat phase 0–1 (reserved for future use)</td></tr>
</table>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="custom-shaders">
<summary>Writing a Custom Shader</summary>
<div class="section-body">
<p>Place fragment shader files in <code>shaders/<name>.frag</code> relative to the working directory. Load with <code>shader_set("name")</code> — no extension, no path.</p>
<h3>Required preamble</h3>
<pre><code>#version 330 core
uniform sampler2D u_texture;
uniform vec2 u_resolution;
uniform float u_time;
uniform float u_beat;
in vec2 v_uv;
out vec4 frag_color;</code></pre>
<h3>Minimal passthrough</h3>
<pre><code>#version 330 core
uniform sampler2D u_texture;
in vec2 v_uv;
out vec4 frag_color;
void main() {
frag_color = texture(u_texture, v_uv);
}</code></pre>
<h3>Example: greyscale with vignette</h3>
<pre><code>#version 330 core
uniform sampler2D u_texture;
uniform vec2 u_resolution;
in vec2 v_uv;
out vec4 frag_color;
void main() {
vec4 c = texture(u_texture, v_uv);
float grey = dot(c.rgb, vec3(0.299, 0.587, 0.114));
// Vignette: darken corners
vec2 uv = v_uv * 2.0 - 1.0;
float vig = 1.0 - dot(uv, uv) * 0.4;
frag_color = vec4(vec3(grey * vig), c.a);
}</code></pre>
<h3>Custom uniforms from Lua</h3>
<table>
<tr><th>Call</th><th>Sets</th></tr>
<tr><td><code>shader_set_uniform(name, x)</code></td><td><code>float</code></td></tr>
<tr><td><code>shader_set_uniform(name, x, y)</code> or <code>(name, vec)</code></td><td><code>vec2</code></td></tr>
<tr><td><code>shader_set_uniform(name, x, y, z [, w])</code></td><td><code>vec3</code> / <code>vec4</code></td></tr>
<tr><td><code>shader_set_data(name, table)</code></td><td><code>sampler2D</code> — see below</td></tr>
</table>
<p><code>canvas:set_uniform</code> and <code>canvas:set_data</code> are the canvas-local equivalents.</p>
<h4>Arrays as data textures</h4>
<p>A uniform carries at most four numbers. A spectrum, an envelope curve or a per-element value is hundreds, and only a texture can be indexed per fragment. <code>shader_set_data</code> uploads a Lua array as a one-row float texture:</p>
<pre><code>-- Lua
local bands = {}
for i = 1, 64 do bands[i] = level_for_band(i) end
shader_set_data("u_bands", bands)</code></pre>
<pre><code>// GLSL
uniform sampler2D u_bands;
float band = texture(u_bands, vec2(v_uv.x, 0.5)).r;</code></pre>
<p>Sampling is nearest-neighbour and clamped at the edges — this is data, not an image, so interpolating between adjacent elements would invent values that were never measured.</p>
<h3>Custom float uniforms from Lua</h3>
<pre><code>-- In Lua:
shader_set("my_shader")
shader_set_uniform("u_my_value", 0.5)
-- In GLSL:
uniform float u_my_value;</code></pre>
<div class="note">
<strong>Note</strong>
Shaders are reloaded from disk each time <code>shader_set()</code> or <code>shader_add()</code> is called.
You can edit a <code>.frag</code> file and call <code>shader_set</code> from the scene to hot-reload it.
</div>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="noise">
<summary>Noise Functions</summary>
<div class="section-body">
<p>Classic Perlin noise (Ken Perlin's 2002 improved permutation table). All functions return values in <strong>[−1, 1]</strong>.</p>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>noise(x)</code></td><td>1-D Perlin noise</td></tr>
<tr><td><code>noise(x, y)</code></td><td>2-D Perlin noise</td></tr>
<tr><td><code>noise(x, y, z)</code></td><td>3-D Perlin noise</td></tr>
<tr><td><code>fbm(x, y [, octaves [, lacunarity [, gain]]])</code></td><td>Fractal Brownian Motion — stacked octaves of noise</td></tr>
</table>
<h3>fbm defaults</h3>
<table>
<tr><th>Parameter</th><th>Default</th><th>Effect</th></tr>
<tr><td><code>octaves</code></td><td>6</td><td>Number of noise layers (1–16)</td></tr>
<tr><td><code>lacunarity</code></td><td>2.0</td><td>Frequency multiplier per octave</td></tr>
<tr><td><code>gain</code></td><td>0.5</td><td>Amplitude multiplier per octave</td></tr>
</table>
<div class="tip">
<strong>Performance note</strong>
Noise is a Lua→C call with moderate overhead. Calling it for every pixel of a full-screen
grid (e.g. step = 4) will drop below 60 fps on large Retina displays.
Use a larger step (12–24 px) for realtime visualisations, or write a custom GLSL shader
for full-resolution noise fields — the GPU runs noise at native speed.
</div>
<pre><code>-- Scrolling 2-D noise field
local scale = 0.006
local t = 0
function on_frame(dt)
t = t + dt * 0.4
clear(0, 0, 0, 1)
local step = 18
for y = 0, screen_height - 1, step do
for x = 0, screen_width - 1, step do
local n = noise(x * scale + t, y * scale) * 0.5 + 0.5
set_color(n, n * 0.8, n * 0.3, 1)
draw_rect(x, y, step, step)
end
end
end</code></pre>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="easing">
<summary>Easing & Math Utilities</summary>
<div class="section-body">
<p>Seven math primitives available as global functions in every scene — no <code>dofile</code> or <code>require</code> needed. They cover the most common animation and control-rate tasks: interpolation, frame-rate-independent smoothing, range mapping, and beat-sync pulses.</p>
<h3>Interpolation</h3>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>lerp(a, b, t)</code></td><td>Linear interpolation. Returns the value <code>t</code> of the way from <code>a</code> to <code>b</code>. <code>t=0</code> → <code>a</code>, <code>t=1</code> → <code>b</code>. Not clamped — pass <code>t</code> outside [0,1] to extrapolate.</td></tr>
<tr><td><code>smoothstep(t)</code></td><td>S-curve: maps <code>t</code> in [0,1] to [0,1] with zero derivative at both ends. Formula: <code>3t²−2t³</code>. Input is clamped automatically. Use this instead of <code>lerp</code> when you want eased start and end rather than a linear ramp.</td></tr>
</table>
<h3>Exponential smoothing</h3>
<p>Both functions move a value toward a target in a frame-rate-independent way — unlike <code>lerp(current, target, k)</code> called each frame, which moves twice as far at 30 fps as at 60 fps.</p>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>smooth(current, target, rate, dt)</code></td><td>Rate-based smoother. Each frame the remaining gap is multiplied by <code>e<sup>−rate·dt</sup></code>. Higher <code>rate</code> = snappier response. Rule of thumb: rate 1 ≈ 63% of gap closed per second; rate 5 ≈ 99%; rate 10 ≈ nearly instant.</td></tr>
<tr><td><code>smooth_hl(current, target, half_life, dt)</code></td><td>Half-life smoother. The gap halves every <code>half_life</code> seconds — more intuitive when thinking in musical time. <code>half_life = 0.1</code> is snappy; <code>2.0</code> is a lazy drift. Returns <code>target</code> immediately if <code>half_life ≤ 0</code>.</td></tr>
</table>
<h3>Range & clamping</h3>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>map(x, in_lo, in_hi, out_lo, out_hi)</code></td><td>Remaps <code>x</code> from [<code>in_lo</code>, <code>in_hi</code>] to [<code>out_lo</code>, <code>out_hi</code>]. Not clamped — use <code>clamp()</code> afterwards if you need hard limits. Typical use: converting OSC 0..1 values to pixel coordinates.</td></tr>
<tr><td><code>clamp(x, lo, hi)</code></td><td>Constrains <code>x</code> to [<code>lo</code>, <code>hi</code>]. Equivalent to <code>math.max(lo, math.min(hi, x))</code> but reads as intent rather than idiom.</td></tr>
</table>
<h3>Beat pulse</h3>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>pulse(t, bpm, width)</code></td><td>Returns a 0..1 value that peaks at 1 on each beat and fades smoothly to 0 within <code>width</code> seconds of the beat. Good for flash and strobe effects driven by a time accumulator. <code>width</code> is the half-window in seconds — try 0.05–0.1 for a tight flash.</td></tr>
</table>
<h3>Choosing smooth() vs smooth_hl()</h3>
<table>
<tr><th></th><th><code>smooth()</code></th><th><code>smooth_hl()</code></th></tr>
<tr><td>Parameter</td><td>rate (nepers/sec)</td><td>half_life (seconds)</td></tr>
<tr><td>Snappy</td><td>rate 10–20</td><td>half_life 0.05–0.1</td></tr>
<tr><td>Medium</td><td>rate 4–7</td><td>half_life 0.2–0.4</td></tr>
<tr><td>Lazy</td><td>rate 1–2</td><td>half_life 1.0–2.0</td></tr>
<tr><td>Best for</td><td>Control-rate intuition</td><td>Musical / tempo-relative intuition</td></tr>
</table>
<pre><code>local t = 0
local pos = 0 -- current smoothed value
local bpm = 120
function on_frame(dt)
t = t + dt
-- map(): OSC 0..1 → pixel x; clamp() guards against out-of-range input
local target = clamp(map(some_osc_value, 0, 1, 0, screen_width), 0, screen_width)
-- smooth(): snappy follow at rate 8
pos = smooth(pos, target, 8, dt)
-- smoothstep(): use distance to target as an eased size modulator
local dist_frac = clamp(map(math.abs(pos - target), 0, 400, 0, 1), 0, 1)
local radius = lerp(10, 50, smoothstep(dist_frac))
-- pulse(): beat-sync flash — 120bpm, 70ms window
local flash = pulse(t, bpm, 0.07)
clear(flash * 0.1, flash * 0.07, flash * 0.03, 1)
set_color(0.3, 1.0, 0.5, 1)
draw_circle(pos, screen_height / 2, radius)
end</code></pre>
<div class="tip">
<strong>See also</strong>
<code>scenes/easing_demo.lua</code> — a live showcase scene that puts all seven functions on screen simultaneously: four dots chasing a drifting target at different smoothing rates, with a beat-sync background pulse and OSC control.
</div>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="slew">
<summary>Slewed Values</summary>
<div class="section-body">
<p><code>smooth_hl()</code> does the maths, but it leaves the scene holding two jobs it would rather not have: finding somewhere to park the value between frames, and remembering to advance it every frame with <code>dt</code> in hand. <code>slew()</code> takes both. Set a target from anywhere; read back a value that is already on its way there.</p>
<pre><code>-- Without slew: two variables and a call you must not forget
function on_load()
persist.hue = 0.55
persist.hue_target = 0.55
end
function on_frame(dt)
persist.hue = smooth_hl(persist.hue, persist.hue_target, 0.15, dt)
set_color_hsv(persist.hue, 0.8, 1.0)
end
-- With slew: one value, no dt, no bookkeeping
function on_frame(dt)
local hue = slew("hue", 0.55, 0.15)
set_color_hsv(hue.get(), 0.8, 1.0)
end</code></pre>
<h3>The handle</h3>
<p>Like <code>param()</code>, <code>slew()</code> <strong>declares and reads in one call and is safe to call every frame</strong> — a repeat call with the same name hands back the same handle onto the same stored state, so it belongs right where the value is used.</p>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>slew(name, default [, slew_time])</code></td><td>Declare and fetch the handle. <code>slew_time</code> is in seconds and defaults to <code>0.25</code>.</td></tr>
<tr><td><code>handle.set(v)</code></td><td>Retarget. The glide starts from wherever the value currently is, not from the default.</td></tr>
<tr><td><code>handle.get()</code></td><td>The current, smoothed value. <code>handle()</code> reads it too.</td></tr>
</table>
<div class="note">
<strong>The handle is not the number</strong>
<code>hue</code> is a table, so <code>draw_circle(x, y, hue)</code> will fail —
read it with <code>hue.get()</code> or <code>hue()</code>. Lua has no
<code>__tonumber</code> metamethod, so there is no way to make a bare handle
stand in for a number everywhere. Arithmetic metamethods were deliberately
left out rather than making <code>hue * 2</code> work while
<code>draw_circle(x, y, hue)</code> still broke.
</div>
<h3>The curve</h3>
<p>Exponential, the same shape as <code>smooth_hl()</code> and <code>env()</code>: fastest when furthest away, easing in as it arrives. Retargeting mid-glide bends the curve rather than kinking it, so a stream of new targets produces one continuous motion.</p>
<p><code>slew_time</code> is <strong>the time to cover 99% of the remaining distance</strong>. It is a ceiling on how long a jump takes, not a fixed duration: the value is already 90% of the way there at 0.5× the slew time. A <code>slew_time</code> of <code>0</code> snaps instantly, matching what <code>smooth_hl()</code> does with a zero half-life.</p>
<table>
<tr><th>Feel</th><th><code>slew_time</code></th><th>Equivalent <code>smooth_hl</code> half-life</th></tr>
<tr><td>Snappy</td><td>0.3 – 0.6</td><td>0.05 – 0.1</td></tr>
<tr><td>Medium</td><td>1.3 – 2.6</td><td>0.2 – 0.4</td></tr>
<tr><td>Lazy</td><td>6.6 – 13</td><td>1.0 – 2.0</td></tr>
</table>
<p>Slews advance on the engine’s scaled clock — the same <code>dt</code> that <code>on_frame</code> receives — so <code>Space</code> freezes a glide where it stands and <code>[</code> / <code>]</code> stretch it, exactly like everything else on screen. This is deliberately unlike the <a href="#clock">musical clock</a>, which runs on the wall clock: a slew is a visual gesture, so it obeys visual transport.</p>
<h3>Which wins after a reload</h3>
<p>Slew values live outside the Lua VM, so they survive a reload. The rule is that <strong>the file wins until <code>set()</code></strong>: a slew follows the default written in your source — so editing that default and saving does what you expect while iterating — until the scene calls <code>set()</code> on it, after which the live value sticks and saving mid-glide will not snap it back. Press <code>0</code> to clear every live value and fall back to the file defaults.</p>
<p>The <code>slew_time</code> is a <em>declaration</em> rather than a value, so it always follows the file. Retune the glide, save, and the new timing applies at once — even to a slew that is already live.</p>
<h3>OSC</h3>
<p><code>slew</code> claims no OSC address of its own, which keeps it from ever quietly colliding with a <a href="#osc"><code>param</code></a> of the same name. Wire it up explicitly when you want incoming messages smoothed:</p>
<pre><code>local hue = slew("hue", 0.55, 0.3)
on("/hue", function(v) hue.set(v) end) -- steppy OSC in, smooth value out</code></pre>
<div class="tip">
<strong>See also</strong>
<code>scenes/slew_test.lua</code> — scrolling traces of the raw square-wave target, the slew, and the same curve built by hand with <code>smooth_hl()</code>. The last two sit exactly on top of each other.
</div>
</div>
</details>
<!-- ═══════════════════════════════════════════════════════════════════════════ -->
<details open id="vectors">
<summary>Vectors</summary>
<div class="section-body">
<p>A 2D vector type in the spirit of Processing’s <code>PVector</code>, implemented in C as userdata. Positions, velocities, forces and directions are the natural unit of animation, and writing them as pairs of loose <code>x</code>/<code>y</code> numbers is where most scene code goes to die.</p>
<h3>Construction</h3>
<table>
<tr><th>Signature</th><th>Description</th></tr>
<tr><td><code>vec(x, y)</code></td><td>A vector. Both components default to 0, so <code>vec()</code> is the origin.</td></tr>
<tr><td><code>vec.new(x, y)</code></td><td>Identical — <code>vec(...)</code> is shorthand for it.</td></tr>
<tr><td><code>vec.from_angle(a [, len])</code></td><td>Unit vector at angle <code>a</code> radians, scaled to <code>len</code> (default 1).</td></tr>
<tr><td><code>vec.random([len])</code></td><td>Uniformly distributed random direction, length <code>len</code> (default 1). Uses <code>math.random</code>, so <code>math.randomseed</code> makes it reproducible.</td></tr>
</table>
<p><code>v.x</code> and <code>v.y</code> are readable and writable. Assigning any other field is an error rather than silently doing nothing, which catches typos immediately.</p>
<h3>Operators</h3>
<table>
<tr><th>Expression</th><th>Result</th></tr>
<tr><td><code>a + b</code>, <code>a - b</code>, <code>-a</code></td><td>Component-wise, new vector.</td></tr>
<tr><td><code>v * 2</code>, <code>2 * v</code>, <code>v / 2</code></td><td>Scalar multiply and divide.</td></tr>
<tr><td><code>a * b</code>, <code>a / b</code></td><td><strong>Component-wise</strong>, not the dot product. Useful for non-uniform scaling: <code>p * vec(screen_width, screen_height)</code> maps a 0..1 position onto the screen. For the dot product use <code>a:dot(b)</code>.</td></tr>
<tr><td><code>a == b</code></td><td>Exact component equality. Two vectors reached by different routes can differ in the last bit; compare <code>a:dist(b)</code> against a tolerance when that matters.</td></tr>
</table>
<h3>Queries — return numbers</h3>
<table>
<tr><th>Method</th><th>Returns</th></tr>
<tr><td><code>v:mag()</code> / <code>v:mag_sq()</code></td><td>Length, and length squared. Prefer the squared form when only comparing distances — same ordering, no square root.</td></tr>
<tr><td><code>v:dist(u)</code> / <code>v:dist_sq(u)</code></td><td>Distance to another point.</td></tr>
<tr><td><code>v:dot(u)</code></td><td>Dot product. Zero when perpendicular.</td></tr>
<tr><td><code>v:cross(u)</code></td><td>2D cross product (a scalar). Its <em>sign</em> tells you which side <code>u</code> lies on — the basis of steering left or right.</td></tr>
<tr><td><code>v:heading()</code></td><td>Angle in radians, from +X.</td></tr>
<tr><td><code>v:angle_to(u)</code></td><td>Signed angle between the two, in [−π, π].</td></tr>
<tr><td><code>v:xy()</code></td><td>The components as two values.</td></tr>
</table>
<h3>Transformations</h3>
<p>Every operation comes in two forms. Without a trailing underscore it returns a <strong>new</strong> vector and leaves the receiver alone; with one it <strong>modifies</strong> the receiver and returns it, so calls chain.</p>
<table>
<tr><th>Pure</th><th>In place</th><th>Effect</th></tr>
<tr><td><code>v:copy()</code></td><td><code>v:set_(u)</code></td><td>Duplicate / overwrite components.</td></tr>
<tr><td>—</td><td><code>v:add_(u)</code>, <code>v:sub_(u)</code>, <code>v:scale_(s)</code></td><td>Arithmetic without allocating (the operators cover the pure form).</td></tr>
<tr><td><code>v:normalize()</code></td><td><code>v:normalize_()</code></td><td>Scale to length 1. A zero vector is returned unchanged rather than becoming NaN.</td></tr>
<tr><td><code>v:limit(m)</code></td><td><code>v:limit_(m)</code></td><td>Cap the length at <code>m</code>, keeping direction. The workhorse of any steering loop.</td></tr>
<tr><td><code>v:rotate(a)</code></td><td><code>v:rotate_(a)</code></td><td>Rotate by <code>a</code> radians.</td></tr>
<tr><td><code>v:lerp(u, t)</code></td><td><code>v:lerp_(u, t)</code></td><td>Move a fraction <code>t</code> of the way toward <code>u</code>.</td></tr>
<tr><td><code>v:smooth(u, rate, dt)</code></td><td><code>v:smooth_(u, rate, dt)</code></td><td>Frame-rate-independent chase — see below.</td></tr>
<tr><td><code>v:smooth_hl(u, hl, dt)</code></td><td><code>v:smooth_hl_(u, hl, dt)</code></td><td>The same, parameterised by half-life in seconds.</td></tr>
</table>
<p>Anywhere a vector argument is accepted, two numbers work as well: <code>p:add_(v)</code> and <code>p:add_(0, 9.8)</code> are both valid, which saves building a temporary just to add a constant.</p>
<h3>smooth() is the one to remember</h3>
<p>Almost everything that follows something else — a camera trailing a subject, a value chasing an OSC parameter, an attractor being pursued — wants this. Chasing a target with <code>v:lerp(target, 0.1)</code> every frame moves twice as far at 30fps as at 60fps; <code>smooth</code> closes a fixed <em>proportion</em> of the gap per second and does not care how the frame rate wanders.</p>
<pre><code>-- rate 1 closes ~63% of the gap per second; 5 is snappy; 10 nearly instant
persist.cursor:smooth_(target_x, target_y, 4, dt)</code></pre>
<h3>Allocation</h3>
<p>Every operator returns a new vector, so <code>a + b</code> allocates. For a few hundred operations a frame that is irrelevant, and the readable form is the right default. A particle system doing several thousand a frame generates enough garbage that Lua’s collector will eventually show up as a frame-time spike — which is what the in-place methods are for. Write it readably first; convert the hot loop only if you measure a problem.</p>