@motionscript/math 0.1.0-alpha.8 → 0.1.0-alpha.9

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 (157) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.js.map +1 -1
  6. package/dist/annotate/curly-brace.js.map +1 -1
  7. package/dist/annotate/emphasis-mark.js.map +1 -1
  8. package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
  9. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  10. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  11. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  12. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  13. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  14. package/dist/browser/index.js +1 -1
  15. package/dist/browser/index.js.map +1 -1
  16. package/dist/browser/kit.js +1 -1
  17. package/dist/browser/tex.js +1 -1
  18. package/dist/browser/tex.js.map +1 -1
  19. package/dist/engine.d.ts +1 -1
  20. package/dist/engine.js +1 -1
  21. package/dist/engine.js.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/kit/brace.js.map +1 -1
  24. package/dist/kit/cache.js.map +1 -1
  25. package/dist/kit/component.d.ts.map +1 -1
  26. package/dist/kit/component.js.map +1 -1
  27. package/dist/kit/contour.js.map +1 -1
  28. package/dist/kit/coords.js.map +1 -1
  29. package/dist/kit/curve.js.map +1 -1
  30. package/dist/kit/equation.js.map +1 -1
  31. package/dist/kit/equation3d.js.map +1 -1
  32. package/dist/kit/expression.js.map +1 -1
  33. package/dist/kit/field-mark.d.ts.map +1 -1
  34. package/dist/kit/field-mark.js.map +1 -1
  35. package/dist/kit/field.js.map +1 -1
  36. package/dist/kit/follow.d.ts.map +1 -1
  37. package/dist/kit/follow.js.map +1 -1
  38. package/dist/kit/furniture.js.map +1 -1
  39. package/dist/kit/geometry.js.map +1 -1
  40. package/dist/kit/graph.js.map +1 -1
  41. package/dist/kit/mark.js.map +1 -1
  42. package/dist/kit/part-fills.js.map +1 -1
  43. package/dist/kit/place.js.map +1 -1
  44. package/dist/kit/sampler.js.map +1 -1
  45. package/dist/kit/scale.js.map +1 -1
  46. package/dist/kit/source.js.map +1 -1
  47. package/dist/kit/system.js.map +1 -1
  48. package/dist/kit/ticks.js.map +1 -1
  49. package/dist/kit/withdrawn.js.map +1 -1
  50. package/dist/marks/curve-area.js.map +1 -1
  51. package/dist/marks/drop-lines.js.map +1 -1
  52. package/dist/marks/plot-point.js.map +1 -1
  53. package/dist/marks/riemann-sum.js.map +1 -1
  54. package/dist/marks/tangent-line.js.map +1 -1
  55. package/dist/marks/vector-arrow.js.map +1 -1
  56. package/dist/nodes.js.map +1 -1
  57. package/dist/plots/function-plot.d.ts.map +1 -1
  58. package/dist/plots/function-plot.js.map +1 -1
  59. package/dist/plots/points-plot.js.map +1 -1
  60. package/dist/plots/stream-lines.js.map +1 -1
  61. package/dist/plots/traced-path.js.map +1 -1
  62. package/dist/plots/vector-field.js.map +1 -1
  63. package/dist/structures/graph-layout.js.map +1 -1
  64. package/dist/structures/network-graph.js.map +1 -1
  65. package/dist/structures/sample-space.js.map +1 -1
  66. package/dist/systems/axes2d.d.ts.map +1 -1
  67. package/dist/systems/axes2d.js.map +1 -1
  68. package/dist/systems/number-line.js.map +1 -1
  69. package/dist/systems/number-plane.js.map +1 -1
  70. package/dist/systems/polar-plane.js.map +1 -1
  71. package/dist/tex/geometry.js.map +1 -1
  72. package/dist/tex/index.js.map +1 -1
  73. package/dist/tex/mathjax.js.map +1 -1
  74. package/dist/tex/matrix.js.map +1 -1
  75. package/dist/tex/node.js.map +1 -1
  76. package/dist/tex/tween.js.map +1 -1
  77. package/dist/tex/typeset.d.ts.map +1 -1
  78. package/dist/tex/typeset.js.map +1 -1
  79. package/dist/three/axes3d.js.map +1 -1
  80. package/dist/three/curve-plot3d.js.map +1 -1
  81. package/dist/three/entries.js.map +1 -1
  82. package/dist/three/label3d.js.map +1 -1
  83. package/dist/three/plot-point3d.js.map +1 -1
  84. package/dist/three/space.js.map +1 -1
  85. package/dist/three/surface-plot3d.js.map +1 -1
  86. package/dist/three/vector-arrow3d.js.map +1 -1
  87. package/package.json +4 -4
  88. package/registry.json +251 -251
  89. package/src/annotate/angle-mark.ts +137 -137
  90. package/src/annotate/curly-brace.ts +148 -148
  91. package/src/annotate/emphasis-mark.ts +118 -118
  92. package/src/engine.ts +86 -86
  93. package/src/index.ts +29 -29
  94. package/src/kit/brace.ts +37 -37
  95. package/src/kit/cache.ts +61 -61
  96. package/src/kit/component.ts +57 -57
  97. package/src/kit/contour.ts +94 -94
  98. package/src/kit/coords.ts +278 -278
  99. package/src/kit/curve.ts +47 -47
  100. package/src/kit/equation.ts +181 -181
  101. package/src/kit/equation3d.ts +137 -137
  102. package/src/kit/expression.ts +159 -159
  103. package/src/kit/field-mark.ts +171 -171
  104. package/src/kit/field.ts +50 -50
  105. package/src/kit/follow.ts +229 -229
  106. package/src/kit/furniture.ts +113 -113
  107. package/src/kit/geometry.ts +49 -49
  108. package/src/kit/graph.ts +116 -116
  109. package/src/kit/mark.ts +152 -152
  110. package/src/kit/part-fills.ts +61 -61
  111. package/src/kit/place.ts +77 -77
  112. package/src/kit/sampler.ts +281 -281
  113. package/src/kit/scale.ts +39 -39
  114. package/src/kit/source.ts +57 -57
  115. package/src/kit/system.ts +105 -105
  116. package/src/kit/ticks.ts +85 -85
  117. package/src/kit/withdrawn.ts +8 -8
  118. package/src/marks/curve-area.ts +89 -89
  119. package/src/marks/drop-lines.ts +71 -71
  120. package/src/marks/plot-point.ts +102 -102
  121. package/src/marks/riemann-sum.ts +99 -99
  122. package/src/marks/tangent-line.ts +119 -119
  123. package/src/marks/vector-arrow.ts +98 -98
  124. package/src/nodes.ts +99 -99
  125. package/src/plots/function-plot.ts +220 -220
  126. package/src/plots/points-plot.ts +103 -103
  127. package/src/plots/stream-lines.ts +160 -160
  128. package/src/plots/traced-path.ts +70 -70
  129. package/src/plots/vector-field.ts +107 -107
  130. package/src/structures/graph-layout.ts +145 -145
  131. package/src/structures/network-graph.ts +291 -291
  132. package/src/structures/sample-space.ts +210 -210
  133. package/src/systems/axes2d.ts +226 -226
  134. package/src/systems/number-line.ts +130 -130
  135. package/src/systems/number-plane.ts +232 -232
  136. package/src/systems/polar-plane.ts +177 -177
  137. package/src/tex/geometry.ts +703 -703
  138. package/src/tex/index.ts +15 -15
  139. package/src/tex/mathjax.ts +82 -82
  140. package/src/tex/matrix.ts +97 -97
  141. package/src/tex/node.ts +74 -74
  142. package/src/tex/tween.ts +308 -308
  143. package/src/tex/typeset.ts +461 -461
  144. package/src/three/axes3d.ts +267 -267
  145. package/src/three/curve-plot3d.ts +146 -146
  146. package/src/three/entries.ts +42 -42
  147. package/src/three/label3d.ts +52 -52
  148. package/src/three/plot-point3d.ts +63 -63
  149. package/src/three/space.ts +92 -92
  150. package/src/three/surface-plot3d.ts +318 -318
  151. package/src/three/vector-arrow3d.ts +88 -88
  152. package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
  153. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  154. package/dist/kit/placement.d.ts +0 -8
  155. package/dist/kit/placement.d.ts.map +0 -1
  156. package/dist/kit/placement.js +0 -8
  157. package/dist/kit/placement.js.map +0 -1
@@ -1,103 +1,103 @@
1
- import {
2
- component2d,
3
- Types,
4
- type NodeOf,
5
- type PaintProps,
6
- type Vector2,
7
- // Named so the emitted declaration reaches these through the sdk, not core.
8
- type Fill as _Fill,
9
- type FillResolved as _FillResolved,
10
- type Node2D as _Node2D,
11
- type Node2DProps as _Node2DProps,
12
- type Shadow as _Shadow,
13
- type ShadowResolved as _ShadowResolved,
14
- type Stroke as _Stroke,
15
- type StrokeResolved as _StrokeResolved,
16
- } from "@motionscript/sdk";
17
- import { PAINT, PAINT_COMMANDS } from "../kit/component";
18
- import { PLOT_COLOR } from "../kit/furniture";
19
- import { PLANE_ENTRY } from "../kit/graph";
20
- import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
21
-
22
- export interface PointsPlotProps extends PaintProps {
23
- points?: Vector2[];
24
- closed?: boolean;
25
- smooth?: boolean;
26
- }
27
-
28
- // A shorter list is padded with its own last point, so a line grows new points out of its end.
29
- function lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {
30
- const count = Math.max(from.length, to.length);
31
- if (from.length === 0 || to.length === 0) return t < 1 ? from : to;
32
- const out = new Array<Vector2>(count);
33
- for (let i = 0; i < count; i++) {
34
- const a = from[Math.min(i, from.length - 1)];
35
- const b = to[Math.min(i, to.length - 1)];
36
- out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
37
- }
38
- return out;
39
- }
40
-
41
- /** Points on the curve between each pair of a smooth line's points. */
42
- const SMOOTH_STEPS = 16;
43
-
44
- const state = {
45
- ...PAINT,
46
- points: Types.custom<Vector2[]>([], { lerp: lerpPoints }),
47
- /** Join the last point to the first. */
48
- closed: Types.boolean(false, { animatable: false }),
49
- /** Curve through the points instead of turning a corner at each. */
50
- smooth: Types.boolean(false, { animatable: false }),
51
- graph: PLANE_ENTRY,
52
- };
53
-
54
- interface PointsValues {
55
- readonly points: Vector2[];
56
- readonly closed: boolean;
57
- readonly smooth: boolean;
58
- }
59
-
60
- /** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
61
- export const PointsPlot = traced(component2d({
62
- key: "pointsPlot",
63
- state,
64
- defaults: { stroke: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
65
- commands: {
66
- ...PAINT_COMMANDS,
67
- /** Move the points to new places; extra ones grow from the line's end. */
68
- pointsTo: { args: ["points"] },
69
- },
70
- measure: (_measurer, s) => inkSize(tracePoints(s, reading(s))),
71
- // An open line has no inside, so its fill waits until it is closed.
72
- render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),
73
- }), tracePoints);
74
-
75
- export type PointsPlot = NodeOf<typeof PointsPlot>;
76
-
77
- function tracePoints(s: PointsValues, { map, shift }: MarkReading): Vector2[][] {
78
- const pixels = s.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
79
- if (pixels.length < 2) return [];
80
- return [s.smooth && pixels.length > 2 ? spline(pixels, s.closed) : pixels];
81
- }
82
-
83
- // Catmull–Rom, so the curve passes through every point it was given.
84
- function spline(points: readonly Vector2[], closed: boolean): Vector2[] {
85
- const n = points.length;
86
- const at = (i: number): Vector2 => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);
87
- const out: Vector2[] = [];
88
- const spans = closed ? n : n - 1;
89
- for (let i = 0; i < spans; i++) {
90
- const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
91
- for (let s = 0; s < SMOOTH_STEPS; s++) {
92
- const t = s / SMOOTH_STEPS;
93
- const t2 = t * t;
94
- const t3 = t2 * t;
95
- out.push({
96
- x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),
97
- y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),
98
- });
99
- }
100
- }
101
- if (!closed) out.push(points[n - 1]);
102
- return out;
103
- }
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type PaintProps,
6
+ type Vector2,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Fill as _Fill,
9
+ type FillResolved as _FillResolved,
10
+ type Node2D as _Node2D,
11
+ type Node2DProps as _Node2DProps,
12
+ type Shadow as _Shadow,
13
+ type ShadowResolved as _ShadowResolved,
14
+ type Stroke as _Stroke,
15
+ type StrokeResolved as _StrokeResolved,
16
+ } from "@motionscript/sdk";
17
+ import { PAINT, PAINT_COMMANDS } from "../kit/component";
18
+ import { PLOT_COLOR } from "../kit/furniture";
19
+ import { PLANE_ENTRY } from "../kit/graph";
20
+ import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
21
+
22
+ export interface PointsPlotProps extends PaintProps {
23
+ points?: Vector2[];
24
+ closed?: boolean;
25
+ smooth?: boolean;
26
+ }
27
+
28
+ // A shorter list is padded with its own last point, so a line grows new points out of its end.
29
+ function lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {
30
+ const count = Math.max(from.length, to.length);
31
+ if (from.length === 0 || to.length === 0) return t < 1 ? from : to;
32
+ const out = new Array<Vector2>(count);
33
+ for (let i = 0; i < count; i++) {
34
+ const a = from[Math.min(i, from.length - 1)];
35
+ const b = to[Math.min(i, to.length - 1)];
36
+ out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
37
+ }
38
+ return out;
39
+ }
40
+
41
+ /** Points on the curve between each pair of a smooth line's points. */
42
+ const SMOOTH_STEPS = 16;
43
+
44
+ const state = {
45
+ ...PAINT,
46
+ points: Types.custom<Vector2[]>([], { lerp: lerpPoints }),
47
+ /** Join the last point to the first. */
48
+ closed: Types.boolean(false, { animatable: false }),
49
+ /** Curve through the points instead of turning a corner at each. */
50
+ smooth: Types.boolean(false, { animatable: false }),
51
+ graph: PLANE_ENTRY,
52
+ };
53
+
54
+ interface PointsValues {
55
+ readonly points: Vector2[];
56
+ readonly closed: boolean;
57
+ readonly smooth: boolean;
58
+ }
59
+
60
+ /** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
61
+ export const PointsPlot = traced(component2d({
62
+ key: "pointsPlot",
63
+ state,
64
+ defaults: { stroke: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
65
+ commands: {
66
+ ...PAINT_COMMANDS,
67
+ /** Move the points to new places; extra ones grow from the line's end. */
68
+ pointsTo: { args: ["points"] },
69
+ },
70
+ measure: (_measurer, s) => inkSize(tracePoints(s, reading(s))),
71
+ // An open line has no inside, so its fill waits until it is closed.
72
+ render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),
73
+ }), tracePoints);
74
+
75
+ export type PointsPlot = NodeOf<typeof PointsPlot>;
76
+
77
+ function tracePoints(s: PointsValues, { map, shift }: MarkReading): Vector2[][] {
78
+ const pixels = s.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
79
+ if (pixels.length < 2) return [];
80
+ return [s.smooth && pixels.length > 2 ? spline(pixels, s.closed) : pixels];
81
+ }
82
+
83
+ // Catmull–Rom, so the curve passes through every point it was given.
84
+ function spline(points: readonly Vector2[], closed: boolean): Vector2[] {
85
+ const n = points.length;
86
+ const at = (i: number): Vector2 => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);
87
+ const out: Vector2[] = [];
88
+ const spans = closed ? n : n - 1;
89
+ for (let i = 0; i < spans; i++) {
90
+ const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
91
+ for (let s = 0; s < SMOOTH_STEPS; s++) {
92
+ const t = s / SMOOTH_STEPS;
93
+ const t2 = t * t;
94
+ const t3 = t2 * t;
95
+ out.push({
96
+ x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),
97
+ y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),
98
+ });
99
+ }
100
+ }
101
+ if (!closed) out.push(points[n - 1]);
102
+ return out;
103
+ }
@@ -1,160 +1,160 @@
1
- import {
2
- component2d,
3
- Types,
4
- type NodeOf,
5
- type Vector2,
6
- // Named so the emitted declaration reaches these through the sdk, not core.
7
- type Fill as _Fill,
8
- type FillResolved as _FillResolved,
9
- type Node2D as _Node2D,
10
- type Node2DProps as _Node2DProps,
11
- type Shadow as _Shadow,
12
- type ShadowResolved as _ShadowResolved,
13
- type Stroke as _Stroke,
14
- type StrokeResolved as _StrokeResolved,
15
- } from "@motionscript/sdk";
16
- import type { CoordinateMap } from "../kit/coords";
17
- import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldSampler, type FieldStroke, type FieldValues } from "../kit/field-mark";
18
- import { PLOT_COLOR } from "../kit/furniture";
19
- import { clamp } from "../kit/geometry";
20
- import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
21
-
22
- export interface StreamLinesProps extends FieldProps {
23
- spacing?: number;
24
- length?: number;
25
- window?: number;
26
- phase?: number;
27
- }
28
-
29
- const MAX_LINES = 1600;
30
- /** Steps a line takes across one spacing: what keeps a tight swirl round. */
31
- const STEPS_PER_SPACING = 8;
32
- const MAX_STEPS = 600;
33
-
34
- const state = {
35
- ...FIELD_STATE,
36
- /** How far apart the lines start, in graph units. */
37
- spacing: Types.number(1, { min: 0.01, step: 0.1 }),
38
- /** How far each line runs from where it starts, in graph units along itself. */
39
- length: Types.number(3, { min: 0, step: 0.1 }),
40
- /** The share of each line that is shown at once; one shows all of it. */
41
- window: Types.number(1, { min: 0.01, max: 1, step: 0.01 }),
42
- /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
43
- phase: Types.number(0, { step: 0.01 }),
44
- };
45
-
46
- interface LinesValues extends FieldValues {
47
- readonly spacing: number;
48
- readonly length: number;
49
- readonly window: number;
50
- readonly phase: number;
51
- }
52
-
53
- const LINES: FieldDrawing<LinesValues> = {
54
- own: (s) => `${s.spacing}|${s.length}|${s.window}|${s.phase}`,
55
- strokes: (s, map, field) => lines(s, map, field),
56
- };
57
-
58
- const traceLines = (s: LinesValues, read: MarkReading) => traceField("streamLines", LINES, s, read);
59
-
60
- /**
61
- * The lines a vector field carries a point along, one from each point of a
62
- * grid: `(-y, x)` draws circles. With a `window` below one, each line shows
63
- * only a stretch of itself, and `flowTo` sends those stretches along.
64
- */
65
- export const StreamLines = traced(component2d({
66
- key: "streamLines",
67
- state,
68
- defaults: { stroke: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
69
- commands: {
70
- ...FIELD_COMMANDS,
71
- /** Send the shown stretches along their lines: a whole number runs each line that many times over. */
72
- flowTo: { args: ["phase"] },
73
- },
74
- measure: (_measurer, s) => inkSize(traceLines(s, reading(s)).runs),
75
- render: (ctx, s) => drawField(ctx, s, traceLines(s, reading(s))),
76
- }), (s: LinesValues, read: MarkReading) => traceLines(s, read).runs);
77
-
78
- export type StreamLines = NodeOf<typeof StreamLines>;
79
-
80
- function lines(s: LinesValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
81
- const [x0, x1] = map.xRange;
82
- const [y0, y1] = map.yRange;
83
- let spacing = Math.max(s.spacing, 1e-6);
84
- const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
85
- while (count() > MAX_LINES) spacing *= 2;
86
- const step = spacing / STEPS_PER_SPACING;
87
- const steps = Math.min(MAX_STEPS, Math.round(s.length / step));
88
- if (steps < 1) return [];
89
- // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
90
- const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
91
- const window = clamp(s.window, 0, 1);
92
-
93
- const drawn: FieldStroke[] = [];
94
- let index = 0;
95
- for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
96
- for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
97
- const line = follow(field, i * spacing, j * spacing, step, steps, inside);
98
- index++;
99
- if (line.points.length < 2) continue;
100
- const shown = window >= 1 ? line.points : stretch(line.points, window, s.phase + stagger(index));
101
- if (shown.length < 2) continue;
102
- drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
103
- }
104
- }
105
- return drawn;
106
- }
107
-
108
- /** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
109
- function follow(
110
- field: FieldSampler,
111
- x: number,
112
- y: number,
113
- step: number,
114
- steps: number,
115
- inside: (x: number, y: number) => boolean,
116
- ): { points: Vector2[]; magnitude: number } {
117
- const points: Vector2[] = [{ x, y }];
118
- const v: Vector2 = { x: 0, y: 0 };
119
- let strength = 0;
120
- let read = 0;
121
- // The field's direction at a point, as a unit step; `false` where it has none or is at rest.
122
- const heading = (px: number, py: number, out: Vector2): boolean => {
123
- if (!field(px, py, v)) return false;
124
- const magnitude = Math.hypot(v.x, v.y);
125
- if (!(magnitude > 1e-9)) return false;
126
- out.x = v.x / magnitude;
127
- out.y = v.y / magnitude;
128
- strength += magnitude;
129
- read++;
130
- return true;
131
- };
132
- const a: Vector2 = { x: 0, y: 0 };
133
- const b: Vector2 = { x: 0, y: 0 };
134
- for (let i = 0; i < steps; i++) {
135
- // Midpoint rule: the direction half a step on is the one the whole step takes.
136
- if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b)) break;
137
- x += b.x * step;
138
- y += b.y * step;
139
- if (!inside(x, y)) break;
140
- points.push({ x, y });
141
- }
142
- return { points, magnitude: read > 0 ? strength / read : 0 };
143
- }
144
-
145
- /** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */
146
- function stretch(points: Vector2[], share: number, phase: number): Vector2[] {
147
- const turn = phase - Math.floor(phase);
148
- const end = turn * (1 + share);
149
- const from = Math.max(0, end - share);
150
- const to = Math.min(1, end);
151
- if (!(to > from)) return [];
152
- const last = points.length - 1;
153
- return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);
154
- }
155
-
156
- /** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */
157
- function stagger(index: number): number {
158
- const mixed = Math.sin(index * 12.9898) * 43758.5453;
159
- return mixed - Math.floor(mixed);
160
- }
1
+ import {
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type Vector2,
6
+ // Named so the emitted declaration reaches these through the sdk, not core.
7
+ type Fill as _Fill,
8
+ type FillResolved as _FillResolved,
9
+ type Node2D as _Node2D,
10
+ type Node2DProps as _Node2DProps,
11
+ type Shadow as _Shadow,
12
+ type ShadowResolved as _ShadowResolved,
13
+ type Stroke as _Stroke,
14
+ type StrokeResolved as _StrokeResolved,
15
+ } from "@motionscript/sdk";
16
+ import type { CoordinateMap } from "../kit/coords";
17
+ import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldSampler, type FieldStroke, type FieldValues } from "../kit/field-mark";
18
+ import { PLOT_COLOR } from "../kit/furniture";
19
+ import { clamp } from "../kit/geometry";
20
+ import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
21
+
22
+ export interface StreamLinesProps extends FieldProps {
23
+ spacing?: number;
24
+ length?: number;
25
+ window?: number;
26
+ phase?: number;
27
+ }
28
+
29
+ const MAX_LINES = 1600;
30
+ /** Steps a line takes across one spacing: what keeps a tight swirl round. */
31
+ const STEPS_PER_SPACING = 8;
32
+ const MAX_STEPS = 600;
33
+
34
+ const state = {
35
+ ...FIELD_STATE,
36
+ /** How far apart the lines start, in graph units. */
37
+ spacing: Types.number(1, { min: 0.01, step: 0.1 }),
38
+ /** How far each line runs from where it starts, in graph units along itself. */
39
+ length: Types.number(3, { min: 0, step: 0.1 }),
40
+ /** The share of each line that is shown at once; one shows all of it. */
41
+ window: Types.number(1, { min: 0.01, max: 1, step: 0.01 }),
42
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
43
+ phase: Types.number(0, { step: 0.01 }),
44
+ };
45
+
46
+ interface LinesValues extends FieldValues {
47
+ readonly spacing: number;
48
+ readonly length: number;
49
+ readonly window: number;
50
+ readonly phase: number;
51
+ }
52
+
53
+ const LINES: FieldDrawing<LinesValues> = {
54
+ own: (s) => `${s.spacing}|${s.length}|${s.window}|${s.phase}`,
55
+ strokes: (s, map, field) => lines(s, map, field),
56
+ };
57
+
58
+ const traceLines = (s: LinesValues, read: MarkReading) => traceField("streamLines", LINES, s, read);
59
+
60
+ /**
61
+ * The lines a vector field carries a point along, one from each point of a
62
+ * grid: `(-y, x)` draws circles. With a `window` below one, each line shows
63
+ * only a stretch of itself, and `flowTo` sends those stretches along.
64
+ */
65
+ export const StreamLines = traced(component2d({
66
+ key: "streamLines",
67
+ state,
68
+ defaults: { stroke: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
69
+ commands: {
70
+ ...FIELD_COMMANDS,
71
+ /** Send the shown stretches along their lines: a whole number runs each line that many times over. */
72
+ flowTo: { args: ["phase"] },
73
+ },
74
+ measure: (_measurer, s) => inkSize(traceLines(s, reading(s)).runs),
75
+ render: (ctx, s) => drawField(ctx, s, traceLines(s, reading(s))),
76
+ }), (s: LinesValues, read: MarkReading) => traceLines(s, read).runs);
77
+
78
+ export type StreamLines = NodeOf<typeof StreamLines>;
79
+
80
+ function lines(s: LinesValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
81
+ const [x0, x1] = map.xRange;
82
+ const [y0, y1] = map.yRange;
83
+ let spacing = Math.max(s.spacing, 1e-6);
84
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
85
+ while (count() > MAX_LINES) spacing *= 2;
86
+ const step = spacing / STEPS_PER_SPACING;
87
+ const steps = Math.min(MAX_STEPS, Math.round(s.length / step));
88
+ if (steps < 1) return [];
89
+ // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
90
+ const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
91
+ const window = clamp(s.window, 0, 1);
92
+
93
+ const drawn: FieldStroke[] = [];
94
+ let index = 0;
95
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
96
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
97
+ const line = follow(field, i * spacing, j * spacing, step, steps, inside);
98
+ index++;
99
+ if (line.points.length < 2) continue;
100
+ const shown = window >= 1 ? line.points : stretch(line.points, window, s.phase + stagger(index));
101
+ if (shown.length < 2) continue;
102
+ drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
103
+ }
104
+ }
105
+ return drawn;
106
+ }
107
+
108
+ /** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
109
+ function follow(
110
+ field: FieldSampler,
111
+ x: number,
112
+ y: number,
113
+ step: number,
114
+ steps: number,
115
+ inside: (x: number, y: number) => boolean,
116
+ ): { points: Vector2[]; magnitude: number } {
117
+ const points: Vector2[] = [{ x, y }];
118
+ const v: Vector2 = { x: 0, y: 0 };
119
+ let strength = 0;
120
+ let read = 0;
121
+ // The field's direction at a point, as a unit step; `false` where it has none or is at rest.
122
+ const heading = (px: number, py: number, out: Vector2): boolean => {
123
+ if (!field(px, py, v)) return false;
124
+ const magnitude = Math.hypot(v.x, v.y);
125
+ if (!(magnitude > 1e-9)) return false;
126
+ out.x = v.x / magnitude;
127
+ out.y = v.y / magnitude;
128
+ strength += magnitude;
129
+ read++;
130
+ return true;
131
+ };
132
+ const a: Vector2 = { x: 0, y: 0 };
133
+ const b: Vector2 = { x: 0, y: 0 };
134
+ for (let i = 0; i < steps; i++) {
135
+ // Midpoint rule: the direction half a step on is the one the whole step takes.
136
+ if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b)) break;
137
+ x += b.x * step;
138
+ y += b.y * step;
139
+ if (!inside(x, y)) break;
140
+ points.push({ x, y });
141
+ }
142
+ return { points, magnitude: read > 0 ? strength / read : 0 };
143
+ }
144
+
145
+ /** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */
146
+ function stretch(points: Vector2[], share: number, phase: number): Vector2[] {
147
+ const turn = phase - Math.floor(phase);
148
+ const end = turn * (1 + share);
149
+ const from = Math.max(0, end - share);
150
+ const to = Math.min(1, end);
151
+ if (!(to > from)) return [];
152
+ const last = points.length - 1;
153
+ return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);
154
+ }
155
+
156
+ /** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */
157
+ function stagger(index: number): number {
158
+ const mixed = Math.sin(index * 12.9898) * 43758.5453;
159
+ return mixed - Math.floor(mixed);
160
+ }