@motionscript/math 0.1.0-alpha.5 → 0.1.0-alpha.7

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 +28 -0
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +77 -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 +94 -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 +69 -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 +56 -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 +37 -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 +45 -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 +56 -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 +47 -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 +58 -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 +48 -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 +135 -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 +55 -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 +60 -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 +54 -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 +48 -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 +140 -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 +90 -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 +159 -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 +80 -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 +140 -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 +76 -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 +88 -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 +84 -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 +214 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +163 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +108 -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 +85 -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 +176 -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 +87 -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 +187 -160
  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
package/src/kit/mark.ts CHANGED
@@ -1,131 +1,152 @@
1
1
  import {
2
2
  Graphics2D,
3
- PaintNode,
4
- type BoxBounds,
5
- type ChildPlane,
3
+ type ChildView,
6
4
  type Extents,
7
- type MeasureContext2D,
8
- type Node2D,
9
- type PaintProps,
10
- type PathCommand,
11
- type RenderContext2D,
5
+ type Fill,
6
+ type RenderContext,
7
+ type Shadow,
12
8
  type Size2D,
9
+ type Stroke,
10
+ type StrokeResolved,
13
11
  type Vector2,
14
12
  } from "@motionscript/sdk";
13
+ import { painted } from "./component";
15
14
  import { PIXEL_MAP, type CoordinateMap } from "./coords";
16
15
  import { boundsOfRuns, runsToPath } from "./geometry";
17
- import { openPlane, placeOnMap } from "./place";
16
+ import type { GraphPlane } from "./graph";
18
17
 
19
18
  /**
20
- * A node drawn in graph coordinates: a plot, an area, an arrow between two
21
- * graph points. It answers with runs of pixel points and is otherwise a shape,
22
- * so fill, stroke, shadow and `progressTo` are the inherited ones and its box
23
- * is the tight box of its ink.
19
+ * A mark is a node drawn in graph coordinates: a plot, an area, an arrow
20
+ * between two graph points. It answers with runs of pixel points and is
21
+ * otherwise a shape, so its box is the tight box of its ink.
24
22
  *
25
- * Inside a coordinate system the system lays it out and its own `x`/`y` are a
26
- * shift in graph units, which {@link shift} reports and {@link trace} applies.
27
- * Outside one a graph unit is a pixel and `x`/`y` are the usual offset.
28
- *
29
- * A mark that takes children holds them on the same map it is drawn through: a
30
- * plot holding the area under it and the point that rides it.
23
+ * Inside a coordinate system (or a plot) the holder lays it out on that ink,
24
+ * calling the mark's own trace with its map, and the mark's `x`/`y` are a shift
25
+ * in graph units. Outside one a graph unit is a pixel and `x`/`y` are the usual
26
+ * offset.
31
27
  */
32
- export abstract class Mark<P extends PaintProps = PaintProps> extends PaintNode<P> {
33
- /** The runs this mark draws, in pixels, y-up from the system's centre. */
34
- protected abstract trace(map: CoordinateMap): Vector2[][];
35
28
 
36
- /** Whether each run is closed into an outline. */
37
- protected isClosed(): boolean {
38
- return false;
39
- }
40
-
41
- /** This mark's own `x`/`y` as a shift in graph units: zero outside a system, where they are pixels. */
42
- protected shift(): Vector2 {
43
- return this._inSystem ? { x: this.x, y: this.y } : NO_SHIFT;
44
- }
45
-
46
- /** A graph point in this mark's own frame, where a handle on it sits. */
47
- protected ownPoint(point: Vector2): Vector2 {
48
- const shift = this.shift();
49
- const at = this._map.toPoint(point.x + shift.x, point.y + shift.y);
50
- const centre = this._centre();
51
- return { x: at.x - centre.x, y: at.y - centre.y };
52
- }
53
-
54
- /** The graph point at `at` in this mark's own frame: {@link ownPoint} undone. */
55
- protected graphPoint(at: Vector2): Vector2 {
56
- const shift = this.shift();
57
- const centre = this._centre();
58
- const point = this._map.toCoords(at.x + centre.x, at.y + centre.y);
59
- return { x: point.x - shift.x, y: point.y - shift.y };
60
- }
29
+ /** What a mark is traced through: the map, its own `x`/`y` as a shift, and the plane it reads curves from. */
30
+ export interface MarkReading {
31
+ readonly map: CoordinateMap;
32
+ /** Zero outside a system, where `x`/`y` are pixels. */
33
+ readonly shift: Vector2;
34
+ readonly plane: GraphPlane | null;
35
+ }
61
36
 
62
- /** Whether the fill is painted: a mark whose ink has no inside yet (an open line) says no. */
63
- protected paintsFill(): boolean {
64
- return true;
65
- }
66
-
67
- override measure(): Partial<Size2D> {
68
- const ink = this._resolve(PIXEL_MAP, false);
69
- return ink ? { width: ink.maxX - ink.minX, height: ink.maxY - ink.minY } : { width: 0, height: 0 };
70
- }
71
-
72
- protected override shapeGraphics(): Graphics2D | null {
73
- const ink = this._ink;
74
- if (!ink || this._data.length === 0) return null;
75
- return new Graphics2D().path({
76
- data: this._data,
77
- centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY],
78
- start: this.start,
79
- end: this.end,
80
- });
81
- }
82
-
83
- // A mark's stroke is its ink, so it is painted before its children rather
84
- // than over them: a point or a tangent riding a plot sits on the curve.
85
- protected override renderSelf(ctx: RenderContext2D): void {
86
- if (this.paintsFill()) super.renderSelf(ctx);
87
- this.renderInk(ctx);
88
- }
89
-
90
- /** The stroke along the ink. A mark that strokes it differently overrides this, not `renderStroke`. */
91
- protected renderInk(ctx: RenderContext2D): void {
92
- super.renderStroke(ctx);
93
- }
94
-
95
- protected override renderStroke(): void {}
96
-
97
- protected override layoutChildren(rect: BoxBounds, scope: MeasureContext2D): void {
98
- const children = this.flowChildren();
99
- if (children.length === 0) return;
100
- const room = { x: 0, y: 0, width: rect.width, height: rect.height };
101
- this._planes = placeOnMap(children, this._map, this._centre(), room, scope);
102
- }
103
-
104
- override _planeOf(child: Node2D | null): ChildPlane | null {
105
- return child ? (this._planes.get(child) ?? null) : openPlane(this._map, this._centre());
106
- }
107
-
108
- private _data: PathCommand[] = [];
109
- private _ink: Extents | null = null;
110
- private _inSystem = false;
111
- private _map: CoordinateMap = PIXEL_MAP;
112
- private _planes = new WeakMap<Node2D, ChildPlane>();
113
-
114
- /** Its own centre in the map's pixels: where its children's boxes are placed from. */
115
- private _centre(): Vector2 {
116
- const ink = this._ink;
117
- return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
118
- }
119
-
120
- /** @internal What a system lays this mark out on: its ink, drawn through `map`. */
121
- _resolve(map: CoordinateMap, inSystem = true): Extents | null {
122
- this._inSystem = inSystem;
123
- this._map = map;
124
- const runs = this.trace(map);
125
- this._data = runsToPath(runs, this.isClosed());
126
- this._ink = boundsOfRuns(runs);
127
- return this._ink;
128
- }
37
+ /** The runs a mark draws, in pixels, y-up from the system's centre. */
38
+ export type Tracer<V> = (state: V, read: MarkReading) => Vector2[][];
39
+
40
+ /** What every mark's state reads: its place, and the plane it is laid out on. */
41
+ export interface MarkState {
42
+ readonly x: number;
43
+ readonly y: number;
44
+ readonly graph: GraphPlane | null;
129
45
  }
130
46
 
131
47
  const NO_SHIFT: Vector2 = { x: 0, y: 0 };
48
+
49
+ const tracers: { readonly is: (child: ChildView) => boolean; readonly trace: Tracer<never> }[] = [];
50
+
51
+ /** `component`, declared a mark: a holder lays it out on the ink `trace` draws. Hand back the component. */
52
+ export function traced<C extends { is(child: ChildView): boolean }>(component: C, trace: Tracer<never>): C {
53
+ tracers.push({ is: (child) => component.is(child), trace });
54
+ return component;
55
+ }
56
+
57
+ /** The runs `child` draws on `plane`, or `undefined` for a child that is no mark. */
58
+ export function markRuns(child: ChildView, map: CoordinateMap, plane: GraphPlane): Vector2[][] | undefined {
59
+ const mark = tracers.find((entry) => entry.is(child));
60
+ if (!mark) return undefined;
61
+ const state = child.state as unknown as MarkState;
62
+ return (mark.trace as Tracer<MarkState>)(state, { map, shift: { x: state.x, y: state.y }, plane });
63
+ }
64
+
65
+ /** How a mark's own fields read it: through its plane's map when one lays it out, through pixels when none does. */
66
+ export function reading(state: MarkState): MarkReading {
67
+ const plane = state.graph;
68
+ return plane ? { map: plane.map, shift: { x: state.x, y: state.y }, plane } : { map: PIXEL_MAP, shift: NO_SHIFT, plane: null };
69
+ }
70
+
71
+ /** The size of a mark's ink: its box when a node other than a system lays it out. */
72
+ export function inkSize(runs: readonly (readonly Vector2[])[]): Size2D {
73
+ const ink = boundsOfRuns(runs);
74
+ return ink ? { width: ink.maxX - ink.minX, height: ink.maxY - ink.minY } : { width: 0, height: 0 };
75
+ }
76
+
77
+ /** The centre of `runs`' ink in the map's pixels: where a mark's children are placed from. */
78
+ export function inkCentre(runs: readonly (readonly Vector2[])[]): Vector2 {
79
+ const ink = boundsOfRuns(runs);
80
+ return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
81
+ }
82
+
83
+ /** A graph point in a mark's own frame, where a handle on it sits. */
84
+ export function ownPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], point: Vector2): Vector2 {
85
+ const at = read.map.toPoint(point.x + read.shift.x, point.y + read.shift.y);
86
+ const centre = inkCentre(runs);
87
+ return { x: at.x - centre.x, y: at.y - centre.y };
88
+ }
89
+
90
+ /** The graph point at `at` in a mark's own frame: {@link ownPoint} undone. */
91
+ export function graphPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], at: Vector2): Vector2 {
92
+ const centre = inkCentre(runs);
93
+ const point = read.map.toCoords(at.x + centre.x, at.y + centre.y);
94
+ return { x: point.x - read.shift.x, y: point.y - read.shift.y };
95
+ }
96
+
97
+ /** What a mark paints with: a shape's paints, any it withdrew left out. */
98
+ export interface MarkPaint {
99
+ readonly fill?: Fill;
100
+ readonly stroke?: Stroke;
101
+ readonly shadow: Shadow;
102
+ readonly start?: number;
103
+ readonly end?: number;
104
+ }
105
+
106
+ export interface MarkOptions {
107
+ /** Whether each run is closed into an outline. */
108
+ readonly closed?: boolean;
109
+ /** Whether the fill is painted: a mark whose ink has no inside yet (an open line) says no. */
110
+ readonly fills?: boolean;
111
+ /** The stroke along the ink, when it is not the plain one. */
112
+ readonly ink?: (ctx: RenderContext, ink: Extents) => void;
113
+ }
114
+
115
+ const NONE: never[] = [];
116
+
117
+ /** One mark's outline, the same commands for each paint. */
118
+ export function markShape(runs: readonly (readonly Vector2[])[], s: MarkPaint, closed = false): (() => Graphics2D) | null {
119
+ const ink = boundsOfRuns(runs);
120
+ const data = runsToPath(runs, closed);
121
+ if (!ink || data.length === 0) return null;
122
+ const centerBounds: [number, number, number, number] = [ink.minX, ink.minY, ink.maxX, ink.maxY];
123
+ return () => new Graphics2D().path({ data, centerBounds, start: s.start ?? 0, end: s.end ?? 1 });
124
+ }
125
+
126
+ /**
127
+ * A mark's paint: the fill under the stroke along its ink, both before its
128
+ * children, so a point or a tangent riding a plot sits on the curve.
129
+ */
130
+ export function drawMark(ctx: RenderContext, s: MarkPaint, runs: readonly (readonly Vector2[])[], options: MarkOptions = {}): void {
131
+ const shape = markShape(runs, s, options.closed);
132
+ const fill = s.fill ?? NONE;
133
+ painted(ctx, () => {
134
+ if (shape && options.fills !== false) ctx.draw(shape().shadow(s.shadow).fill(fill));
135
+ const ink = options.ink ? boundsOfRuns(runs) : null;
136
+ if (ink && options.ink) options.ink(ctx, ink);
137
+ else if (shape) strokeShape(ctx, shape, s);
138
+ });
139
+ }
140
+
141
+ /** The stroke along a shape, casting the shadow when nothing fills it. */
142
+ export function strokeShape(ctx: RenderContext, shape: () => Graphics2D, s: MarkPaint): void {
143
+ const stroke = (s.stroke ?? NONE) as StrokeResolved[];
144
+ if (stroke.length === 0) return;
145
+ ctx.draw(castsShadow(s) ? shape().shadow(s.shadow).stroke(stroke) : shape().stroke(stroke));
146
+ }
147
+
148
+ /** A shadow is cast by the stroke when nothing visible fills the shape. */
149
+ export function castsShadow(s: MarkPaint): boolean {
150
+ if ((s.shadow as unknown[]).length === 0) return false;
151
+ return !((s.fill ?? NONE) as { opacity?: number }[]).some((layer) => (layer.opacity ?? 1) > 0);
152
+ }
package/src/kit/place.ts CHANGED
@@ -1,31 +1,18 @@
1
- import {
2
- flowOps,
3
- type BoxBounds,
4
- type ChildPlane,
5
- type Extents,
6
- type MeasureContext2D,
7
- type Node2D,
8
- type Vector2,
9
- } from "@motionscript/sdk";
1
+ import type { ChildPlane, ChildView, Placement2D, Size2D, Vector2 } from "@motionscript/sdk";
10
2
  import type { CoordinateMap } from "./coords";
3
+ import { boundsOfRuns } from "./geometry";
4
+ import type { GraphPlane } from "./graph";
5
+ import { markRuns } from "./mark";
11
6
 
12
- /** A child that sits somewhere other than its own `x`/`y`: a point riding a curve. */
13
- export interface Anchored {
14
- /** The graph coordinates the child is at, given its own `x`/`y`. */
15
- anchorAt(x: number, y: number): Vector2;
16
- }
17
-
18
- function isAnchored(node: object): node is Anchored {
19
- return typeof (node as Partial<Anchored>).anchorAt === "function";
20
- }
7
+ /** Where a child that sits somewhere other than its own `x`/`y` is (a point riding a curve), given its own `x`/`y`. */
8
+ export type Anchor = (x: number, y: number) => Vector2;
21
9
 
22
- /** A child drawn through a map and laid out on its ink: a mark, told apart by what it does so a copied one is too. */
23
- interface Traced {
24
- _resolve(map: CoordinateMap, inSystem?: boolean): Extents | null;
25
- }
10
+ const anchors: { readonly is: (child: ChildView) => boolean; readonly anchor: (state: never, plane: GraphPlane) => Anchor | null }[] = [];
26
11
 
27
- function isTraced(node: object): node is Traced {
28
- return typeof (node as Partial<Traced>)._resolve === "function";
12
+ /** `component`, declared to sit at `anchor` of its own `x`/`y` on a graph. Hand back the component. */
13
+ export function anchored<C extends { is(child: ChildView): boolean }>(component: C, anchor: (state: never, plane: GraphPlane) => Anchor | null): C {
14
+ anchors.push({ is: (child) => component.is(child), anchor });
15
+ return component;
29
16
  }
30
17
 
31
18
  /**
@@ -33,39 +20,29 @@ function isTraced(node: object): node is Traced {
33
20
  * system, or a plot holding the marks that read it. A mark goes on its ink;
34
21
  * anything else keeps its own size and has its `x`/`y` read as graph
35
22
  * coordinates. `origin` is the holder's own centre in the map's pixels (y-up),
36
- * since a child's box is placed from there. Returns each child's plane.
23
+ * since a child's box is placed from there.
37
24
  */
38
- export function placeOnMap(
39
- children: readonly Node2D[],
40
- map: CoordinateMap,
41
- origin: Vector2,
42
- room: BoxBounds,
43
- scope: MeasureContext2D,
44
- ): WeakMap<Node2D, ChildPlane> {
45
- const planes = new WeakMap<Node2D, ChildPlane>();
46
- const others: Node2D[] = [];
47
- for (const child of children) {
48
- if (!isTraced(child)) {
49
- others.push(child);
50
- continue;
25
+ export function placeOnMap(children: readonly ChildView[], map: CoordinateMap, origin: Vector2, room: Size2D, plane: GraphPlane): Placement2D[] {
26
+ return children.map((child) => {
27
+ const runs = markRuns(child, map, plane);
28
+ if (runs) {
29
+ const ink = boundsOfRuns(runs);
30
+ const cx = ink ? (ink.minX + ink.maxX) / 2 : origin.x;
31
+ const cy = ink ? (ink.minY + ink.maxY) / 2 : origin.y;
32
+ return {
33
+ x: cx - origin.x,
34
+ y: cy - origin.y,
35
+ width: ink ? ink.maxX - ink.minX : 0,
36
+ height: ink ? ink.maxY - ink.minY : 0,
37
+ plane: markPlane(map, cx, cy),
38
+ };
51
39
  }
52
- const ink = child._resolve(map);
53
- const cx = ink ? (ink.minX + ink.maxX) / 2 : origin.x;
54
- const cy = ink ? (ink.minY + ink.maxY) / 2 : origin.y;
55
- child.layout(
56
- { x: cx - origin.x, y: origin.y - cy, width: ink ? ink.maxX - ink.minX : 0, height: ink ? ink.maxY - ink.minY : 0 },
57
- scope,
58
- );
59
- planes.set(child, markPlane(map, cx, cy));
60
- }
61
- if (others.length === 0) return planes;
62
- const sizes = flowOps.measure({}, others, room, scope).sizes;
63
- others.forEach((child, i) => {
40
+ const size = child.measure({ width: room.width, height: room.height });
41
+ const entry = anchors.find((a) => a.is(child));
42
+ const anchor = entry ? (entry.anchor as (state: unknown, plane: GraphPlane) => Anchor | null)(child.state, plane) : null;
64
43
  // Its box is centred on the map's own centre, so its offset from there is its point.
65
- child.layout({ x: -origin.x, y: origin.y, width: sizes[i].width, height: sizes[i].height }, scope);
66
- planes.set(child, pointPlane(map, child));
44
+ return { x: -origin.x, y: -origin.y, width: size.width, height: size.height, plane: pointPlane(map, anchor) };
67
45
  });
68
- return planes;
69
46
  }
70
47
 
71
48
  /** The plane of a child that is not there yet, under a holder centred on `origin`: where a host would drop one. */
@@ -79,12 +56,11 @@ export function openPlane(map: CoordinateMap, origin: Vector2): ChildPlane {
79
56
  };
80
57
  }
81
58
 
82
- function pointPlane(map: CoordinateMap, child: Node2D): ChildPlane {
59
+ function pointPlane(map: CoordinateMap, anchor: Anchor | null): ChildPlane {
83
60
  const point: Vector2 = { x: 0, y: 0 };
84
- const anchored = isAnchored(child) ? child : null;
85
61
  return {
86
62
  toOffset: (x, y) => {
87
- const at = anchored?.anchorAt(x, y);
63
+ const at = anchor?.(x, y);
88
64
  return at ? map.toPoint(at.x, at.y, point) : map.toPoint(x, y, point);
89
65
  },
90
66
  toCoords: (x, y) => map.toCoords(x, y),
package/src/kit/system.ts CHANGED
@@ -1,99 +1,105 @@
1
1
  import {
2
- Clip,
3
- flowOps,
4
2
  Graphics2D,
5
3
  insetsOps,
6
- PaintNode,
7
- type BoxBounds,
4
+ Paint,
5
+ provides,
8
6
  type ChildPlane,
7
+ type ChildView,
9
8
  type Extents,
10
- type MeasureContext2D,
11
- type Node2D,
12
- type PaintProps,
13
- type RenderContext2D,
9
+ type InsetsResolved,
10
+ type Placement2D,
11
+ type Provided,
12
+ type RenderContext,
13
+ type Shadow,
14
+ type Fill,
14
15
  type Size2D,
15
- type SizeConstraints,
16
16
  type Vector2,
17
17
  } from "@motionscript/sdk";
18
18
  import type { CoordinateMap } from "./coords";
19
+ import { settle, SYSTEM, systemPlane } from "./graph";
19
20
  import { openPlane, placeOnMap } from "./place";
20
21
 
21
- export type { Anchored } from "./place";
22
-
23
- const NO_CONTENT: Size2D = { width: 0, height: 0 };
24
- const CENTRE: Vector2 = { x: 0, y: 0 };
25
-
26
22
  /**
27
- * A node whose children sit in its graph coordinates. A child's `x`/`y` are in
28
- * the system's units and the child keeps its own size, so anything can be put
29
- * on a graph: a dot, a label, an image. A mark is drawn through the
30
- * system's map instead and laid out on its ink.
23
+ * What a coordinate system shares: a node whose children sit in its graph
24
+ * coordinates. A child's `x`/`y` are in the system's units and the child keeps
25
+ * its own size, so anything can be put on a graph: a dot, a label, an image. A
26
+ * mark is drawn through the system's map instead and laid out on its ink.
31
27
  *
32
28
  * The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
33
29
  * the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
34
30
  * document's children would otherwise share their indices with them.
35
31
  */
36
- export abstract class CoordinateSystem<P extends PaintProps = PaintProps> extends PaintNode<P> {
37
- /** How graph values land on a plot area of this size. */
38
- protected abstract mapFor(area: Extents): CoordinateMap;
39
-
40
- /** What the system draws of itself, under its children. */
41
- protected abstract furniture(map: CoordinateMap): Graphics2D[];
42
32
 
43
- /** This system's coordinates, as last laid out. */
44
- get coordinates(): CoordinateMap {
45
- return this._map ?? (this._map = this.mapFor(this._area(this.layoutBounds)));
46
- }
33
+ const CENTRE: Vector2 = { x: 0, y: 0 };
47
34
 
48
- // A system is the size it is given: its children are placed in it, not measured into it.
49
- override measure(constraints: SizeConstraints): Partial<Size2D> {
50
- return flowOps.size(this, constraints, this.effectivePadding(), NO_CONTENT);
51
- }
35
+ /** What a system's helpers read of its state. */
36
+ export interface SystemState {
37
+ readonly padding: InsetsResolved;
38
+ readonly fill: Fill;
39
+ readonly shadow: Shadow;
40
+ readonly clip: boolean;
41
+ }
52
42
 
53
- protected override layoutChildren(rect: BoxBounds, scope: MeasureContext2D): void {
54
- const map = this.mapFor(this._area(rect));
55
- this._map = map;
56
- const inner = insetsOps.insetBounds(rect.width, rect.height, this.effectivePadding());
57
- this._planes = placeOnMap(this.flowChildren(), map, CENTRE, inner, scope);
58
- }
43
+ /** How a system's graph values land on its plot area. */
44
+ export type MapFor<S> = (area: Extents, state: S) => CoordinateMap;
59
45
 
60
- override _planeOf(child: Node2D | null): ChildPlane | null {
61
- return child ? (this._planes.get(child) ?? null) : openPlane(this.coordinates, CENTRE);
62
- }
46
+ /** The plot area: the box less its padding, y-up from the box's centre. */
47
+ export function plotArea(size: Size2D, padding: InsetsResolved): Extents {
48
+ const inner = insetsOps.insetBounds(size.width, size.height, padding);
49
+ return {
50
+ minX: inner.x - inner.width / 2,
51
+ maxX: inner.x + inner.width / 2,
52
+ minY: -inner.y - inner.height / 2,
53
+ maxY: -inner.y + inner.height / 2,
54
+ };
55
+ }
63
56
 
64
- protected override shapeGraphics(): Graphics2D | null {
65
- const r = this.layoutBounds;
66
- return new Graphics2D().rect({ x: 0, y: 0, width: r.width, height: r.height });
67
- }
57
+ /** A system's map at a laid-out `size`. */
58
+ export function systemMap<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): CoordinateMap {
59
+ return mapFor(plotArea(size, state.padding), state);
60
+ }
68
61
 
69
- protected override renderSelf(ctx: RenderContext2D): void {
70
- super.renderSelf(ctx);
71
- const map = this.coordinates;
72
- const clip = map.spills ? this.clipSelf() : null;
73
- if (clip) ctx.beginClip(clip);
74
- for (const part of this.furniture(map)) ctx.draw(part);
75
- if (clip) ctx.endClip();
76
- }
62
+ /** A system is the size it is given: its children are placed in it, not measured into it. */
63
+ export function systemSize(state: SystemState): Size2D {
64
+ const { left, right, top, bottom } = state.padding;
65
+ return { width: left + right, height: top + bottom };
66
+ }
77
67
 
78
- // The stroke is the axis line, drawn with the furniture: painted here it would land over the children.
79
- protected override renderStroke(): void {}
68
+ /** What a system hands its subtree: its plane, which its layout keeps current. */
69
+ export function systemProvides(state: object): readonly Provided[] {
70
+ return [provides(SYSTEM, systemPlane(state))];
71
+ }
80
72
 
81
- protected override clipSelf(): Clip | null {
82
- const { minX, minY, maxX, maxY } = this.coordinates.area;
83
- return new Clip().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
84
- }
73
+ /** Lay a system's children out on its map. */
74
+ export function systemLayout<S extends SystemState>(size: Size2D, state: S, children: readonly ChildView[], mapFor: MapFor<S>): Placement2D[] {
75
+ const map = systemMap(size, state, mapFor);
76
+ const plane = systemPlane(state);
77
+ settle(plane, map, children);
78
+ const inner = insetsOps.insetBounds(size.width, size.height, state.padding);
79
+ return placeOnMap(children, map, CENTRE, inner, plane);
80
+ }
85
81
 
86
- private _map: CoordinateMap | null = null;
87
- private _planes = new WeakMap<Node2D, ChildPlane>();
82
+ /** Where a child dropped on a system would sit. */
83
+ export function systemOpenPlane<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): ChildPlane {
84
+ return openPlane(systemMap(size, state, mapFor), CENTRE);
85
+ }
88
86
 
89
- /** The plot area: the box less its padding, y-up from the box's centre. */
90
- private _area(rect: BoxBounds): Extents {
91
- const inner = insetsOps.insetBounds(rect.width, rect.height, this.effectivePadding());
92
- return {
93
- minX: inner.x - inner.width / 2,
94
- maxX: inner.x + inner.width / 2,
95
- minY: -inner.y - inner.height / 2,
96
- maxY: -inner.y + inner.height / 2,
97
- };
98
- }
87
+ /**
88
+ * A system's paint: the backdrop, then the furniture under its children, kept
89
+ * in the plot area where the map spills from it, then the children, confined
90
+ * to the plot area when `clip` is set. The stroke is the axis line, drawn with
91
+ * the furniture: painted after the children it would land over them.
92
+ */
93
+ export function drawSystem(ctx: RenderContext, state: SystemState, map: CoordinateMap, furniture: (map: CoordinateMap) => Graphics2D[]): void {
94
+ const { width, height } = ctx.size;
95
+ const { minX, minY, maxX, maxY } = map.area;
96
+ const area = new Graphics2D().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
97
+ ctx.inFront(() => {
98
+ ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).shadow(state.shadow).fill(state.fill));
99
+ const parts = furniture(map);
100
+ if (map.spills) ctx.clip(area, () => ctx.draw(parts));
101
+ else ctx.draw(parts);
102
+ });
103
+ // No shape of its own to paint here; the plot area is what clips the children and what picking tests.
104
+ Paint.draw(ctx, state as unknown as Paint.PaintState, null, { clipShape: area });
99
105
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
- * What a math node built on a general shape inherits and may have no use for,
3
- * as `withdraw` takes it: a node that is only a line has no fill, an area no
4
- * outline, and one that reveals itself in its own way no outline trim.
2
+ * The paint entries and commands a math node may have no use for: a node that
3
+ * is only a line has no fill, an area no outline, and one that reveals itself
4
+ * in its own way no outline trim.
5
5
  */
6
6
  export const FILL_PAINT = ["fill", "fillTo"] as const;
7
7
  export const STROKE_PAINT = ["stroke", "strokeTo"] as const;