@motionscript/math 0.1.0-alpha.4 → 0.1.0-alpha.6

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 (233) hide show
  1. package/CHANGELOG.md +132 -92
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +52 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +59 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +43 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +58 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +24 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +28 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +37 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +30 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +38 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +30 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +107 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +34 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +38 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +34 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +31 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +99 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +69 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +126 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +51 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +92 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +47 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +56 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +55 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +140 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +159 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +70 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +57 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +131 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +58 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +182 -161
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
@@ -1,28 +1,31 @@
1
1
  import {
2
- command,
3
- node,
4
- property,
5
- strokeProperty,
6
- withdraw,
7
- type Command,
8
- type CommandArgs,
9
- type NodeConfig,
2
+ component2d,
3
+ provides,
4
+ Types,
5
+ type EntryOptions,
6
+ type NodeOf,
10
7
  type PaintProps,
11
- type Stroke,
12
8
  type Vector2,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
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,
13
16
  } from "@motionscript/sdk";
17
+ import { createBoundedCache } from "../kit/cache";
18
+ import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
14
19
  import { traceContour } from "../kit/contour";
15
20
  import type { CoordinateMap, Interval } from "../kit/coords";
16
- import type { Curve } from "../kit/curve";
17
21
  import { equationParams, parseEquation, type Equation } from "../kit/equation";
18
22
  import type { ExpressionScope } from "../kit/expression";
19
23
  import { PLOT_COLOR } from "../kit/furniture";
20
- import { stated } from "../kit/geometry";
21
- import { Mark } from "../kit/mark";
24
+ import { PLOT, plotPlane, SYSTEM, SYSTEM_ENTRY } from "../kit/graph";
25
+ import { drawMark, inkCentre, inkSize, reading, traced, type MarkReading } from "../kit/mark";
26
+ import { openPlane, placeOnMap } from "../kit/place";
22
27
  import { sampleCurve, type CurveProjection } from "../kit/sampler";
23
28
  import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
24
- import { SYSTEMS } from "../kit/placement";
25
- import { FILL_PAINT } from "../kit/withdrawn";
26
29
 
27
30
  export interface FunctionPlotProps extends PaintProps {
28
31
  expression?: string | PlotSource;
@@ -32,189 +35,170 @@ export interface FunctionPlotProps extends PaintProps {
32
35
 
33
36
  const TAU = Math.PI * 2;
34
37
 
35
- /**
36
- * The curve of an equation, drawn in the coordinate system it is a child of.
37
- * How the equation is written says what it is: see `Equation`. A mark put
38
- * inside it reads it: the area under it, a tangent, a point that rides it.
39
- */
40
- @node({
41
- key: "functionPlot",
42
- within: SYSTEMS,
43
- closed: true,
44
- parentKey: "shape",
45
- forkable: true,
46
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: true },
47
- seed: { expression: "sin(x)" },
48
- })
49
- @withdraw(...FILL_PAINT)
50
- export class FunctionPlot<P extends FunctionPlotProps = FunctionPlotProps> extends Mark<P> implements Curve {
51
- @property({ default: "", mapper: readSource, tween: lerpSource, kind: "equation" })
52
- declare expression: PlotSource;
53
-
38
+ /** A plot's entries: what a traced path is built on too. It is only a line, so it has no fill. */
39
+ export const PLOT_STATE = {
40
+ ...omit(PAINT, "fill"),
41
+ expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "equation" }),
54
42
  /**
55
43
  * The span of the free variable that is drawn. Left out, a function runs the
56
44
  * width of its system, a polar curve one turn and a parametric one from 0 to 1.
57
45
  */
58
- @property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 }) declare domain: Interval | null;
59
-
46
+ domain: Types.custom<Interval | null, Interval | null, EntryOptions & { readonly defaultMin: number; readonly defaultMax: number }>(null, { lerp: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 }),
60
47
  /**
61
48
  * The equation's named values: any name in it that is not a variable, a
62
49
  * function or a constant. One left unset is 1, and a formula can bind each
63
50
  * to a variable.
64
51
  */
65
- @property({ default: {}, tween: lerpParams }) declare params: ExpressionScope;
66
-
67
- @strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
68
- declare stroke: Stroke;
69
-
70
- constructor(props?: NodeConfig<FunctionPlot<P>, P>) {
71
- super((props ?? {}) as NodeConfig<FunctionPlot<P>, P>);
72
- }
52
+ params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
53
+ graph: SYSTEM_ENTRY,
54
+ };
73
55
 
56
+ /** A plot's own commands, beside a line's paints. */
57
+ export const PLOT_COMMANDS = {
58
+ ...omit(PAINT_COMMANDS, "fillTo"),
74
59
  /** Change the equation, the curve bending from the one into the other. */
75
- @command({ args: ["expression"] })
76
- morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<P> {
77
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
78
- }
79
-
60
+ morphTo: { args: ["expression"] as const },
80
61
  /** Animate the equation's named values. */
81
- @command({ args: ["params"] })
82
- paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<P> {
83
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
84
- }
85
-
62
+ paramsTo: { args: ["params"] as const },
86
63
  /** Animate the span that is drawn. */
87
- @command({ args: ["domain"] })
88
- domainTo(args: CommandArgs<{ domain: Interval | null }> & { duration: number }): Command<P> {
89
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
90
- }
64
+ domainTo: { args: ["domain"] as const },
65
+ };
91
66
 
92
- /**
93
- * The curve's height at graph `x`, or `NaN` where it has none or is not a
94
- * function of `x`. What a point, a tangent or an area riding this plot reads.
95
- */
96
- valueAt(x: number): number {
97
- const { source, blend } = this.expression;
98
- const shift = this.shift();
99
- const value = this._heightOf(source, x - shift.x) + shift.y;
100
- if (!blend) return value;
101
- const from = this._heightOf(blend.from, x - shift.x) + shift.y;
102
- return from + (value - from) * blend.t;
103
- }
67
+ export const PLOT_STROKE = { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } as const;
104
68
 
105
- /** The span of the free variable that is drawn: `domain`, unless a node built on this one says otherwise. */
106
- protected span(): Interval | null {
107
- return this.domain;
108
- }
109
-
110
- protected trace(map: CoordinateMap): Vector2[][] {
111
- const { area } = map;
112
- if (!Number.isFinite(area.maxX - area.minX)) return [];
113
- const { source, blend } = this.expression;
114
- const shift = this.shift();
115
- const key = [source, blend?.from, blend?.t, map.revision, this.span(), JSON.stringify(this.params), shift.x, shift.y].join("|");
116
- if (key === this._key) return this._runs;
117
- this._key = key;
118
-
119
- // A curve with no parameter to walk cannot bend into another: it changes over half way.
120
- const shown = blend && blend.t < 0.5 ? blend.from : source;
121
- if (this._equation(source)?.kind === "implicit" || (blend && this._equation(blend.from)?.kind === "implicit")) {
122
- this._runs = this._contour(shown, map, shift);
123
- return this._runs;
124
- }
69
+ /** What a plot's curve is drawn from. */
70
+ export interface PlotValues {
71
+ readonly expression: PlotSource;
72
+ readonly params: ExpressionScope;
73
+ }
125
74
 
126
- let curve = this._curve(source, map, shift);
75
+ /**
76
+ * The curve of an equation, drawn in the coordinate system it is a child of.
77
+ * How the equation is written says what it is: see `Equation`. A mark put
78
+ * inside it reads it: the area under it, a tangent, a point that rides it.
79
+ */
80
+ export const FunctionPlot = traced(component2d({
81
+ key: "functionPlot",
82
+ state: PLOT_STATE,
83
+ defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
84
+ commands: PLOT_COMMANDS,
85
+ // The marks inside it ride it, on its system's map.
86
+ provide: (s, inherited) => [provides(PLOT, plotPlane(s, inherited(SYSTEM)))],
87
+ measure: (_measurer, s) => inkSize(tracePlot(s, reading(s), s.domain)),
88
+ layout: (box, s, children) => {
89
+ const read = reading(s);
90
+ return placeOnMap(children, read.map, inkCentre(tracePlot(s, read, s.domain)), box, plotPlane(s, s.graph));
91
+ },
92
+ plane: (_box, s) => {
93
+ const read = reading(s);
94
+ return openPlane(read.map, inkCentre(tracePlot(s, read, s.domain)));
95
+ },
96
+ render: (ctx, s) => drawMark(ctx, s, tracePlot(s, reading(s), s.domain)),
97
+ }), (s: PlotValues & { readonly domain: Interval | null }, read: MarkReading) => tracePlot(s, read, s.domain));
98
+
99
+ export type FunctionPlot = NodeOf<typeof FunctionPlot>;
100
+
101
+ const traces = createBoundedCache<Vector2[][]>(256);
102
+
103
+ /** The runs a plot of `s` draws through `read`, over `span` of its free variable (all of it for `null`). */
104
+ export function tracePlot(s: PlotValues, read: MarkReading, span: Interval | null): Vector2[][] {
105
+ const { map, shift } = read;
106
+ const { area } = map;
107
+ if (!Number.isFinite(area.maxX - area.minX)) return [];
108
+ const { source, blend } = s.expression;
109
+ const key = [source, blend?.from, blend?.t, map.revision, span, JSON.stringify(s.params), shift.x, shift.y].join("|");
110
+ const known = traces.get(key);
111
+ if (known) return known;
112
+
113
+ let runs: Vector2[][];
114
+ // A curve with no parameter to walk cannot bend into another: it changes over half way.
115
+ const shown = blend && blend.t < 0.5 ? blend.from : source;
116
+ if (equationOf(source)?.kind === "implicit" || (blend && equationOf(blend.from)?.kind === "implicit")) {
117
+ runs = contour(shown, s.params, map, shift);
118
+ } else {
119
+ let curve = projectionOf(source, s.params, span, map, shift);
127
120
  if (curve && blend) {
128
- const from = this._curve(blend.from, map, shift);
121
+ const from = projectionOf(blend.from, s.params, span, map, shift);
129
122
  if (from) curve = between(from, curve, blend.t);
130
123
  }
131
124
  const width = area.maxX - area.minX;
132
- this._runs = curve
125
+ runs = curve
133
126
  ? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })
134
127
  : [];
135
- return this._runs;
136
- }
137
-
138
- private _key = "";
139
- private _runs: Vector2[][] = [];
140
-
141
- private _equation(source: string): Equation | null {
142
- const equation = parseEquation(source, equationParams(source));
143
- return equation instanceof Error ? null : equation;
144
128
  }
129
+ traces.set(key, runs);
130
+ return runs;
131
+ }
145
132
 
146
- private _heightOf(source: string, x: number): number {
147
- const equation = this._equation(source);
148
- if (equation?.kind !== "function" || equation.of !== "x") return NaN;
149
- return equation.f(x, 0, scopeFor(equationParams(source), this.params));
150
- }
133
+ function equationOf(source: string): Equation | null {
134
+ const equation = parseEquation(source, equationParams(source));
135
+ return equation instanceof Error ? null : equation;
136
+ }
151
137
 
152
- /** Where an implicit `source` holds, over what the system shows, as pixel runs. */
153
- private _contour(source: string, map: CoordinateMap, shift: Vector2): Vector2[][] {
154
- const equation = this._equation(source);
155
- if (equation?.kind !== "implicit") return [];
156
- const params = scopeFor(equationParams(source), this.params);
157
- const { area } = map;
158
- const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
159
- const { f } = equation;
160
- const runs = traceContour(
161
- (x, y) => f(x - shift.x, y - shift.y, params),
162
- map.xRange,
163
- map.yRange,
164
- cells(area.maxX - area.minX),
165
- cells(area.maxY - area.minY),
166
- );
167
- return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
168
- }
138
+ /** Where an implicit `source` holds, over what the system shows, as pixel runs. */
139
+ function contour(source: string, params: ExpressionScope, map: CoordinateMap, shift: Vector2): Vector2[][] {
140
+ const equation = equationOf(source);
141
+ if (equation?.kind !== "implicit") return [];
142
+ const scope = scopeFor(equationParams(source), params);
143
+ const { area } = map;
144
+ const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
145
+ const { f } = equation;
146
+ const runs = traceContour(
147
+ (x, y) => f(x - shift.x, y - shift.y, scope),
148
+ map.xRange,
149
+ map.yRange,
150
+ cells(area.maxX - area.minX),
151
+ cells(area.maxY - area.minY),
152
+ );
153
+ return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
154
+ }
169
155
 
170
- /** `source` as a pixel curve over a parameter running 0 to 1. */
171
- private _curve(source: string, map: CoordinateMap, shift: Vector2): CurveProjection | null {
172
- const equation = this._equation(source);
173
- if (!equation || equation.kind === "implicit") return null;
174
- const params = scopeFor(equationParams(source), this.params);
175
- const domain = this.span();
176
- switch (equation.kind) {
177
- case "function": {
178
- const horizontal = equation.of === "x";
179
- const [lo, hi] = horizontal ? map.xRange : map.yRange;
180
- const offset = horizontal ? shift.x : shift.y;
181
- // Only what the system shows is sampled, however wide the domain.
182
- const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
183
- const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
184
- if (!(b > a)) return null;
185
- const { f } = equation;
186
- return (u, out) => {
187
- const v = a + (b - a) * u;
188
- const w = f(v, 0, params);
189
- if (!Number.isFinite(w)) return false;
190
- if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);
191
- else map.toPoint(w + shift.x, v + shift.y, out);
192
- return true;
193
- };
194
- }
195
- case "polar": {
196
- const [a, b] = domain ?? [0, TAU];
197
- const { f } = equation;
198
- return (u, out) => {
199
- const theta = a + (b - a) * u;
200
- const r = f(theta, 0, params);
201
- if (!Number.isFinite(r)) return false;
202
- map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
203
- return true;
204
- };
205
- }
206
- case "parametric": {
207
- const [a, b] = domain ?? [0, 1];
208
- const { x, y } = equation;
209
- return (u, out) => {
210
- const t = a + (b - a) * u;
211
- const gx = x(t, 0, params);
212
- const gy = y(t, 0, params);
213
- if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;
214
- map.toPoint(gx + shift.x, gy + shift.y, out);
215
- return true;
216
- };
217
- }
156
+ /** `source` as a pixel curve over a parameter running 0 to 1. */
157
+ function projectionOf(source: string, params: ExpressionScope, domain: Interval | null, map: CoordinateMap, shift: Vector2): CurveProjection | null {
158
+ const equation = equationOf(source);
159
+ if (!equation || equation.kind === "implicit") return null;
160
+ const scope = scopeFor(equationParams(source), params);
161
+ switch (equation.kind) {
162
+ case "function": {
163
+ const horizontal = equation.of === "x";
164
+ const [lo, hi] = horizontal ? map.xRange : map.yRange;
165
+ const offset = horizontal ? shift.x : shift.y;
166
+ // Only what the system shows is sampled, however wide the domain.
167
+ const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
168
+ const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
169
+ if (!(b > a)) return null;
170
+ const { f } = equation;
171
+ return (u, out) => {
172
+ const v = a + (b - a) * u;
173
+ const w = f(v, 0, scope);
174
+ if (!Number.isFinite(w)) return false;
175
+ if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);
176
+ else map.toPoint(w + shift.x, v + shift.y, out);
177
+ return true;
178
+ };
179
+ }
180
+ case "polar": {
181
+ const [a, b] = domain ?? [0, TAU];
182
+ const { f } = equation;
183
+ return (u, out) => {
184
+ const theta = a + (b - a) * u;
185
+ const r = f(theta, 0, scope);
186
+ if (!Number.isFinite(r)) return false;
187
+ map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
188
+ return true;
189
+ };
190
+ }
191
+ case "parametric": {
192
+ const [a, b] = domain ?? [0, 1];
193
+ const { x, y } = equation;
194
+ return (u, out) => {
195
+ const t = a + (b - a) * u;
196
+ const gx = x(t, 0, scope);
197
+ const gy = y(t, 0, scope);
198
+ if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;
199
+ map.toPoint(gx + shift.x, gy + shift.y, out);
200
+ return true;
201
+ };
218
202
  }
219
203
  }
220
204
  }
@@ -1,20 +1,23 @@
1
1
  import {
2
- command,
3
- node,
4
- property,
5
- strokeProperty,
6
- type Command,
7
- type CommandArgs,
8
- type NodeConfig,
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
9
5
  type PaintProps,
10
- type Stroke,
11
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,
12
16
  } from "@motionscript/sdk";
13
- import type { CoordinateMap } from "../kit/coords";
14
- import { stated } from "../kit/geometry";
15
- import { Mark } from "../kit/mark";
17
+ import { PAINT, PAINT_COMMANDS } from "../kit/component";
16
18
  import { PLOT_COLOR } from "../kit/furniture";
17
- import { SYSTEMS } from "../kit/placement";
19
+ import { PLANE_ENTRY } from "../kit/graph";
20
+ import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
18
21
 
19
22
  export interface PointsPlotProps extends PaintProps {
20
23
  points?: Vector2[];
@@ -38,50 +41,43 @@ function lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {
38
41
  /** Points on the curve between each pair of a smooth line's points. */
39
42
  const SMOOTH_STEPS = 16;
40
43
 
41
- /** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
42
- @node({
43
- key: "pointsPlot",
44
- within: SYSTEMS,
45
- parentKey: "shape",
46
- forkable: true,
47
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
48
- seed: { points: [{ x: -2, y: -1 }, { x: 0, y: 1 }, { x: 2, y: 0 }] },
49
- })
50
- export class PointsPlot extends Mark<PointsPlotProps> {
51
- @property({ default: [], tween: lerpPoints }) declare points: Vector2[];
44
+ const state = {
45
+ ...PAINT,
46
+ points: Types.custom<Vector2[]>([], { lerp: lerpPoints }),
52
47
  /** Join the last point to the first. */
53
- @property({ default: false, animatable: false }) declare closed: boolean;
48
+ closed: Types.boolean(false, { animatable: false }),
54
49
  /** Curve through the points instead of turning a corner at each. */
55
- @property({ default: false, animatable: false }) declare smooth: boolean;
56
-
57
- @strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
58
- declare stroke: Stroke;
59
-
60
- constructor(props?: NodeConfig<PointsPlot, PointsPlotProps>) {
61
- super(props ?? {});
62
- }
63
-
64
- /** Move the points to new places; extra ones grow from the line's end. */
65
- @command({ args: ["points"] })
66
- pointsTo(args: CommandArgs<{ points: Vector2[] }> & { duration: number }): Command<PointsPlotProps> {
67
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
68
- }
50
+ smooth: Types.boolean(false, { animatable: false }),
51
+ graph: PLANE_ENTRY,
52
+ };
69
53
 
70
- protected override isClosed(): boolean {
71
- return this.closed;
72
- }
54
+ interface PointsValues {
55
+ readonly points: Vector2[];
56
+ readonly closed: boolean;
57
+ readonly smooth: boolean;
58
+ }
73
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))),
74
71
  // An open line has no inside, so its fill waits until it is closed.
75
- protected override paintsFill(): boolean {
76
- return this.closed;
77
- }
72
+ render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),
73
+ }), tracePoints);
78
74
 
79
- protected trace(map: CoordinateMap): Vector2[][] {
80
- const shift = this.shift();
81
- const pixels = this.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
82
- if (pixels.length < 2) return [];
83
- return [this.smooth && pixels.length > 2 ? spline(pixels, this.closed) : pixels];
84
- }
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];
85
81
  }
86
82
 
87
83
  // Catmull–Rom, so the curve passes through every point it was given.