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

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