@motionscript/math 0.0.0-stage → 0.1.0-alpha.4

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 (322) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +48 -0
  4. package/README.md +385 -3
  5. package/dist/annotate/angle-mark.d.ts +46 -0
  6. package/dist/annotate/angle-mark.d.ts.map +1 -0
  7. package/dist/annotate/angle-mark.js +132 -0
  8. package/dist/annotate/angle-mark.js.map +1 -0
  9. package/dist/annotate/curly-brace.d.ts +46 -0
  10. package/dist/annotate/curly-brace.d.ts.map +1 -0
  11. package/dist/annotate/curly-brace.js +114 -0
  12. package/dist/annotate/curly-brace.js.map +1 -0
  13. package/dist/annotate/emphasis-mark.d.ts +20 -0
  14. package/dist/annotate/emphasis-mark.d.ts.map +1 -0
  15. package/dist/annotate/emphasis-mark.js +81 -0
  16. package/dist/annotate/emphasis-mark.js.map +1 -0
  17. package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
  18. package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
  19. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
  20. package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
  21. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
  22. package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
  23. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
  24. package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
  25. package/dist/browser/chunks/mathjax-DNP3YRES.js.map +7 -0
  26. package/dist/browser/index.js +2 -0
  27. package/dist/browser/index.js.map +7 -0
  28. package/dist/browser/kit.js +2 -0
  29. package/dist/browser/kit.js.map +7 -0
  30. package/dist/browser/manifest.json +30 -0
  31. package/dist/browser/tex.js +2 -0
  32. package/dist/browser/tex.js.map +7 -0
  33. package/dist/engine.d.ts +64 -0
  34. package/dist/engine.d.ts.map +1 -0
  35. package/dist/engine.js +42 -0
  36. package/dist/engine.js.map +1 -0
  37. package/dist/index.d.ts +30 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +30 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/kit/brace.d.ts +8 -0
  42. package/dist/kit/brace.d.ts.map +1 -0
  43. package/dist/kit/brace.js +36 -0
  44. package/dist/kit/brace.js.map +1 -0
  45. package/dist/kit/cache.d.ts +30 -0
  46. package/dist/kit/cache.d.ts.map +1 -0
  47. package/dist/kit/cache.js +53 -0
  48. package/dist/kit/cache.js.map +1 -0
  49. package/dist/kit/contour.d.ts +8 -0
  50. package/dist/kit/contour.d.ts.map +1 -0
  51. package/dist/kit/contour.js +93 -0
  52. package/dist/kit/contour.js.map +1 -0
  53. package/dist/kit/coords.d.ts +69 -0
  54. package/dist/kit/coords.d.ts.map +1 -0
  55. package/dist/kit/coords.js +224 -0
  56. package/dist/kit/coords.js.map +1 -0
  57. package/dist/kit/curve.d.ts +17 -0
  58. package/dist/kit/curve.d.ts.map +1 -0
  59. package/dist/kit/curve.js +22 -0
  60. package/dist/kit/curve.js.map +1 -0
  61. package/dist/kit/equation.d.ts +61 -0
  62. package/dist/kit/equation.d.ts.map +1 -0
  63. package/dist/kit/equation.js +168 -0
  64. package/dist/kit/equation.js.map +1 -0
  65. package/dist/kit/equation3d.d.ts +50 -0
  66. package/dist/kit/equation3d.d.ts.map +1 -0
  67. package/dist/kit/equation3d.js +103 -0
  68. package/dist/kit/equation3d.js.map +1 -0
  69. package/dist/kit/expression.d.ts +35 -0
  70. package/dist/kit/expression.d.ts.map +1 -0
  71. package/dist/kit/expression.js +124 -0
  72. package/dist/kit/expression.js.map +1 -0
  73. package/dist/kit/field-mark.d.ts +58 -0
  74. package/dist/kit/field-mark.d.ts.map +1 -0
  75. package/dist/kit/field-mark.js +129 -0
  76. package/dist/kit/field-mark.js.map +1 -0
  77. package/dist/kit/field.d.ts +14 -0
  78. package/dist/kit/field.d.ts.map +1 -0
  79. package/dist/kit/field.js +46 -0
  80. package/dist/kit/field.js.map +1 -0
  81. package/dist/kit/follow.d.ts +71 -0
  82. package/dist/kit/follow.d.ts.map +1 -0
  83. package/dist/kit/follow.js +159 -0
  84. package/dist/kit/follow.js.map +1 -0
  85. package/dist/kit/furniture.d.ts +30 -0
  86. package/dist/kit/furniture.d.ts.map +1 -0
  87. package/dist/kit/furniture.js +87 -0
  88. package/dist/kit/furniture.js.map +1 -0
  89. package/dist/kit/geometry.d.ts +13 -0
  90. package/dist/kit/geometry.d.ts.map +1 -0
  91. package/dist/kit/geometry.js +54 -0
  92. package/dist/kit/geometry.js.map +1 -0
  93. package/dist/kit/mark.d.ts +47 -0
  94. package/dist/kit/mark.d.ts.map +1 -0
  95. package/dist/kit/mark.js +103 -0
  96. package/dist/kit/mark.js.map +1 -0
  97. package/dist/kit/part-fills.d.ts +23 -0
  98. package/dist/kit/part-fills.d.ts.map +1 -0
  99. package/dist/kit/part-fills.js +46 -0
  100. package/dist/kit/part-fills.js.map +1 -0
  101. package/dist/kit/place.d.ts +18 -0
  102. package/dist/kit/place.d.ts.map +1 -0
  103. package/dist/kit/place.js +68 -0
  104. package/dist/kit/place.js.map +1 -0
  105. package/dist/kit/placement.d.ts +8 -0
  106. package/dist/kit/placement.d.ts.map +1 -0
  107. package/dist/kit/placement.js +8 -0
  108. package/dist/kit/placement.js.map +1 -0
  109. package/dist/kit/sampler.d.ts +17 -0
  110. package/dist/kit/sampler.d.ts.map +1 -0
  111. package/dist/kit/sampler.js +250 -0
  112. package/dist/kit/sampler.js.map +1 -0
  113. package/dist/kit/scale.d.ts +12 -0
  114. package/dist/kit/scale.d.ts.map +1 -0
  115. package/dist/kit/scale.js +37 -0
  116. package/dist/kit/scale.js.map +1 -0
  117. package/dist/kit/source.d.ts +22 -0
  118. package/dist/kit/source.d.ts.map +1 -0
  119. package/dist/kit/source.js +45 -0
  120. package/dist/kit/source.js.map +1 -0
  121. package/dist/kit/system.d.ts +33 -0
  122. package/dist/kit/system.d.ts.map +1 -0
  123. package/dist/kit/system.js +67 -0
  124. package/dist/kit/system.js.map +1 -0
  125. package/dist/kit/ticks.d.ts +19 -0
  126. package/dist/kit/ticks.d.ts.map +1 -0
  127. package/dist/kit/ticks.js +78 -0
  128. package/dist/kit/ticks.js.map +1 -0
  129. package/dist/kit/withdrawn.d.ts +9 -0
  130. package/dist/kit/withdrawn.d.ts.map +1 -0
  131. package/dist/kit/withdrawn.js +9 -0
  132. package/dist/kit/withdrawn.js.map +1 -0
  133. package/dist/marks/curve-area.d.ts +32 -0
  134. package/dist/marks/curve-area.d.ts.map +1 -0
  135. package/dist/marks/curve-area.js +82 -0
  136. package/dist/marks/curve-area.js.map +1 -0
  137. package/dist/marks/drop-lines.d.ts +22 -0
  138. package/dist/marks/drop-lines.d.ts.map +1 -0
  139. package/dist/marks/drop-lines.js +58 -0
  140. package/dist/marks/drop-lines.js.map +1 -0
  141. package/dist/marks/plot-point.d.ts +35 -0
  142. package/dist/marks/plot-point.d.ts.map +1 -0
  143. package/dist/marks/plot-point.js +97 -0
  144. package/dist/marks/plot-point.js.map +1 -0
  145. package/dist/marks/riemann-sum.d.ts +37 -0
  146. package/dist/marks/riemann-sum.d.ts.map +1 -0
  147. package/dist/marks/riemann-sum.js +91 -0
  148. package/dist/marks/riemann-sum.js.map +1 -0
  149. package/dist/marks/tangent-line.d.ts +27 -0
  150. package/dist/marks/tangent-line.d.ts.map +1 -0
  151. package/dist/marks/tangent-line.js +103 -0
  152. package/dist/marks/tangent-line.js.map +1 -0
  153. package/dist/marks/vector-arrow.d.ts +32 -0
  154. package/dist/marks/vector-arrow.d.ts.map +1 -0
  155. package/dist/marks/vector-arrow.js +87 -0
  156. package/dist/marks/vector-arrow.js.map +1 -0
  157. package/dist/nodes.d.ts +35 -0
  158. package/dist/nodes.d.ts.map +1 -0
  159. package/dist/nodes.js +63 -0
  160. package/dist/nodes.js.map +1 -0
  161. package/dist/plots/function-plot.d.ts +67 -0
  162. package/dist/plots/function-plot.d.ts.map +1 -0
  163. package/dist/plots/function-plot.js +215 -0
  164. package/dist/plots/function-plot.js.map +1 -0
  165. package/dist/plots/points-plot.d.ts +28 -0
  166. package/dist/plots/points-plot.d.ts.map +1 -0
  167. package/dist/plots/points-plot.js +99 -0
  168. package/dist/plots/points-plot.js.map +1 -0
  169. package/dist/plots/stream-lines.d.ts +35 -0
  170. package/dist/plots/stream-lines.d.ts.map +1 -0
  171. package/dist/plots/stream-lines.js +141 -0
  172. package/dist/plots/stream-lines.js.map +1 -0
  173. package/dist/plots/traced-path.d.ts +31 -0
  174. package/dist/plots/traced-path.d.ts.map +1 -0
  175. package/dist/plots/traced-path.js +56 -0
  176. package/dist/plots/traced-path.js.map +1 -0
  177. package/dist/plots/vector-field.d.ts +23 -0
  178. package/dist/plots/vector-field.d.ts.map +1 -0
  179. package/dist/plots/vector-field.js +91 -0
  180. package/dist/plots/vector-field.js.map +1 -0
  181. package/dist/structures/graph-layout.d.ts +11 -0
  182. package/dist/structures/graph-layout.d.ts.map +1 -0
  183. package/dist/structures/graph-layout.js +135 -0
  184. package/dist/structures/graph-layout.js.map +1 -0
  185. package/dist/structures/network-graph.d.ts +83 -0
  186. package/dist/structures/network-graph.d.ts.map +1 -0
  187. package/dist/structures/network-graph.js +289 -0
  188. package/dist/structures/network-graph.js.map +1 -0
  189. package/dist/structures/sample-space.d.ts +62 -0
  190. package/dist/structures/sample-space.d.ts.map +1 -0
  191. package/dist/structures/sample-space.js +199 -0
  192. package/dist/structures/sample-space.js.map +1 -0
  193. package/dist/systems/axes2d.d.ts +75 -0
  194. package/dist/systems/axes2d.d.ts.map +1 -0
  195. package/dist/systems/axes2d.js +193 -0
  196. package/dist/systems/axes2d.js.map +1 -0
  197. package/dist/systems/number-line.d.ts +49 -0
  198. package/dist/systems/number-line.d.ts.map +1 -0
  199. package/dist/systems/number-line.js +127 -0
  200. package/dist/systems/number-line.js.map +1 -0
  201. package/dist/systems/number-plane.d.ts +59 -0
  202. package/dist/systems/number-plane.d.ts.map +1 -0
  203. package/dist/systems/number-plane.js +193 -0
  204. package/dist/systems/number-plane.js.map +1 -0
  205. package/dist/systems/polar-plane.d.ts +48 -0
  206. package/dist/systems/polar-plane.d.ts.map +1 -0
  207. package/dist/systems/polar-plane.js +165 -0
  208. package/dist/systems/polar-plane.js.map +1 -0
  209. package/dist/tex/geometry.d.ts +85 -0
  210. package/dist/tex/geometry.d.ts.map +1 -0
  211. package/dist/tex/geometry.js +632 -0
  212. package/dist/tex/geometry.js.map +1 -0
  213. package/dist/tex/index.d.ts +11 -0
  214. package/dist/tex/index.d.ts.map +1 -0
  215. package/dist/tex/index.js +9 -0
  216. package/dist/tex/index.js.map +1 -0
  217. package/dist/tex/mathjax.d.ts +33 -0
  218. package/dist/tex/mathjax.d.ts.map +1 -0
  219. package/dist/tex/mathjax.js +74 -0
  220. package/dist/tex/mathjax.js.map +1 -0
  221. package/dist/tex/matrix.d.ts +31 -0
  222. package/dist/tex/matrix.d.ts.map +1 -0
  223. package/dist/tex/matrix.js +73 -0
  224. package/dist/tex/matrix.js.map +1 -0
  225. package/dist/tex/node.d.ts +215 -0
  226. package/dist/tex/node.d.ts.map +1 -0
  227. package/dist/tex/node.js +595 -0
  228. package/dist/tex/node.js.map +1 -0
  229. package/dist/tex/tween.d.ts +50 -0
  230. package/dist/tex/tween.d.ts.map +1 -0
  231. package/dist/tex/tween.js +253 -0
  232. package/dist/tex/tween.js.map +1 -0
  233. package/dist/three/axes3d.d.ts +87 -0
  234. package/dist/three/axes3d.d.ts.map +1 -0
  235. package/dist/three/axes3d.js +228 -0
  236. package/dist/three/axes3d.js.map +1 -0
  237. package/dist/three/curve-plot3d.d.ts +60 -0
  238. package/dist/three/curve-plot3d.d.ts.map +1 -0
  239. package/dist/three/curve-plot3d.js +143 -0
  240. package/dist/three/curve-plot3d.js.map +1 -0
  241. package/dist/three/label3d.d.ts +11 -0
  242. package/dist/three/label3d.d.ts.map +1 -0
  243. package/dist/three/label3d.js +40 -0
  244. package/dist/three/label3d.js.map +1 -0
  245. package/dist/three/plot-point3d.d.ts +26 -0
  246. package/dist/three/plot-point3d.d.ts.map +1 -0
  247. package/dist/three/plot-point3d.js +60 -0
  248. package/dist/three/plot-point3d.js.map +1 -0
  249. package/dist/three/space.d.ts +28 -0
  250. package/dist/three/space.d.ts.map +1 -0
  251. package/dist/three/space.js +47 -0
  252. package/dist/three/space.js.map +1 -0
  253. package/dist/three/surface-plot3d.d.ts +80 -0
  254. package/dist/three/surface-plot3d.d.ts.map +1 -0
  255. package/dist/three/surface-plot3d.js +290 -0
  256. package/dist/three/surface-plot3d.js.map +1 -0
  257. package/dist/three/vector-arrow3d.d.ts +25 -0
  258. package/dist/three/vector-arrow3d.d.ts.map +1 -0
  259. package/dist/three/vector-arrow3d.js +72 -0
  260. package/dist/three/vector-arrow3d.js.map +1 -0
  261. package/package.json +79 -3
  262. package/registry.json +251 -0
  263. package/src/annotate/angle-mark.ts +147 -0
  264. package/src/annotate/curly-brace.ts +126 -0
  265. package/src/annotate/emphasis-mark.ts +89 -0
  266. package/src/engine.ts +72 -0
  267. package/src/index.ts +29 -0
  268. package/src/kit/brace.ts +37 -0
  269. package/src/kit/cache.ts +61 -0
  270. package/src/kit/contour.ts +94 -0
  271. package/src/kit/coords.ts +278 -0
  272. package/src/kit/curve.ts +29 -0
  273. package/src/kit/equation.ts +181 -0
  274. package/src/kit/equation3d.ts +137 -0
  275. package/src/kit/expression.ts +159 -0
  276. package/src/kit/field-mark.ts +155 -0
  277. package/src/kit/field.ts +50 -0
  278. package/src/kit/follow.ts +203 -0
  279. package/src/kit/furniture.ts +108 -0
  280. package/src/kit/geometry.ts +49 -0
  281. package/src/kit/mark.ts +131 -0
  282. package/src/kit/part-fills.ts +61 -0
  283. package/src/kit/place.ts +101 -0
  284. package/src/kit/placement.ts +8 -0
  285. package/src/kit/sampler.ts +281 -0
  286. package/src/kit/scale.ts +39 -0
  287. package/src/kit/source.ts +57 -0
  288. package/src/kit/system.ts +99 -0
  289. package/src/kit/ticks.ts +85 -0
  290. package/src/kit/withdrawn.ts +8 -0
  291. package/src/marks/curve-area.ts +89 -0
  292. package/src/marks/drop-lines.ts +63 -0
  293. package/src/marks/plot-point.ts +107 -0
  294. package/src/marks/riemann-sum.ts +98 -0
  295. package/src/marks/tangent-line.ts +103 -0
  296. package/src/marks/vector-arrow.ts +98 -0
  297. package/src/nodes.ts +63 -0
  298. package/src/plots/function-plot.ts +236 -0
  299. package/src/plots/points-plot.ts +107 -0
  300. package/src/plots/stream-lines.ts +151 -0
  301. package/src/plots/traced-path.ts +52 -0
  302. package/src/plots/vector-field.ts +88 -0
  303. package/src/structures/graph-layout.ts +145 -0
  304. package/src/structures/network-graph.ts +308 -0
  305. package/src/structures/sample-space.ts +209 -0
  306. package/src/systems/axes2d.ts +217 -0
  307. package/src/systems/number-line.ts +131 -0
  308. package/src/systems/number-plane.ts +203 -0
  309. package/src/systems/polar-plane.ts +176 -0
  310. package/src/tex/geometry.ts +703 -0
  311. package/src/tex/index.ts +14 -0
  312. package/src/tex/mathjax.ts +82 -0
  313. package/src/tex/matrix.ts +75 -0
  314. package/src/tex/node.ts +641 -0
  315. package/src/tex/tween.ts +308 -0
  316. package/src/three/axes3d.ts +240 -0
  317. package/src/three/curve-plot3d.ts +147 -0
  318. package/src/three/label3d.ts +50 -0
  319. package/src/three/plot-point3d.ts +66 -0
  320. package/src/three/space.ts +67 -0
  321. package/src/three/surface-plot3d.ts +323 -0
  322. package/src/three/vector-arrow3d.ts +77 -0
@@ -0,0 +1,217 @@
1
+ import {
2
+ colorProperty,
3
+ command,
4
+ Graphics2D,
5
+ node,
6
+ property,
7
+ strokeProperty,
8
+ type AssetScope,
9
+ type Color,
10
+ type Command,
11
+ type CommandArgs,
12
+ type Extents,
13
+ type NodeConfig,
14
+ type NormalizedColor,
15
+ type PaintProps,
16
+ type Stroke,
17
+ type StrokeResolved,
18
+ type Vector2,
19
+ } from "@motionscript/sdk";
20
+ 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";
23
+ import { formatTick, ticksFor, type Ticks } from "../kit/ticks";
24
+ import { along, AXIS_COLOR, declareFormulas, drawLabels, drawn, type Label, lerpInterval, readInterval, withoutTips } from "../kit/furniture";
25
+
26
+ export interface Axes2dProps extends PaintProps {
27
+ xRange?: Interval;
28
+ yRange?: Interval;
29
+ xStep?: number;
30
+ yStep?: number;
31
+ xScaling?: AxisScaling;
32
+ yScaling?: AxisScaling;
33
+ squareUnits?: boolean;
34
+ ticks?: boolean;
35
+ tickSize?: number;
36
+ labels?: boolean;
37
+ labelSize?: number;
38
+ labelColor?: Color;
39
+ fontFamily?: string;
40
+ xLabel?: string;
41
+ yLabel?: string;
42
+ }
43
+
44
+ /** One axis, as its furniture is drawn: its line's ends and what is marked along it. */
45
+ interface AxisLayout {
46
+ from: Vector2;
47
+ to: Vector2;
48
+ /** The pixel points that draw its line: its two ends, or more where the system's map bends it. */
49
+ line: Vector2[];
50
+ marks: Ticks;
51
+ /** The value on this axis the other one crosses at, which is left unmarked. */
52
+ crossing: number;
53
+ /** A mark's pixel point. */
54
+ at(value: number): Vector2;
55
+ }
56
+
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;
73
+
74
+ /** 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;
79
+ /** 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;
88
+ /** 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
+ }
98
+
99
+ /** 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
+ }
104
+
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
+ }
110
+
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 });
121
+ }
122
+
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;
129
+
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
+ }
138
+
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
+ );
164
+ }
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" });
175
+ }
176
+ parts.push(...drawLabels(labels, { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor }));
177
+ return parts;
178
+ }
179
+
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 [];
183
+ }
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
+ ];
214
+ }
215
+ }
216
+
217
+ export type { AxisLayout };
@@ -0,0 +1,131 @@
1
+ import {
2
+ colorProperty,
3
+ command,
4
+ Graphics2D,
5
+ node,
6
+ property,
7
+ strokeProperty,
8
+ type AssetScope,
9
+ type Color,
10
+ type Command,
11
+ type CommandArgs,
12
+ type Extents,
13
+ type NodeConfig,
14
+ type NormalizedColor,
15
+ type PaintProps,
16
+ type Stroke,
17
+ type StrokeResolved,
18
+ type Vector2,
19
+ } from "@motionscript/sdk";
20
+ 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
+ import { along, AXIS_COLOR, drawLabels, drawn, lerpInterval, readInterval, withoutTips, type Label } from "../kit/furniture";
25
+
26
+ export interface NumberLineProps extends PaintProps {
27
+ range?: Interval;
28
+ step?: number;
29
+ scaling?: AxisScaling;
30
+ ticks?: boolean;
31
+ tickSize?: number;
32
+ longTicks?: number[];
33
+ labels?: boolean;
34
+ labelExclude?: number[];
35
+ labelSide?: "below" | "above";
36
+ labelSize?: number;
37
+ labelColor?: Color;
38
+ fontFamily?: string;
39
+ }
40
+
41
+ /**
42
+ * One axis with ticks and numbers. A child's `x` is a value on the line and its
43
+ * `y` is pixels above it, so a dot or a label sits on a number.
44
+ */
45
+ @node({
46
+ 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
+
70
+ @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center" } })
71
+ declare stroke: Stroke;
72
+
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
+ }
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));
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
+ }
126
+ }
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
+ }
131
+ }
@@ -0,0 +1,203 @@
1
+ import {
2
+ command,
3
+ Graphics2D,
4
+ node,
5
+ property,
6
+ strokeProperty,
7
+ type Command,
8
+ type CommandArgs,
9
+ type Extents,
10
+ type NodeConfig,
11
+ type Stroke,
12
+ type StrokeResolved,
13
+ } from "@motionscript/sdk";
14
+ import {
15
+ isIdentityMatrix,
16
+ lerpMatrix,
17
+ lineThrough,
18
+ readMatrix,
19
+ transformedMap,
20
+ warpedMap,
21
+ type CoordinateMap,
22
+ type Matrix2,
23
+ type Warp,
24
+ } from "../kit/coords";
25
+ import { fieldParams, parseField } from "../kit/field";
26
+ import { Axes2d, type Axes2dProps, type AxisLayout } from "./axes2d";
27
+ import { AXIS_COLOR } from "../kit/furniture";
28
+ import { stated } from "../kit/geometry";
29
+ import { lerpSource, readSource, type PlotSource } from "../kit/source";
30
+
31
+ export interface NumberPlaneProps extends Axes2dProps {
32
+ grid?: boolean;
33
+ gridStroke?: Stroke;
34
+ minorStroke?: Stroke;
35
+ minorDivisions?: number;
36
+ matrix?: Matrix2 | readonly (readonly number[])[];
37
+ warp?: string | PlotSource;
38
+ ghost?: boolean;
39
+ }
40
+
41
+ /**
42
+ * Axes over a full grid, with square units unless told otherwise: the plane
43
+ * vectors and transformations are drawn on. Its `matrix` shears, turns or
44
+ * squeezes the whole plane about its origin, and its `warp` bends it by a
45
+ * function of its own points; the grid, the axes and everything on the plane
46
+ * go with it.
47
+ */
48
+ @node({
49
+ key: "numberPlane",
50
+ parentKey: "axes2d",
51
+ forkable: true,
52
+ layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
53
+ seed: { width: 800, height: 500 },
54
+ })
55
+ export class NumberPlane extends Axes2d<NumberPlaneProps> {
56
+ @property({ default: true, animatable: false }) declare grid: boolean;
57
+ /** The line at every tick. */
58
+ @strokeProperty({ default: { weight: 1.5, fill: "#3b4658", align: "center" } }) declare gridStroke: Stroke;
59
+ /** The lines between ticks. */
60
+ @strokeProperty({ default: { weight: 1, fill: "#232b38", align: "center" } }) declare minorStroke: Stroke;
61
+ /** How many parts the space between two ticks is cut into; one draws no line between them. */
62
+ @property({ default: 2, min: 1, max: 10, step: 1 }) declare minorDivisions: number;
63
+
64
+ /** A linear map of the plane, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
65
+ @property({ default: [1, 0, 0, 1], mapper: readMatrix, tween: lerpMatrix }) declare matrix: Matrix2;
66
+ /**
67
+ * Where each point of the plane is sent, two parts in x and y:
68
+ * `(x + sin(y), y + sin(x))`. Empty leaves the plane flat.
69
+ */
70
+ @property({ default: "", mapper: readSource, tween: lerpSource, kind: "fieldEquation" }) declare warp: PlotSource;
71
+ /** Leave the plane as it was, faintly, behind one that has been transformed or warped. */
72
+ @property({ default: true, animatable: false }) declare ghost: boolean;
73
+
74
+ @property({ default: true, animatable: false }) declare squareUnits: boolean;
75
+ @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center" } }) declare stroke: Stroke;
76
+
77
+ constructor(props?: NodeConfig<NumberPlane, NumberPlaneProps>) {
78
+ super((props ?? {}) as NodeConfig<Axes2d<NumberPlaneProps>, NumberPlaneProps>);
79
+ // A transformed plane reaches past its box, and so does what is on it: both are kept in unless told otherwise.
80
+ if (props?.clip === undefined) this.applyProp("clip", () => this.coordinates.spills === true);
81
+ }
82
+
83
+ /** Send the plane through a matrix: a shear, a turn, a squeeze, a flip. */
84
+ @command({ args: ["matrix"] })
85
+ transformTo(args: CommandArgs<{ matrix: Matrix2 | readonly (readonly number[])[] }> & { duration: number }): Command<NumberPlaneProps> {
86
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
87
+ }
88
+
89
+ /** Bend the plane, each point travelling to where the warp sends it; an empty one flattens it again. */
90
+ @command({ args: ["warp"] })
91
+ warpTo(args: CommandArgs<{ warp: string }> & { duration: number }): Command<NumberPlaneProps> {
92
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
93
+ }
94
+
95
+ protected override mapFor(area: Extents): CoordinateMap {
96
+ const flat = super.mapFor(area);
97
+ this._flat = flat;
98
+ const map = isIdentityMatrix(this.matrix) ? flat : transformedMap(flat, this.matrix);
99
+ const { source, blend } = this.warp;
100
+ const to = warpOf(source);
101
+ if (!blend) return to ? warpedMap(map, to, source) : map;
102
+ // Mid-warp each point is part of the way from where the old warp had it to where the new one sends it.
103
+ const from = warpOf(blend.from);
104
+ if (!from && !to) return map;
105
+ const start = { x: 0, y: 0 };
106
+ return warpedMap(
107
+ map,
108
+ (x, y, out) => {
109
+ if (!(from ?? STAY)(x, y, start) || !(to ?? STAY)(x, y, out)) return false;
110
+ out.x = start.x + (out.x - start.x) * blend.t;
111
+ out.y = start.y + (out.y - start.y) * blend.t;
112
+ return true;
113
+ },
114
+ `${blend.from}>${source}@${blend.t}`,
115
+ );
116
+ }
117
+
118
+ protected override backdrop(map: CoordinateMap, x: AxisLayout, y: AxisLayout): Graphics2D[] {
119
+ if (!this.grid) return [];
120
+ const parts: Graphics2D[] = [];
121
+ const flat = this._flat;
122
+ if (this.ghost && flat && flat.revision !== map.revision) {
123
+ const [gx, gy] = this.axes(flat);
124
+ parts.push(...this._ruled(flat, gx, gy, true));
125
+ }
126
+ parts.push(...this._ruled(map, x, y, false));
127
+ return parts;
128
+ }
129
+
130
+ private _flat: CoordinateMap | null = null;
131
+
132
+ /** The grid of `map`: every line, or only the ones at a tick, in the fainter stroke, for a ghost. */
133
+ private _ruled(map: CoordinateMap, x: AxisLayout, y: AxisLayout, ghost: boolean): Graphics2D[] {
134
+ const { start, end } = this;
135
+ const [xMin, xMax] = map.xRange;
136
+ const [yMin, yMax] = map.yRange;
137
+ const lines = (values: readonly number[], vertical: boolean): Graphics2D => {
138
+ const g = new Graphics2D();
139
+ for (const value of values) {
140
+ g.line({
141
+ points: vertical ? lineThrough(map, value, yMin, value, yMax) : lineThrough(map, xMin, value, xMax, value),
142
+ radius: 0,
143
+ start,
144
+ end,
145
+ });
146
+ }
147
+ return g;
148
+ };
149
+ const parts: Graphics2D[] = [];
150
+ const minor = this.minorStroke as StrokeResolved[];
151
+ if (ghost) {
152
+ if (minor.length === 0) return parts;
153
+ if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(minor));
154
+ if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(minor));
155
+ return parts;
156
+ }
157
+ const divisions = Math.round(this.minorDivisions);
158
+ if (minor.length > 0 && divisions > 1) {
159
+ const between = (axis: AxisLayout, [min, max]: readonly [number, number]): number[] => {
160
+ const { step, values } = axis.marks;
161
+ if (!(step > 0) || values.length === 0) return [];
162
+ const out: number[] = [];
163
+ for (let base = values[0] - step; base < max; base += step) {
164
+ for (let i = 1; i < divisions; i++) {
165
+ const value = base + (step * i) / divisions;
166
+ if (value > min && value < max) out.push(value);
167
+ }
168
+ }
169
+ return out;
170
+ };
171
+ const xs = between(x, map.xRange);
172
+ const ys = between(y, map.yRange);
173
+ if (xs.length > 0) parts.push(lines(xs, true).stroke(minor));
174
+ if (ys.length > 0) parts.push(lines(ys, false).stroke(minor));
175
+ }
176
+ const major = this.gridStroke as StrokeResolved[];
177
+ if (major.length > 0) {
178
+ if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(major));
179
+ if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(major));
180
+ }
181
+ return parts;
182
+ }
183
+ }
184
+
185
+ const STAY: Warp = (x, y, out) => {
186
+ out.x = x;
187
+ out.y = y;
188
+ return true;
189
+ };
190
+
191
+ /** `source` as a warp, or `null` for text that is empty or not a field: the plane is then left as it is. */
192
+ function warpOf(source: string): Warp | null {
193
+ const field = source.trim() === "" ? null : parseField(source, fieldParams(source));
194
+ if (!field || field instanceof Error) return null;
195
+ const { x, y } = field;
196
+ return (gx, gy, out) => {
197
+ const wx = x(gx, gy);
198
+ const wy = y(gx, gy);
199
+ out.x = wx;
200
+ out.y = wy;
201
+ return Number.isFinite(wx) && Number.isFinite(wy);
202
+ };
203
+ }