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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/CHANGELOG.md +132 -92
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +52 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +59 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +43 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +58 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +24 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +28 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +37 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +30 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +38 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +30 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +107 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +34 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +38 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +34 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +31 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +99 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +69 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +126 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +51 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +92 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +47 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +56 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +55 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +140 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +159 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +70 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +57 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +131 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +58 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +182 -161
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
@@ -1,17 +1,22 @@
1
1
  import {
2
- node,
3
- property,
4
- strokeProperty,
5
- withdraw,
6
- type NodeConfig,
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
5
+ type NodeView,
7
6
  type PaintProps,
8
- type Stroke,
9
7
  type Vector2,
8
+ // Named so the emitted declaration reaches these through the sdk, not core.
9
+ type Node2D as _Node2D,
10
+ type Node2DProps as _Node2DProps,
11
+ type Shadow as _Shadow,
12
+ type ShadowResolved as _ShadowResolved,
13
+ type Stroke as _Stroke,
14
+ type StrokeResolved as _StrokeResolved,
10
15
  } from "@motionscript/sdk";
11
- import type { CoordinateMap } from "../kit/coords";
12
- import { Mark } from "../kit/mark";
13
- import { curveOf, type Curve } from "../kit/curve";
14
- import { FILL_PAINT } from "../kit/withdrawn";
16
+ import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
17
+ import { curveValue, type CurveValues } from "../kit/curve";
18
+ import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
19
+ import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
15
20
 
16
21
  export interface TangentLineProps extends PaintProps {
17
22
  on?: string;
@@ -23,81 +28,92 @@ export interface TangentLineProps extends PaintProps {
23
28
  /** The step a slope is measured over, as a share of the system's width. */
24
29
  const SLOPE_STEP = 1e-4;
25
30
 
31
+ const state = {
32
+ ...omit(PAINT, "fill"),
33
+ on: PLOT_REF,
34
+ length: Types.number(260, { min: 0, unit: "px" }),
35
+ /** The second point's distance along x; zero is the tangent itself. */
36
+ dx: Types.number(0, { min: 0 }),
37
+ legs: Types.boolean(false, { animatable: false }),
38
+ graph: PLANE_ENTRY,
39
+ /** The slope drawn, in graph units: the curve's at `x`, or the secant's when there is a `dx`. A formula reads it back as `[key.slope]`. */
40
+ slope: Types.number(NaN, { private: true }),
41
+ };
42
+
43
+ interface TangentValues {
44
+ readonly x: number;
45
+ readonly on: NodeView | null;
46
+ readonly length: number;
47
+ readonly dx: number;
48
+ readonly legs: boolean;
49
+ }
50
+
26
51
  /**
27
52
  * The tangent to a plot at this node's `x`, a fixed length on screen. With a
28
53
  * `dx` it is the secant through `x` and `x + dx` instead, and `legs` draws that
29
54
  * secant's run and rise.
30
55
  */
31
- @node({
56
+ export const TangentLine = traced(component2d({
32
57
  key: "tangentLine",
33
- within: ["functionPlot"],
34
- parentKey: "shape",
35
- forkable: true,
36
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
37
- })
38
- @withdraw(...FILL_PAINT)
39
- export class TangentLine extends Mark<TangentLineProps> {
40
- @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
41
- @property({ default: 260, min: 0, unit: "px" }) declare length: number;
42
- /** The second point's distance along x; zero is the tangent itself. */
43
- @property({ default: 0, min: 0 }) declare dx: number;
44
- @property({ default: false, animatable: false }) declare legs: boolean;
58
+ state,
59
+ defaults: {
60
+ stroke: { weight: 3, fill: "#f59e0b", align: "center", cap: "round", join: "round" },
61
+ width: "hug",
62
+ height: "hug",
63
+ slope: (s) => {
64
+ const plot = curveFor(s.graph, s.on);
65
+ return plot ? tangentSlope(plot, s.x, s.dx) : NaN;
66
+ },
67
+ },
68
+ commands: omit(PAINT_COMMANDS, "fillTo"),
69
+ measure: (_measurer, s) => inkSize(traceTangent(s, reading(s))),
70
+ render: (ctx, s) => drawMark(ctx, s, traceTangent(s, reading(s))),
71
+ }), traceTangent);
45
72
 
46
- @strokeProperty({ default: { weight: 3, fill: "#f59e0b", align: "center", cap: "round", join: "round" } })
47
- declare stroke: Stroke;
73
+ export type TangentLine = NodeOf<typeof TangentLine>;
48
74
 
49
- constructor(props?: NodeConfig<TangentLine, TangentLineProps>) {
50
- super(props ?? {});
51
- }
52
-
53
- /** The slope drawn, in graph units: the curve's at `x`, or the secant's when there is a `dx`. */
54
- get slope(): number {
55
- const plot = curveOf(this, this.on);
56
- if (!plot) return NaN;
57
- if (this.dx > 0) return (plot.valueAt(this.x + this.dx) - plot.valueAt(this.x)) / this.dx;
58
- return slopeAt(plot, this.x, 1e-6 * Math.max(1, Math.abs(this.x)));
59
- }
60
-
61
- protected trace(map: CoordinateMap): Vector2[][] {
62
- const plot = curveOf(this, this.on);
63
- if (!plot) return [];
64
- const x = this.x;
65
- const y = plot.valueAt(x);
66
- if (!Number.isFinite(y)) return [];
67
- const half = this.length / 2;
75
+ /** The slope a tangent at `x` draws on `curve`, in graph units: the curve's at `x`, or the secant's to `x + dx`. */
76
+ export function tangentSlope(curve: CurveValues, x: number, dx = 0): number {
77
+ if (dx > 0) return (curveValue(curve, x + dx) - curveValue(curve, x)) / dx;
78
+ const h = 1e-6 * Math.max(1, Math.abs(x));
79
+ return (curveValue(curve, x + h) - curveValue(curve, x - h)) / (2 * h);
80
+ }
68
81
 
69
- if (this.dx > 0) {
70
- const y2 = plot.valueAt(x + this.dx);
71
- if (!Number.isFinite(y2)) return [];
72
- const a = map.toPoint(x, y);
73
- const b = map.toPoint(x + this.dx, y2);
74
- const span = Math.hypot(b.x - a.x, b.y - a.y);
75
- if (span === 0) return [];
76
- const reach = Math.max(half, span / 2 + 16);
77
- const ux = (b.x - a.x) / span;
78
- const uy = (b.y - a.y) / span;
79
- const mx = (a.x + b.x) / 2;
80
- const my = (a.y + b.y) / 2;
81
- const secant = [{ x: mx - ux * reach, y: my - uy * reach }, { x: mx + ux * reach, y: my + uy * reach }];
82
- return this.legs ? [secant, [a, map.toPoint(x + this.dx, y), b]] : [secant];
83
- }
82
+ function traceTangent(s: TangentValues, { map, plane }: MarkReading): Vector2[][] {
83
+ const plot = curveFor(plane, s.on);
84
+ if (!plot) return [];
85
+ const x = s.x;
86
+ const y = curveValue(plot, x);
87
+ if (!Number.isFinite(y)) return [];
88
+ const half = s.length / 2;
84
89
 
85
- const [xMin, xMax] = map.xRange;
86
- const h = Number.isFinite(xMax - xMin) ? (xMax - xMin) * SLOPE_STEP : 1e-4;
87
- const before = plot.valueAt(x - h);
88
- const after = plot.valueAt(x + h);
89
- if (!Number.isFinite(before) || !Number.isFinite(after)) return [];
90
- const p = map.toPoint(x, y);
91
- const a = map.toPoint(x - h, before);
92
- const b = map.toPoint(x + h, after);
90
+ if (s.dx > 0) {
91
+ const y2 = curveValue(plot, x + s.dx);
92
+ if (!Number.isFinite(y2)) return [];
93
+ const a = map.toPoint(x, y);
94
+ const b = map.toPoint(x + s.dx, y2);
93
95
  const span = Math.hypot(b.x - a.x, b.y - a.y);
94
96
  if (span === 0) return [];
97
+ const reach = Math.max(half, span / 2 + 16);
95
98
  const ux = (b.x - a.x) / span;
96
99
  const uy = (b.y - a.y) / span;
97
- return [[{ x: p.x - ux * half, y: p.y - uy * half }, { x: p.x + ux * half, y: p.y + uy * half }]];
100
+ const mx = (a.x + b.x) / 2;
101
+ const my = (a.y + b.y) / 2;
102
+ const secant = [{ x: mx - ux * reach, y: my - uy * reach }, { x: mx + ux * reach, y: my + uy * reach }];
103
+ return s.legs ? [secant, [a, map.toPoint(x + s.dx, y), b]] : [secant];
98
104
  }
99
- }
100
105
 
101
- function slopeAt(plot: Curve, x: number, h: number): number {
102
- return (plot.valueAt(x + h) - plot.valueAt(x - h)) / (2 * h);
106
+ const [xMin, xMax] = map.xRange;
107
+ const h = Number.isFinite(xMax - xMin) ? (xMax - xMin) * SLOPE_STEP : 1e-4;
108
+ const before = curveValue(plot, x - h);
109
+ const after = curveValue(plot, x + h);
110
+ if (!Number.isFinite(before) || !Number.isFinite(after)) return [];
111
+ const p = map.toPoint(x, y);
112
+ const a = map.toPoint(x - h, before);
113
+ const b = map.toPoint(x + h, after);
114
+ const span = Math.hypot(b.x - a.x, b.y - a.y);
115
+ if (span === 0) return [];
116
+ const ux = (b.x - a.x) / span;
117
+ const uy = (b.y - a.y) / span;
118
+ return [[{ x: p.x - ux * half, y: p.y - uy * half }, { x: p.x + ux * half, y: p.y + uy * half }]];
103
119
  }
@@ -1,23 +1,20 @@
1
1
  import {
2
- command,
3
- node,
4
- property,
5
- strokeProperty,
6
- vector2Property,
7
- withdraw,
8
- type Command,
9
- type CommandArgs,
10
- type NodeConfig,
11
- type NodeHandle,
2
+ component2d,
3
+ Types,
4
+ type NodeOf,
12
5
  type PaintProps,
13
- type Stroke,
14
6
  type Vector2,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Node2D as _Node2D,
9
+ type Node2DProps as _Node2DProps,
10
+ type Shadow as _Shadow,
11
+ type ShadowResolved as _ShadowResolved,
12
+ type Stroke as _Stroke,
13
+ type StrokeResolved as _StrokeResolved,
15
14
  } from "@motionscript/sdk";
16
- import type { CoordinateMap } from "../kit/coords";
17
- import { stated } from "../kit/geometry";
18
- import { Mark } from "../kit/mark";
19
- import { SYSTEMS } from "../kit/placement";
20
- import { FILL_PAINT } from "../kit/withdrawn";
15
+ import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
16
+ import { PLANE_ENTRY } from "../kit/graph";
17
+ import { drawMark, graphPoint, inkSize, ownPoint, reading, traced, type MarkReading, type MarkState } from "../kit/mark";
21
18
 
22
19
  export interface VectorArrowProps extends PaintProps {
23
20
  tail?: Vector2;
@@ -28,71 +25,74 @@ export interface VectorArrowProps extends PaintProps {
28
25
  /** Degrees of arc per drawn segment of a bent arrow. */
29
26
  const BEND_STEP = 4;
30
27
 
28
+ const state = {
29
+ ...omit(PAINT, "fill"),
30
+ tail: Types.vector2(),
31
+ head: Types.vector2({ x: 1, y: 1 }),
32
+ /** The arc the arrow sweeps, in degrees: zero is straight, positive bows to its left. */
33
+ bend: Types.number(0, { min: -180, max: 180, unit: "°" }),
34
+ graph: PLANE_ENTRY,
35
+ };
36
+
37
+ interface ArrowValues {
38
+ readonly tail: Vector2;
39
+ readonly head: Vector2;
40
+ readonly bend: number;
41
+ }
42
+
43
+ /** A handle on one end of the arrow: where the end is in its own frame, and the graph point a drop there gives. */
44
+ const end = (which: "tail" | "head") => ({
45
+ at: (s: ArrowValues & MarkState) => {
46
+ const read = reading(s);
47
+ return ownPoint(read, traceArrow(s, read), s[which]);
48
+ },
49
+ value: (at: Vector2, s: ArrowValues & MarkState) => {
50
+ const read = reading(s);
51
+ return graphPoint(read, traceArrow(s, read), at);
52
+ },
53
+ });
54
+
31
55
  /**
32
56
  * An arrow from `tail` to `head`, two graph points. Its arrowhead is the stroke's
33
57
  * `endCap`, so a plain segment, a double arrow or a dashed one is a change of
34
58
  * stroke. (`from` and `to` are not its names because every node has a `to`.)
35
59
  */
36
- @node({
60
+ export const VectorArrow = traced(component2d({
37
61
  key: "vectorArrow",
38
- within: SYSTEMS,
39
- parentKey: "shape",
40
- forkable: true,
41
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
42
- })
43
- @withdraw(...FILL_PAINT)
44
- export class VectorArrow extends Mark<VectorArrowProps> {
45
- @vector2Property() declare tail: Vector2;
46
- @vector2Property({ default: { x: 1, y: 1 } }) declare head: Vector2;
47
- /** The arc the arrow sweeps, in degrees: zero is straight, positive bows to its left. */
48
- @property({ default: 0, min: -180, max: 180, unit: "°" }) declare bend: number;
49
-
50
- @strokeProperty({ default: { weight: 4, fill: "#e5e7eb", align: "center", endCap: "triangle" } })
51
- declare stroke: Stroke;
62
+ state,
63
+ defaults: { stroke: { weight: 4, fill: "#e5e7eb", align: "center", endCap: "triangle" }, width: "hug", height: "hug" },
64
+ commands: {
65
+ ...omit(PAINT_COMMANDS, "fillTo"),
66
+ /** Move either end, in graph coordinates. */
67
+ endpointsTo: { args: ["tail", "head"] },
68
+ },
69
+ handles: { tail: end("tail"), head: end("head") },
70
+ measure: (_measurer, s) => inkSize(traceArrow(s, reading(s))),
71
+ render: (ctx, s) => drawMark(ctx, s, traceArrow(s, reading(s))),
72
+ }), traceArrow);
52
73
 
53
- constructor(props?: NodeConfig<VectorArrow, VectorArrowProps>) {
54
- super(props ?? {});
55
- }
56
-
57
- /** Move either end, in graph coordinates. */
58
- @command({ args: ["tail", "head"] })
59
- endpointsTo(args: CommandArgs<{ tail?: Vector2; head?: Vector2 }> & { duration: number }): Command<VectorArrowProps> {
60
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
61
- }
62
-
63
- protected trace(map: CoordinateMap): Vector2[][] {
64
- const shift = this.shift();
65
- const a = map.toPoint(this.tail.x + shift.x, this.tail.y + shift.y);
66
- const b = map.toPoint(this.head.x + shift.x, this.head.y + shift.y);
67
- const sweep = (this.bend * Math.PI) / 180;
68
- const chord = Math.hypot(b.x - a.x, b.y - a.y);
69
- if (Math.abs(sweep) < 1e-3 || chord === 0) return [[a, b]];
74
+ export type VectorArrow = NodeOf<typeof VectorArrow>;
70
75
 
71
- // The circle through both ends that subtends `sweep` at its centre.
72
- const radius = chord / (2 * Math.sin(Math.abs(sweep) / 2));
73
- const side = Math.sign(sweep);
74
- const reach = radius * Math.cos(sweep / 2);
75
- // The centre sits to the arrow's right for a positive sweep, so the arc bows to its left.
76
- const cx = (a.x + b.x) / 2 + (side * (b.y - a.y) * reach) / chord;
77
- const cy = (a.y + b.y) / 2 - (side * (b.x - a.x) * reach) / chord;
78
- const from = Math.atan2(a.y - cy, a.x - cx);
79
- const steps = Math.max(8, Math.ceil(Math.abs(this.bend) / BEND_STEP));
80
- const run: Vector2[] = [];
81
- for (let i = 0; i <= steps; i++) {
82
- const angle = from - (sweep * i) / steps;
83
- run.push({ x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
84
- }
85
- return [run];
86
- }
87
-
88
- override _handles(): readonly NodeHandle[] {
89
- return [
90
- { prop: "tail", at: this.ownPoint(this.tail) },
91
- { prop: "head", at: this.ownPoint(this.head) },
92
- ];
93
- }
76
+ function traceArrow(s: ArrowValues, { map, shift }: MarkReading): Vector2[][] {
77
+ const a = map.toPoint(s.tail.x + shift.x, s.tail.y + shift.y);
78
+ const b = map.toPoint(s.head.x + shift.x, s.head.y + shift.y);
79
+ const sweep = (s.bend * Math.PI) / 180;
80
+ const chord = Math.hypot(b.x - a.x, b.y - a.y);
81
+ if (Math.abs(sweep) < 1e-3 || chord === 0) return [[a, b]];
94
82
 
95
- override _handleValue(prop: string, at: Vector2): Vector2 | null {
96
- return prop === "tail" || prop === "head" ? this.graphPoint(at) : null;
83
+ // The circle through both ends that subtends `sweep` at its centre.
84
+ const radius = chord / (2 * Math.sin(Math.abs(sweep) / 2));
85
+ const side = Math.sign(sweep);
86
+ const reach = radius * Math.cos(sweep / 2);
87
+ // The centre sits to the arrow's right for a positive sweep, so the arc bows to its left.
88
+ const cx = (a.x + b.x) / 2 + (side * (b.y - a.y) * reach) / chord;
89
+ const cy = (a.y + b.y) / 2 - (side * (b.x - a.x) * reach) / chord;
90
+ const from = Math.atan2(a.y - cy, a.x - cx);
91
+ const steps = Math.max(8, Math.ceil(Math.abs(s.bend) / BEND_STEP));
92
+ const run: Vector2[] = [];
93
+ for (let i = 0; i <= steps; i++) {
94
+ const angle = from - (sweep * i) / steps;
95
+ run.push({ x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
97
96
  }
97
+ return [run];
98
98
  }
package/src/nodes.ts CHANGED
@@ -27,12 +27,48 @@ import { SurfacePlot3d } from "./three/surface-plot3d";
27
27
  import { VectorArrow3d } from "./three/vector-arrow3d";
28
28
 
29
29
  /**
30
- * Every node type this package publishes. A host registers them by handing the
31
- * array to an engine (`nodes: NODES`); `@node()` only declares each key. Class
32
- * values rather than side-effect imports, because the package is
33
- * `sideEffects: false` and a document names a type by string.
30
+ * Every node type this package publishes.
31
+ *
32
+ * A host registers these by handing the array to an engine —
33
+ * `new Engine(platform, { nodes: NODES })` — which reads each component's key.
34
+ * Defining a component only *declares* that key: nothing is registered by
35
+ * importing a module, so a registry holds exactly what its owner asked for.
36
+ *
37
+ * Listing the component **values** is also what survives bundling: this package
38
+ * declares `sideEffects: false`, and a document names a node type by string, so
39
+ * an import kept only for its side effect is exactly what gets shaken out.
34
40
  */
35
- export const NODES = [
41
+ // Typed by name, so the declaration names each component rather than spelling out its type through core.
42
+ type KitNode =
43
+ | typeof Latex
44
+ | typeof LatexMatrix
45
+ | typeof NumberLine
46
+ | typeof Axes2d
47
+ | typeof NumberPlane
48
+ | typeof FunctionPlot
49
+ | typeof PointsPlot
50
+ | typeof PlotPoint
51
+ | typeof VectorArrow
52
+ | typeof CurveArea
53
+ | typeof RiemannSum
54
+ | typeof TangentLine
55
+ | typeof DropLines
56
+ | typeof CurlyBrace
57
+ | typeof AngleMark
58
+ | typeof EmphasisMark
59
+ | typeof Axes3d
60
+ | typeof SurfacePlot3d
61
+ | typeof CurvePlot3d
62
+ | typeof PlotPoint3d
63
+ | typeof VectorArrow3d
64
+ | typeof PolarPlane
65
+ | typeof VectorField
66
+ | typeof StreamLines
67
+ | typeof TracedPath
68
+ | typeof NetworkGraph
69
+ | typeof SampleSpace;
70
+
71
+ export const NODES: KitNode[] = [
36
72
  Latex,
37
73
  LatexMatrix,
38
74
  NumberLine,