@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,113 @@
1
+ import { Fills, Graphics2D, toPathString, type AssetScope, type Color, type Loader, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
2
+ import { buildLatexPath, LatexNotLoaded, loadMathJax, MATHJAX_LOADER } from "../tex/geometry";
3
+ import type { Interval } from "./coords";
4
+
5
+ /** Shared drawing for the systems' own furniture: axis lines, tick marks and their numbers. */
6
+
7
+ /** What a system draws its axes and numbers in, and a plot its curve, until told otherwise. */
8
+ export const AXIS_COLOR = "#9ca3af";
9
+ export const PLOT_COLOR = "#58a6ff";
10
+
11
+ /** A stroke without end markers: an axis wears a tip, its ticks do not. */
12
+ export function withoutTips(stroke: StrokeResolved[]): StrokeResolved[] {
13
+ return stroke.map((layer) => ({ ...layer, startCap: undefined, endCap: undefined }));
14
+ }
15
+
16
+ export interface LabelStyle {
17
+ fontSize: number;
18
+ fontFamily: string;
19
+ color: Color;
20
+ }
21
+
22
+ export interface Label {
23
+ text: string;
24
+ /** Where the label's box is anchored, in pixels. */
25
+ at: Vector2;
26
+ align: "left" | "center" | "right";
27
+ }
28
+
29
+ /** How wide a label's box is, in ems: room for a long number, so alignment is by the box's edge. */
30
+ const LABEL_BOX_EM = 8;
31
+
32
+ /** A label written between dollar signs is a formula: `$\\frac{\\pi}{2}$`. */
33
+ const FORMULA = /^\$([\s\S]+)\$$/;
34
+
35
+ /** Ask the host for MathJax when any of `texts` is a formula, so it is there before the node is laid out. */
36
+ export function declareFormulas(assets: AssetScope, texts: readonly string[]): void {
37
+ if (texts.some((text) => FORMULA.test(text.trim()))) assets.loader(MATHJAX_LOADER, loadMathJax, { gate: "layout" });
38
+ }
39
+
40
+ /** {@link declareFormulas}, for a component's `load`. */
41
+ export function loadFormulas(loader: Loader, texts: readonly string[]): void {
42
+ if (texts.some((text) => FORMULA.test(text.trim()))) loader.task(MATHJAX_LOADER, loadMathJax, { before: "layout" });
43
+ }
44
+
45
+ /** What draws `labels`: text in the scene's face, and a typeset formula for each one written as such. */
46
+ export function drawLabels(labels: readonly Label[], style: LabelStyle): Graphics2D[] {
47
+ const out: Graphics2D[] = [];
48
+ const fill = Fills.color(style.color);
49
+ const text = new Graphics2D();
50
+ const width = style.fontSize * LABEL_BOX_EM;
51
+ let written = false;
52
+ for (const label of labels) {
53
+ const formula = FORMULA.exec(label.text.trim());
54
+ const glyphs = formula ? typeset(formula[1], label, style.fontSize) : null;
55
+ if (glyphs) {
56
+ for (const glyph of glyphs) out.push(glyph.fill(fill));
57
+ continue;
58
+ }
59
+ const { at, align } = label;
60
+ written = true;
61
+ text.text({
62
+ text: label.text,
63
+ fontSize: style.fontSize,
64
+ // An empty family is left out, so the label takes the scene's own face.
65
+ ...(style.fontFamily === "" ? {} : { fontFamily: style.fontFamily }),
66
+ wrap: false,
67
+ width,
68
+ textAlign: align,
69
+ x: align === "center" ? at.x : align === "right" ? at.x - width / 2 : at.x + width / 2,
70
+ y: at.y,
71
+ });
72
+ }
73
+ if (written) out.push(text.fill(fill));
74
+ return out;
75
+ }
76
+
77
+ // A formula's glyphs with its box anchored at the label's point; `null` for one that will not typeset, which is then
78
+ // drawn as written, and nothing at all while MathJax is still on its way: its source is not a stand-in for it.
79
+ function typeset(source: string, { at, align }: Label, fontSize: number): Graphics2D[] | null {
80
+ let formula: ReturnType<typeof buildLatexPath>;
81
+ try {
82
+ formula = buildLatexPath(source, fontSize);
83
+ } catch (error) {
84
+ return error instanceof LatexNotLoaded ? [] : null;
85
+ }
86
+ const [minX, minY, maxX, maxY] = formula.bounds;
87
+ const dx = at.x + (align === "left" ? formula.width / 2 : align === "right" ? -formula.width / 2 : 0);
88
+ // Glyph paths are y-down; the frame whose centre lands on the node's is shifted the other way.
89
+ const frame: [number, number, number, number] = [minX - dx, minY + at.y, maxX - dx, maxY + at.y];
90
+ return formula.tokens.map((token) => new Graphics2D().path({ data: toPathString(token.path), centerBounds: frame }));
91
+ }
92
+
93
+ /** Whether a mark `u` of the way along an axis is inside the part of it drawn so far. */
94
+ export function drawn(u: number, start: number, end: number): boolean {
95
+ return u >= start - 1e-9 && u <= end + 1e-9;
96
+ }
97
+
98
+ /** How far along `range` a value is, 0 to 1, by pixel distance. */
99
+ export function along(at: Vector2, from: Vector2, to: Vector2): number {
100
+ const dx = to.x - from.x;
101
+ const dy = to.y - from.y;
102
+ const length = dx * dx + dy * dy;
103
+ return length === 0 ? 0 : ((at.x - from.x) * dx + (at.y - from.y) * dy) / length;
104
+ }
105
+
106
+ export function lerpInterval(from: Interval, to: Interval, t: number): Interval {
107
+ return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
108
+ }
109
+
110
+ export function readInterval(value: Interval | undefined, previous?: Interval): Interval {
111
+ const [min, max] = Array.isArray(value) ? value : (previous ?? [0, 1]);
112
+ return [Number(min), Number(max)];
113
+ }
@@ -0,0 +1,49 @@
1
+ import type { Extents, PathCommand, Vector2 } from "@motionscript/sdk";
2
+
3
+ /**
4
+ * Runs of pixel points as path commands. Commands are y-up like every other
5
+ * `Graphics2D` op (only an SVG `d` string is y-down), so nothing is flipped here.
6
+ */
7
+ export function runsToPath(runs: readonly (readonly Vector2[])[], closed = false): PathCommand[] {
8
+ const out: PathCommand[] = [];
9
+ for (const run of runs) {
10
+ if (run.length < 2) continue;
11
+ out.push({ type: "M", x: run[0].x, y: run[0].y });
12
+ for (let i = 1; i < run.length; i++) out.push({ type: "L", x: run[i].x, y: run[i].y });
13
+ if (closed) out.push({ type: "Z" });
14
+ }
15
+ return out;
16
+ }
17
+
18
+ export function boundsOfRuns(runs: readonly (readonly Vector2[])[]): Extents | null {
19
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
20
+ for (const run of runs) {
21
+ if (run.length < 2) continue;
22
+ for (const p of run) {
23
+ if (p.x < minX) minX = p.x;
24
+ if (p.x > maxX) maxX = p.x;
25
+ if (p.y < minY) minY = p.y;
26
+ if (p.y > maxY) maxY = p.y;
27
+ }
28
+ }
29
+ return minX <= maxX ? { minX, minY, maxX, maxY } : null;
30
+ }
31
+
32
+ /** `area` grown by `margin` on every side. */
33
+ export function inflate(area: Extents, margin: number): Extents {
34
+ return { minX: area.minX - margin, minY: area.minY - margin, maxX: area.maxX + margin, maxY: area.maxY + margin };
35
+ }
36
+
37
+ export function clamp(value: number, min: number, max: number): number {
38
+ return value < min ? min : value > max ? max : value;
39
+ }
40
+
41
+ /** `data` without the arguments a command was not given, which arrive as `undefined` and are not destinations. */
42
+ export function stated<T extends object>(data: T | undefined): Partial<T> {
43
+ const out: Partial<T> = {};
44
+ if (!data) return out;
45
+ for (const key of Object.keys(data) as (keyof T)[]) {
46
+ if (data[key] !== undefined) out[key] = data[key];
47
+ }
48
+ return out;
49
+ }
@@ -0,0 +1,116 @@
1
+ import {
2
+ createContext,
3
+ Types,
4
+ type ChildView,
5
+ type EntryOptions,
6
+ type NodeView,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Context as _Context,
9
+ } from "@motionscript/sdk";
10
+ import { PIXEL_MAP, type CoordinateMap } from "./coords";
11
+ import { CURVE_KEYS, isCurve, type CurveValues } from "./curve";
12
+
13
+ /**
14
+ * What a coordinate system hands the plots and marks inside it: the map it was
15
+ * last laid out on, the curves it holds by id, and, under a plot, that plot.
16
+ * Inherited once, so it is a live object the system's layout keeps current.
17
+ */
18
+ export interface GraphPlane {
19
+ readonly map: CoordinateMap;
20
+ readonly curves: ReadonlyMap<string, CurveValues>;
21
+ /** The curve a mark rides when it names none: the plot it sits in. */
22
+ readonly enclosing: CurveValues | null;
23
+ }
24
+
25
+ /** The plane of the nearest coordinate system: what a plot is laid out on. */
26
+ export const SYSTEM = createContext<GraphPlane | null>(null, "mathSystem");
27
+ /** The plane under a plot: the system's, riding that plot. */
28
+ export const PLOT = createContext<GraphPlane | null>(null, "mathPlot");
29
+
30
+ type Inherited = { readonly private: true };
31
+
32
+ /** A plot's plane: the system it sits in, `null` outside one. */
33
+ export const SYSTEM_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
34
+ private: true,
35
+ inherit: (use) => use(SYSTEM),
36
+ });
37
+
38
+ /** A mark's plane: the plot or the system that lays it out, `null` outside both. */
39
+ export const PLANE_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
40
+ private: true,
41
+ inherit: (use) => use(PLOT) ?? use(SYSTEM),
42
+ });
43
+
44
+ /** A reference to the plot a mark reads, by id: a host offers the plots. */
45
+ export const PLOT_REF = Types.custom<string, NodeView | null, EntryOptions & { readonly nodeTypes: readonly string[] }>("", { kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false });
46
+
47
+ const NO_CURVES: ReadonlyMap<string, CurveValues> = new Map();
48
+
49
+ class SystemPlane implements GraphPlane {
50
+ map: CoordinateMap = PIXEL_MAP;
51
+ curves: ReadonlyMap<string, CurveValues> = NO_CURVES;
52
+ readonly enclosing = null;
53
+ }
54
+
55
+ class PlotPlane implements GraphPlane {
56
+ constructor(readonly parent: GraphPlane | null, readonly enclosing: CurveValues) { }
57
+
58
+ get map(): CoordinateMap {
59
+ return this.parent?.map ?? PIXEL_MAP;
60
+ }
61
+
62
+ get curves(): ReadonlyMap<string, CurveValues> {
63
+ return this.parent?.curves ?? NO_CURVES;
64
+ }
65
+ }
66
+
67
+ const systems = new WeakMap<object, SystemPlane>();
68
+ const plots = new WeakMap<object, PlotPlane>();
69
+
70
+ /** The plane a system provides, one per system: its `layout` writes the map and the curves into it. */
71
+ export function systemPlane(state: object): SystemPlane {
72
+ let plane = systems.get(state);
73
+ if (!plane) systems.set(state, (plane = new SystemPlane()));
74
+ return plane;
75
+ }
76
+
77
+ /** Lay `plane` out on `map`, holding the curves among `children`. */
78
+ export function settle(plane: SystemPlane, map: CoordinateMap, children: readonly ChildView[]): void {
79
+ plane.map = map;
80
+ const curves = new Map<string, CurveValues>();
81
+ collectCurves(children, curves);
82
+ plane.curves = curves;
83
+ }
84
+
85
+ /** The plane a plot provides the marks inside it, one per plot and system. */
86
+ export function plotPlane(plot: CurveValues, parent: GraphPlane | null): GraphPlane {
87
+ let plane = plots.get(plot);
88
+ if (!plane || plane.parent !== parent) plots.set(plot, (plane = new PlotPlane(parent, plot)));
89
+ return plane;
90
+ }
91
+
92
+ /**
93
+ * The curve a mark reads: the one `on` names in its system, or, naming none or
94
+ * one the system does not hold, the plot it sits in. `on` as the mark's own
95
+ * fields read a reference (a view) or its holder's layout does (an id). Outside
96
+ * any system a named curve is read wherever it is.
97
+ */
98
+ export function curveFor(plane: GraphPlane | null, on: unknown, enclosing = true): CurveValues | null {
99
+ if (!plane) return isCurve(on) ? on.state : null;
100
+ const id = refId(on);
101
+ return (id === "" ? undefined : plane.curves.get(id)) ?? (enclosing ? plane.enclosing : null);
102
+ }
103
+
104
+ /** The id a reference holds, read as an id or as a view of the node. */
105
+ export function refId(on: unknown): string {
106
+ if (typeof on === "string") return on;
107
+ if (typeof on === "object" && on !== null && typeof (on as { id?: unknown }).id === "string") return (on as { id: string }).id;
108
+ return "";
109
+ }
110
+
111
+ function collectCurves(children: readonly ChildView[], into: Map<string, CurveValues>): void {
112
+ for (const child of children) {
113
+ if (child.id !== "" && CURVE_KEYS.includes(child.key)) into.set(child.id, child.state as unknown as CurveValues);
114
+ collectCurves(child.children, into);
115
+ }
116
+ }
@@ -0,0 +1,152 @@
1
+ import {
2
+ Graphics2D,
3
+ type ChildView,
4
+ type Extents,
5
+ type Fill,
6
+ type RenderContext,
7
+ type Shadow,
8
+ type Size2D,
9
+ type Stroke,
10
+ type StrokeResolved,
11
+ type Vector2,
12
+ } from "@motionscript/sdk";
13
+ import { painted } from "./component";
14
+ import { PIXEL_MAP, type CoordinateMap } from "./coords";
15
+ import { boundsOfRuns, runsToPath } from "./geometry";
16
+ import type { GraphPlane } from "./graph";
17
+
18
+ /**
19
+ * A mark is a node drawn in graph coordinates: a plot, an area, an arrow
20
+ * between two graph points. It answers with runs of pixel points and is
21
+ * otherwise a shape, so its box is the tight box of its ink.
22
+ *
23
+ * Inside a coordinate system (or a plot) the holder lays it out on that ink,
24
+ * calling the mark's own trace with its map, and the mark's `x`/`y` are a shift
25
+ * in graph units. Outside one a graph unit is a pixel and `x`/`y` are the usual
26
+ * offset.
27
+ */
28
+
29
+ /** What a mark is traced through: the map, its own `x`/`y` as a shift, and the plane it reads curves from. */
30
+ export interface MarkReading {
31
+ readonly map: CoordinateMap;
32
+ /** Zero outside a system, where `x`/`y` are pixels. */
33
+ readonly shift: Vector2;
34
+ readonly plane: GraphPlane | null;
35
+ }
36
+
37
+ /** The runs a mark draws, in pixels, y-up from the system's centre. */
38
+ export type Tracer<V> = (state: V, read: MarkReading) => Vector2[][];
39
+
40
+ /** What every mark's state reads: its place, and the plane it is laid out on. */
41
+ export interface MarkState {
42
+ readonly x: number;
43
+ readonly y: number;
44
+ readonly graph: GraphPlane | null;
45
+ }
46
+
47
+ const NO_SHIFT: Vector2 = { x: 0, y: 0 };
48
+
49
+ const tracers: { readonly is: (child: ChildView) => boolean; readonly trace: Tracer<never> }[] = [];
50
+
51
+ /** `component`, declared a mark: a holder lays it out on the ink `trace` draws. Hand back the component. */
52
+ export function traced<C extends { is(child: ChildView): boolean }>(component: C, trace: Tracer<never>): C {
53
+ tracers.push({ is: (child) => component.is(child), trace });
54
+ return component;
55
+ }
56
+
57
+ /** The runs `child` draws on `plane`, or `undefined` for a child that is no mark. */
58
+ export function markRuns(child: ChildView, map: CoordinateMap, plane: GraphPlane): Vector2[][] | undefined {
59
+ const mark = tracers.find((entry) => entry.is(child));
60
+ if (!mark) return undefined;
61
+ const state = child.state as unknown as MarkState;
62
+ return (mark.trace as Tracer<MarkState>)(state, { map, shift: { x: state.x, y: state.y }, plane });
63
+ }
64
+
65
+ /** How a mark's own fields read it: through its plane's map when one lays it out, through pixels when none does. */
66
+ export function reading(state: MarkState): MarkReading {
67
+ const plane = state.graph;
68
+ return plane ? { map: plane.map, shift: { x: state.x, y: state.y }, plane } : { map: PIXEL_MAP, shift: NO_SHIFT, plane: null };
69
+ }
70
+
71
+ /** The size of a mark's ink: its box when a node other than a system lays it out. */
72
+ export function inkSize(runs: readonly (readonly Vector2[])[]): Size2D {
73
+ const ink = boundsOfRuns(runs);
74
+ return ink ? { width: ink.maxX - ink.minX, height: ink.maxY - ink.minY } : { width: 0, height: 0 };
75
+ }
76
+
77
+ /** The centre of `runs`' ink in the map's pixels: where a mark's children are placed from. */
78
+ export function inkCentre(runs: readonly (readonly Vector2[])[]): Vector2 {
79
+ const ink = boundsOfRuns(runs);
80
+ return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
81
+ }
82
+
83
+ /** A graph point in a mark's own frame, where a handle on it sits. */
84
+ export function ownPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], point: Vector2): Vector2 {
85
+ const at = read.map.toPoint(point.x + read.shift.x, point.y + read.shift.y);
86
+ const centre = inkCentre(runs);
87
+ return { x: at.x - centre.x, y: at.y - centre.y };
88
+ }
89
+
90
+ /** The graph point at `at` in a mark's own frame: {@link ownPoint} undone. */
91
+ export function graphPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], at: Vector2): Vector2 {
92
+ const centre = inkCentre(runs);
93
+ const point = read.map.toCoords(at.x + centre.x, at.y + centre.y);
94
+ return { x: point.x - read.shift.x, y: point.y - read.shift.y };
95
+ }
96
+
97
+ /** What a mark paints with: a shape's paints, any it withdrew left out. */
98
+ export interface MarkPaint {
99
+ readonly fill?: Fill;
100
+ readonly stroke?: Stroke;
101
+ readonly shadow: Shadow;
102
+ readonly start?: number;
103
+ readonly end?: number;
104
+ }
105
+
106
+ export interface MarkOptions {
107
+ /** Whether each run is closed into an outline. */
108
+ readonly closed?: boolean;
109
+ /** Whether the fill is painted: a mark whose ink has no inside yet (an open line) says no. */
110
+ readonly fills?: boolean;
111
+ /** The stroke along the ink, when it is not the plain one. */
112
+ readonly ink?: (ctx: RenderContext, ink: Extents) => void;
113
+ }
114
+
115
+ const NONE: never[] = [];
116
+
117
+ /** One mark's outline, the same commands for each paint. */
118
+ export function markShape(runs: readonly (readonly Vector2[])[], s: MarkPaint, closed = false): (() => Graphics2D) | null {
119
+ const ink = boundsOfRuns(runs);
120
+ const data = runsToPath(runs, closed);
121
+ if (!ink || data.length === 0) return null;
122
+ const centerBounds: [number, number, number, number] = [ink.minX, ink.minY, ink.maxX, ink.maxY];
123
+ return () => new Graphics2D().path({ data, centerBounds, start: s.start ?? 0, end: s.end ?? 1 });
124
+ }
125
+
126
+ /**
127
+ * A mark's paint: the fill under the stroke along its ink, both before its
128
+ * children, so a point or a tangent riding a plot sits on the curve.
129
+ */
130
+ export function drawMark(ctx: RenderContext, s: MarkPaint, runs: readonly (readonly Vector2[])[], options: MarkOptions = {}): void {
131
+ const shape = markShape(runs, s, options.closed);
132
+ const fill = s.fill ?? NONE;
133
+ painted(ctx, () => {
134
+ if (shape && options.fills !== false) ctx.draw(shape().shadow(s.shadow).fill(fill));
135
+ const ink = options.ink ? boundsOfRuns(runs) : null;
136
+ if (ink && options.ink) options.ink(ctx, ink);
137
+ else if (shape) strokeShape(ctx, shape, s);
138
+ });
139
+ }
140
+
141
+ /** The stroke along a shape, casting the shadow when nothing fills it. */
142
+ export function strokeShape(ctx: RenderContext, shape: () => Graphics2D, s: MarkPaint): void {
143
+ const stroke = (s.stroke ?? NONE) as StrokeResolved[];
144
+ if (stroke.length === 0) return;
145
+ ctx.draw(castsShadow(s) ? shape().shadow(s.shadow).stroke(stroke) : shape().stroke(stroke));
146
+ }
147
+
148
+ /** A shadow is cast by the stroke when nothing visible fills the shape. */
149
+ export function castsShadow(s: MarkPaint): boolean {
150
+ if ((s.shadow as unknown[]).length === 0) return false;
151
+ return !((s.fill ?? NONE) as { opacity?: number }[]).some((layer) => (layer.opacity ?? 1) > 0);
152
+ }
@@ -0,0 +1,61 @@
1
+ import { fillOps, lerpNumber, type Fill, type FillResolved } from "@motionscript/sdk";
2
+
3
+ /** A paint for one part of a node, named the way the node names its parts: a formula's term, a graph's vertex or edge. */
4
+ export interface PartFill {
5
+ part: string;
6
+ fill: Fill;
7
+ /** Which of the places the part is written, counted from 1; 0 is all of them. */
8
+ occurrence?: number;
9
+ /** How far from the node's own paint to `fill`, 0 to 1. A tween's state: an author leaves it out. */
10
+ mix?: number;
11
+ }
12
+
13
+ export interface PartFillResolved {
14
+ part: string;
15
+ fill: FillResolved[];
16
+ occurrence: number;
17
+ mix: number;
18
+ }
19
+
20
+ // Every entry is kept as written, one with no part yet included, so an editor
21
+ // showing the list shows the row it just added; the drawing skips it.
22
+ export function readPartFills(value: readonly PartFill[] | null | undefined): PartFillResolved[] {
23
+ if (!Array.isArray(value)) return [];
24
+ return value.map((entry) => ({
25
+ part: String(entry?.part ?? ""),
26
+ fill: fillOps.resolve(entry?.fill ?? "#ffffff"),
27
+ occurrence: Math.max(0, Math.round(entry?.occurrence ?? 0)),
28
+ mix: Math.min(1, Math.max(0, entry?.mix ?? 1)),
29
+ }));
30
+ }
31
+
32
+ const keyOf = (entry: PartFillResolved) => `${entry.occurrence}|${entry.part}`;
33
+
34
+ // A paint that arrives grows from the node's own, and one that leaves fades back to it.
35
+ export function lerpPartFills(from: PartFillResolved[], to: PartFillResolved[], t: number): PartFillResolved[] {
36
+ if (t <= 0) return from;
37
+ if (t >= 1) return to;
38
+ const before = new Map(from.map((entry) => [keyOf(entry), entry]));
39
+ const out = to.map((entry) => {
40
+ const was = before.get(keyOf(entry));
41
+ before.delete(keyOf(entry));
42
+ return was
43
+ ? { ...entry, fill: fillOps.lerp(was.fill, entry.fill, t), mix: lerpNumber(was.mix, entry.mix, t) }
44
+ : { ...entry, mix: entry.mix * t };
45
+ });
46
+ for (const gone of before.values()) out.push({ ...gone, mix: gone.mix * (1 - t) });
47
+ return out;
48
+ }
49
+
50
+ /** Each part fill with its own key, the one in place of an earlier one for the same part and occurrence. */
51
+ export function withPartFill(fills: readonly PartFillResolved[], entry: PartFill): PartFill[] {
52
+ const occurrence = Math.max(0, Math.round(entry.occurrence ?? 0));
53
+ return [...fills.filter((was) => was.part !== entry.part || was.occurrence !== occurrence), { ...entry, occurrence }];
54
+ }
55
+
56
+ /** `base` with a part's fill laid over it while it arrives, in place of it once it has. */
57
+ export function partPaint(base: FillResolved[], entry: PartFillResolved | undefined): FillResolved[] {
58
+ if (!entry || entry.mix <= 0) return base;
59
+ if (entry.mix >= 1) return entry.fill;
60
+ return [...base, ...entry.fill.map((layer) => ({ ...layer, opacity: (layer.opacity ?? 1) * entry.mix }))];
61
+ }
@@ -0,0 +1,77 @@
1
+ import type { ChildPlane, ChildView, Placement2D, Size2D, Vector2 } from "@motionscript/sdk";
2
+ import type { CoordinateMap } from "./coords";
3
+ import { boundsOfRuns } from "./geometry";
4
+ import type { GraphPlane } from "./graph";
5
+ import { markRuns } from "./mark";
6
+
7
+ /** Where a child that sits somewhere other than its own `x`/`y` is (a point riding a curve), given its own `x`/`y`. */
8
+ export type Anchor = (x: number, y: number) => Vector2;
9
+
10
+ const anchors: { readonly is: (child: ChildView) => boolean; readonly anchor: (state: never, plane: GraphPlane) => Anchor | null }[] = [];
11
+
12
+ /** `component`, declared to sit at `anchor` of its own `x`/`y` on a graph. Hand back the component. */
13
+ export function anchored<C extends { is(child: ChildView): boolean }>(component: C, anchor: (state: never, plane: GraphPlane) => Anchor | null): C {
14
+ anchors.push({ is: (child) => component.is(child), anchor });
15
+ return component;
16
+ }
17
+
18
+ /**
19
+ * Lay `children` out on `map`, for whichever node holds them: a coordinate
20
+ * system, or a plot holding the marks that read it. A mark goes on its ink;
21
+ * anything else keeps its own size and has its `x`/`y` read as graph
22
+ * coordinates. `origin` is the holder's own centre in the map's pixels (y-up),
23
+ * since a child's box is placed from there.
24
+ */
25
+ export function placeOnMap(children: readonly ChildView[], map: CoordinateMap, origin: Vector2, room: Size2D, plane: GraphPlane): Placement2D[] {
26
+ return children.map((child) => {
27
+ const runs = markRuns(child, map, plane);
28
+ if (runs) {
29
+ const ink = boundsOfRuns(runs);
30
+ const cx = ink ? (ink.minX + ink.maxX) / 2 : origin.x;
31
+ const cy = ink ? (ink.minY + ink.maxY) / 2 : origin.y;
32
+ return {
33
+ x: cx - origin.x,
34
+ y: cy - origin.y,
35
+ width: ink ? ink.maxX - ink.minX : 0,
36
+ height: ink ? ink.maxY - ink.minY : 0,
37
+ plane: markPlane(map, cx, cy),
38
+ };
39
+ }
40
+ const size = child.measure({ width: room.width, height: room.height });
41
+ const entry = anchors.find((a) => a.is(child));
42
+ const anchor = entry ? (entry.anchor as (state: unknown, plane: GraphPlane) => Anchor | null)(child.state, plane) : null;
43
+ // Its box is centred on the map's own centre, so its offset from there is its point.
44
+ return { x: -origin.x, y: -origin.y, width: size.width, height: size.height, plane: pointPlane(map, anchor) };
45
+ });
46
+ }
47
+
48
+ /** The plane of a child that is not there yet, under a holder centred on `origin`: where a host would drop one. */
49
+ export function openPlane(map: CoordinateMap, origin: Vector2): ChildPlane {
50
+ return {
51
+ toOffset: (x, y) => {
52
+ const at = map.toPoint(x, y);
53
+ return { x: at.x - origin.x, y: at.y - origin.y };
54
+ },
55
+ toCoords: (x, y) => map.toCoords(x + origin.x, y + origin.y),
56
+ };
57
+ }
58
+
59
+ function pointPlane(map: CoordinateMap, anchor: Anchor | null): ChildPlane {
60
+ const point: Vector2 = { x: 0, y: 0 };
61
+ return {
62
+ toOffset: (x, y) => {
63
+ const at = anchor?.(x, y);
64
+ return at ? map.toPoint(at.x, at.y, point) : map.toPoint(x, y, point);
65
+ },
66
+ toCoords: (x, y) => map.toCoords(x, y),
67
+ };
68
+ }
69
+
70
+ // A mark reads its own `x`/`y` as it draws, so its holder adds nothing; a drag still answers in graph units.
71
+ function markPlane(map: CoordinateMap, cx: number, cy: number): ChildPlane {
72
+ const none: Vector2 = { x: 0, y: 0 };
73
+ return {
74
+ toOffset: () => none,
75
+ toCoords: (x, y) => map.toCoords(cx + x, cy + y),
76
+ };
77
+ }