@motionscript/math 0.1.0-alpha.5 → 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 +22 -0
  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,15 +1,20 @@
1
1
  import {
2
- command,
2
+ component2d,
3
3
  Graphics2D,
4
- node,
5
- property,
6
- strokeProperty,
7
- type Command,
8
- type CommandArgs,
4
+ Types,
9
5
  type Extents,
10
- type NodeConfig,
6
+ type NodeOf,
11
7
  type Stroke,
12
8
  type StrokeResolved,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
10
+ type Color as _Color,
11
+ type Fill as _Fill,
12
+ type FillResolved as _FillResolved,
13
+ type Node2D as _Node2D,
14
+ type Node2DProps as _Node2DProps,
15
+ type NormalizedColor as _NormalizedColor,
16
+ type Shadow as _Shadow,
17
+ type ShadowResolved as _ShadowResolved,
13
18
  } from "@motionscript/sdk";
14
19
  import {
15
20
  isIdentityMatrix,
@@ -23,10 +28,10 @@ import {
23
28
  type Warp,
24
29
  } from "../kit/coords";
25
30
  import { fieldParams, parseField } from "../kit/field";
26
- import { Axes2d, type Axes2dProps, type AxisLayout } from "./axes2d";
27
31
  import { AXIS_COLOR } from "../kit/furniture";
28
- import { stated } from "../kit/geometry";
29
32
  import { lerpSource, readSource, type PlotSource } from "../kit/source";
33
+ import { drawSystem, plotArea, systemLayout, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
34
+ import { AXES, AXES_COMMANDS, axesFurniture, axesMap, axesOf, loadAxes, type Axes2dProps, type AxesValues, type AxisLayout } from "./axes2d";
30
35
 
31
36
  export interface NumberPlaneProps extends Axes2dProps {
32
37
  grid?: boolean;
@@ -38,72 +43,91 @@ export interface NumberPlaneProps extends Axes2dProps {
38
43
  ghost?: boolean;
39
44
  }
40
45
 
41
- /**
42
- * Axes over a full grid, with square units unless told otherwise: the plane
43
- * vectors and transformations are drawn on. Its `matrix` shears, turns or
44
- * squeezes the whole plane about its origin, and its `warp` bends it by a
45
- * function of its own points; the grid, the axes and everything on the plane
46
- * go with it.
47
- */
48
- @node({
49
- key: "numberPlane",
50
- parentKey: "axes2d",
51
- forkable: true,
52
- layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
53
- seed: { width: 800, height: 500 },
54
- })
55
- export class NumberPlane extends Axes2d<NumberPlaneProps> {
56
- @property({ default: true, animatable: false }) declare grid: boolean;
46
+ const state = {
47
+ ...AXES,
48
+ grid: Types.boolean(true, { animatable: false }),
57
49
  /** The line at every tick. */
58
- @strokeProperty({ default: { weight: 1.5, fill: "#3b4658", align: "center" } }) declare gridStroke: Stroke;
50
+ gridStroke: Types.stroke({ weight: 1.5, fill: "#3b4658", align: "center" }),
59
51
  /** The lines between ticks. */
60
- @strokeProperty({ default: { weight: 1, fill: "#232b38", align: "center" } }) declare minorStroke: Stroke;
52
+ minorStroke: Types.stroke({ weight: 1, fill: "#232b38", align: "center" }),
61
53
  /** How many parts the space between two ticks is cut into; one draws no line between them. */
62
- @property({ default: 2, min: 1, max: 10, step: 1 }) declare minorDivisions: number;
63
-
54
+ minorDivisions: Types.number(2, { min: 1, max: 10, step: 1 }),
64
55
  /** A linear map of the plane, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
65
- @property({ default: [1, 0, 0, 1], mapper: readMatrix, tween: lerpMatrix }) declare matrix: Matrix2;
56
+ matrix: Types.custom<Matrix2 | readonly (readonly number[])[], Matrix2>([1, 0, 0, 1], { resolve: readMatrix, lerp: lerpMatrix }),
66
57
  /**
67
58
  * Where each point of the plane is sent, two parts in x and y:
68
59
  * `(x + sin(y), y + sin(x))`. Empty leaves the plane flat.
69
60
  */
70
- @property({ default: "", mapper: readSource, tween: lerpSource, kind: "fieldEquation" }) declare warp: PlotSource;
61
+ warp: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
71
62
  /** Leave the plane as it was, faintly, behind one that has been transformed or warped. */
72
- @property({ default: true, animatable: false }) declare ghost: boolean;
63
+ ghost: Types.boolean(true, { animatable: false }),
64
+ };
73
65
 
74
- @property({ default: true, animatable: false }) declare squareUnits: boolean;
75
- @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center" } }) declare stroke: Stroke;
66
+ type PlaneValues = AxesValues & {
67
+ readonly grid: boolean;
68
+ readonly gridStroke: StrokeResolved[];
69
+ readonly minorStroke: StrokeResolved[];
70
+ readonly minorDivisions: number;
71
+ readonly matrix: Matrix2;
72
+ readonly warp: PlotSource;
73
+ readonly ghost: boolean;
74
+ };
76
75
 
77
- constructor(props?: NodeConfig<NumberPlane, NumberPlaneProps>) {
78
- super((props ?? {}) as NodeConfig<Axes2d<NumberPlaneProps>, NumberPlaneProps>);
76
+ /**
77
+ * Axes over a full grid, with square units unless told otherwise: the plane
78
+ * vectors and transformations are drawn on. Its `matrix` shears, turns or
79
+ * squeezes the whole plane about its origin, and its `warp` bends it by a
80
+ * function of its own points; the grid, the axes and everything on the plane
81
+ * go with it.
82
+ */
83
+ export const NumberPlane = component2d({
84
+ key: "numberPlane",
85
+ state,
86
+ defaults: {
87
+ squareUnits: true,
88
+ stroke: { weight: 2, fill: AXIS_COLOR, align: "center" },
79
89
  // A transformed plane reaches past its box, and so does what is on it: both are kept in unless told otherwise.
80
- if (props?.clip === undefined) this.applyProp("clip", () => this.coordinates.spills === true);
81
- }
90
+ clip: (s) => spills(s),
91
+ },
92
+ load: (loader, s) => loadAxes(loader, s),
93
+ commands: {
94
+ ...AXES_COMMANDS,
95
+ /** Send the plane through a matrix: a shear, a turn, a squeeze, a flip. */
96
+ transformTo: { args: ["matrix"] },
97
+ /** Bend the plane, each point travelling to where the warp sends it; an empty one flattens it again. */
98
+ warpTo: { args: ["warp"] },
99
+ },
100
+ provide: (s) => systemProvides(s),
101
+ measure: (_measurer, s) => systemSize(s),
102
+ layout: (box, s, children) => systemLayout(box, s, children, planeMap),
103
+ plane: (box, s) => systemOpenPlane(box, s, planeMap),
104
+ render: (ctx, s) => {
105
+ const { flat, map } = planeMaps(plotArea(ctx.size, s.padding), s);
106
+ drawSystem(ctx, s, map, (on) => axesFurniture(on, s, (x, y) => grid(s, flat, on, x, y)));
107
+ },
108
+ });
82
109
 
83
- /** Send the plane through a matrix: a shear, a turn, a squeeze, a flip. */
84
- @command({ args: ["matrix"] })
85
- transformTo(args: CommandArgs<{ matrix: Matrix2 | readonly (readonly number[])[] }> & { duration: number }): Command<NumberPlaneProps> {
86
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
87
- }
110
+ export type NumberPlane = NodeOf<typeof NumberPlane>;
88
111
 
89
- /** Bend the plane, each point travelling to where the warp sends it; an empty one flattens it again. */
90
- @command({ args: ["warp"] })
91
- warpTo(args: CommandArgs<{ warp: string }> & { duration: number }): Command<NumberPlaneProps> {
92
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
93
- }
112
+ /** The plane's map on a plot area: the flat one, sent through its matrix and its warp. */
113
+ export function planeMap(area: Extents, s: PlaneValues): CoordinateMap {
114
+ return planeMaps(area, s).map;
115
+ }
94
116
 
95
- protected override mapFor(area: Extents): CoordinateMap {
96
- const flat = super.mapFor(area);
97
- this._flat = flat;
98
- const map = isIdentityMatrix(this.matrix) ? flat : transformedMap(flat, this.matrix);
99
- const { source, blend } = this.warp;
100
- const to = warpOf(source);
101
- if (!blend) return to ? warpedMap(map, to, source) : map;
102
- // Mid-warp each point is part of the way from where the old warp had it to where the new one sends it.
103
- const from = warpOf(blend.from);
104
- if (!from && !to) return map;
105
- const start = { x: 0, y: 0 };
106
- return warpedMap(
117
+ /** The flat plane on a plot area, and the plane as its matrix and its warp have it. */
118
+ function planeMaps(area: Extents, s: PlaneValues): { flat: CoordinateMap; map: CoordinateMap } {
119
+ const flat = axesMap(area, s);
120
+ const map = isIdentityMatrix(s.matrix) ? flat : transformedMap(flat, s.matrix);
121
+ const { source, blend } = s.warp;
122
+ const to = warpOf(source);
123
+ if (!blend) return { flat, map: to ? warpedMap(map, to, source) : map };
124
+ // Mid-warp each point is part of the way from where the old warp had it to where the new one sends it.
125
+ const from = warpOf(blend.from);
126
+ if (!from && !to) return { flat, map };
127
+ const start = { x: 0, y: 0 };
128
+ return {
129
+ flat,
130
+ map: warpedMap(
107
131
  map,
108
132
  (x, y, out) => {
109
133
  if (!(from ?? STAY)(x, y, start) || !(to ?? STAY)(x, y, out)) return false;
@@ -112,74 +136,79 @@ export class NumberPlane extends Axes2d<NumberPlaneProps> {
112
136
  return true;
113
137
  },
114
138
  `${blend.from}>${source}@${blend.t}`,
115
- );
139
+ ),
140
+ };
141
+ }
142
+
143
+ /** Whether the plane reaches past its box: it is transformed or warped. */
144
+ function spills(s: PlaneValues): boolean {
145
+ if (!isIdentityMatrix(s.matrix)) return true;
146
+ const { source, blend } = s.warp;
147
+ return warpOf(source) !== null || (blend !== undefined && warpOf(blend.from) !== null);
148
+ }
149
+
150
+ /** The grid behind the axes, with the flat plane faintly behind a transformed one. */
151
+ function grid(s: PlaneValues, flat: CoordinateMap, map: CoordinateMap, x: AxisLayout, y: AxisLayout): Graphics2D[] {
152
+ if (!s.grid) return [];
153
+ const parts: Graphics2D[] = [];
154
+ if (s.ghost && flat.revision !== map.revision) {
155
+ const [gx, gy] = axesOf(flat, s);
156
+ parts.push(...ruled(s, flat, gx, gy, true));
116
157
  }
158
+ parts.push(...ruled(s, map, x, y, false));
159
+ return parts;
160
+ }
117
161
 
118
- protected override backdrop(map: CoordinateMap, x: AxisLayout, y: AxisLayout): Graphics2D[] {
119
- if (!this.grid) return [];
120
- const parts: Graphics2D[] = [];
121
- const flat = this._flat;
122
- if (this.ghost && flat && flat.revision !== map.revision) {
123
- const [gx, gy] = this.axes(flat);
124
- parts.push(...this._ruled(flat, gx, gy, true));
162
+ /** The grid of `map`: every line, or only the ones at a tick, in the fainter stroke, for a ghost. */
163
+ function ruled(s: PlaneValues, map: CoordinateMap, x: AxisLayout, y: AxisLayout, ghost: boolean): Graphics2D[] {
164
+ const { start, end } = s;
165
+ const [xMin, xMax] = map.xRange;
166
+ const [yMin, yMax] = map.yRange;
167
+ const lines = (values: readonly number[], vertical: boolean): Graphics2D => {
168
+ const g = new Graphics2D();
169
+ for (const value of values) {
170
+ g.line({
171
+ points: vertical ? lineThrough(map, value, yMin, value, yMax) : lineThrough(map, xMin, value, xMax, value),
172
+ radius: 0,
173
+ start,
174
+ end,
175
+ });
125
176
  }
126
- parts.push(...this._ruled(map, x, y, false));
177
+ return g;
178
+ };
179
+ const parts: Graphics2D[] = [];
180
+ const minor = s.minorStroke;
181
+ if (ghost) {
182
+ if (minor.length === 0) return parts;
183
+ if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(minor));
184
+ if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(minor));
127
185
  return parts;
128
186
  }
129
-
130
- private _flat: CoordinateMap | null = null;
131
-
132
- /** The grid of `map`: every line, or only the ones at a tick, in the fainter stroke, for a ghost. */
133
- private _ruled(map: CoordinateMap, x: AxisLayout, y: AxisLayout, ghost: boolean): Graphics2D[] {
134
- const { start, end } = this;
135
- const [xMin, xMax] = map.xRange;
136
- const [yMin, yMax] = map.yRange;
137
- const lines = (values: readonly number[], vertical: boolean): Graphics2D => {
138
- const g = new Graphics2D();
139
- for (const value of values) {
140
- g.line({
141
- points: vertical ? lineThrough(map, value, yMin, value, yMax) : lineThrough(map, xMin, value, xMax, value),
142
- radius: 0,
143
- start,
144
- end,
145
- });
187
+ const divisions = Math.round(s.minorDivisions);
188
+ if (minor.length > 0 && divisions > 1) {
189
+ const between = (axis: AxisLayout, [min, max]: readonly [number, number]): number[] => {
190
+ const { step, values } = axis.marks;
191
+ if (!(step > 0) || values.length === 0) return [];
192
+ const out: number[] = [];
193
+ for (let base = values[0] - step; base < max; base += step) {
194
+ for (let i = 1; i < divisions; i++) {
195
+ const value = base + (step * i) / divisions;
196
+ if (value > min && value < max) out.push(value);
197
+ }
146
198
  }
147
- return g;
199
+ return out;
148
200
  };
149
- const parts: Graphics2D[] = [];
150
- const minor = this.minorStroke as StrokeResolved[];
151
- if (ghost) {
152
- if (minor.length === 0) return parts;
153
- if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(minor));
154
- if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(minor));
155
- return parts;
156
- }
157
- const divisions = Math.round(this.minorDivisions);
158
- if (minor.length > 0 && divisions > 1) {
159
- const between = (axis: AxisLayout, [min, max]: readonly [number, number]): number[] => {
160
- const { step, values } = axis.marks;
161
- if (!(step > 0) || values.length === 0) return [];
162
- const out: number[] = [];
163
- for (let base = values[0] - step; base < max; base += step) {
164
- for (let i = 1; i < divisions; i++) {
165
- const value = base + (step * i) / divisions;
166
- if (value > min && value < max) out.push(value);
167
- }
168
- }
169
- return out;
170
- };
171
- const xs = between(x, map.xRange);
172
- const ys = between(y, map.yRange);
173
- if (xs.length > 0) parts.push(lines(xs, true).stroke(minor));
174
- if (ys.length > 0) parts.push(lines(ys, false).stroke(minor));
175
- }
176
- const major = this.gridStroke as StrokeResolved[];
177
- if (major.length > 0) {
178
- if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(major));
179
- if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(major));
180
- }
181
- return parts;
201
+ const xs = between(x, map.xRange);
202
+ const ys = between(y, map.yRange);
203
+ if (xs.length > 0) parts.push(lines(xs, true).stroke(minor));
204
+ if (ys.length > 0) parts.push(lines(ys, false).stroke(minor));
205
+ }
206
+ const major = s.gridStroke;
207
+ if (major.length > 0) {
208
+ if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(major));
209
+ if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(major));
182
210
  }
211
+ return parts;
183
212
  }
184
213
 
185
214
  const STAY: Warp = (x, y, out) => {
@@ -1,25 +1,27 @@
1
1
  import {
2
- colorProperty,
3
- command,
2
+ component2d,
4
3
  Graphics2D,
5
- node,
6
- property,
7
- strokeProperty,
8
- type AssetScope,
4
+ Types,
9
5
  type Color,
10
- type Command,
11
- type CommandArgs,
12
6
  type Extents,
13
- type NodeConfig,
7
+ type NodeOf,
14
8
  type NormalizedColor,
15
9
  type PaintProps,
16
10
  type Stroke,
17
11
  type StrokeResolved,
12
+ type Values,
13
+ // Named so the emitted declaration reaches these through the sdk, not core.
14
+ type Fill as _Fill,
15
+ type FillResolved as _FillResolved,
16
+ type Node2D as _Node2D,
17
+ type Node2DProps as _Node2DProps,
18
+ type Shadow as _Shadow,
19
+ type ShadowResolved as _ShadowResolved,
18
20
  } from "@motionscript/sdk";
21
+ import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
19
22
  import { cartesianMap, type CoordinateMap } from "../kit/coords";
20
23
  import { AXIS_COLOR, drawLabels, type Label } from "../kit/furniture";
21
- import { stated } from "../kit/geometry";
22
- import { CoordinateSystem } from "../kit/system";
24
+ import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
23
25
  import { formatTick, ticksFor } from "../kit/ticks";
24
26
 
25
27
  export interface PolarPlaneProps extends PaintProps {
@@ -37,126 +39,125 @@ export interface PolarPlaneProps extends PaintProps {
37
39
  /** How much room, in label sizes, the angles written round the outer ring take from the plane. */
38
40
  const LABEL_ROOM = 2.2;
39
41
 
40
- /**
41
- * Rings and spokes about an origin: the plane a polar curve is read on.
42
- * Children sit in its graph coordinates, x and y in the units its rings count,
43
- * so a plot of `r = 1 + cos(theta)`, a point or an arrow goes on it as on axes.
44
- */
45
- @node({
46
- key: "polarPlane",
47
- parentKey: "node",
48
- forkable: true,
49
- layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
50
- seed: { width: 560, height: 560 },
51
- })
52
- export class PolarPlane extends CoordinateSystem<PolarPlaneProps> {
42
+ const state = {
43
+ ...PAINT,
53
44
  /** The radius of the outermost ring, in graph units. */
54
- @property({ default: 4, min: 0.0001, step: 0.5 }) declare radius: number;
45
+ radius: Types.number(4, { min: 0.0001, step: 0.5 }),
55
46
  /** The spacing of the rings; zero picks a tidy one for the plane's size. */
56
- @property({ default: 0, min: 0 }) declare ringStep: number;
47
+ ringStep: Types.number(0, { min: 0 }),
57
48
  /** How many spokes divide the turn; zero draws none. */
58
- @property({ default: 12, min: 0, max: 72, step: 1, animatable: false }) declare spokes: number;
59
- @property({ default: "degrees", options: ["degrees", "radians"], animatable: false }) declare angleUnit: "degrees" | "radians";
60
-
49
+ spokes: Types.number(12, { min: 0, max: 72, step: 1, animatable: false }),
50
+ angleUnit: listed(["degrees", "radians"], "degrees", { animatable: false }),
61
51
  /** The inner rings and the spokes. */
62
- @strokeProperty({ default: { weight: 1.25, fill: "#3b4658", align: "center" } }) declare gridStroke: Stroke;
63
- /** The outer ring and the two lines through the origin. */
64
- @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center" } }) declare stroke: Stroke;
65
-
52
+ gridStroke: Types.stroke({ weight: 1.25, fill: "#3b4658", align: "center" }),
66
53
  /** A number at each ring, and an angle at each spoke. */
67
- @property({ default: true, animatable: false }) declare labels: boolean;
68
- @property({ default: 16, min: 1, unit: "px" }) declare labelSize: number;
69
- @colorProperty({ default: AXIS_COLOR }) declare labelColor: Color;
70
- @property({ default: "", animatable: false }) declare fontFamily: string;
71
-
72
- constructor(props?: NodeConfig<PolarPlane, PolarPlaneProps>) {
73
- super(props ?? {});
74
- }
54
+ labels: Types.boolean(true, { animatable: false }),
55
+ labelSize: Types.number(16, { min: 1, unit: "px" }),
56
+ labelColor: Types.color(AXIS_COLOR),
57
+ fontFamily: Types.text("", { animatable: false }),
58
+ };
75
59
 
76
- /** Animate how far out the plane reaches: a zoom about its origin. */
77
- @command({ args: ["radius"] })
78
- rangeTo(args: CommandArgs<{ radius: number }> & { duration: number }): Command<PolarPlaneProps> {
79
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
80
- }
60
+ type PolarValues = Values<typeof state>;
81
61
 
82
- override declareAssets(assets: AssetScope): void {
83
- super.declareAssets(assets);
84
- if (this.fontFamily !== "") assets.font(this.fontFamily);
85
- }
62
+ /**
63
+ * Rings and spokes about an origin: the plane a polar curve is read on.
64
+ * Children sit in its graph coordinates, x and y in the units its rings count,
65
+ * so a plot of `r = 1 + cos(theta)`, a point or an arrow goes on it as on axes.
66
+ */
67
+ export const PolarPlane = component2d({
68
+ key: "polarPlane",
69
+ state,
70
+ /** The outer ring and the two lines through the origin. */
71
+ defaults: { stroke: { weight: 2, fill: AXIS_COLOR, align: "center" } },
72
+ load: (loader, s) => {
73
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
74
+ },
75
+ commands: {
76
+ ...PAINT_COMMANDS,
77
+ /** Animate how far out the plane reaches: a zoom about its origin. */
78
+ rangeTo: { args: ["radius"] },
79
+ },
80
+ provide: (s) => systemProvides(s),
81
+ measure: (_measurer, s) => systemSize(s),
82
+ layout: (box, s, children) => systemLayout(box, s, children, polarMap),
83
+ plane: (box, s) => systemOpenPlane(box, s, polarMap),
84
+ render: (ctx, s) => drawSystem(ctx, s, systemMap(ctx.size, s, polarMap), (map) => polarFurniture(map, s)),
85
+ });
86
86
 
87
- protected mapFor(area: Extents): CoordinateMap {
88
- const width = area.maxX - area.minX;
89
- const height = area.maxY - area.minY;
90
- // The outer ring fits the shorter side, less the room its angles are written in; units are square.
91
- const reach = Math.max(1, Math.min(width, height) / 2 - (this.labels ? this.labelSize * LABEL_ROOM : 0));
92
- const unit = reach / Math.max(this.radius, 1e-9);
93
- return cartesianMap({
94
- xRange: [-width / 2 / unit, width / 2 / unit],
95
- yRange: [-height / 2 / unit, height / 2 / unit],
96
- area,
97
- });
98
- }
87
+ export type PolarPlane = NodeOf<typeof PolarPlane>;
99
88
 
100
- protected furniture(map: CoordinateMap): Graphics2D[] {
101
- const stroke = this.stroke as StrokeResolved[];
102
- const grid = this.gridStroke as StrokeResolved[];
103
- const { start, end, radius } = this;
104
- const centre = map.toPoint(0, 0);
105
- const reach = map.toPoint(radius, 0).x - centre.x;
106
- if (!(reach > 0)) return [];
107
- const at = (r: number, degrees: number) => {
108
- const angle = (degrees * Math.PI) / 180;
109
- return map.toPoint(r * Math.cos(angle), r * Math.sin(angle));
110
- };
111
- const ring = (g: Graphics2D, r: number): Graphics2D => {
112
- const size = (map.toPoint(r, 0).x - centre.x) * 2;
113
- return g.ellipse({ x: centre.x, y: centre.y, width: size, height: size, start, end });
114
- };
89
+ function polarMap(area: Extents, s: PolarValues): CoordinateMap {
90
+ const width = area.maxX - area.minX;
91
+ const height = area.maxY - area.minY;
92
+ // The outer ring fits the shorter side, less the room its angles are written in; units are square.
93
+ const reach = Math.max(1, Math.min(width, height) / 2 - (s.labels ? s.labelSize * LABEL_ROOM : 0));
94
+ const unit = reach / Math.max(s.radius, 1e-9);
95
+ return cartesianMap({
96
+ xRange: [-width / 2 / unit, width / 2 / unit],
97
+ yRange: [-height / 2 / unit, height / 2 / unit],
98
+ area,
99
+ });
100
+ }
115
101
 
116
- const parts: Graphics2D[] = [];
117
- const marks = ticksFor([0, radius], this.ringStep, reach);
118
- const rings = marks.values.filter((r) => r > 0 && r < radius * (1 - 1e-9));
119
- const count = Math.round(this.spokes);
120
- const turns = Array.from({ length: count }, (_, i) => (360 * i) / count);
102
+ function polarFurniture(map: CoordinateMap, s: PolarValues): Graphics2D[] {
103
+ const stroke = s.stroke as StrokeResolved[];
104
+ const grid = s.gridStroke as StrokeResolved[];
105
+ const { start, end, radius } = s;
106
+ const centre = map.toPoint(0, 0);
107
+ const reach = map.toPoint(radius, 0).x - centre.x;
108
+ if (!(reach > 0)) return [];
109
+ const at = (r: number, degrees: number) => {
110
+ const angle = (degrees * Math.PI) / 180;
111
+ return map.toPoint(r * Math.cos(angle), r * Math.sin(angle));
112
+ };
113
+ const ring = (g: Graphics2D, r: number): Graphics2D => {
114
+ const size = (map.toPoint(r, 0).x - centre.x) * 2;
115
+ return g.ellipse({ x: centre.x, y: centre.y, width: size, height: size, start, end });
116
+ };
121
117
 
122
- if (grid.length > 0) {
123
- const inner = new Graphics2D();
124
- for (const r of rings) ring(inner, r);
125
- // The four along the axes are drawn with them.
126
- for (const degrees of turns) {
127
- if (degrees % 90 !== 0) inner.line({ points: [centre, at(radius, degrees)], radius: 0, start, end });
128
- }
129
- if (rings.length > 0 || turns.some((degrees) => degrees % 90 !== 0)) parts.push(inner.stroke(grid));
130
- }
131
- if (stroke.length > 0) {
132
- parts.push(
133
- ring(new Graphics2D(), radius)
134
- .line({ points: [at(radius, 180), at(radius, 0)], radius: 0, start, end })
135
- .line({ points: [at(radius, 270), at(radius, 90)], radius: 0, start, end })
136
- .stroke(stroke),
137
- );
138
- }
139
- if (!this.labels) return parts;
118
+ const parts: Graphics2D[] = [];
119
+ const marks = ticksFor([0, radius], s.ringStep, reach);
120
+ const rings = marks.values.filter((r) => r > 0 && r < radius * (1 - 1e-9));
121
+ const count = Math.round(s.spokes);
122
+ const turns = Array.from({ length: count }, (_, i) => (360 * i) / count);
140
123
 
141
- const size = this.labelSize;
142
- const labels: Label[] = [];
143
- for (const r of [...rings, radius]) {
144
- const p = map.toPoint(r, 0);
145
- labels.push({ text: formatTick(r, marks.step), at: { x: p.x - size * 0.2, y: p.y - size * 0.95 }, align: "right" });
146
- }
124
+ if (grid.length > 0) {
125
+ const inner = new Graphics2D();
126
+ for (const r of rings) ring(inner, r);
127
+ // The four along the axes are drawn with them.
147
128
  for (const degrees of turns) {
148
- const p = at(radius, degrees);
149
- const cos = Math.cos((degrees * Math.PI) / 180);
150
- const sin = Math.sin((degrees * Math.PI) / 180);
151
- labels.push({
152
- text: this.angleUnit === "degrees" ? `${formatTick(degrees, 1)}°` : radians(degrees),
153
- at: { x: p.x + cos * size * 0.6, y: p.y + sin * size * 1.05 },
154
- align: cos > 0.3 ? "left" : cos < -0.3 ? "right" : "center",
155
- });
129
+ if (degrees % 90 !== 0) inner.line({ points: [centre, at(radius, degrees)], radius: 0, start, end });
156
130
  }
157
- parts.push(...drawLabels(labels, { fontSize: size, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor }));
158
- return parts;
131
+ if (rings.length > 0 || turns.some((degrees) => degrees % 90 !== 0)) parts.push(inner.stroke(grid));
132
+ }
133
+ if (stroke.length > 0) {
134
+ parts.push(
135
+ ring(new Graphics2D(), radius)
136
+ .line({ points: [at(radius, 180), at(radius, 0)], radius: 0, start, end })
137
+ .line({ points: [at(radius, 270), at(radius, 90)], radius: 0, start, end })
138
+ .stroke(stroke),
139
+ );
140
+ }
141
+ if (!s.labels) return parts;
142
+
143
+ const size = s.labelSize;
144
+ const labels: Label[] = [];
145
+ for (const r of [...rings, radius]) {
146
+ const p = map.toPoint(r, 0);
147
+ labels.push({ text: formatTick(r, marks.step), at: { x: p.x - size * 0.2, y: p.y - size * 0.95 }, align: "right" });
148
+ }
149
+ for (const degrees of turns) {
150
+ const p = at(radius, degrees);
151
+ const cos = Math.cos((degrees * Math.PI) / 180);
152
+ const sin = Math.sin((degrees * Math.PI) / 180);
153
+ labels.push({
154
+ text: s.angleUnit === "degrees" ? `${formatTick(degrees, 1)}°` : radians(degrees),
155
+ at: { x: p.x + cos * size * 0.6, y: p.y + sin * size * 1.05 },
156
+ align: cos > 0.3 ? "left" : cos < -0.3 ? "right" : "center",
157
+ });
159
158
  }
159
+ parts.push(...drawLabels(labels, { fontSize: size, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
160
+ return parts;
160
161
  }
161
162
 
162
163
  /** An angle in degrees as a fraction of π: 90 is `π/2`, 240 is `4π/3`. */
package/src/tex/index.ts CHANGED
@@ -11,4 +11,5 @@ export { defaultLatexMorph } from './tween';
11
11
  export type { AnimatedToken, LatexMorphStrategy } from './tween';
12
12
 
13
13
  /** The typeset-math node types, for a host that registers only these. */
14
- export const TEX_NODES = [Latex, LatexMatrix];
14
+ // Typed by name, so the declaration names each component rather than spelling out its type through core.
15
+ export const TEX_NODES: (typeof Latex | typeof LatexMatrix)[] = [Latex, LatexMatrix];