@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
@@ -1,27 +1,32 @@
1
1
  import {
2
- colorProperty,
3
- command,
2
+ component2d,
4
3
  Graphics2D,
5
- node,
6
- property,
7
- strokeProperty,
8
- type AssetScope,
4
+ Types,
9
5
  type Color,
10
- type Command,
11
- type CommandArgs,
12
6
  type Extents,
13
- type NodeConfig,
7
+ type InsetsResolved,
8
+ type Loader,
9
+ type NodeOf,
14
10
  type NormalizedColor,
15
11
  type PaintProps,
16
- type Stroke,
17
12
  type StrokeResolved,
13
+ type Values,
18
14
  type Vector2,
15
+ // Named so the emitted declaration reaches these through the sdk, not core.
16
+ type Fill as _Fill,
17
+ type FillResolved as _FillResolved,
18
+ type Node2D as _Node2D,
19
+ type Node2DProps as _Node2DProps,
20
+ type Shadow as _Shadow,
21
+ type ShadowResolved as _ShadowResolved,
22
+ type Stroke as _Stroke,
19
23
  } from "@motionscript/sdk";
24
+ import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
20
25
  import { cartesianMap, lineThrough, resolveInterval, type AxisScaling, type CoordinateMap, type Interval } from "../kit/coords";
21
- import { clamp, stated } from "../kit/geometry";
22
- import { CoordinateSystem } from "../kit/system";
26
+ import { clamp } from "../kit/geometry";
27
+ import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
23
28
  import { formatTick, ticksFor, type Ticks } from "../kit/ticks";
24
- import { along, AXIS_COLOR, declareFormulas, drawLabels, drawn, type Label, lerpInterval, readInterval, withoutTips } from "../kit/furniture";
29
+ import { along, AXIS_COLOR, drawLabels, drawn, loadFormulas, type Label, lerpInterval, readInterval, withoutTips } from "../kit/furniture";
25
30
 
26
31
  export interface Axes2dProps extends PaintProps {
27
32
  xRange?: Interval;
@@ -42,7 +47,7 @@ export interface Axes2dProps extends PaintProps {
42
47
  }
43
48
 
44
49
  /** One axis, as its furniture is drawn: its line's ends and what is marked along it. */
45
- interface AxisLayout {
50
+ export interface AxisLayout {
46
51
  from: Vector2;
47
52
  to: Vector2;
48
53
  /** The pixel points that draw its line: its two ends, or more where the system's map bends it. */
@@ -54,164 +59,168 @@ interface AxisLayout {
54
59
  at(value: number): Vector2;
55
60
  }
56
61
 
57
- /**
58
- * An x and a y axis, crossing at the origin while it is in range and at the
59
- * nearest edge otherwise. Children sit in its graph coordinates.
60
- */
61
- @node({
62
- key: "axes2d",
63
- parentKey: "node",
64
- forkable: true,
65
- layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
66
- seed: { width: 800, height: 500, padding: 32 },
67
- })
68
- export class Axes2d<P extends Axes2dProps = Axes2dProps> extends CoordinateSystem<P> {
69
- @property({ default: [-5, 5], mapper: readInterval, tween: lerpInterval })
70
- declare xRange: Interval;
71
- @property({ default: [-3, 3], mapper: readInterval, tween: lerpInterval })
72
- declare yRange: Interval;
62
+ const interval = (initial: Interval) => Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
73
63
 
64
+ /** An x and a y axis's entries: what a number plane is built on too. */
65
+ export const AXES = {
66
+ ...PAINT,
67
+ xRange: interval([-5, 5]),
68
+ yRange: interval([-3, 3]),
74
69
  /** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
75
- @property({ default: 0, min: 0 }) declare xStep: number;
76
- @property({ default: 0, min: 0 }) declare yStep: number;
77
- @property({ default: "linear", options: ["linear", "log"], animatable: false }) declare xScaling: AxisScaling;
78
- @property({ default: "linear", options: ["linear", "log"], animatable: false }) declare yScaling: AxisScaling;
70
+ xStep: Types.number(0, { min: 0 }),
71
+ yStep: Types.number(0, { min: 0 }),
72
+ xScaling: listed(["linear", "log"], "linear", { animatable: false }),
73
+ yScaling: listed(["linear", "log"], "linear", { animatable: false }),
79
74
  /** Give a y unit the length of an x unit: `yRange` keeps its centre and takes the span that fits. */
80
- @property({ default: false, animatable: false }) declare squareUnits: boolean;
81
-
82
- @property({ default: true, animatable: false }) declare ticks: boolean;
83
- @property({ default: 10, min: 0, unit: "px" }) declare tickSize: number;
84
- @property({ default: true, animatable: false }) declare labels: boolean;
85
- @property({ default: 18, min: 1, unit: "px" }) declare labelSize: number;
86
- @colorProperty({ default: AXIS_COLOR }) declare labelColor: Color;
87
- @property({ default: "", animatable: false }) declare fontFamily: string;
75
+ squareUnits: Types.boolean(false, { animatable: false }),
76
+ ticks: Types.boolean(true, { animatable: false }),
77
+ tickSize: Types.number(10, { min: 0, unit: "px" }),
78
+ labels: Types.boolean(true, { animatable: false }),
79
+ labelSize: Types.number(18, { min: 1, unit: "px" }),
80
+ labelColor: Types.color(AXIS_COLOR),
81
+ fontFamily: Types.text("", { animatable: false }),
88
82
  /** A title at the positive end of each axis. */
89
- @property({ default: "", animatable: false }) declare xLabel: string;
90
- @property({ default: "", animatable: false }) declare yLabel: string;
91
-
92
- @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center", endCap: "triangle", capScale: 5 } })
93
- declare stroke: Stroke;
94
-
95
- constructor(props?: NodeConfig<Axes2d<P>, P>) {
96
- super((props ?? {}) as NodeConfig<Axes2d<P>, P>);
97
- }
83
+ xLabel: Types.text("", { animatable: false }),
84
+ yLabel: Types.text("", { animatable: false }),
85
+ };
98
86
 
87
+ /** The axes' own commands, beside a shape's paints. */
88
+ export const AXES_COMMANDS = {
89
+ ...PAINT_COMMANDS,
99
90
  /** Animate the window of values the axes show: a pan when a range's ends move alike, a zoom when they do not. */
100
- @command({ args: ["xRange", "yRange"] })
101
- rangeTo(args: CommandArgs<{ xRange?: Interval; yRange?: Interval }> & { duration: number }): Command<P> {
102
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
103
- }
91
+ rangeTo: { args: ["xRange", "yRange"] as const },
92
+ };
104
93
 
105
- override declareAssets(assets: AssetScope): void {
106
- super.declareAssets(assets);
107
- if (this.fontFamily !== "") assets.font(this.fontFamily);
108
- declareFormulas(assets, [this.xLabel, this.yLabel]);
109
- }
94
+ /** What axes are drawn from: their own entries, as a number plane's helpers read them too. */
95
+ export type AxesValues = Values<typeof AXES> & { readonly padding: InsetsResolved; readonly clip: boolean };
110
96
 
111
- protected mapFor(area: Extents): CoordinateMap {
112
- const xRange = resolveInterval(this.xRange, this.xScaling);
113
- let yRange = resolveInterval(this.yRange, this.yScaling);
114
- const width = area.maxX - area.minX;
115
- if (this.squareUnits && this.xScaling === "linear" && this.yScaling === "linear" && width > 0) {
116
- const span = ((xRange[1] - xRange[0]) * (area.maxY - area.minY)) / width;
117
- const centre = (yRange[0] + yRange[1]) / 2;
118
- if (span > 0) yRange = [centre - span / 2, centre + span / 2];
119
- }
120
- return cartesianMap({ xRange, yRange, area, xScaling: this.xScaling, yScaling: this.yScaling });
97
+ /** What axes declare before they are laid out: their face, and MathJax for a title written as a formula. */
98
+ export function loadAxes(loader: Loader, s: AxesValues): void {
99
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
100
+ loadFormulas(loader, [s.xLabel, s.yLabel]);
101
+ }
102
+
103
+ /** How the axes' values land on a plot area. */
104
+ export function axesMap(area: Extents, s: AxesValues): CoordinateMap {
105
+ const xRange = resolveInterval(s.xRange, s.xScaling);
106
+ let yRange = resolveInterval(s.yRange, s.yScaling);
107
+ const width = area.maxX - area.minX;
108
+ if (s.squareUnits && s.xScaling === "linear" && s.yScaling === "linear" && width > 0) {
109
+ const span = ((xRange[1] - xRange[0]) * (area.maxY - area.minY)) / width;
110
+ const centre = (yRange[0] + yRange[1]) / 2;
111
+ if (span > 0) yRange = [centre - span / 2, centre + span / 2];
121
112
  }
113
+ return cartesianMap({ xRange, yRange, area, xScaling: s.xScaling, yScaling: s.yScaling });
114
+ }
122
115
 
123
- protected furniture(map: CoordinateMap): Graphics2D[] {
124
- const stroke = this.stroke as StrokeResolved[];
125
- const { start, end } = this;
126
- const [x, y] = this.axes(map);
127
- const parts: Graphics2D[] = [...this.backdrop(map, x, y)];
128
- if (stroke.length === 0 && !this.labels) return parts;
116
+ /** The two axes as laid out on `map`: where each runs, what it marks, and where the other crosses it. */
117
+ export function axesOf(map: CoordinateMap, s: AxesValues): [AxisLayout, AxisLayout] {
118
+ const [xMin, xMax] = map.xRange;
119
+ const [yMin, yMax] = map.yRange;
120
+ // An axis sits on the other's zero, or on its near edge when zero is out of range or it is logarithmic.
121
+ const crossX = s.xScaling === "log" ? xMin : clamp(0, xMin, xMax);
122
+ const crossY = s.yScaling === "log" ? yMin : clamp(0, yMin, yMax);
123
+ const xFrom = map.toPoint(xMin, crossY);
124
+ const xTo = map.toPoint(xMax, crossY);
125
+ const yFrom = map.toPoint(crossX, yMin);
126
+ const yTo = map.toPoint(crossX, yMax);
127
+ return [
128
+ {
129
+ from: xFrom,
130
+ to: xTo,
131
+ line: lineThrough(map, xMin, crossY, xMax, crossY),
132
+ marks: ticksFor(map.xRange, s.xStep, Math.hypot(xTo.x - xFrom.x, xTo.y - xFrom.y), s.xScaling),
133
+ crossing: crossX,
134
+ at: (value) => map.toPoint(value, crossY),
135
+ },
136
+ {
137
+ from: yFrom,
138
+ to: yTo,
139
+ line: lineThrough(map, crossX, yMin, crossX, yMax),
140
+ marks: ticksFor(map.yRange, s.yStep, Math.hypot(yTo.x - yFrom.x, yTo.y - yFrom.y), s.yScaling),
141
+ crossing: crossY,
142
+ at: (value) => map.toPoint(crossX, value),
143
+ },
144
+ ];
145
+ }
129
146
 
130
- if (stroke.length > 0) {
131
- parts.push(
132
- new Graphics2D()
133
- .line({ points: x.line, radius: 0, start, end })
134
- .line({ points: y.line, radius: 0, start, end })
135
- .stroke(stroke),
136
- );
137
- }
147
+ /** What the axes draw of themselves on `map`, over what `backdrop` draws behind them (a number plane's grid). */
148
+ export function axesFurniture(
149
+ map: CoordinateMap,
150
+ s: AxesValues,
151
+ backdrop: (x: AxisLayout, y: AxisLayout) => Graphics2D[] = () => [],
152
+ ): Graphics2D[] {
153
+ const stroke = s.stroke as StrokeResolved[];
154
+ const { start, end } = s;
155
+ const [x, y] = axesOf(map, s);
156
+ const parts: Graphics2D[] = [...backdrop(x, y)];
157
+ if (stroke.length === 0 && !s.labels) return parts;
158
+
159
+ if (stroke.length > 0) {
160
+ parts.push(
161
+ new Graphics2D()
162
+ .line({ points: x.line, radius: 0, start, end })
163
+ .line({ points: y.line, radius: 0, start, end })
164
+ .stroke(stroke),
165
+ );
166
+ }
138
167
 
139
- const half = this.tickSize / 2;
140
- const gap = half + this.labelSize * 0.35;
141
- const tickMarks = new Graphics2D();
142
- const labels: Label[] = [];
143
- let ticked = false;
144
- const mark = (axis: AxisLayout, horizontal: boolean): void => {
145
- for (const value of axis.marks.values) {
146
- if (Math.abs(value - axis.crossing) < 1e-9 * Math.max(1, Math.abs(value))) continue;
147
- const at = axis.at(value);
148
- if (!drawn(along(at, axis.from, axis.to), start, end)) continue;
149
- if (this.ticks && half > 0) {
150
- tickMarks.line({
151
- points: horizontal
152
- ? [{ x: at.x, y: at.y - half }, { x: at.x, y: at.y + half }]
153
- : [{ x: at.x - half, y: at.y }, { x: at.x + half, y: at.y }],
154
- radius: 0,
155
- });
156
- ticked = true;
157
- }
158
- if (!this.labels) continue;
159
- labels.push(
160
- horizontal
161
- ? { text: formatTick(value, axis.marks.step), at: { x: at.x, y: at.y - gap - this.labelSize / 2 }, align: "center" }
162
- : { text: formatTick(value, axis.marks.step), at: { x: at.x - gap, y: at.y }, align: "right" },
163
- );
168
+ const half = s.tickSize / 2;
169
+ const gap = half + s.labelSize * 0.35;
170
+ const tickMarks = new Graphics2D();
171
+ const labels: Label[] = [];
172
+ let ticked = false;
173
+ const mark = (axis: AxisLayout, horizontal: boolean): void => {
174
+ for (const value of axis.marks.values) {
175
+ if (Math.abs(value - axis.crossing) < 1e-9 * Math.max(1, Math.abs(value))) continue;
176
+ const at = axis.at(value);
177
+ if (!drawn(along(at, axis.from, axis.to), start, end)) continue;
178
+ if (s.ticks && half > 0) {
179
+ tickMarks.line({
180
+ points: horizontal
181
+ ? [{ x: at.x, y: at.y - half }, { x: at.x, y: at.y + half }]
182
+ : [{ x: at.x - half, y: at.y }, { x: at.x + half, y: at.y }],
183
+ radius: 0,
184
+ });
185
+ ticked = true;
164
186
  }
165
- };
166
- mark(x, true);
167
- mark(y, false);
168
- if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
169
-
170
- if (this.xLabel !== "" && end >= 1) {
171
- labels.push({ text: this.xLabel, at: { x: x.to.x, y: x.to.y + gap + this.labelSize / 2 }, align: "right" });
172
- }
173
- if (this.yLabel !== "" && end >= 1) {
174
- labels.push({ text: this.yLabel, at: { x: y.to.x + gap, y: y.to.y - this.labelSize / 2 }, align: "left" });
187
+ if (!s.labels) continue;
188
+ labels.push(
189
+ horizontal
190
+ ? { text: formatTick(value, axis.marks.step), at: { x: at.x, y: at.y - gap - s.labelSize / 2 }, align: "center" }
191
+ : { text: formatTick(value, axis.marks.step), at: { x: at.x - gap, y: at.y }, align: "right" },
192
+ );
175
193
  }
176
- parts.push(...drawLabels(labels, { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor }));
177
- return parts;
178
- }
194
+ };
195
+ mark(x, true);
196
+ mark(y, false);
197
+ if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
179
198
 
180
- /** What is drawn behind the axes: nothing here, a grid on a number plane. */
181
- protected backdrop(_map: CoordinateMap, _x: AxisLayout, _y: AxisLayout): Graphics2D[] {
182
- return [];
199
+ if (s.xLabel !== "" && end >= 1) {
200
+ labels.push({ text: s.xLabel, at: { x: x.to.x, y: x.to.y + gap + s.labelSize / 2 }, align: "right" });
183
201
  }
184
-
185
- /** The two axes as laid out: where each runs, what it marks, and where the other crosses it. */
186
- protected axes(map: CoordinateMap): [AxisLayout, AxisLayout] {
187
- const [xMin, xMax] = map.xRange;
188
- const [yMin, yMax] = map.yRange;
189
- // An axis sits on the other's zero, or on its near edge when zero is out of range or it is logarithmic.
190
- const crossX = this.xScaling === "log" ? xMin : clamp(0, xMin, xMax);
191
- const crossY = this.yScaling === "log" ? yMin : clamp(0, yMin, yMax);
192
- const xFrom = map.toPoint(xMin, crossY);
193
- const xTo = map.toPoint(xMax, crossY);
194
- const yFrom = map.toPoint(crossX, yMin);
195
- const yTo = map.toPoint(crossX, yMax);
196
- return [
197
- {
198
- from: xFrom,
199
- to: xTo,
200
- line: lineThrough(map, xMin, crossY, xMax, crossY),
201
- marks: ticksFor(map.xRange, this.xStep, Math.hypot(xTo.x - xFrom.x, xTo.y - xFrom.y), this.xScaling),
202
- crossing: crossX,
203
- at: (value) => map.toPoint(value, crossY),
204
- },
205
- {
206
- from: yFrom,
207
- to: yTo,
208
- line: lineThrough(map, crossX, yMin, crossX, yMax),
209
- marks: ticksFor(map.yRange, this.yStep, Math.hypot(yTo.x - yFrom.x, yTo.y - yFrom.y), this.yScaling),
210
- crossing: crossY,
211
- at: (value) => map.toPoint(crossX, value),
212
- },
213
- ];
202
+ if (s.yLabel !== "" && end >= 1) {
203
+ labels.push({ text: s.yLabel, at: { x: y.to.x + gap, y: y.to.y - s.labelSize / 2 }, align: "left" });
214
204
  }
205
+ parts.push(...drawLabels(labels, { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
206
+ return parts;
215
207
  }
216
208
 
217
- export type { AxisLayout };
209
+ /**
210
+ * An x and a y axis, crossing at the origin while it is in range and at the
211
+ * nearest edge otherwise. Children sit in its graph coordinates.
212
+ */
213
+ export const Axes2d = component2d({
214
+ key: "axes2d",
215
+ state: AXES,
216
+ defaults: { stroke: { weight: 2, fill: AXIS_COLOR, align: "center", endCap: "triangle", capScale: 5 } },
217
+ load: (loader, s) => loadAxes(loader, s),
218
+ commands: AXES_COMMANDS,
219
+ provide: (s) => systemProvides(s),
220
+ measure: (_measurer, s) => systemSize(s),
221
+ layout: (box, s, children) => systemLayout(box, s, children, axesMap),
222
+ plane: (box, s) => systemOpenPlane(box, s, axesMap),
223
+ render: (ctx, s) => drawSystem(ctx, s, systemMap(ctx.size, s, axesMap), (map) => axesFurniture(map, s)),
224
+ });
225
+
226
+ export type Axes2d = NodeOf<typeof Axes2d>;
@@ -1,27 +1,29 @@
1
1
  import {
2
- colorProperty,
3
- command,
2
+ component2d,
4
3
  Graphics2D,
5
- node,
6
- property,
7
- strokeProperty,
8
- type AssetScope,
4
+ Types,
9
5
  type Color,
10
- type Command,
11
- type CommandArgs,
12
6
  type Extents,
13
- type NodeConfig,
7
+ type NodeOf,
14
8
  type NormalizedColor,
15
9
  type PaintProps,
16
- type Stroke,
17
10
  type StrokeResolved,
11
+ type Values,
18
12
  type Vector2,
13
+ // Named so the emitted declaration reaches these through the sdk, not core.
14
+ type Fill as _Fill,
15
+ type FillResolved as _FillResolved,
16
+ type Node2D as _Node2D,
17
+ type Node2DProps as _Node2DProps,
18
+ type Shadow as _Shadow,
19
+ type ShadowResolved as _ShadowResolved,
20
+ type Stroke as _Stroke,
19
21
  } from "@motionscript/sdk";
22
+ import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
20
23
  import { cartesianMap, resolveInterval, type AxisScaling, type CoordinateMap, type Interval } from "../kit/coords";
21
- import { stated } from "../kit/geometry";
22
- import { CoordinateSystem } from "../kit/system";
23
- import { formatTick, ticksFor } from "../kit/ticks";
24
24
  import { along, AXIS_COLOR, drawLabels, drawn, lerpInterval, readInterval, withoutTips, type Label } from "../kit/furniture";
25
+ import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
26
+ import { formatTick, ticksFor } from "../kit/ticks";
25
27
 
26
28
  export interface NumberLineProps extends PaintProps {
27
29
  range?: Interval;
@@ -38,94 +40,91 @@ export interface NumberLineProps extends PaintProps {
38
40
  fontFamily?: string;
39
41
  }
40
42
 
43
+ const state = {
44
+ ...PAINT,
45
+ range: Types.custom<Interval>([-5, 5], { resolve: readInterval, lerp: lerpInterval }),
46
+ /** The spacing of the ticks; zero picks a tidy one for the line's length. */
47
+ step: Types.number(0, { min: 0 }),
48
+ scaling: listed(["linear", "log"], "linear", { animatable: false }),
49
+ ticks: Types.boolean(true, { animatable: false }),
50
+ tickSize: Types.number(12, { min: 0, unit: "px" }),
51
+ /** Values whose ticks are drawn twice as long. */
52
+ longTicks: Types.custom<number[]>([], { animatable: false }),
53
+ labels: Types.boolean(true, { animatable: false }),
54
+ labelExclude: Types.custom<number[]>([], { animatable: false }),
55
+ labelSide: listed(["below", "above"], "below", { animatable: false }),
56
+ labelSize: Types.number(18, { min: 1, unit: "px" }),
57
+ labelColor: Types.color(AXIS_COLOR),
58
+ fontFamily: Types.text("", { animatable: false }),
59
+ };
60
+
61
+ type LineValues = Values<typeof state>;
62
+
41
63
  /**
42
64
  * One axis with ticks and numbers. A child's `x` is a value on the line and its
43
65
  * `y` is pixels above it, so a dot or a label sits on a number.
44
66
  */
45
- @node({
67
+ export const NumberLine = component2d({
46
68
  key: "numberLine",
47
- parentKey: "node",
48
- forkable: true,
49
- layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
50
- seed: { width: 800, height: 120 },
51
- })
52
- export class NumberLine extends CoordinateSystem<NumberLineProps> {
53
- @property({ default: [-5, 5], mapper: readInterval, tween: lerpInterval })
54
- declare range: Interval;
55
-
56
- /** The spacing of the ticks; zero picks a tidy one for the line's length. */
57
- @property({ default: 0, min: 0 }) declare step: number;
58
- @property({ default: "linear", options: ["linear", "log"], animatable: false }) declare scaling: AxisScaling;
59
- @property({ default: true, animatable: false }) declare ticks: boolean;
60
- @property({ default: 12, min: 0, unit: "px" }) declare tickSize: number;
61
- /** Values whose ticks are drawn twice as long. */
62
- @property({ default: [], animatable: false }) declare longTicks: number[];
63
- @property({ default: true, animatable: false }) declare labels: boolean;
64
- @property({ default: [], animatable: false }) declare labelExclude: number[];
65
- @property({ default: "below", options: ["below", "above"], animatable: false }) declare labelSide: "below" | "above";
66
- @property({ default: 18, min: 1, unit: "px" }) declare labelSize: number;
67
- @colorProperty({ default: AXIS_COLOR }) declare labelColor: Color;
68
- @property({ default: "", animatable: false }) declare fontFamily: string;
69
+ state,
70
+ defaults: { stroke: { weight: 2, fill: AXIS_COLOR, align: "center" } },
71
+ load: (loader, s) => {
72
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
73
+ },
74
+ commands: {
75
+ ...PAINT_COMMANDS,
76
+ /** Animate the span of values the line shows: a pan when both ends move alike, a zoom when they do not. */
77
+ rangeTo: { args: ["range"] },
78
+ },
79
+ provide: (s) => systemProvides(s),
80
+ measure: (_measurer, s) => systemSize(s),
81
+ layout: (box, s, children) => systemLayout(box, s, children, lineMap),
82
+ plane: (box, s) => systemOpenPlane(box, s, lineMap),
83
+ render: (ctx, s) => drawSystem(ctx, s, systemMap(ctx.size, s, lineMap), (map) => lineFurniture(map, s)),
84
+ });
69
85
 
70
- @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center" } })
71
- declare stroke: Stroke;
86
+ export type NumberLine = NodeOf<typeof NumberLine>;
72
87
 
73
- constructor(props?: NodeConfig<NumberLine, NumberLineProps>) {
74
- super(props ?? {});
75
- }
76
-
77
- /** Animate the span of values the line shows: a pan when both ends move alike, a zoom when they do not. */
78
- @command({ args: ["range"] })
79
- rangeTo(args: CommandArgs<{ range?: Interval }> & { duration: number }): Command<NumberLineProps> {
80
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
81
- }
82
-
83
- override declareAssets(assets: AssetScope): void {
84
- super.declareAssets(assets);
85
- if (this.fontFamily !== "") assets.font(this.fontFamily);
86
- }
87
-
88
- protected mapFor(area: Extents): CoordinateMap {
89
- return cartesianMap({
90
- xRange: resolveInterval(this.range, this.scaling),
91
- yRange: [area.minY, area.maxY],
92
- area,
93
- xScaling: this.scaling,
94
- });
95
- }
88
+ function lineMap(area: Extents, s: LineValues): CoordinateMap {
89
+ return cartesianMap({
90
+ xRange: resolveInterval(s.range, s.scaling),
91
+ yRange: [area.minY, area.maxY],
92
+ area,
93
+ xScaling: s.scaling,
94
+ });
95
+ }
96
96
 
97
- protected furniture(map: CoordinateMap): Graphics2D[] {
98
- const stroke = this.stroke as StrokeResolved[];
99
- const { start, end } = this;
100
- const [min, max] = map.xRange;
101
- const from = map.toPoint(min, 0);
102
- const to = map.toPoint(max, 0);
103
- const parts: Graphics2D[] = [];
104
- if (stroke.length > 0) parts.push(new Graphics2D().line({ points: [from, to], radius: 0, start, end }).stroke(stroke));
97
+ function lineFurniture(map: CoordinateMap, s: LineValues): Graphics2D[] {
98
+ const stroke = s.stroke as StrokeResolved[];
99
+ const { start, end } = s;
100
+ const [min, max] = map.xRange;
101
+ const from = map.toPoint(min, 0);
102
+ const to = map.toPoint(max, 0);
103
+ const parts: Graphics2D[] = [];
104
+ if (stroke.length > 0) parts.push(new Graphics2D().line({ points: [from, to], radius: 0, start, end }).stroke(stroke));
105
105
 
106
- const marks = ticksFor(map.xRange, this.step, to.x - from.x, this.scaling);
107
- const sign = this.labelSide === "below" ? -1 : 1;
108
- const tickMarks = new Graphics2D();
109
- const labels: Label[] = [];
110
- let ticked = false;
111
- for (const value of marks.values) {
112
- const at: Vector2 = map.toPoint(value, 0);
113
- if (!drawn(along(at, from, to), start, end)) continue;
114
- const half = this.longTicks.includes(value) ? this.tickSize : this.tickSize / 2;
115
- if (this.ticks && half > 0) {
116
- tickMarks.line({ points: [{ x: at.x, y: at.y - half }, { x: at.x, y: at.y + half }], radius: 0 });
117
- ticked = true;
118
- }
119
- if (this.labels && !this.labelExclude.includes(value)) {
120
- labels.push({
121
- text: formatTick(value, marks.step),
122
- at: { x: at.x, y: at.y + sign * (this.tickSize / 2 + this.labelSize * 0.9) },
123
- align: "center",
124
- });
125
- }
106
+ const marks = ticksFor(map.xRange, s.step, to.x - from.x, s.scaling);
107
+ const sign = s.labelSide === "below" ? -1 : 1;
108
+ const tickMarks = new Graphics2D();
109
+ const labels: Label[] = [];
110
+ let ticked = false;
111
+ for (const value of marks.values) {
112
+ const at: Vector2 = map.toPoint(value, 0);
113
+ if (!drawn(along(at, from, to), start, end)) continue;
114
+ const half = s.longTicks.includes(value) ? s.tickSize : s.tickSize / 2;
115
+ if (s.ticks && half > 0) {
116
+ tickMarks.line({ points: [{ x: at.x, y: at.y - half }, { x: at.x, y: at.y + half }], radius: 0 });
117
+ ticked = true;
118
+ }
119
+ if (s.labels && !s.labelExclude.includes(value)) {
120
+ labels.push({
121
+ text: formatTick(value, marks.step),
122
+ at: { x: at.x, y: at.y + sign * (s.tickSize / 2 + s.labelSize * 0.9) },
123
+ align: "center",
124
+ });
126
125
  }
127
- if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
128
- parts.push(...drawLabels(labels, { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor }));
129
- return parts;
130
126
  }
127
+ if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
128
+ parts.push(...drawLabels(labels, { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
129
+ return parts;
131
130
  }