@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/follow.ts CHANGED
@@ -1,22 +1,24 @@
1
1
  import {
2
- command,
3
- findNode2D,
4
- frameIn,
5
- Graphics2D,
6
- PaintNode,
2
+ commandsFor,
7
3
  pathOps,
8
- property,
9
- withdraw,
4
+ Types,
10
5
  type BoxBounds,
11
- type Command,
12
- type CommandArgs,
13
- type Node2D,
6
+ type NodeView,
14
7
  type PaintProps,
15
8
  type PathCommand,
16
- type Size2D,
9
+ type RenderContext,
17
10
  type Vector2,
11
+ // Named so the emitted declaration reaches these through the sdk, not core.
12
+ type Shadow as _Shadow,
13
+ type ShadowResolved as _ShadowResolved,
14
+ type Stroke as _Stroke,
15
+ type StrokeResolved as _StrokeResolved,
18
16
  } from "@motionscript/sdk";
19
- import { FILL_PAINT } from "./withdrawn";
17
+ import { omit, PAINT, PAINT_COMMANDS } from "./component";
18
+ import { LatexMatrix } from "../tex/matrix";
19
+ import { Latex } from "../tex/node";
20
+ import { isFormula, latexPartBounds } from "../tex/typeset";
21
+ import { refId } from "./graph";
20
22
 
21
23
  /** A node with parts that can be pointed at by name: a formula's terms. */
22
24
  export interface Parted {
@@ -24,8 +26,14 @@ export interface Parted {
24
26
  partBounds(part: string, occurrence?: number): BoxBounds | null;
25
27
  }
26
28
 
27
- function isParted(node: object): node is Parted {
28
- return typeof (node as Partial<Parted>).partBounds === "function";
29
+ function isParted(node: object): boolean {
30
+ return typeof (node as Partial<Parted>).partBounds === "function" || isFormula(node);
31
+ }
32
+
33
+ /** The box of `part` in `node`: its own `partBounds` answer, or a formula component's by its source. */
34
+ function partBoundsOf(node: object, part: string, occurrence: number): BoxBounds | null {
35
+ if (typeof (node as Partial<Parted>).partBounds === "function") return (node as Parted).partBounds(part, occurrence);
36
+ return latexPartBounds(node, part, occurrence) ?? null;
29
37
  }
30
38
 
31
39
  /** What a mark is drawn on: a node by id, and one part of it where it has parts. */
@@ -41,35 +49,132 @@ export interface Retarget extends Target {
41
49
  t: number;
42
50
  }
43
51
 
52
+ export interface FollowProps extends PaintProps {
53
+ on?: string;
54
+ part?: string;
55
+ occurrence?: number;
56
+ retarget?: Retarget | null;
57
+ }
58
+
44
59
  /** A target's four corners in its mark's own frame, y-up: top left, top right, bottom right, bottom left. */
45
60
  export type Quad = [Vector2, Vector2, Vector2, Vector2];
46
61
 
62
+ const restOf = ({ on, part, occurrence }: Target): Target => ({ on, part, occurrence });
63
+
64
+ function lerpRetarget(from: Retarget | null, to: Retarget | null, t: number): Retarget | null {
65
+ if (t <= 0 || !to) return t < 1 ? from : to;
66
+ return { ...restOf(to), from: from ? restOf(from) : null, t };
67
+ }
68
+
69
+ const REF = Types.custom<string, NodeView | null>("", { kind: "nodeRef", animatable: false });
70
+ const HELD_REF = Types.custom<string, NodeView | null, { readonly private: true }>("", { kind: "nodeRef", private: true });
71
+
72
+ /**
73
+ * A mark drawn on another node, or on one part of a formula: a brace, a box,
74
+ * an underline. It has no size of its own and takes no place in a layout; it
75
+ * is drawn where its target is, wherever the two are in the tree, and its
76
+ * selection box is its ink. Its entries, and what it draws on.
77
+ */
78
+ export const FOLLOW_STATE = {
79
+ // It is only an outline.
80
+ ...omit(PAINT, "fill"),
81
+ /** The node this mark is drawn on, by id. Left empty, it is the formula the mark sits inside. */
82
+ on: REF,
83
+ /** One part of that node, by its source, where it has parts: `x^2` of a formula. */
84
+ part: Types.text("", { animatable: false }),
85
+ /** Which of the places the part is written, counted from 1. */
86
+ occurrence: Types.number(1, { min: 1, step: 1, animatable: false }),
87
+ /** Where a `retargetTo` has taken the mark, and how far along it is. Not an author's to state. */
88
+ retarget: Types.custom<Retarget | null>(null, { lerp: lerpRetarget }),
89
+ /** The nodes the retarget names, there and where it set out from, read as nodes. */
90
+ retargetOn: HELD_REF,
91
+ retargetFrom: HELD_REF,
92
+ };
93
+
94
+ export const FOLLOW_COMMANDS = {
95
+ ...omit(PAINT_COMMANDS, "fillTo"),
96
+ ...commandsFor(FOLLOW_STATE, {
97
+ /** Move the mark onto another node, or another part. */
98
+ retargetTo: {
99
+ // No defaults for the node and the part: one left out keeps the mark's own.
100
+ args: {
101
+ on: Types.custom<string | undefined>(undefined, { kind: "nodeRef" }),
102
+ part: Types.custom<string | undefined>(undefined, { kind: "text" }),
103
+ occurrence: Types.number(1, { min: 1, step: 1 }),
104
+ },
105
+ run: (timeline, s) => {
106
+ const next = timeline.args;
107
+ const held = s.retarget ?? restingOf(s);
108
+ const to: Retarget = {
109
+ on: next.on ?? held.on,
110
+ part: next.part ?? (next.on ? "" : held.part),
111
+ occurrence: next.occurrence ?? 1,
112
+ t: 1,
113
+ };
114
+ const from = s.retarget;
115
+ return timeline.animate((t) => {
116
+ const retarget = lerpRetarget(from, to, t);
117
+ return { retarget, retargetOn: retarget?.on ?? "", retargetFrom: retarget?.from?.on ?? "" };
118
+ });
119
+ },
120
+ },
121
+ }),
122
+ };
123
+
124
+ /** What a follow mark reads of its state. */
125
+ export interface FollowValues {
126
+ readonly on: NodeView | null;
127
+ readonly part: string;
128
+ readonly occurrence: number;
129
+ readonly retarget: Retarget | null;
130
+ readonly retargetOn: NodeView | null;
131
+ readonly retargetFrom: NodeView | null;
132
+ }
133
+
134
+ function restingOf(s: FollowValues): Target {
135
+ return { on: refId(s.on), part: s.part, occurrence: s.occurrence };
136
+ }
137
+
138
+ const containers = new WeakMap<object, NodeView | null>();
139
+
47
140
  /**
48
- * The node a mark is drawn on: the one `on` names, or, naming none, the nearest
49
- * node with parts the mark sits inside. Putting a box in a formula is how it is
50
- * told which.
141
+ * The formula a follow mark sits in, as its render last found it: what a mark
142
+ * naming no node is drawn on. Its bounds and handles read it from there, where
143
+ * there is no context to ask.
51
144
  */
52
- export function followed(mark: Node2D, on: string): Node2D | null {
53
- if (on !== "") {
54
- const node = findNode2D(mark, on);
55
- return node && node !== mark ? node : null;
56
- }
57
- for (let at: Node2D | null = mark.parent; at; at = at.parent) {
58
- if (isParted(at)) return at;
59
- }
60
- return null;
61
- }
62
-
63
- /** Where `target` is, as `mark` sees it; `null` when it names no node or part. */
64
- export function quadOf(mark: Node2D, target: Target): Quad | null {
65
- const node = followed(mark, target.on);
145
+ export function container(ctx: RenderContext | null, s: object): NodeView | null {
146
+ if (!ctx) return containers.get(s) ?? null;
147
+ const found = ctx.ancestor(Latex) ?? ctx.ancestor(LatexMatrix);
148
+ const parted = found && isParted(found.state) ? found : null;
149
+ containers.set(s, parted);
150
+ return parted;
151
+ }
152
+
153
+ /** The node the mark rests on: the one `on` names, or the formula it sits in. */
154
+ export function resting(s: FollowValues, parted: NodeView | null): NodeView | null {
155
+ return s.on ?? parted;
156
+ }
157
+
158
+ /** The target's corners now: its resting one, or part-way to where a `retargetTo` is taking it. */
159
+ export function followedQuad(s: FollowValues, parted: NodeView | null): Quad | null {
160
+ const state = s.retarget;
161
+ const rest = (): Quad | null => quadOf(resting(s, parted), s);
162
+ if (!state) return rest();
163
+ const to = quadOf(state.on === "" ? parted : s.retargetOn, state);
164
+ const from = state.from ? quadOf(state.from.on === "" ? parted : s.retargetFrom, state.from) : rest();
165
+ if (!from || !to || state.t >= 1) return to ?? from;
166
+ return lerpQuad(from, to, state.t);
167
+ }
168
+
169
+ /** Where `target` is, as the mark sees it; `null` when it names no node or part. */
170
+ function quadOf(node: NodeView | null, target: Omit<Target, "on">): Quad | null {
66
171
  if (!node) return null;
67
172
  const box: BoxBounds | null =
68
- target.part && isParted(node)
69
- ? node.partBounds(target.part, target.occurrence || 1)
70
- : { x: 0, y: 0, width: node.layoutBounds.width, height: node.layoutBounds.height };
173
+ target.part && isParted(node.state)
174
+ ? partBoundsOf(node.state, target.part, target.occurrence || 1)
175
+ : { x: 0, y: 0, width: node.size.width, height: node.size.height };
71
176
  if (!box) return null;
72
- const frame = frameIn(mark, node);
177
+ const frame = node.frame;
73
178
  const turn = (frame.rotation * Math.PI) / 180;
74
179
  const cos = Math.cos(turn);
75
180
  const sin = Math.sin(turn);
@@ -87,94 +192,15 @@ function lerpQuad(a: Quad, b: Quad, t: number): Quad {
87
192
  return a.map((from, i) => ({ x: from.x + (b[i].x - from.x) * t, y: from.y + (b[i].y - from.y) * t })) as Quad;
88
193
  }
89
194
 
90
- const restOf = ({ on, part, occurrence }: Target): Target => ({ on, part, occurrence });
91
-
92
- function lerpRetarget(from: Retarget | null, to: Retarget | null, t: number): Retarget | null {
93
- if (t <= 0 || !to) return t < 1 ? from : to;
94
- return { ...restOf(to), from: from ? restOf(from) : null, t };
95
- }
96
-
97
- export interface FollowProps extends PaintProps {
98
- on?: string;
99
- part?: string;
100
- occurrence?: number;
101
- retarget?: Retarget | null;
195
+ /** The box of an outline, as a follow mark's bounds: it has no size of its own, so its ink is what is selected. */
196
+ export function outlineBounds(outline: readonly PathCommand[]): BoxBounds {
197
+ const ink = pathOps.bounds(outline as PathCommand[]);
198
+ if (!ink) return { x: 0, y: 0, width: 0, height: 0 };
199
+ return { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2, width: ink.maxX - ink.minX, height: ink.maxY - ink.minY };
102
200
  }
103
201
 
104
202
  /** Every drawn point's centre frame: the mark's own centre, so its outline is where it says. */
105
- const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
106
-
107
- /**
108
- * A mark drawn on another node, or on one part of a formula: a brace, a box,
109
- * an underline. It has no size of its own and takes no place in a layout; it
110
- * is drawn where its target is, wherever the two are in the tree, and its
111
- * selection box is its ink.
112
- */
113
- @withdraw(...FILL_PAINT)
114
- export abstract class FollowMark<P extends FollowProps = FollowProps> extends PaintNode<P> {
115
- /** The node this mark is drawn on, by id. Left empty, it is the formula the mark sits inside. */
116
- @property({ default: "", kind: "nodeRef", animatable: false }) declare on: string;
117
- /** One part of that node, by its source, where it has parts: `x^2` of a formula. */
118
- @property({ default: "", animatable: false }) declare part: string;
119
- /** Which of the places the part is written, counted from 1. */
120
- @property({ default: 1, min: 1, step: 1, animatable: false }) declare occurrence: number;
121
- /** Where a `retargetTo` has taken the mark, and how far along it is. Not an author's to state. */
122
- @property({ default: null, tween: lerpRetarget }) declare retarget: Retarget | null;
123
-
124
- /** Move the mark onto another node, or another part. */
125
- @command({
126
- args: [
127
- { key: "on", kind: "nodeRef" },
128
- { key: "part", kind: "text", default: "" },
129
- { key: "occurrence", kind: "number", min: 1, step: 1, default: 1 },
130
- ],
131
- })
132
- retargetTo(args: CommandArgs<Partial<Target>> & { duration: number }): Command<P> {
133
- const next = args.data ?? {};
134
- const held = this.retarget ?? this;
135
- const to: Retarget = {
136
- on: next.on ?? held.on,
137
- part: next.part ?? (next.on ? "" : held.part),
138
- occurrence: next.occurrence ?? 1,
139
- t: 1,
140
- };
141
- return this.to({ data: { retarget: to } as Partial<P>, duration: args.duration, easing: args.easing });
142
- }
143
-
144
- /** The outline to draw around `quad`, in the mark's own frame, y-up. */
145
- protected abstract outline(quad: Quad): PathCommand[];
146
-
147
- /** The target's corners now: its resting one, or part-way to where a `retargetTo` is taking it. */
148
- protected quad(): Quad | null {
149
- const state = this.retarget;
150
- if (!state) return quadOf(this, this);
151
- const to = quadOf(this, state);
152
- const from = quadOf(this, state.from ?? this);
153
- if (!from || !to || state.t >= 1) return to ?? from;
154
- return lerpQuad(from, to, state.t);
155
- }
156
-
157
- override measure(): Partial<Size2D> {
158
- return { width: 0, height: 0 };
159
- }
160
-
161
- protected override shapeGraphics(): Graphics2D | null {
162
- const data = this._outline();
163
- if (data.length === 0) return null;
164
- return new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: this.start, end: this.end });
165
- }
166
-
167
- override _localBounds(): BoxBounds {
168
- const ink = pathOps.bounds(this._outline());
169
- if (!ink) return { x: 0, y: 0, width: 0, height: 0 };
170
- return { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2, width: ink.maxX - ink.minX, height: ink.maxY - ink.minY };
171
- }
172
-
173
- private _outline(): PathCommand[] {
174
- const quad = this.quad();
175
- return quad ? this.outline(quad) : [];
176
- }
177
- }
203
+ export const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
178
204
 
179
205
  // --- Geometry the marks share ---
180
206
 
@@ -1,4 +1,4 @@
1
- import { Fills, Graphics2D, toPathString, type AssetScope, type Color, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
1
+ import { Fills, Graphics2D, toPathString, type AssetScope, type Color, type Loader, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
2
2
  import { buildLatexPath, LatexNotLoaded, loadMathJax, MATHJAX_LOADER } from "../tex/geometry";
3
3
  import type { Interval } from "./coords";
4
4
 
@@ -37,6 +37,11 @@ export function declareFormulas(assets: AssetScope, texts: readonly string[]): v
37
37
  if (texts.some((text) => FORMULA.test(text.trim()))) assets.loader(MATHJAX_LOADER, loadMathJax, { gate: "layout" });
38
38
  }
39
39
 
40
+ /** {@link declareFormulas}, for a component's `load`. */
41
+ export function loadFormulas(loader: Loader, texts: readonly string[]): void {
42
+ if (texts.some((text) => FORMULA.test(text.trim()))) loader.task(MATHJAX_LOADER, loadMathJax, { before: "layout" });
43
+ }
44
+
40
45
  /** What draws `labels`: text in the scene's face, and a typeset formula for each one written as such. */
41
46
  export function drawLabels(labels: readonly Label[], style: LabelStyle): Graphics2D[] {
42
47
  const out: Graphics2D[] = [];
@@ -0,0 +1,116 @@
1
+ import {
2
+ createContext,
3
+ Types,
4
+ type ChildView,
5
+ type EntryOptions,
6
+ type NodeView,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Context as _Context,
9
+ } from "@motionscript/sdk";
10
+ import { PIXEL_MAP, type CoordinateMap } from "./coords";
11
+ import { CURVE_KEYS, isCurve, type CurveValues } from "./curve";
12
+
13
+ /**
14
+ * What a coordinate system hands the plots and marks inside it: the map it was
15
+ * last laid out on, the curves it holds by id, and, under a plot, that plot.
16
+ * Inherited once, so it is a live object the system's layout keeps current.
17
+ */
18
+ export interface GraphPlane {
19
+ readonly map: CoordinateMap;
20
+ readonly curves: ReadonlyMap<string, CurveValues>;
21
+ /** The curve a mark rides when it names none: the plot it sits in. */
22
+ readonly enclosing: CurveValues | null;
23
+ }
24
+
25
+ /** The plane of the nearest coordinate system: what a plot is laid out on. */
26
+ export const SYSTEM = createContext<GraphPlane | null>(null, "mathSystem");
27
+ /** The plane under a plot: the system's, riding that plot. */
28
+ export const PLOT = createContext<GraphPlane | null>(null, "mathPlot");
29
+
30
+ type Inherited = { readonly private: true };
31
+
32
+ /** A plot's plane: the system it sits in, `null` outside one. */
33
+ export const SYSTEM_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
34
+ private: true,
35
+ inherit: (use) => use(SYSTEM),
36
+ });
37
+
38
+ /** A mark's plane: the plot or the system that lays it out, `null` outside both. */
39
+ export const PLANE_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
40
+ private: true,
41
+ inherit: (use) => use(PLOT) ?? use(SYSTEM),
42
+ });
43
+
44
+ /** A reference to the plot a mark reads, by id: a host offers the plots. */
45
+ export const PLOT_REF = Types.custom<string, NodeView | null, EntryOptions & { readonly nodeTypes: readonly string[] }>("", { kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false });
46
+
47
+ const NO_CURVES: ReadonlyMap<string, CurveValues> = new Map();
48
+
49
+ class SystemPlane implements GraphPlane {
50
+ map: CoordinateMap = PIXEL_MAP;
51
+ curves: ReadonlyMap<string, CurveValues> = NO_CURVES;
52
+ readonly enclosing = null;
53
+ }
54
+
55
+ class PlotPlane implements GraphPlane {
56
+ constructor(readonly parent: GraphPlane | null, readonly enclosing: CurveValues) { }
57
+
58
+ get map(): CoordinateMap {
59
+ return this.parent?.map ?? PIXEL_MAP;
60
+ }
61
+
62
+ get curves(): ReadonlyMap<string, CurveValues> {
63
+ return this.parent?.curves ?? NO_CURVES;
64
+ }
65
+ }
66
+
67
+ const systems = new WeakMap<object, SystemPlane>();
68
+ const plots = new WeakMap<object, PlotPlane>();
69
+
70
+ /** The plane a system provides, one per system: its `layout` writes the map and the curves into it. */
71
+ export function systemPlane(state: object): SystemPlane {
72
+ let plane = systems.get(state);
73
+ if (!plane) systems.set(state, (plane = new SystemPlane()));
74
+ return plane;
75
+ }
76
+
77
+ /** Lay `plane` out on `map`, holding the curves among `children`. */
78
+ export function settle(plane: SystemPlane, map: CoordinateMap, children: readonly ChildView[]): void {
79
+ plane.map = map;
80
+ const curves = new Map<string, CurveValues>();
81
+ collectCurves(children, curves);
82
+ plane.curves = curves;
83
+ }
84
+
85
+ /** The plane a plot provides the marks inside it, one per plot and system. */
86
+ export function plotPlane(plot: CurveValues, parent: GraphPlane | null): GraphPlane {
87
+ let plane = plots.get(plot);
88
+ if (!plane || plane.parent !== parent) plots.set(plot, (plane = new PlotPlane(parent, plot)));
89
+ return plane;
90
+ }
91
+
92
+ /**
93
+ * The curve a mark reads: the one `on` names in its system, or, naming none or
94
+ * one the system does not hold, the plot it sits in. `on` as the mark's own
95
+ * fields read a reference (a view) or its holder's layout does (an id). Outside
96
+ * any system a named curve is read wherever it is.
97
+ */
98
+ export function curveFor(plane: GraphPlane | null, on: unknown, enclosing = true): CurveValues | null {
99
+ if (!plane) return isCurve(on) ? on.state : null;
100
+ const id = refId(on);
101
+ return (id === "" ? undefined : plane.curves.get(id)) ?? (enclosing ? plane.enclosing : null);
102
+ }
103
+
104
+ /** The id a reference holds, read as an id or as a view of the node. */
105
+ export function refId(on: unknown): string {
106
+ if (typeof on === "string") return on;
107
+ if (typeof on === "object" && on !== null && typeof (on as { id?: unknown }).id === "string") return (on as { id: string }).id;
108
+ return "";
109
+ }
110
+
111
+ function collectCurves(children: readonly ChildView[], into: Map<string, CurveValues>): void {
112
+ for (const child of children) {
113
+ if (child.id !== "" && CURVE_KEYS.includes(child.key)) into.set(child.id, child.state as unknown as CurveValues);
114
+ collectCurves(child.children, into);
115
+ }
116
+ }