vizmatic 0.3.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/PROMPT.md +75 -525
  2. package/README.md +87 -578
  3. package/ROADMAP.md +49 -67
  4. package/SCIENCE.md +250 -0
  5. package/assets/THIRD_PARTY_LICENSES.md +40 -1
  6. package/assets/fonts/InstrumentSerif-Italic.ttf +0 -0
  7. package/assets/fonts/InstrumentSerif-Regular.ttf +0 -0
  8. package/assets/licenses/raster-codecs-MIT.txt +74 -0
  9. package/dist/chunk-3F2VQ7NW.js +1 -0
  10. package/dist/chunk-4XE64I2U.js +755 -0
  11. package/dist/chunk-JHHPLALU.js +79 -0
  12. package/dist/chunk-LTX44BAE.js +112 -0
  13. package/dist/chunk-MLKGABMK.js +9 -0
  14. package/dist/chunk-PSBF4TEK.js +1 -0
  15. package/dist/chunk-RPAHMKAR.js +605 -0
  16. package/dist/chunk-RWF4VBUL.js +3617 -0
  17. package/dist/chunk-WHT6HRMX.js +4201 -0
  18. package/dist/cli.js +371 -10
  19. package/dist/index.cjs +11129 -2922
  20. package/dist/index.d.cts +1225 -369
  21. package/dist/index.d.ts +1225 -369
  22. package/dist/index.js +6220 -3015
  23. package/dist/math-LXB4FH4E.js +7 -0
  24. package/dist/math-QVCETQDQ.js +1 -0
  25. package/dist/scene-browser-assets.d.ts +38 -0
  26. package/dist/scene-browser-assets.js +53 -0
  27. package/dist/scene-browser-chunks/chunk-5MVLYGXM.js +70 -0
  28. package/dist/scene-browser-chunks/chunk-ASXKN3NU.js +1 -0
  29. package/dist/scene-browser-chunks/chunk-CM5Y5LZP.js +1 -0
  30. package/dist/scene-browser-chunks/chunk-DP7NB4EW.js +6 -0
  31. package/dist/scene-browser-chunks/emscripten-module.browser-M5AEKJGE.js +1 -0
  32. package/dist/scene-browser-chunks/ffi-FL6ORJU3.js +1 -0
  33. package/dist/scene-browser-chunks/module-ES6BEMUI-JD5S5VKR.js +1 -0
  34. package/dist/scene-browser-chunks/module-asyncify-2EFITU5U-HWPFJFJC.js +1 -0
  35. package/dist/scene-browser-chunks/scene-browser-layout-M3XSXMV4.js +27 -0
  36. package/dist/scene-browser.d.ts +117 -0
  37. package/dist/scene-browser.js +220 -0
  38. package/dist/scene-cli.d.ts +1 -0
  39. package/dist/scene-cli.js +4 -0
  40. package/dist/scene-runtime.cjs +995 -0
  41. package/dist/scene-runtime.d.cts +81 -0
  42. package/dist/scene-runtime.d.ts +81 -0
  43. package/dist/scene-runtime.js +22 -0
  44. package/dist/server-2FYHZ4UO.js +331 -0
  45. package/dist/stage-browser/browser.d.ts +2482 -0
  46. package/dist/stage-browser/chunk-SQE76S5B.js +1 -0
  47. package/dist/stage-browser/chunk-VZNL6JY2.js +59 -0
  48. package/dist/stage-browser/dagre.esm-N2NIVIAZ.js +6 -0
  49. package/dist/stage-browser/math-4WFNHVV2.js +18 -0
  50. package/dist/stage-browser/stage.js +19 -0
  51. package/dist/stage-export.cjs +4950 -0
  52. package/dist/stage-export.d.cts +112 -0
  53. package/dist/stage-export.d.ts +112 -0
  54. package/dist/stage-export.js +544 -0
  55. package/dist/stage.cjs +9745 -0
  56. package/dist/stage.d.cts +1678 -0
  57. package/dist/stage.d.ts +1678 -0
  58. package/dist/stage.js +5404 -0
  59. package/dist/svg-import-Djj2q7Pc.d.cts +742 -0
  60. package/dist/svg-import-Djj2q7Pc.d.ts +742 -0
  61. package/dist/video-CcHW1qhV.d.cts +532 -0
  62. package/dist/video-CcHW1qhV.d.ts +532 -0
  63. package/examples/README.md +18 -0
  64. package/examples/agent-pipeline.tsx +4 -4
  65. package/examples/animated-pipeline.tsx +1 -5
  66. package/examples/attention-head.tsx +12 -12
  67. package/examples/aurora-incident.tsx +4 -3
  68. package/examples/building-blocks.tsx +8 -9
  69. package/examples/catalog-charts.tsx +4 -4
  70. package/examples/catalog-content.tsx +7 -9
  71. package/examples/catalog-convolution-kernel.tsx +116 -0
  72. package/examples/catalog-data.tsx +2 -8
  73. package/examples/catalog-dataflow.tsx +5 -9
  74. package/examples/catalog-dense.tsx +80 -0
  75. package/examples/catalog-deployment.tsx +5 -11
  76. package/examples/catalog-diagrams.tsx +13 -16
  77. package/examples/catalog-drawing.tsx +7 -7
  78. package/examples/catalog-flows.tsx +4 -4
  79. package/examples/catalog-foundations.tsx +11 -11
  80. package/examples/catalog-gaussian-blur.tsx +146 -0
  81. package/examples/catalog-image-tensors.tsx +115 -0
  82. package/examples/catalog-ml.tsx +36 -31
  83. package/examples/catalog-numeric-matrices.tsx +93 -0
  84. package/examples/catalog-photo-blur.tsx +209 -0
  85. package/examples/catalog-science-ai.tsx +130 -0
  86. package/examples/catalog-science-chemistry.tsx +103 -0
  87. package/examples/catalog-science-math.tsx +93 -0
  88. package/examples/catalog-science-physics.tsx +84 -0
  89. package/examples/catalog-sequence.tsx +59 -38
  90. package/examples/data-pipeline-lineage-presentation.tsx +5 -0
  91. package/examples/data-pipeline-lineage.tsx +85 -0
  92. package/examples/daybreak-launch.tsx +1 -0
  93. package/examples/desired-quadrant.tsx +7 -8
  94. package/examples/engineering-dashboard-presentation.tsx +13 -0
  95. package/examples/engineering-dashboard.tsx +160 -0
  96. package/examples/engineering-flow.tsx +2 -4
  97. package/examples/eval-dashboard.tsx +6 -6
  98. package/examples/films/attention.ts +278 -0
  99. package/examples/films/cache-miss.ts +431 -0
  100. package/examples/films/catalog.json +132 -0
  101. package/examples/films/central-limit.ts +269 -0
  102. package/examples/films/complex-maps.ts +273 -0
  103. package/examples/films/deck-example.ts +224 -0
  104. package/examples/films/euler-circle.ts +179 -0
  105. package/examples/films/fourier-epicycles.ts +265 -0
  106. package/examples/films/gradient-descent.ts +345 -0
  107. package/examples/films/linear-maps.ts +175 -0
  108. package/examples/films/lorenz.ts +207 -0
  109. package/examples/films/pythagoras.ts +185 -0
  110. package/examples/films/taylor-series.ts +159 -0
  111. package/examples/films/vizmatic-title.ts +117 -0
  112. package/examples/llm-serving-stack-presentation.tsx +5 -0
  113. package/examples/llm-serving-stack.tsx +94 -0
  114. package/examples/model-garden.tsx +1 -0
  115. package/examples/model-router.tsx +6 -6
  116. package/examples/nccl-all-gather.tsx +3 -3
  117. package/examples/nccl-all-reduce.tsx +3 -3
  118. package/examples/nccl-all-to-all.tsx +1 -1
  119. package/examples/nccl-broadcast.tsx +1 -1
  120. package/examples/nccl-reduce-scatter.tsx +5 -5
  121. package/examples/orthogonal-routing.tsx +4 -4
  122. package/examples/pareto-frontier.tsx +9 -10
  123. package/examples/presentation-frame.tsx +6 -6
  124. package/examples/pressure-mechanism.scene.json +14 -0
  125. package/examples/production-system-map-presentation.tsx +5 -0
  126. package/examples/production-system-map.tsx +130 -0
  127. package/examples/rag-graph.tsx +7 -7
  128. package/examples/release-card.tsx +5 -5
  129. package/examples/spec-anatomy.tsx +5 -5
  130. package/examples/system-architecture.tsx +19 -32
  131. package/examples/theme-system.tsx +1 -1
  132. package/examples/token-matrix.tsx +4 -2
  133. package/examples/transparent-badge.tsx +1 -2
  134. package/examples/videos/README.md +62 -0
  135. package/examples/videos/calculus-tangent.tsx +101 -0
  136. package/examples/videos/catalyst-pathway.tsx +56 -0
  137. package/examples/videos/causal-attention.tsx +91 -0
  138. package/examples/videos/convolution-kernel.tsx +34 -0
  139. package/examples/videos/gaussian-blur.tsx +32 -0
  140. package/examples/videos/image-to-matrix.tsx +22 -0
  141. package/examples/videos/matrix-multiplication.tsx +128 -0
  142. package/examples/videos/photo-gaussian-blur.tsx +39 -0
  143. package/examples/videos/request-lifecycle.tsx +99 -0
  144. package/examples/videos/signal-to-story.tsx +126 -0
  145. package/examples/videos/wave-interference.tsx +80 -0
  146. package/examples/vizmatic-bare.d.ts +1 -0
  147. package/examples/vizmatic-hero.tsx +9 -18
  148. package/examples/website-workflow.tsx +7 -7
  149. package/examples/weekend-field-notes.tsx +3 -2
  150. package/package.json +77 -12
@@ -0,0 +1,269 @@
1
+ /**
2
+ * A Galton board with 12 rows and 900 balls. Each bounce is a deterministic
3
+ * pseudorandom coin flip, so the same counts appear on every render. The readouts
4
+ * are measured from the landed balls, and the curve is the Gaussian with
5
+ * mean 12 * 1/2 = 6 and standard deviation sqrt(12 * 1/4) = sqrt(3). The seed is a
6
+ * chosen one: this sample happens to land close to theory (6.00 and 1.73).
7
+ */
8
+ import {
9
+ Node, circlePath, curveThrough, draw, dot, dynamic, fadeIn, fadeOut, film, line, linear, morphMatching, parseColor, ramp, smooth, text, tex, write,
10
+ type Path, type Rgba, type Ring, type Vec,
11
+ } from 'vizmatic/stage'
12
+
13
+ const ROWS = 12
14
+ const BALLS = 900
15
+ const COLUMN = 480
16
+ const CX = -240
17
+ const DX = 66, DY = 38
18
+ const TOP = -478
19
+ const FLOOR = 482
20
+ const BALL = 5.8
21
+ const PER_ROW = 5
22
+ const ROW_HEIGHT = 11
23
+
24
+ const pegX = (row: number, index: number) => CX + (index - row / 2) * DX
25
+ const pegY = (row: number) => TOP + row * DY
26
+ const binX = (bin: number) => CX + (bin - ROWS / 2) * DX
27
+
28
+ function binomial(n: number, k: number): number {
29
+ let out = 1
30
+ for (let i = 1; i <= k; i++) out = (out * (n - k + i)) / i
31
+ return out
32
+ }
33
+
34
+ /** xorshift32: small, deterministic, and good enough for coin flips. */
35
+ function coin(seed: number): () => boolean {
36
+ let state = seed >>> 0 || 1
37
+ return () => {
38
+ state ^= state << 13
39
+ state ^= state >>> 17
40
+ state ^= state << 5
41
+ state >>>= 0
42
+ return (state & 0x8000) !== 0
43
+ }
44
+ }
45
+
46
+ interface Ball { flips: boolean[]; bin: number; start: number; tau: number; drop: number; land: number; slot: number }
47
+
48
+ export default film({
49
+ title: 'The central limit theorem',
50
+ description: 'A Galton board drops 900 balls into a histogram that settles onto the Gaussian curve.',
51
+ fps: 60,
52
+ }, (s) => {
53
+ const flip = coin(837343728)
54
+ const balls: Ball[] = []
55
+ const DEMO_END = 8.6
56
+ let start = 0
57
+ for (let i = 0; i < BALLS; i++) {
58
+ const flips = Array.from({ length: ROWS }, () => flip())
59
+ const bin = flips.filter(Boolean).length
60
+ const demo = i === 0
61
+ const tau = demo ? 0.4 : i === 1 ? 0.2 : 0.082
62
+ const drop = demo ? 0.7 : 0.5
63
+ // Spacing shrinks as the film goes on: first single balls, then a downpour.
64
+ const progress = i / BALLS
65
+ const spacing = i === 0 ? 6.6 : i === 1 ? 1.5 : 0.13 * (1 - smooth(Math.min(1, progress * 6))) + 0.0118
66
+ balls.push({ flips, bin, start, tau, drop, land: start + ROWS * tau + drop, slot: 0 })
67
+ start += spacing
68
+ }
69
+ const order = balls.map((_, i) => i).sort((a, b) => balls[a]!.land - balls[b]!.land)
70
+ const counts = new Array<number>(ROWS + 1).fill(0)
71
+ for (const index of order) {
72
+ const ball = balls[index]!
73
+ ball.slot = counts[ball.bin]!
74
+ counts[ball.bin]! += 1
75
+ }
76
+
77
+ // Running statistics over landed balls, in landing order.
78
+ const landings = order.map((index) => balls[index]!)
79
+ const sum1: number[] = [0], sum2: number[] = [0]
80
+ for (const ball of landings) {
81
+ sum1.push(sum1[sum1.length - 1]! + ball.bin)
82
+ sum2.push(sum2[sum2.length - 1]! + ball.bin * ball.bin)
83
+ }
84
+ const landedAt = (t: number): number => {
85
+ let lo = 0, hi = landings.length
86
+ while (lo < hi) {
87
+ const mid = (lo + hi) >> 1
88
+ if (landings[mid]!.land <= t) lo = mid + 1
89
+ else hi = mid
90
+ }
91
+ return lo
92
+ }
93
+
94
+ const binColors: readonly Rgba[] = ramp(ROWS + 1, 'blue', 'teal', 'yellow', 'teal', 'blue')
95
+ const slotPoint = (bin: number, slot: number): Vec => {
96
+ const row = Math.floor(slot / PER_ROW), col = slot % PER_ROW
97
+ return [binX(bin) + (col - (PER_ROW - 1) / 2) * 12.2 + (row % 2 === 0 ? -1.5 : 1.5), FLOOR - BALL - 1 - row * ROW_HEIGHT]
98
+ }
99
+ const piles: Ring[][] = counts.map((count, bin) => Array.from({ length: count }, (_, slot) => {
100
+ const [x, y] = slotPoint(bin, slot)
101
+ return circlePath(x, y, BALL)[0]!
102
+ }))
103
+ const pileCache = new Map<number, Path>()
104
+ const pile = (bin: number, count: number): Path => {
105
+ const key = bin * 10000 + count
106
+ let path = pileCache.get(key)
107
+ if (!path) {
108
+ path = piles[bin]!.slice(0, count)
109
+ pileCache.set(key, path)
110
+ }
111
+ return path
112
+ }
113
+
114
+ const position = (ball: Ball, t: number): Vec | null => {
115
+ const local = t - ball.start
116
+ if (local < 0) return null
117
+ const end = ROWS * ball.tau
118
+ if (local < end) {
119
+ const row = Math.floor(local / ball.tau)
120
+ const u = local / ball.tau - row
121
+ let rights = 0
122
+ for (let i = 0; i < row; i++) if (ball.flips[i]) rights++
123
+ const sign = ball.flips[row] ? 1 : -1
124
+ // Sit on top of the peg, hop off, and fall to the next row.
125
+ const x = pegX(row, rights) + (sign * DX * 0.5) * smooth(u)
126
+ const y = pegY(row) - 13 + DY * u * u - 15 * Math.sin(Math.PI * u) * (1 - u * 0.5)
127
+ return [x, y]
128
+ }
129
+ const u = Math.min(1, (local - end) / ball.drop)
130
+ const [tx, ty] = slotPoint(ball.bin, ball.slot)
131
+ const rights = ball.bin
132
+ const fromX = pegX(ROWS, rights), fromY = pegY(ROWS) - 13
133
+ return [fromX + (tx - fromX) * u, fromY + (ty - fromY) * u * u]
134
+ }
135
+
136
+ const time = s.value(0)
137
+
138
+ const board = new Node()
139
+ for (let row = 0; row < ROWS; row++) {
140
+ for (let i = 0; i <= row; i++) board.add(dot(4, 'dim').at(pegX(row, i), pegY(row)))
141
+ }
142
+ const bins = new Node()
143
+ for (let bin = 0; bin <= ROWS + 1; bin++) {
144
+ const x = binX(bin) - DX / 2
145
+ bins.add(line([x, pegY(ROWS) + 6], [x, FLOOR], { stroke: 'faint', width: 1.4 }))
146
+ }
147
+ bins.add(line([binX(0) - DX / 2 - 12, FLOOR], [binX(ROWS) + DX / 2 + 12, FLOOR], { stroke: 'dim', width: 2 }))
148
+ const ticks = new Node()
149
+ for (let bin = 0; bin <= ROWS; bin++) ticks.add(tex(String(bin), { size: 21, color: 'dim', display: false }).moveTo([binX(bin), FLOOR + 26]))
150
+
151
+ const action = dynamic(() => {
152
+ const t = time.value
153
+ const out: Node[] = []
154
+ const landed = landedAt(t)
155
+ const heights = new Array<number>(ROWS + 1).fill(0)
156
+ for (let i = 0; i < landed; i++) heights[landings[i]!.bin]! += 1
157
+ heights.forEach((count, bin) => {
158
+ if (count > 0) out.push(new Node(pile(bin, count)).fill(binColors[bin]!))
159
+ })
160
+ for (const ball of balls) {
161
+ if (ball.start > t) break
162
+ if (ball.land <= t) continue
163
+ const point = position(ball, t)
164
+ if (!point) continue
165
+ const color = ball === balls[0] ? parseColor('yellow') : binColors[ball.bin]!
166
+ // A short streak behind each ball carries the speed through still frames.
167
+ const before = position(ball, t - 0.028)
168
+ if (before) out.push(line(before, point, { stroke: color, width: BALL * 1.5, opacity: 0.4 }))
169
+ const mark = new Node(circlePath(point[0], point[1], BALL)).fill(color)
170
+ if (ball === balls[0]) mark.glow(12, 'yellow')
171
+ out.push(mark)
172
+ }
173
+ return out
174
+ })
175
+
176
+ // The theoretical curve, drawn in stack heights: one ball adds this much height.
177
+ const heightPerBall = ROW_HEIGHT / PER_ROW
178
+ const sigma = Math.sqrt(ROWS / 4)
179
+ const gaussianPoints: Vec[] = []
180
+ for (let i = 0; i <= 200; i++) {
181
+ const k = -0.5 + (i * (ROWS + 1)) / 200
182
+ const density = Math.exp(-((k - ROWS / 2) ** 2) / (2 * sigma * sigma)) / (sigma * Math.sqrt(2 * Math.PI))
183
+ gaussianPoints.push([binX(k), FLOOR - BALLS * density * heightPerBall - 2])
184
+ }
185
+ const gaussian = new Node(curveThrough(gaussianPoints)).stroke('ink', 3.6).glow(8, 'ink')
186
+ // Exact binomial probabilities, as a thin mark over each bin.
187
+ const exactMarks = new Node()
188
+ for (let bin = 0; bin <= ROWS; bin++) {
189
+ const y = FLOOR - BALLS * (binomial(ROWS, bin) / 2 ** ROWS) * heightPerBall - 2
190
+ exactMarks.add(line([binX(bin) - DX * 0.4, y], [binX(bin) + DX * 0.4, y], { stroke: 'pink', width: 3 }))
191
+ }
192
+
193
+ // ── Column ──────────────────────────────────────────────────────────────
194
+
195
+ const rule = line([COLUMN - 70, -540], [COLUMN - 70, 540], { stroke: 'grid', width: 1.2 })
196
+ const place = <T extends Node>(node: T, top: number): T => {
197
+ node.alignTo([COLUMN, 0], 'left').alignTo([0, top], 'top')
198
+ return node
199
+ }
200
+ const label = (content: string, top: number) => place(text(content, { size: 19, color: 'dim', uppercase: true, tracking: 0.08, weight: 600 }), top)
201
+ const title = place(text('Twelve coin flips. Nine hundred times.', { font: 'serif', size: 58, maxWidth: 410, leading: 1.08 }), -490)
202
+
203
+ const flipLabel = label('First ball, bounce by bounce', -340)
204
+ const flipRead = dynamic(() => {
205
+ const t = time.value
206
+ const ball = balls[0]!
207
+ const done = Math.max(0, Math.min(ROWS, Math.floor((t - ball.start) / ball.tau)))
208
+ const letters = ball.flips.slice(0, done).map((right) => right ? 'R' : 'L').join(' ')
209
+ return place(text(letters.length ? letters : '...', { font: 'mono', size: 25, color: 'yellow' }), -306)
210
+ })
211
+ const rightRead = dynamic(() => {
212
+ const t = time.value
213
+ const ball = balls[0]!
214
+ const done = Math.max(0, Math.min(ROWS, Math.floor((t - ball.start) / ball.tau)))
215
+ const rights = ball.flips.slice(0, done).filter(Boolean).length
216
+ return place(text(`${rights} right, so far`, { size: 24, color: 'dim' }), -264)
217
+ })
218
+
219
+ const countLabel = label('Balls landed', -196)
220
+ const countRead = dynamic(() => place(text(String(landedAt(time.value)).padStart(3, ' '), { font: 'mono', size: 64, color: 'ink' }), -170))
221
+ const statLabelA = label('Mean', -66)
222
+ const statReadA = dynamic(() => {
223
+ const n = landedAt(time.value)
224
+ return place(text(n ? (sum1[n]! / n).toFixed(2) : '-', { font: 'mono', size: 38, color: 'ink' }), -40)
225
+ })
226
+ const statLabelB = label('Std deviation', -66)
227
+ statLabelB.alignTo([COLUMN + 200, 0], 'left')
228
+ const statReadB = dynamic(() => {
229
+ const n = landedAt(time.value)
230
+ const variance = n ? sum2[n]! / n - (sum1[n]! / n) ** 2 : 0
231
+ const node = place(text(n > 1 ? Math.sqrt(variance).toFixed(2) : '-', { font: 'mono', size: 38, color: 'ink' }), -40)
232
+ node.alignTo([COLUMN + 200, 0], 'left')
233
+ return node
234
+ })
235
+ const expected = place(tex('\\mu = 6, \\quad \\sigma = \\sqrt{3} \\approx 1.73', { size: 30, color: 'dim' }), 28)
236
+
237
+ const eqExact = place(tex('P(k) = \\binom{12}{k} \\frac{1}{2^{12}}', { size: 40, color: 'ink' }), 108)
238
+ for (const glyph of eqExact.part('P(k)')) glyph.fill('pink')
239
+ const eqGauss = place(tex('P(k) \\approx \\frac{1}{\\sigma \\sqrt{2\\pi}} e^{-\\frac{(k-\\mu)^2}{2\\sigma^2}}', { size: 40, color: 'ink' }), 108)
240
+ for (const glyph of eqGauss.part('P(k)')) glyph.fill('ink')
241
+ const caption = place(text('Add up enough small independent effects, and this shape appears. No shape was put in.', { size: 24, color: 'dim', maxWidth: 400, leading: 1.42 }), 232)
242
+
243
+ // ── Timeline ────────────────────────────────────────────────────────────
244
+
245
+ const total = balls[BALLS - 1]!.land
246
+ s.add(rule)
247
+ s.chapter('The board')
248
+ s.play(write(title), fadeIn(board, { lag: 0.008 }), { duration: 2.2 })
249
+ s.play(draw(bins, { lag: 0.02 }), fadeIn(ticks, { lag: 0.04 }), { duration: 1.2 })
250
+ s.add(action)
251
+ s.play(fadeIn([flipLabel, flipRead, rightRead]), { duration: 0.6 })
252
+
253
+ s.chapter('One ball')
254
+ s.play(time.to(DEMO_END - 1.2, { ease: linear }), { duration: DEMO_END - 1.2 })
255
+ s.play(fadeIn([countLabel, countRead, statLabelA, statReadA, statLabelB, statReadB]), time.to(DEMO_END, { ease: linear }), { duration: 1.2 })
256
+
257
+ s.chapter('A downpour')
258
+ s.play(time.to(total + 0.2, { ease: linear }), { duration: total + 0.2 - DEMO_END })
259
+ s.wait(0.5)
260
+
261
+ s.chapter('The exact answer')
262
+ s.play(fadeOut([flipLabel, flipRead, rightRead], { duration: 0.6 }), fadeIn(expected), write(eqExact), fadeIn(exactMarks, { lag: 0.06 }), { duration: 1.6 })
263
+ s.wait(1)
264
+
265
+ s.chapter('The bell')
266
+ s.play(draw(gaussian), morphMatching(eqExact, eqGauss), { duration: 2.4 })
267
+ s.play(fadeIn(caption, { shift: [0, 8] }), { duration: 1 })
268
+ s.wait(3.4)
269
+ })
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Complex functions bend a grid and keep its right angles. Both acts move through
3
+ * a family of maps that are conformal at every instant: z^p as p runs from 1 to
4
+ * 2, then Mobius maps from the identity to (z - i) / (z + i). The angle readout
5
+ * is measured from the drawn geometry, not assumed.
6
+ */
7
+ import {
8
+ Node, dot, dynamic, fade, fadeIn, film, flash, line, morphMatching, polyPath, rect, shape, smooth, smoother, smoothPath, text, tex, write,
9
+ type Vec,
10
+ } from 'vizmatic/stage'
11
+
12
+ type C = readonly [number, number]
13
+ const ORIGIN_X = -250
14
+ const COLUMN = 480
15
+ const SAMPLES = 72
16
+
17
+ const mul = (a: C, b: C): C => [a[0] * b[0] - a[1] * b[1], a[0] * b[1] + a[1] * b[0]]
18
+ const div = (a: C, b: C): C => {
19
+ const d = b[0] * b[0] + b[1] * b[1]
20
+ return [(a[0] * b[0] + a[1] * b[1]) / d, (a[1] * b[0] - a[0] * b[1]) / d]
21
+ }
22
+ const add = (a: C, b: C): C => [a[0] + b[0], a[1] + b[1]]
23
+ const abs = (a: C) => Math.hypot(a[0], a[1])
24
+
25
+ function power(z: C, p: number): C {
26
+ const r = abs(z)
27
+ if (r === 0) return [0, 0]
28
+ const angle = Math.atan2(z[1], z[0]) * p, size = Math.pow(r, p)
29
+ return [size * Math.cos(angle), size * Math.sin(angle)]
30
+ }
31
+
32
+ /** Mobius family from the identity (k = 0) to the Cayley map (k = 1). The pole stays below the real axis. */
33
+ function mobius(z: C, k: number): C {
34
+ const b: C = [0, -k], c: C = [k, 0], d: C = [1 - k, k]
35
+ return div(add(z, b), add(mul(c, z), d))
36
+ }
37
+
38
+ interface Strand { points: C[]; color: 'blue' | 'pink' | 'ink'; major: boolean }
39
+
40
+ function rightHalfGrid(): Strand[] {
41
+ const out: Strand[] = []
42
+ for (let i = 1; i <= 22; i++) {
43
+ const x = i * 0.25
44
+ const points: C[] = []
45
+ for (let j = 0; j <= SAMPLES; j++) points.push([x, -5 + (10 * j) / SAMPLES])
46
+ out.push({ points, color: 'blue', major: i % 4 === 0 })
47
+ }
48
+ for (let i = -20; i <= 20; i++) {
49
+ if (i === 0) continue
50
+ const y = i * 0.25
51
+ const points: C[] = []
52
+ for (let j = 0; j <= SAMPLES; j++) points.push([(5.5 * j) / SAMPLES, y])
53
+ out.push({ points, color: 'pink', major: i % 4 === 0 })
54
+ }
55
+ const axisReal: C[] = [], axisImag: C[] = []
56
+ for (let j = 0; j <= SAMPLES; j++) {
57
+ axisReal.push([(5.5 * j) / SAMPLES, 0])
58
+ axisImag.push([0, -5 + (10 * j) / SAMPLES])
59
+ }
60
+ out.push({ points: axisReal, color: 'ink', major: true }, { points: axisImag, color: 'ink', major: true })
61
+ return out
62
+ }
63
+
64
+ function upperHalfGrid(): Strand[] {
65
+ const out: Strand[] = []
66
+ const reach = Math.atan(600)
67
+ for (let i = -14; i <= 14; i++) {
68
+ const x = i * 0.5
69
+ const points: C[] = []
70
+ for (let j = 0; j <= SAMPLES * 2; j++) points.push([x, Math.tan((reach * j) / (SAMPLES * 2))])
71
+ out.push({ points, color: 'blue', major: i % 2 === 0 })
72
+ }
73
+ for (const y of [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 6, 9]) {
74
+ const points: C[] = []
75
+ for (let j = -SAMPLES; j <= SAMPLES; j++) points.push([y * 0 + Math.tan(reach * (j / SAMPLES)), y])
76
+ out.push({ points, color: 'pink', major: y === 1 || y === 2 || y === 4 })
77
+ }
78
+ const boundary: C[] = []
79
+ for (let j = -SAMPLES * 2; j <= SAMPLES * 2; j++) boundary.push([Math.tan(reach * (j / (SAMPLES * 2))), 0])
80
+ out.push({ points: boundary, color: 'ink', major: true })
81
+ return out
82
+ }
83
+
84
+ export default film({
85
+ title: 'Complex maps keep right angles',
86
+ description: 'A grid unfolds under z squared and curls into a disk under a Mobius map, with every crossing staying square.',
87
+ fps: 60,
88
+ bleed: true,
89
+ }, (s) => {
90
+ const firstGrid = rightHalfGrid()
91
+ const secondGrid = upperHalfGrid()
92
+ const p = s.value(1)
93
+ const k = s.value(0)
94
+ const reveal = s.value(0)
95
+ const reveal2 = s.value(0)
96
+ const zoom = s.value(1)
97
+ const firstAlpha = s.value(1)
98
+ const secondAlpha = s.value(0)
99
+ const focusX = s.value(0), focusY = s.value(0)
100
+
101
+ const UNIT = 150
102
+ const toStageA = (w: C): Vec => [ORIGIN_X + w[0] * UNIT, -w[1] * UNIT]
103
+ const toStageB = (w: C): Vec => {
104
+ const t = k.value
105
+ const unit = 92 + (410 - 92) * t
106
+ const oy = 410 * (1 - t)
107
+ return [ORIGIN_X + w[0] * unit, oy - w[1] * unit]
108
+ }
109
+
110
+ const Z0: C = [1.25, 0.75]
111
+ const Z1: C = [1, 1]
112
+
113
+ const strokes = (grid: Strand[], map: (z: C) => Vec, amount: number, alpha: number): Node[] => {
114
+ const z = zoom.value
115
+ const scale = 1 / Math.pow(z, 0.75)
116
+ return grid.map((strand) => {
117
+ const count = Math.max(2, Math.ceil(strand.points.length * amount))
118
+ const pts: Vec[] = []
119
+ for (let i = 0; i < count; i++) pts.push(map(strand.points[i]!))
120
+ const width = (strand.color === 'ink' ? 2.6 : strand.major ? 2 : 1.1) * scale
121
+ return new Node(smoothPath(pts)).stroke(strand.color, width, (strand.major ? 0.95 : 0.6) * alpha)
122
+ })
123
+ }
124
+
125
+ const firstField = dynamic(() => {
126
+ if (firstAlpha.value <= 0) return null
127
+ return strokes(firstGrid, (z) => toStageA(power(z, p.value)), reveal.value, firstAlpha.value)
128
+ })
129
+ const secondField = dynamic(() => {
130
+ if (secondAlpha.value <= 0) return null
131
+ return strokes(secondGrid, (z) => toStageB(mobius(z, k.value)), reveal2.value, secondAlpha.value)
132
+ })
133
+
134
+ /** A right-angle marker built from the images of two short steps along the grid directions. */
135
+ const marker = (map: (z: C) => Vec, at: C, color: string, label: string): Node[] => {
136
+ const z = zoom.value
137
+ const scale = 1 / Math.pow(z, 0.85)
138
+ const h = 1e-4
139
+ const o = map(at), a = map(add(at, [h, 0])), b = map(add(at, [0, h]))
140
+ const length = (d: Vec) => Math.hypot(d[0], d[1]) || 1
141
+ const u: Vec = [(a[0] - o[0]) / length([a[0] - o[0], a[1] - o[1]]), (a[1] - o[1]) / length([a[0] - o[0], a[1] - o[1]])]
142
+ const v: Vec = [(b[0] - o[0]) / length([b[0] - o[0], b[1] - o[1]]), (b[1] - o[1]) / length([b[0] - o[0], b[1] - o[1]])]
143
+ const size = 38 * scale
144
+ const corner = polyPath([
145
+ [o[0], o[1]], [o[0] + u[0] * size, o[1] + u[1] * size],
146
+ [o[0] + (u[0] + v[0]) * size, o[1] + (u[1] + v[1]) * size], [o[0] + v[0] * size, o[1] + v[1] * size],
147
+ ], true)
148
+ const tag = tex(label, { size: 30 * scale, color, display: false })
149
+ tag.moveTo([o[0] - (u[0] + v[0]) * 30 * scale, o[1] - (u[1] + v[1]) * 30 * scale])
150
+ const b2 = tag.bounds()
151
+ return [
152
+ shape(corner, { fill: color, stroke: color, width: 2.6 * scale }).set('fillOpacity', 0.28),
153
+ dot(6.5 * scale, color).at(o),
154
+ rect(b2.width + 10 * scale, b2.height + 8 * scale, { fill: 'bg', stroke: null, radius: 5 * scale })
155
+ .at(b2.x + b2.width / 2, b2.y + b2.height / 2).opacity(0.94),
156
+ tag,
157
+ ]
158
+ }
159
+
160
+ const markerA = dynamic(() => firstAlpha.value <= 0 ? null : marker((z) => toStageA(power(z, p.value)), Z0, 'yellow', 'z_0'))
161
+ const markerB = dynamic(() => secondAlpha.value <= 0 ? null : marker((z) => toStageB(mobius(z, k.value)), Z1, 'yellow', 'z_0'))
162
+
163
+ // Four landmarks of the second map.
164
+ const landmarks: readonly [C, string][] = [[[0, 1], 'i'], [[0, 0], '0'], [[1, 0], '1'], [[-1, 0], '-1']]
165
+ const pointsB = dynamic(() => landmarks.flatMap(([z, label]) => {
166
+ const at = toStageB(mobius(z, k.value))
167
+ const tag = tex(label, { size: 30, color: 'ink', display: false })
168
+ tag.moveTo([at[0] + 26, at[1] - 22])
169
+ const b = tag.bounds()
170
+ return [
171
+ dot(7, 'teal').at(at),
172
+ rect(b.width + 10, b.height + 8, { fill: 'bg', stroke: null, radius: 5 }).at(b.x + b.width / 2, b.y + b.height / 2).opacity(0.94),
173
+ tag,
174
+ ]
175
+ }))
176
+
177
+ const infinityMark = tex('\\infty', { size: 38, color: 'teal', display: false }).moveTo([ORIGIN_X + 410 + 34, -32])
178
+
179
+ // ── Column ──────────────────────────────────────────────────────────────
180
+
181
+ const backing = rect(960 - COLUMN + 70, 1080, { fill: 'bg', stroke: null }).at((COLUMN - 70 + 960) / 2, 0).pin().zIndex(5)
182
+ const rule = line([COLUMN - 70, -540], [COLUMN - 70, 540], { stroke: 'grid', width: 1.2 }).pin().zIndex(5)
183
+ const place = <T extends Node>(node: T, top: number): T => {
184
+ node.alignTo([COLUMN, 0], 'left').alignTo([0, top], 'top')
185
+ node.pin().zIndex(6)
186
+ return node
187
+ }
188
+ const label = (content: string, top: number) => place(text(content, { size: 19, color: 'dim', uppercase: true, tracking: 0.08, weight: 600 }), top)
189
+
190
+ const title = place(text('The grid bends. Its right angles survive.', { font: 'serif', size: 58, maxWidth: 410, leading: 1.08 }), -490)
191
+ const eqIdentity = place(tex('w = z', { size: 54 }), -296)
192
+ const eqPower = place(tex('w = z^{p}', { size: 54 }), -296)
193
+ const eqSquare = place(tex('w = z^{2}', { size: 54, color: 'ink' }), -296)
194
+ const eqMobius = place(tex('w = \\frac{z - i}{z + i}', { size: 46 }), -356)
195
+
196
+ const pLabel = label('Exponent', -196)
197
+ const pRead = dynamic(() => place(text(`p = ${p.value.toFixed(2)}`, { font: 'mono', size: 44, color: 'ink' }), -168)).pin().zIndex(6)
198
+ const kLabel = label('Along the Mobius family', -196)
199
+ const kRead = dynamic(() => place(text(`k = ${k.value.toFixed(2)}`, { font: 'mono', size: 44, color: 'ink' }), -168)).pin().zIndex(6)
200
+
201
+ const derivative = (): { size: number; angle: number; crossing: number } => {
202
+ const act = secondAlpha.value > 0.5
203
+ const at = act ? Z1 : Z0
204
+ const map = (z: C): C => act ? mobius(z, k.value) : power(z, p.value)
205
+ const h = 1e-5
206
+ const o = map(at), a = map(add(at, [h, 0])), b = map(add(at, [0, h]))
207
+ const dx: C = [(a[0] - o[0]) / h, (a[1] - o[1]) / h], dy: C = [(b[0] - o[0]) / h, (b[1] - o[1]) / h]
208
+ const crossing = Math.abs(Math.atan2(dx[0] * dy[1] - dx[1] * dy[0], dx[0] * dy[0] + dx[1] * dy[1])) * 180 / Math.PI
209
+ return { size: abs(dx), angle: Math.atan2(dx[1], dx[0]) * 180 / Math.PI, crossing }
210
+ }
211
+ const angleLabel = label('Angle between grid lines at z0', -92)
212
+ const angleRead = dynamic(() => place(text(`${derivative().crossing.toFixed(2)} deg`, { font: 'mono', size: 44, color: 'yellow' }), -64)).pin().zIndex(6)
213
+ const scaleLabel = label('Local stretch', 20)
214
+ const scaleRead = dynamic(() => place(text(derivative().size.toFixed(2), { font: 'mono', size: 36, color: 'ink' }), 48)).pin().zIndex(6)
215
+ const turnLabel = label('Local turn', 20)
216
+ turnLabel.alignTo([COLUMN + 200, 0], 'left')
217
+ const turnRead = dynamic(() => {
218
+ const node = place(text(`${derivative().angle.toFixed(1)} deg`, { font: 'mono', size: 36, color: 'ink' }), 48)
219
+ node.alignTo([COLUMN + 200, 0], 'left')
220
+ return node
221
+ }).pin().zIndex(6)
222
+ const note = place(text('Near any point, a complex derivative is one rotation and one scale. A small square can only come out as another square.', {
223
+ size: 24, color: 'dim', maxWidth: 400, leading: 1.42,
224
+ }), 130)
225
+ const deriv = place(tex("f(z_0 + h) \\approx f(z_0) + f'(z_0)\\, h", { size: 30 }), 316)
226
+ const note2 = place(text('A map of this kind sends lines to circles. The whole upper half-plane lands inside the unit disk.', {
227
+ size: 24, color: 'dim', maxWidth: 400, leading: 1.42,
228
+ }), 130)
229
+ const landing = place(tex('i \\mapsto 0, \\quad 0 \\mapsto -1', { size: 32, color: 'teal' }), 300)
230
+ const landing2 = place(tex('1 \\mapsto -i, \\quad -1 \\mapsto i', { size: 32, color: 'teal' }), 354)
231
+
232
+ // ── Timeline ────────────────────────────────────────────────────────────
233
+
234
+ s.add(firstField, markerA)
235
+ markerA.opacity(0)
236
+ s.add(backing, rule)
237
+ s.chapter('The grid')
238
+ s.play(reveal.to(1, { ease: smooth }), write(title), { duration: 2.4 })
239
+ s.play(write(eqIdentity), fade(markerA, 1), fadeIn([angleLabel, angleRead, scaleLabel, scaleRead, turnLabel, turnRead]), { duration: 1.2 })
240
+ s.play(fadeIn(note, { shift: [0, 8] }), write(deriv), { duration: 1.2 })
241
+ s.wait(0.8)
242
+
243
+ s.chapter('Square it')
244
+ s.play(morphMatching(eqIdentity, eqPower), fadeIn([pLabel, pRead]), { duration: 1 })
245
+ s.play(p.to(2, { ease: smoother }), { duration: 7 })
246
+ s.play(morphMatching(eqPower, eqSquare), { duration: 0.9 })
247
+ s.wait(0.6)
248
+
249
+ s.chapter('Look closer')
250
+ const w0 = power(Z0, 2)
251
+ const target = toStageA(w0)
252
+ s.update(s.camera, (camera) => {
253
+ camera.at(focusX.value, focusY.value)
254
+ camera.set('sx', zoom.value)
255
+ })
256
+ const offset = 240
257
+ s.play(zoom.to(3.4, { ease: smoother }), focusX.to(target[0] + offset / 3.4, { ease: smoother }), focusY.to(target[1], { ease: smoother }), fade(title, 0.25), { duration: 3 })
258
+ s.play(flash([-240, 0], { color: 'yellow', radius: 74 }), { duration: 0.8 })
259
+ s.wait(2)
260
+ s.play(zoom.to(1, { ease: smoother }), focusX.to(0, { ease: smoother }), focusY.to(0, { ease: smoother }), fade(title, 1), { duration: 2.4 })
261
+
262
+ s.chapter('A different map')
263
+ s.add(secondField, markerB)
264
+ markerB.opacity(0)
265
+ backing.zIndex(5)
266
+ s.play(firstAlpha.to(0), fade(markerA, 0), fadeIn(eqMobius), fade(eqSquare, 0), fade(pLabel, 0), fade(pRead, 0), fade(note, 0), fade(deriv, 0), { duration: 1 })
267
+ s.play(reveal2.to(1), secondAlpha.to(1, { ease: smoother }), fadeIn([kLabel, kRead]), fadeIn(note2, { shift: [0, 8] }), { duration: 2.2 })
268
+ s.play(fade(markerB, 1), fadeIn(pointsB), { duration: 0.8 })
269
+ s.wait(0.6)
270
+ s.play(k.to(1, { ease: smoother }), { duration: 8 })
271
+ s.play(write(landing), write(landing2), write(infinityMark), { duration: 1.6 })
272
+ s.wait(3)
273
+ })