vizmatic 0.8.3 → 0.9.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 (99) hide show
  1. package/PROMPT.md +32 -0
  2. package/README.md +8 -3
  3. package/ROADMAP.md +4 -2
  4. package/SCIENCE.md +69 -3
  5. package/dist/chunk-6JI55YX4.js +604 -0
  6. package/dist/{chunk-LTX44BAE.js → chunk-BYKXNUX7.js} +25 -4
  7. package/dist/{chunk-4MXGLMBG.js → chunk-EPZLNABN.js} +107 -37
  8. package/dist/chunk-EWCO6ZT6.js +696 -0
  9. package/dist/{chunk-JZPMGVBP.js → chunk-G7HRSKHV.js} +1 -1
  10. package/dist/{chunk-RPAHMKAR.js → chunk-JTWC2OPN.js} +7 -5
  11. package/dist/chunk-LJZO36IR.js +124 -0
  12. package/dist/chunk-REL2BZ2B.js +88 -0
  13. package/dist/chunk-RLPEOWST.js +1 -0
  14. package/dist/chunk-VMHOWCDL.js +296 -0
  15. package/dist/chunk-YDD63HHD.js +1 -0
  16. package/dist/{chunk-I745F4QK.js → chunk-YNGDEARY.js} +1134 -643
  17. package/dist/cli.js +80 -543
  18. package/dist/gpu-selection-CDWCZH27.js +1 -0
  19. package/dist/gpu-selection-MX6NHMNA.js +635 -0
  20. package/dist/index.cjs +473 -155
  21. package/dist/index.d.cts +30 -239
  22. package/dist/index.d.ts +30 -239
  23. package/dist/index.js +410 -457
  24. package/dist/math-E7W5LFWO.js +1 -0
  25. package/dist/{math-LXB4FH4E.js → math-NEFQUPRV.js} +3 -1
  26. package/dist/scene-browser.js +17 -17
  27. package/dist/scene-cli.js +1 -1
  28. package/dist/scene-runtime.cjs +4 -2
  29. package/dist/scene-runtime.js +2 -2
  30. package/dist/stage-browser/browser.d.ts +1206 -854
  31. package/dist/stage-browser/gpu-renderer.js +138 -0
  32. package/dist/stage-browser/{math-4WFNHVV2.js → math-KYHUOK7A.js} +46 -10
  33. package/dist/stage-browser/stage.js +107 -19
  34. package/dist/stage-export.cjs +8221 -6784
  35. package/dist/stage-export.d.cts +13 -5
  36. package/dist/stage-export.d.ts +13 -5
  37. package/dist/stage-export.js +111 -601
  38. package/dist/stage.cjs +6243 -3474
  39. package/dist/stage.d.cts +1064 -765
  40. package/dist/stage.d.ts +1064 -765
  41. package/dist/stage.js +3571 -2405
  42. package/dist/{svg-import-B5BGbC1t.d.cts → svg-import-CSCFWYaB.d.cts} +21 -12
  43. package/dist/{svg-import-B5BGbC1t.d.ts → svg-import-CSCFWYaB.d.ts} +21 -12
  44. package/dist/teaching-math-DTKc0XsB.d.cts +243 -0
  45. package/dist/teaching-math-ydDBhJ2M.d.ts +243 -0
  46. package/dist/{video-CiN1MDCS.d.cts → theme-DhVdDQKO.d.cts} +4 -237
  47. package/dist/{video-CiN1MDCS.d.ts → theme-DhVdDQKO.d.ts} +4 -237
  48. package/dist/video-BYCeOz8F.d.ts +259 -0
  49. package/dist/video-CGLxJXqu.d.cts +259 -0
  50. package/examples/catalog-science-physics.tsx +6 -6
  51. package/examples/films/_boltzmann-entropy/model.ts +63 -0
  52. package/examples/films/_catalysis/model.ts +49 -0
  53. package/examples/films/_gravitational-waves/model.ts +32 -0
  54. package/examples/films/_heat-equation/model.ts +54 -0
  55. package/examples/films/_hydrogen-orbitals/model.ts +31 -0
  56. package/examples/films/_maxwell/model.ts +84 -0
  57. package/examples/films/_planetary-orbits/appearance.ts +71 -0
  58. package/examples/films/_planetary-orbits/model.ts +152 -0
  59. package/examples/films/_planetary-orbits/saturn.ts +142 -0
  60. package/examples/films/_quantum-tunneling/model.ts +58 -0
  61. package/examples/films/_reaction-diffusion/model.ts +61 -0
  62. package/examples/films/_stationary-action/model.ts +38 -0
  63. package/examples/films/attention.ts +309 -253
  64. package/examples/films/beam-search.ts +321 -49
  65. package/examples/films/boltzmann-entropy.ts +104 -0
  66. package/examples/films/cache-miss.ts +387 -394
  67. package/examples/films/catalog.json +238 -77
  68. package/examples/films/catalysis.ts +128 -0
  69. package/examples/films/central-limit.ts +27 -22
  70. package/examples/films/complex-maps.ts +70 -56
  71. package/examples/films/deck-example.ts +261 -200
  72. package/examples/films/euler-circle.ts +72 -28
  73. package/examples/films/flash-attention.ts +549 -0
  74. package/examples/films/forecast.ts +2 -2
  75. package/examples/films/fourier-epicycles.ts +21 -20
  76. package/examples/films/gradient-descent.ts +23 -49
  77. package/examples/films/gravitational-waves.ts +179 -0
  78. package/examples/films/heat-equation.ts +124 -0
  79. package/examples/films/hydrogen-orbitals.ts +135 -0
  80. package/examples/films/io-uring.ts +635 -0
  81. package/examples/films/linear-maps.ts +42 -31
  82. package/examples/films/lorenz.ts +42 -79
  83. package/examples/films/maxwell.ts +197 -0
  84. package/examples/films/pathlines.ts +85 -0
  85. package/examples/films/planetary-orbits.ts +146 -0
  86. package/examples/films/pythagoras.ts +18 -10
  87. package/examples/films/quantum-tunneling.ts +116 -0
  88. package/examples/films/rag-citations.ts +423 -0
  89. package/examples/films/reaction-diffusion.ts +70 -0
  90. package/examples/films/ring-allreduce.ts +531 -0
  91. package/examples/films/stationary-action.ts +170 -0
  92. package/examples/films/svd-3d.ts +605 -0
  93. package/examples/films/taylor-series.ts +9 -9
  94. package/examples/films/vizmatic-title.ts +1 -0
  95. package/examples/vizmatic-hero.tsx +102 -32
  96. package/package.json +3 -1
  97. package/dist/chunk-3F2VQ7NW.js +0 -1
  98. package/dist/chunk-GGFDBJ25.js +0 -88
  99. package/dist/math-QVCETQDQ.js +0 -1
@@ -0,0 +1,146 @@
1
+ import {
2
+ Box, circle, column, curve3d, dot, dynamic, fadeIn, fadeOut, film,
3
+ place, point3d, radialGradient, rectPath, scene3d, sceneKit, tex, text, parseColor, mixColor,
4
+ type Node, type Vec3,
5
+ } from 'vizmatic/stage'
6
+ import { PLANETS, orbitalState } from './_planetary-orbits/model'
7
+ import { COLORS, RADII, SPACING, planetSurface, saturnRings } from './_planetary-orbits/appearance'
8
+ import { saturnPortrait } from './_planetary-orbits/saturn'
9
+
10
+ const DURATION = 50
11
+ const smooth = (x: number) => { const p = Math.max(0, Math.min(1, x)); return p * p * (3 - 2 * p) }
12
+ const saturnIn = (t: number) => smooth((t - 26) / 1.2) * (1 - smooth((t - 38) / 1.2))
13
+ const outerOnly = (t: number) => smooth((t - 13) / 1.5) * (1 - smooth((t - 39) / 1.5))
14
+
15
+ // Integrate a smooth change in the common clock. Every visible planet always
16
+ // uses the same model time; rapid inner orbits fade before the clock speeds up.
17
+ function years(t: number): number {
18
+ const rampIntegral = (start: number, width: number) => {
19
+ const x = Math.max(0, (t - start) / width)
20
+ return width * (x < 1 ? x ** 3 - x ** 4 / 2 : x - 0.5)
21
+ }
22
+ return 0.07 * t + 2.33 * (rampIntegral(14.5, 2) - rampIntegral(37, 2))
23
+ }
24
+
25
+ function position(index: number, at: number): Vec3 {
26
+ const planet = PLANETS[index]
27
+ const scale = SPACING[index] / planet.a
28
+ const p = orbitalState(planet, at).position
29
+ return [p[0] * scale, p[1] * scale, p[2] * scale]
30
+ }
31
+
32
+ export default film({
33
+ title: 'Eight worlds, one gravity',
34
+ description: 'Keplerian orbits carry eight worlds around the Sun. A shared clock shifts from the inner planets to the giants, then a moving camera explores Saturn’s tilted rings and their shadow. Planet sizes are enlarged, orbital spacing is compressed, and surfaces are stylized.',
35
+ look: 'midnight', font: 'text-serif', fps: 60, poster: 32, bloom: 0.08,
36
+ }, (s) => {
37
+ const kit = sceneKit({ size: 40, heading: { size: 70, y: -460 }, area: { left: -850, right: 850, top: -330, bottom: 335 } })
38
+ s.add(kit.heading('Eight worlds, one gravity'))
39
+ s.add(place(text('Keplerian motion · compressed spacing · enlarged planets · stylized surfaces', { size: 27, color: 'dim' }), new Box(-850, -410, 850, -365), 'left'))
40
+ s.add(place(text('NASA / JPL · approximate J2000 elements', { size: 25, color: 'dim' }), new Box(-850, 351, 850, 390), 'right'))
41
+
42
+ // A fixed, sparse sky is stable under arbitrary seeks and has no flicker.
43
+ for (let i = 0; i < 130; i++) {
44
+ const x = Math.sin(i * 127.1 + 3.7) * 43758.5453
45
+ const y = Math.sin(i * 269.5 + 9.1) * 19642.349
46
+ s.add(dot(0.6 + (i % 4) * 0.23, 'dim').opacity(s.look.dark ? 0.3 : 0.17).at(-900 + (x - Math.floor(x)) * 1800, -325 + (y - Math.floor(y)) * 655))
47
+ }
48
+
49
+ const systemCenter = [0, 18] as const
50
+ s.add(dynamic((t) => {
51
+ const opacity = 1 - saturnIn(t)
52
+ if (opacity < 0.001) return []
53
+ const halo = circle(140, {
54
+ fill: radialGradient([0, 0], 140, [[0, '#f5bf56'], [0.22, '#d289302b'], [1, '#d2893000']]), stroke: null,
55
+ }).at(...systemCenter).opacity(opacity * (s.look.dark ? 0.65 : 0.18))
56
+ const pushIn = smooth((t - 1) / 4) * (1 - smooth((t - 9) / 5))
57
+ const theta = -1.15 + 0.014 * t, phi = 1.22 + 0.05 * Math.sin(t * 0.08)
58
+ const camera: Vec3 = [Math.sin(phi) * Math.cos(theta), Math.sin(phi) * Math.sin(theta), Math.cos(phi)]
59
+ const view = scene3d({
60
+ theta, phi, unit: 96 + 52 * pushIn,
61
+ perspective: false, fog: 0, ambient: 0.4, shine: 0, brightness: 1,
62
+ }).at(...systemCenter).opacity(opacity)
63
+ view.clipPath = rectPath(-850, -300, 1700, 580)
64
+ view.add(point3d([0, 0, 0], { radius: 0.34 * (96 + 52 * pushIn), color: '#f2c46e' }))
65
+ const at = years(t), outer = outerOnly(t)
66
+ for (let i = 0; i < PLANETS.length; i++) {
67
+ const planet = PLANETS[i], bodyOpacity = i < 4 ? 1 - outer : 1
68
+ const p = position(i, at), light: Vec3 = [-p[0], -p[1], -p[2]]
69
+ view.add(curve3d((phase) => position(i, phase * planet.period), 0, 1, {
70
+ color: COLORS[i], width: 1.2, opacity: (s.look.dark ? 0.27 : 0.5) * (i < 4 ? 1 - 0.8 * outer : 1), samples: 150,
71
+ }))
72
+ if (bodyOpacity < 0.005) continue
73
+ // Each trail is a fixed fraction of this planet's orbital period.
74
+ view.add(curve3d((past) => position(i, at - past * planet.period), 0.18, 0, {
75
+ colors: [mixColor(parseColor('bg'), parseColor(COLORS[i]), 0.05), COLORS[i]], width: 2.9, glow: s.look.dark ? 2 : 0, opacity: bodyOpacity, samples: 72,
76
+ }))
77
+ view.add(planetSurface(i, p, RADII[i], light, t * 0.17).opacity(bodyOpacity))
78
+ if (i === 5) view.add(...saturnRings(p, RADII[i], light, camera))
79
+ }
80
+ return [halo, view]
81
+ }))
82
+
83
+ // A separate magnified camera studies ring geometry while the system clock
84
+ // continues offscreen. Camera motion is not Saturn's physical precession.
85
+ const portrait = saturnPortrait().at(-260, 24)
86
+ s.at(26, () => s.play(fadeIn(portrait, { duration: 1.2 })))
87
+ s.at(38, () => s.play(fadeOut(portrait, { duration: 1.2 })))
88
+
89
+ const chapters = [
90
+ { start: 0, end: 13, title: '01 / The inner worlds move first' },
91
+ { start: 13, end: 26, title: '02 / Give the giants time' },
92
+ { start: 26, end: 39, title: '03 / A world inside its rings' },
93
+ { start: 39, end: 50, title: '04 / Different rhythms. The same law.' },
94
+ ]
95
+ for (const chapter of chapters) {
96
+ const heading = place(text(chapter.title, { size: 36, color: 'yellow' }), new Box(-850, -350, 850, -300), 'left')
97
+ s.at(chapter.start, () => {
98
+ s.chapter(chapter.title.slice(5))
99
+ s.play(fadeIn(heading, { duration: 0.3 }))
100
+ })
101
+ if (chapter.end < DURATION) s.at(chapter.end - 0.3, () => s.play(fadeOut(heading, { duration: 0.25 })))
102
+ }
103
+
104
+ const legend = PLANETS.map((planet, i) => {
105
+ const x = -815 + i * 210
106
+ return [dot(5, COLORS[i]).at(x, 320), text(planet.name, { size: 27, color: 'dim' }).at(x + 75, 320)]
107
+ })
108
+ s.add(dynamic(t => {
109
+ const opacity = 1 - saturnIn(t)
110
+ return opacity < 0.001 ? [] : legend.flatMap((nodes, i) => nodes.map(node => node.clone().opacity(opacity * (i < 4 ? 1 - 0.8 * outerOnly(t) : 1))))
111
+ }))
112
+
113
+ const [saturnTitle, saturnMath, saturnNote] = new Box(280, -190, 850, 255).rows([1.25, 1.4, 2], 28)
114
+ const detail: Node[] = [
115
+ place(text('Magnified geometry · illustrative lighting', { size: 27, color: 'dim' }), new Box(-850, 305, 850, 345), 'left'),
116
+ place(text('Saturn', { size: 80 }), saturnTitle, 'left'),
117
+ ...place(column([
118
+ text('29.4 Earth years', { size: 40, color: 'yellow' }),
119
+ text('for one trip around the Sun', { size: 28, color: 'dim' }),
120
+ ], { gap: 13 }), saturnMath, 'left').nodes,
121
+ ...place(column([
122
+ text('Tilted rings catch the light.', { size: 31 }),
123
+ text('The planet casts a shadow.', { size: 31 }),
124
+ text('The dark gap is the Cassini division.', { size: 27, color: 'dim' }),
125
+ ], { gap: 18 }), saturnNote, 'left').nodes,
126
+ ]
127
+ s.at(27.3, () => s.play(fadeIn(detail, { duration: 0.6 })))
128
+ s.at(37.6, () => s.play(fadeOut(detail, { duration: 0.35 })))
129
+
130
+ const law = place(tex('T^2\\propto a^3', { size: 52, color: 'yellow' }), new Box(480, -280, 850, -190), 'right')
131
+ const lawNote = place(text('Farther out. Longer years.', { size: 29, color: 'dim' }), new Box(430, -190, 850, -145), 'right')
132
+ s.at(40, () => s.play(fadeIn([law, lawNote], { duration: 0.8 })))
133
+
134
+ const captions: [number, number, string][] = [
135
+ [0, 6.5, 'Eight planets share one clock. Close to the Sun, the inner planets move through their years quickly.'],
136
+ [6.5, 6.5, 'These are fixed Keplerian ellipses with approximate J2000 elements. Distances are compressed and bodies enlarged so every world is visible.'],
137
+ [13, 6.5, 'The inner planets fade before the shared clock speeds up. Now the slower outer planets come alive.'],
138
+ [19.5, 6.5, 'Jupiter takes about twelve Earth years to orbit the Sun. Neptune takes about one hundred sixty-five.'],
139
+ [26, 6.5, 'Saturn’s rings contain countless particles. Here, magnified annular surfaces and illustrative lighting reveal their geometry.'],
140
+ [32.5, 6.5, 'The camera rises above the rings. The Cassini division stays open, and the planet casts its shadow across the ring plane.'],
141
+ [39, 5.5, 'Return to the full system and the slower clock. The planets have continued moving while the camera was away.'],
142
+ [44.5, 5.5, 'The square of an orbital period is proportional to the cube of its semimajor axis. Farther worlds take longer to return.'],
143
+ ]
144
+ for (const [start, duration, caption] of captions) s.at(start, () => s.caption(caption, duration))
145
+ s.wait(Math.max(0, DURATION - s.time))
146
+ })
@@ -21,6 +21,7 @@ export default film({
21
21
  title: 'Pythagoras by rearrangement',
22
22
  description: 'Four triangles slide inside a square, and the leftover area shows why a squared plus b squared equals c squared.',
23
23
  fps: 60,
24
+ look: 'chalk',
24
25
  }, (s) => {
25
26
  // A single right triangle, introduced at the center.
26
27
  const triangle: Vec[] = [[0, 0], [A, 0], [0, B]]
@@ -36,9 +37,9 @@ export default film({
36
37
  hero.moveTo([0, 40])
37
38
  const heroBounds = hero.bounds()
38
39
  const heroAt = (x: number, y: number): Vec => [heroBounds.x + x * UNIT, heroBounds.y + y * UNIT]
39
- const labelA = tex('a', { size: 44, color: 'yellow', display: false }).moveTo([heroAt(A / 2, 0)[0], heroAt(0, 0)[1] - 30])
40
- const labelB = tex('b', { size: 44, color: 'teal', display: false }).moveTo([heroAt(0, 0)[0] - 30, heroAt(0, B / 2)[1]])
41
- const labelC = tex('c', { size: 44, color: 'pink', display: false }).moveTo([heroAt(A / 2, B / 2)[0] + 28, heroAt(A / 2, B / 2)[1] + 22])
40
+ const labelA = tex('a', { size: 44, color: 'yellow', display: false }).moveTo([heroAt(A / 2, 0)[0], heroAt(0, 0)[1] - 36])
41
+ const labelB = tex('b', { size: 44, color: 'teal', display: false }).moveTo([heroAt(0, 0)[0] - 36, heroAt(0, B / 2)[1]])
42
+ const labelC = tex('c', { size: 44, color: 'pink', display: false }).moveTo([heroAt(A / 2, B / 2)[0] + 34, heroAt(A / 2, B / 2)[1] + 28])
42
43
  const corner = shape(polyPath([heroAt(0.3, 0), heroAt(0.3, 0.3), heroAt(0, 0.3)]), { stroke: 'ink', width: 2 })
43
44
 
44
45
  const title = text('Move the pieces. Keep the square.', { font: 'text-serif', size: 66 })
@@ -72,10 +73,10 @@ export default film({
72
73
  return tex(label, { size: 34, color, display: false }).moveTo(mid)
73
74
  }
74
75
  const edgeLabels = new Node().add(
75
- sideMark(LEFT_X, [0, 0], [A, 0], 'a', 'yellow', [0, -30]),
76
- sideMark(LEFT_X, [A, 0], [SIDE, 0], 'b', 'teal', [0, -30]),
77
- sideMark(LEFT_X, [0, 0], [0, B], 'b', 'teal', [-28, 0]),
78
- sideMark(LEFT_X, [0, B], [0, SIDE], 'a', 'yellow', [-28, 0]),
76
+ sideMark(LEFT_X, [0, 0], [A, 0], 'a', 'yellow', [0, -36]),
77
+ sideMark(LEFT_X, [A, 0], [SIDE, 0], 'b', 'teal', [0, -36]),
78
+ sideMark(LEFT_X, [0, 0], [0, B], 'b', 'teal', [-36, 0]),
79
+ sideMark(LEFT_X, [0, B], [0, SIDE], 'a', 'yellow', [-36, 0]),
79
80
  )
80
81
 
81
82
  // Squares the moved triangles reveal.
@@ -108,11 +109,12 @@ export default film({
108
109
  s.wait(0.8)
109
110
 
110
111
  s.chapter('Four copies')
111
- s.play(fadeOut([labelA, labelB, labelC, corner]), to(hero, (node) => {
112
+ s.play(fadeOut([labelA, labelB, labelC, corner]), { duration: 0.45 })
113
+ s.play(to(hero, (node) => {
112
114
  node.set('px', 0)
113
115
  node.set('py', 0)
114
116
  node.at(at(0, 0, 0))
115
- }), { duration: 0.9 })
117
+ }), { duration: 0.75 })
116
118
  const center = placements.map((placement) => triangleAt(0, placement))
117
119
  const frameCenter = rect(SIDE * UNIT, SIDE * UNIT, { stroke: 'ink', width: 3 }).at(0, CY)
118
120
  s.add(center[0]!)
@@ -180,6 +182,12 @@ export default film({
180
182
  s.play(fadeIn([...equation.part('='), ...equation.part('+')]), { duration: 0.6 })
181
183
  s.wait(1)
182
184
  check.moveTo([430, 448])
183
- s.play(to(equation, (eq) => eq.shift(-300, 0)), fadeIn([tilesA, tilesB], { lag: 0.02 }), delay(fadeIn(check, { shift: [-30, 0] }), 0.5), fade(aSquared, 0), fade(bSquared, 0), { duration: 1.4 })
185
+ s.play(
186
+ to(equation, (eq) => eq.shift(-300, 0), { duration: 1.2 }),
187
+ fade(aSquared, 0, { duration: 0.45 }),
188
+ fade(bSquared, 0, { duration: 0.45 }),
189
+ delay(fadeIn([tilesA, tilesB], { lag: 0.02, duration: 0.95 }), 0.45),
190
+ delay(fadeIn(check, { shift: [-30, 0], duration: 0.9 }), 0.5),
191
+ )
184
192
  s.wait(3)
185
193
  })
@@ -0,0 +1,116 @@
1
+ import {
2
+ Box, axes, column, dot, dynamic, fadeIn, fadeOut, film, line, linear, morphMatching,
3
+ place, rect, row, scalarField, sceneKit, tex, text,
4
+ } from 'vizmatic/stage'
5
+ import { ENERGY, norm2, scattering, verifyScattering, WIDTHS } from './_quantum-tunneling/model'
6
+
7
+ export default film({
8
+ title: 'A barrier is not a wall',
9
+ description: 'Exact stationary scattering below a rectangular barrier: complex phase rotates, probability density holds still, and thicker barriers transmit less current.',
10
+ look: 'midnight', font: 'text-serif', fps: 60, poster: 23, bloom: 0,
11
+ }, (s) => {
12
+ verifyScattering()
13
+ const models = WIDTHS.map((width) => scattering(width))
14
+ const experiment = s.value(0), phaseTime = s.value(0)
15
+ const model = () => models[experiment.value]!
16
+ const kit = sceneKit({ size: 40, area: { left: -860, right: 860, top: -320, bottom: 294 }, heading: { size: 68, y: -466 }, source: { size: 27, y: -401 } })
17
+ s.add(kit.heading('A barrier is not a wall.'))
18
+ s.add(kit.source(`Exact stationary scattering · D. Tong, Quantum Mechanics §2.4.2 · E / V₀ = ${ENERGY}`))
19
+ s.add(place(text('Yellow band: V₀ = 1. Outside: V = 0. Units: ℏ = 2m = V₀ = 1. Incident amplitude = 1.', { size: 27, color: 'dim' }), new Box(-860, -374, 860, -338), 'left'))
20
+ const [left, right] = kit.area.columns([2.1, 1], 80)
21
+ const waveRegion = new Box(left.left + 30, -240, left.right - 10, -26)
22
+ const densityRegion = new Box(left.left + 30, 130, left.right - 10, 282)
23
+ const x: readonly [number, number] = [-9, 9]
24
+ const waves = axes({ x, y: [-2.1, 2.1], width: waveRegion.width, height: waveRegion.height, xTicks: [], yTicks: [], labelSize: 27, tips: false })
25
+ const density = axes({ x, y: [0, 4.2], width: densityRegion.width, height: densityRegion.height, xTicks: [-8, -4, 0, 4, 8], yTicks: [], labelSize: 27, tips: false })
26
+ // Center the plotting rectangles, rather than the tick labels that extend outside them.
27
+ waves.at(waveRegion.cx, waveRegion.cy)
28
+ density.at(densityRegion.cx, densityRegion.cy)
29
+ // Put scale labels outside the plotted waves, where no phase can cross them.
30
+ for (const [plot, values] of [[waves, [-1, 1]], [density, [2, 4]]] as const) for (const value of values) {
31
+ const [px, py] = plot.world(x[0], value)
32
+ s.add(text(String(value), { size: 27, color: 'dim' }).moveTo([px - 32, py]))
33
+ }
34
+ const shade = (plot: typeof waves) => dynamic(() => {
35
+ const a = plot.p(0, 0), b = plot.p(model().width, 0)
36
+ return rect(b[0] - a[0], plot.plotHeight, { fill: 'yellow', opacity: 0.12 }).at((a[0] + b[0]) / 2, 0)
37
+ })
38
+ waves.add(shade(waves))
39
+ density.add(shade(density))
40
+ waves.plot((x) => model().psi(x, phaseTime.value)[0], { from: () => x[0], to: () => x[1], stroke: 'blue', width: 3.5, samples: 400 })
41
+ waves.plot((x) => model().psi(x, phaseTime.value)[1], { from: () => x[0], to: () => x[1], stroke: 'pink', width: 3.5, samples: 400 })
42
+ density.plot((x) => norm2(model().stationary(x)), { from: () => x[0], to: () => x[1], stroke: 'teal', width: 4, samples: 400 })
43
+ s.add(waves, density)
44
+ const waveLegend = row([
45
+ text('Complex amplitude', { size: 32 }), text('real', { size: 30, color: 'blue' }), text('imaginary', { size: 30, color: 'pink' }),
46
+ ], { gap: 28 })
47
+ s.add(place(waveLegend, new Box(left.left, -320, left.right, -274), 'left'))
48
+ const phaseRegion = new Box(waveRegion.left, 32, waveRegion.right, 56)
49
+ const phaseColors = ['teal', 'blue', 'purple', 'pink', 'orange', 'yellow', 'teal'] as const
50
+ const phaseField = scalarField((x) => {
51
+ const z = model().psi(x, phaseTime.value)
52
+ return Math.atan2(z[1], z[0])
53
+ }, { x, y: [0, 1], domain: [-Math.PI, Math.PI], colors: phaseColors, width: phaseRegion.width, height: phaseRegion.height, resolution: [400, 2] })
54
+ s.add(place(text('Phase angle (hue)', { size: 27, color: 'dim' }), new Box(left.left, -12, left.right, 24), 'left'))
55
+ s.add(place(phaseField, phaseRegion))
56
+ s.add(place(row([tex('|\\Psi|^2', { size: 32, color: 'teal' }), text('relative probability density', { size: 30, color: 'teal' })], { gap: 16 }), new Box(left.left, 70, left.right, 114), 'left'))
57
+ s.add(place(text('position x', { size: 27, color: 'dim' }), new Box(left.left, 324, left.right, 360)))
58
+
59
+ const [chapterRegion, equationRegion, bodyRegion, currentRegion, chartRegion] = right.rows([0.6, 1, 1.9, 1.1, 2], 15)
60
+ const words = [
61
+ ['Below the barrier', 'The wave and its slope join at both edges. An oscillating transmitted wave survives where a classical particle cannot pass.'],
62
+ ['Phase moves. Density holds.', 'A common phase rotates the complex wave. Its squared magnitude stays fixed. This is a stationary state, with no launched wave packet.'],
63
+ ['Twice the width', 'A second stationary experiment, at the same energy. More barrier means less transmitted probability current.'],
64
+ ['Three times the width', 'The transmitted current falls again. Reflection and transmission still sum to one. The particle does not lose energy crossing the barrier.'],
65
+ ]
66
+ const headings = words.map(([heading]) => place(text(heading, { size: 33, color: 'yellow', maxWidth: right.width }), chapterRegion, 'left'))
67
+ const bodies = words.map(([, body]) => place(text(body, { size: 31, color: 'dim', maxWidth: right.width, leading: 1.24 }), bodyRegion, 'left'))
68
+ const equations = [
69
+ tex('-\\psi^{\\prime\\prime}+V\\psi=E\\psi', { size: 34 }),
70
+ tex('\\Psi=\\psi(x)e^{-iEt}', { size: 36 }),
71
+ tex('R+T=1', { size: 44 }),
72
+ ].map((eq) => place(eq, equationRegion, 'left'))
73
+ s.add(headings[0], bodies[0], equations[0])
74
+ s.add(dynamic(() => place(column([
75
+ text(`L = ${model().width.toFixed(1)} T = ${(100 * model().T).toFixed(1)}%`, { size: 34, font: 'mono', color: 'teal' }),
76
+ text(`Reflected: ${(100 * model().R).toFixed(1)}%`, { size: 29, color: 'dim' }),
77
+ ], { gap: 12, align: 'left' }), currentRegion, 'left').nodes))
78
+ const curve = axes({ x: [0, 4], y: [0, 1], width: right.width - 72, height: chartRegion.height - 91, xTicks: [0, 2, 4], yTicks: [0, 1], labelSize: 27, tips: false })
79
+ curve.at(chartRegion.cx + 16, chartRegion.cy + 2)
80
+ curve.plot((width) => scattering(width).T, { stroke: 'teal', width: 3 })
81
+ curve.add(dynamic(() => [
82
+ line(curve.p(model().width, 0), curve.p(model().width, model().T), { stroke: 'yellow', width: 2, dash: [5, 5] }),
83
+ dot(7, 'yellow').at(curve.p(model().width, model().T)),
84
+ ]))
85
+ s.add(curve)
86
+ s.add(place(text('Transmission T vs width L', { size: 27, color: 'dim' }), new Box(right.left, chartRegion.top - 12, right.right, chartRegion.top + 24), 'left'))
87
+ const phaseKey = scalarField((angle) => angle, { x: [-Math.PI, Math.PI], y: [0, 1], domain: [-Math.PI, Math.PI], colors: phaseColors, width: right.width, height: 12, resolution: [160, 2] })
88
+ s.add(place(phaseKey, new Box(right.left, 310, right.right, 322)))
89
+ for (const [px, label] of [[right.left + 14, '-\\pi'], [right.cx, '0'], [right.right - 14, '\\pi']] as const) {
90
+ s.add(tex(label, { size: 27, color: 'dim' }).moveTo([px, 344]))
91
+ }
92
+
93
+ const change = (index: number, equationFrom?: number, equationTo?: number, nextExperiment?: number) => {
94
+ s.play(fadeOut([headings[index - 1]!, bodies[index - 1]!]), { duration: 0.25 })
95
+ if (nextExperiment !== undefined) experiment.set(nextExperiment)
96
+ if (equationFrom !== undefined && equationTo !== undefined) {
97
+ s.play(morphMatching(equations[equationFrom]!, equations[equationTo]!), fadeIn([headings[index]!, bodies[index]!]), { duration: 0.75 })
98
+ } else s.play(fadeIn([headings[index]!, bodies[index]!]), { duration: 0.75 })
99
+ }
100
+ s.at(0, () => s.play(phaseTime.to(120, { ease: linear }), { duration: 40 }))
101
+ s.chapter('Below the barrier')
102
+ s.caption('Energy is below the barrier. The wave and its slope match at both boundaries, leaving nonzero transmission.', 9)
103
+ s.wait(9)
104
+ s.chapter('Phase and probability')
105
+ change(1, 0, 1)
106
+ s.caption('The phase rotates, but probability density stays still. This is stationary scattering, not a traveling wave packet.', 8)
107
+ s.wait(8)
108
+ s.chapter('Double the width')
109
+ change(2, 1, 2, 1)
110
+ s.caption('Compare a separate stationary solution at twice the barrier width. The energy stays fixed; transmission falls.', 10)
111
+ s.wait(9)
112
+ s.chapter('Triple the width')
113
+ change(3, undefined, undefined, 2)
114
+ s.caption('At triple width, transmission is smaller still. The transmitted and reflected probability currents sum to the incoming current.', 11)
115
+ s.wait(11)
116
+ })