@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,57 @@
1
+ import type { Interval } from "./coords";
2
+ import type { ExpressionScope } from "./expression";
3
+
4
+ /** What a plot's equation, domain and named values are held as, and how each tweens: shared by every plot, 2D and 3D. */
5
+
6
+ /** An equation on its way to another: both are drawn, `t` of the way across. */
7
+ export interface PlotSource {
8
+ source: string;
9
+ blend?: { from: string; t: number };
10
+ }
11
+
12
+ export function readSource(value: string | PlotSource | undefined): PlotSource {
13
+ if (typeof value === "string") return { source: value };
14
+ return value ?? { source: "" };
15
+ }
16
+
17
+ // Mid-tween the prop holds both equations, so a seek restores the blend with it.
18
+ export function lerpSource(from: PlotSource, to: PlotSource, t: number): PlotSource {
19
+ if (t <= 0 || from.source === to.source) return t <= 0 ? from : to;
20
+ return t >= 1 ? to : { source: to.source, blend: { from: from.source, t } };
21
+ }
22
+
23
+ export function lerpDomain(from: Interval | null, to: Interval | null, t: number): Interval | null {
24
+ if (!from || !to) return t < 1 ? from : to;
25
+ return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
26
+ }
27
+
28
+ /** What a named value is until it is given one. */
29
+ export const UNSET_PARAM = 1;
30
+
31
+ export function lerpParams(from: ExpressionScope, to: ExpressionScope, t: number): ExpressionScope {
32
+ const out: Record<string, number> = { ...from };
33
+ for (const [name, value] of Object.entries(to)) {
34
+ const start = from[name] ?? UNSET_PARAM;
35
+ out[name] = start + (value - start) * t;
36
+ }
37
+ return out;
38
+ }
39
+
40
+ /** `params` with every name of `names` given a value, so one nobody has set yet still draws. */
41
+ export function scopeFor(names: readonly string[], params: ExpressionScope): ExpressionScope {
42
+ let filled: Record<string, number> | null = null;
43
+ for (const name of names) {
44
+ if (typeof params[name] !== "number") (filled ??= { ...params })[name] = UNSET_PARAM;
45
+ }
46
+ return filled ?? params;
47
+ }
48
+
49
+ /** A string as a number that changes when it does: what a geometry's `revision` wants. */
50
+ export function revisionOf(key: string): number {
51
+ let hash = 0x811c9dc5;
52
+ for (let i = 0; i < key.length; i++) {
53
+ hash ^= key.charCodeAt(i);
54
+ hash = Math.imul(hash, 0x01000193);
55
+ }
56
+ return hash >>> 0;
57
+ }
@@ -0,0 +1,99 @@
1
+ import {
2
+ Clip,
3
+ flowOps,
4
+ Graphics2D,
5
+ insetsOps,
6
+ PaintNode,
7
+ type BoxBounds,
8
+ type ChildPlane,
9
+ type Extents,
10
+ type MeasureContext2D,
11
+ type Node2D,
12
+ type PaintProps,
13
+ type RenderContext2D,
14
+ type Size2D,
15
+ type SizeConstraints,
16
+ type Vector2,
17
+ } from "@motionscript/sdk";
18
+ import type { CoordinateMap } from "./coords";
19
+ import { openPlane, placeOnMap } from "./place";
20
+
21
+ export type { Anchored } from "./place";
22
+
23
+ const NO_CONTENT: Size2D = { width: 0, height: 0 };
24
+ const CENTRE: Vector2 = { x: 0, y: 0 };
25
+
26
+ /**
27
+ * A node whose children sit in its graph coordinates. A child's `x`/`y` are in
28
+ * the system's units and the child keeps its own size, so anything can be put
29
+ * on a graph: a dot, a label, an image. A mark is drawn through the
30
+ * system's map instead and laid out on its ink.
31
+ *
32
+ * The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
33
+ * the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
34
+ * document's children would otherwise share their indices with them.
35
+ */
36
+ export abstract class CoordinateSystem<P extends PaintProps = PaintProps> extends PaintNode<P> {
37
+ /** How graph values land on a plot area of this size. */
38
+ protected abstract mapFor(area: Extents): CoordinateMap;
39
+
40
+ /** What the system draws of itself, under its children. */
41
+ protected abstract furniture(map: CoordinateMap): Graphics2D[];
42
+
43
+ /** This system's coordinates, as last laid out. */
44
+ get coordinates(): CoordinateMap {
45
+ return this._map ?? (this._map = this.mapFor(this._area(this.layoutBounds)));
46
+ }
47
+
48
+ // A system is the size it is given: its children are placed in it, not measured into it.
49
+ override measure(constraints: SizeConstraints): Partial<Size2D> {
50
+ return flowOps.size(this, constraints, this.effectivePadding(), NO_CONTENT);
51
+ }
52
+
53
+ protected override layoutChildren(rect: BoxBounds, scope: MeasureContext2D): void {
54
+ const map = this.mapFor(this._area(rect));
55
+ this._map = map;
56
+ const inner = insetsOps.insetBounds(rect.width, rect.height, this.effectivePadding());
57
+ this._planes = placeOnMap(this.flowChildren(), map, CENTRE, inner, scope);
58
+ }
59
+
60
+ override _planeOf(child: Node2D | null): ChildPlane | null {
61
+ return child ? (this._planes.get(child) ?? null) : openPlane(this.coordinates, CENTRE);
62
+ }
63
+
64
+ protected override shapeGraphics(): Graphics2D | null {
65
+ const r = this.layoutBounds;
66
+ return new Graphics2D().rect({ x: 0, y: 0, width: r.width, height: r.height });
67
+ }
68
+
69
+ protected override renderSelf(ctx: RenderContext2D): void {
70
+ super.renderSelf(ctx);
71
+ const map = this.coordinates;
72
+ const clip = map.spills ? this.clipSelf() : null;
73
+ if (clip) ctx.beginClip(clip);
74
+ for (const part of this.furniture(map)) ctx.draw(part);
75
+ if (clip) ctx.endClip();
76
+ }
77
+
78
+ // The stroke is the axis line, drawn with the furniture: painted here it would land over the children.
79
+ protected override renderStroke(): void {}
80
+
81
+ protected override clipSelf(): Clip | null {
82
+ const { minX, minY, maxX, maxY } = this.coordinates.area;
83
+ return new Clip().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
84
+ }
85
+
86
+ private _map: CoordinateMap | null = null;
87
+ private _planes = new WeakMap<Node2D, ChildPlane>();
88
+
89
+ /** The plot area: the box less its padding, y-up from the box's centre. */
90
+ private _area(rect: BoxBounds): Extents {
91
+ const inner = insetsOps.insetBounds(rect.width, rect.height, this.effectivePadding());
92
+ return {
93
+ minX: inner.x - inner.width / 2,
94
+ maxX: inner.x + inner.width / 2,
95
+ minY: -inner.y - inner.height / 2,
96
+ maxY: -inner.y + inner.height / 2,
97
+ };
98
+ }
99
+ }
@@ -0,0 +1,85 @@
1
+ import { niceStep } from "@motionscript/sdk/component";
2
+ import type { AxisScaling, Interval } from "./coords";
3
+
4
+ /** Where an axis is marked, and the spacing its labels are rounded to. */
5
+ export interface Ticks {
6
+ values: number[];
7
+ step: number;
8
+ }
9
+
10
+ /** Past this an axis is not marked at all, rather than drawn as a solid bar of ticks. */
11
+ const MAX_TICKS = 200;
12
+
13
+ /** How far apart, in pixels, marks sit when the axis picks its own step. */
14
+ const AUTO_SPACING = 64;
15
+
16
+ /**
17
+ * The marks along `range`, drawn over `length` pixels. A `step` of zero lets the
18
+ * axis choose a tidy one (1, 2 or 5 times a power of ten) for its size; a log
19
+ * axis is marked at the powers of ten whatever the step.
20
+ */
21
+ export function ticksFor(range: Interval, step: number, length: number, scaling: AxisScaling = "linear"): Ticks {
22
+ const [min, max] = range;
23
+ if (scaling === "log") return decades(min, max);
24
+ const span = max - min;
25
+ if (!(span > 0)) return { values: [], step: 1 };
26
+ const chosen = step > 0 ? step : niceStep((AUTO_SPACING / Math.max(1, length)) * span);
27
+ return { values: multiplesWithin(min, max, chosen), step: chosen };
28
+ }
29
+
30
+ function decades(min: number, max: number): Ticks {
31
+ const values: number[] = [];
32
+ for (let k = Math.ceil(Math.log10(min) - 1e-9); k <= Math.floor(Math.log10(max) + 1e-9) && values.length < MAX_TICKS; k++) {
33
+ values.push(10 ** k);
34
+ }
35
+ return { values, step: 0 };
36
+ }
37
+
38
+ function multiplesWithin(min: number, max: number, step: number): number[] {
39
+ const first = Math.ceil(min / step - 1e-9);
40
+ const last = Math.floor(max / step + 1e-9);
41
+ const count = last - first + 1;
42
+ if (!Number.isFinite(count) || count <= 0 || count > MAX_TICKS) return [];
43
+ const out = new Array<number>(count);
44
+ // Multiplied rather than accumulated, so the tenth tick of 0.1 is 1 and not 0.9999999999999999.
45
+ for (let i = 0; i < count; i++) out[i] = (first + i) * step;
46
+ return out;
47
+ }
48
+
49
+ function decadeOf(value: number): number {
50
+ return Math.floor(Math.log10(value) + 1e-12);
51
+ }
52
+
53
+ /**
54
+ * `value` as a tick label, with as many decimals as `step` needs and no more:
55
+ * 0.30000000000000004 on a 0.1 ladder is `0.3`. A `step` of zero (a log axis)
56
+ * prints the value as it is.
57
+ */
58
+ export function formatTick(value: number, step: number): string {
59
+ if (value === 0) return "0";
60
+ if (!Number.isFinite(value)) return "";
61
+ if (!(step > 0)) {
62
+ const decade = decadeOf(Math.abs(value));
63
+ return decade >= 6 || decade <= -5 ? trimExponent(value.toExponential(0)) : String(Number(value.toPrecision(12)));
64
+ }
65
+ const decade = decadeOf(Math.abs(value));
66
+ const digits = Math.max(1, decade - decadeOf(step) + 1);
67
+ if ((decade >= 6 || decade <= -5) && digits <= 6) return trimExponent(value.toExponential(digits - 1));
68
+ const decimals = decimalsOf(step);
69
+ const text = value.toFixed(decimals);
70
+ return decimals > 0 ? text.replace(/\.?0+$/, "") : text;
71
+ }
72
+
73
+ /** The decimal places it takes to write `step` out: 1 for 0.5, 2 for 0.25. */
74
+ function decimalsOf(step: number): number {
75
+ for (let d = 0; d < 12; d++) {
76
+ const scaled = step * 10 ** d;
77
+ if (Math.abs(scaled - Math.round(scaled)) < 1e-9 * Math.max(1, scaled)) return d;
78
+ }
79
+ return 12;
80
+ }
81
+
82
+ function trimExponent(text: string): string {
83
+ const [mantissa, exponent] = text.split("e");
84
+ return `${mantissa.replace(/\.?0+$/, "")}e${Number(exponent)}`;
85
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * What a math node built on a general shape inherits and may have no use for,
3
+ * as `withdraw` takes it: a node that is only a line has no fill, an area no
4
+ * outline, and one that reveals itself in its own way no outline trim.
5
+ */
6
+ export const FILL_PAINT = ["fill", "fillTo"] as const;
7
+ export const STROKE_PAINT = ["stroke", "strokeTo"] as const;
8
+ export const OUTLINE_TRIM = ["start", "end", "progressTo", "flash"] as const;
@@ -0,0 +1,89 @@
1
+ import {
2
+ command,
3
+ fillProperty,
4
+ node,
5
+ property,
6
+ withdraw,
7
+ type Command,
8
+ type CommandArgs,
9
+ type Fill,
10
+ type NodeConfig,
11
+ type PaintProps,
12
+ type Vector2,
13
+ } from "@motionscript/sdk";
14
+ import type { CoordinateMap, Interval } from "../kit/coords";
15
+ import { clamp, stated } from "../kit/geometry";
16
+ import { Mark } from "../kit/mark";
17
+ import { curveNamed, curveOf } from "../kit/curve";
18
+ import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
19
+ import { SYSTEMS } from "../kit/placement";
20
+ import { OUTLINE_TRIM, STROKE_PAINT } from "../kit/withdrawn";
21
+
22
+ export interface CurveAreaProps extends PaintProps {
23
+ on?: string;
24
+ under?: string;
25
+ domain?: Interval;
26
+ }
27
+
28
+ /**
29
+ * The region between a plot and the x axis, or between two plots, over a span
30
+ * of x. It sits beside the plots it reads rather than inside one: it may read
31
+ * two, and it has a life of its own on the timeline, swept open or kept while
32
+ * a curve changes.
33
+ */
34
+ @node({
35
+ key: "curveArea",
36
+ within: SYSTEMS,
37
+ beside: ["functionPlot"],
38
+ parentKey: "shape",
39
+ forkable: true,
40
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
41
+ })
42
+ @withdraw(...STROKE_PAINT, ...OUTLINE_TRIM)
43
+ export class CurveArea extends Mark<CurveAreaProps> {
44
+ /** The plot that bounds the area, by id. */
45
+ @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
46
+ /** A second plot to fill down to, in place of the x axis. */
47
+ @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare under: string;
48
+ @property({ default: [0, 1], mapper: readInterval, tween: lerpInterval })
49
+ declare domain: Interval;
50
+
51
+ @fillProperty({ default: { type: "solid", color: PLOT_COLOR, opacity: 0.35 } }) declare fill: Fill;
52
+
53
+ constructor(props?: NodeConfig<CurveArea, CurveAreaProps>) {
54
+ super(props ?? {});
55
+ }
56
+
57
+ /** Animate the span of x that is filled: sweep an area open, or slide it along. */
58
+ @command({ args: ["domain"] })
59
+ domainTo(args: CommandArgs<{ domain: Interval }> & { duration: number }): Command<CurveAreaProps> {
60
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
61
+ }
62
+
63
+ protected override isClosed(): boolean {
64
+ return true;
65
+ }
66
+
67
+ protected trace(map: CoordinateMap): Vector2[][] {
68
+ const top = curveOf(this, this.on);
69
+ if (!top) return [];
70
+ const floor = curveNamed(this, this.under);
71
+ const [yMin, yMax] = map.yRange;
72
+ const a = Math.max(this.domain[0], map.xRange[0]);
73
+ const b = Math.min(this.domain[1], map.xRange[1]);
74
+ if (!(b > a)) return [];
75
+
76
+ const width = Math.abs(map.toPoint(b, 0).x - map.toPoint(a, 0).x);
77
+ const steps = Math.max(8, Math.min(600, Math.round(width / 2)));
78
+ // Held inside the system's range, and at the axis where a curve has no value.
79
+ const height = (value: number) => (Number.isFinite(value) ? clamp(value, yMin, yMax) : clamp(0, yMin, yMax));
80
+ const upper: Vector2[] = [];
81
+ const lower: Vector2[] = [];
82
+ for (let i = 0; i <= steps; i++) {
83
+ const x = a + ((b - a) * i) / steps;
84
+ upper.push(map.toPoint(x, height(top.valueAt(x))));
85
+ lower.push(map.toPoint(x, height(floor ? floor.valueAt(x) : 0)));
86
+ }
87
+ return [[...upper, ...lower.reverse()]];
88
+ }
89
+ }
@@ -0,0 +1,63 @@
1
+ import {
2
+ node,
3
+ property,
4
+ strokeProperty,
5
+ withdraw,
6
+ type NodeConfig,
7
+ type PaintProps,
8
+ type Stroke,
9
+ type Vector2,
10
+ } from "@motionscript/sdk";
11
+ import type { CoordinateMap } from "../kit/coords";
12
+ import { clamp } from "../kit/geometry";
13
+ import { Mark } from "../kit/mark";
14
+ import { curveOf } from "../kit/curve";
15
+ import { AXIS_COLOR } from "../kit/furniture";
16
+ import { SYSTEMS_AND_PLOT } from "../kit/placement";
17
+ import { FILL_PAINT } from "../kit/withdrawn";
18
+
19
+ export type DropAxis = "x" | "y" | "both";
20
+
21
+ export interface DropLinesProps extends PaintProps {
22
+ on?: string;
23
+ axis?: DropAxis;
24
+ }
25
+
26
+ /**
27
+ * Guide lines from a graph point to the axes, reading its coordinates off them.
28
+ * The point is this node's `x`/`y`, or the point of a plot above its `x`.
29
+ */
30
+ @node({
31
+ key: "dropLines",
32
+ within: SYSTEMS_AND_PLOT,
33
+ parentKey: "shape",
34
+ forkable: true,
35
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
36
+ })
37
+ @withdraw(...FILL_PAINT)
38
+ export class DropLines extends Mark<DropLinesProps> {
39
+ /** A plot to take the point's height from, by id. */
40
+ @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
41
+ /** Which axis to drop to: `x` draws the vertical line, `y` the horizontal one. */
42
+ @property({ default: "both", options: ["x", "y", "both"], animatable: false }) declare axis: DropAxis;
43
+
44
+ @strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center", dash: [8, 6] } })
45
+ declare stroke: Stroke;
46
+
47
+ constructor(props?: NodeConfig<DropLines, DropLinesProps>) {
48
+ super(props ?? {});
49
+ }
50
+
51
+ protected trace(map: CoordinateMap): Vector2[][] {
52
+ const x = this.x;
53
+ const plot = curveOf(this, this.on);
54
+ const y = plot ? plot.valueAt(x) : this.y;
55
+ if (!Number.isFinite(y)) return [];
56
+ const at = map.toPoint(x, y);
57
+ const runs: Vector2[][] = [];
58
+ // Each line starts on its axis, so drawing it on grows it towards the point.
59
+ if (this.axis !== "y") runs.push([map.toPoint(x, clamp(0, map.yRange[0], map.yRange[1])), at]);
60
+ if (this.axis !== "x") runs.push([map.toPoint(clamp(0, map.xRange[0], map.xRange[1]), y), at]);
61
+ return runs;
62
+ }
63
+ }
@@ -0,0 +1,107 @@
1
+ import {
2
+ colorProperty,
3
+ fillProperty,
4
+ Graphics2D,
5
+ node,
6
+ PaintNode,
7
+ property,
8
+ withdraw,
9
+ type AssetScope,
10
+ type Color,
11
+ type Fill,
12
+ type NodeConfig,
13
+ type NormalizedColor,
14
+ type PaintProps,
15
+ type RenderContext2D,
16
+ type Size2D,
17
+ type Vector2,
18
+ } from "@motionscript/sdk";
19
+ import type { Anchored } from "../kit/place";
20
+ import { curveOf } from "../kit/curve";
21
+ import { declareFormulas, drawLabels, type Label } from "../kit/furniture";
22
+ import { SYSTEMS_AND_PLOT } from "../kit/placement";
23
+ import { OUTLINE_TRIM } from "../kit/withdrawn";
24
+
25
+ export interface PlotPointProps extends PaintProps {
26
+ on?: string;
27
+ radius?: number;
28
+ label?: string;
29
+ labelSide?: "above" | "below" | "left" | "right";
30
+ labelSize?: number;
31
+ labelColor?: Color;
32
+ fontFamily?: string;
33
+ }
34
+
35
+ /**
36
+ * A dot at graph coordinates, with an optional label. Named a plot in `on`, it
37
+ * rides that curve: its `x` is where along it, and its height is the curve's.
38
+ */
39
+ @node({
40
+ key: "plotPoint",
41
+ within: SYSTEMS_AND_PLOT,
42
+ parentKey: "shape",
43
+ forkable: true,
44
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
45
+ })
46
+ @withdraw(...OUTLINE_TRIM)
47
+ export class PlotPoint extends PaintNode<PlotPointProps> implements Anchored {
48
+ /** The plot this point rides, by id; empty for a point that sits at its own `x`/`y`. */
49
+ @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
50
+ @property({ default: 7, min: 0, unit: "px" }) declare radius: number;
51
+ @property({ default: "", animatable: false }) declare label: string;
52
+ @property({ default: "above", options: ["above", "below", "left", "right"], animatable: false })
53
+ declare labelSide: "above" | "below" | "left" | "right";
54
+ @property({ default: 18, min: 1, unit: "px" }) declare labelSize: number;
55
+ @colorProperty({ default: "#e5e7eb" }) declare labelColor: Color;
56
+ @property({ default: "", animatable: false }) declare fontFamily: string;
57
+
58
+ @fillProperty({ default: "#e5e7eb" }) declare fill: Fill;
59
+
60
+ constructor(props?: NodeConfig<PlotPoint, PlotPointProps>) {
61
+ super(props ?? {});
62
+ }
63
+
64
+ /** The graph height the point is at: the curve's when it rides one. */
65
+ get value(): number {
66
+ return this.anchorAt(this.x, this.y).y;
67
+ }
68
+
69
+ anchorAt(x: number, y: number): Vector2 {
70
+ const height = curveOf(this, this.on)?.valueAt(x);
71
+ // Off the end of its curve a point stays where its own `y` puts it rather than vanishing.
72
+ return { x, y: height !== undefined && Number.isFinite(height) ? height : y };
73
+ }
74
+
75
+ override measure(): Partial<Size2D> {
76
+ return { width: this.radius * 2, height: this.radius * 2 };
77
+ }
78
+
79
+ override declareAssets(assets: AssetScope): void {
80
+ super.declareAssets(assets);
81
+ if (this.label !== "" && this.fontFamily !== "") assets.font(this.fontFamily);
82
+ declareFormulas(assets, [this.label]);
83
+ }
84
+
85
+ protected override shapeGraphics(): Graphics2D | null {
86
+ const size = this.radius * 2;
87
+ return size > 0 ? new Graphics2D().ellipse({ x: 0, y: 0, width: size, height: size }) : null;
88
+ }
89
+
90
+ protected override renderSelf(ctx: RenderContext2D): void {
91
+ super.renderSelf(ctx);
92
+ if (this.label === "") return;
93
+ const gap = this.radius + this.labelSize * 0.45;
94
+ const side = this.labelSide;
95
+ const horizontal = side === "left" || side === "right";
96
+ const label: Label = {
97
+ text: this.label,
98
+ align: side === "left" ? "right" : side === "right" ? "left" : "center",
99
+ at: {
100
+ x: horizontal ? (side === "left" ? -1 : 1) * gap : 0,
101
+ y: horizontal ? 0 : (side === "below" ? -1 : 1) * (gap + this.labelSize / 2),
102
+ },
103
+ };
104
+ const style = { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor };
105
+ for (const part of drawLabels([label], style)) ctx.draw(part);
106
+ }
107
+ }
@@ -0,0 +1,98 @@
1
+ import {
2
+ command,
3
+ fillProperty,
4
+ node,
5
+ property,
6
+ strokeProperty,
7
+ withdraw,
8
+ type Command,
9
+ type CommandArgs,
10
+ type Fill,
11
+ type NodeConfig,
12
+ type PaintProps,
13
+ type Stroke,
14
+ type Vector2,
15
+ } from "@motionscript/sdk";
16
+ import type { CoordinateMap, Interval } from "../kit/coords";
17
+ import { clamp, stated } from "../kit/geometry";
18
+ import { Mark } from "../kit/mark";
19
+ import { curveOf } from "../kit/curve";
20
+ import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
21
+ import { SYSTEMS } from "../kit/placement";
22
+ import { OUTLINE_TRIM } from "../kit/withdrawn";
23
+
24
+ export type RiemannSample = "left" | "right" | "center";
25
+
26
+ export interface RiemannSumProps extends PaintProps {
27
+ on?: string;
28
+ domain?: Interval;
29
+ dx?: number;
30
+ sample?: RiemannSample;
31
+ }
32
+
33
+ /** More rectangles than this is a filled area with extra steps. */
34
+ const MAX_RECTANGLES = 2000;
35
+
36
+ /** Rectangles under a plot, each `dx` wide and as tall as the curve where it is sampled. */
37
+ @node({
38
+ key: "riemannSum",
39
+ within: SYSTEMS,
40
+ beside: ["functionPlot"],
41
+ parentKey: "shape",
42
+ forkable: true,
43
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
44
+ })
45
+ @withdraw(...OUTLINE_TRIM)
46
+ export class RiemannSum extends Mark<RiemannSumProps> {
47
+ @property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
48
+ @property({ default: [0, 1], mapper: readInterval, tween: lerpInterval })
49
+ declare domain: Interval;
50
+ /** Each rectangle's width, in graph units. */
51
+ @property({ default: 0.25, min: 0.001 }) declare dx: number;
52
+ /** Where in its width a rectangle reads the curve's height. */
53
+ @property({ default: "left", options: ["left", "right", "center"], animatable: false }) declare sample: RiemannSample;
54
+
55
+ @fillProperty({ default: { type: "solid", color: PLOT_COLOR, opacity: 0.35 } }) declare fill: Fill;
56
+ @strokeProperty({ default: { weight: 1.5, fill: PLOT_COLOR, align: "center", join: "round" } }) declare stroke: Stroke;
57
+
58
+ constructor(props?: NodeConfig<RiemannSum, RiemannSumProps>) {
59
+ super(props ?? {});
60
+ }
61
+
62
+ /** Animate the rectangles' width: narrower is the sum closing in on the area. */
63
+ @command({ args: ["dx"] })
64
+ refineTo(args: CommandArgs<{ dx: number }> & { duration: number }): Command<RiemannSumProps> {
65
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
66
+ }
67
+
68
+ /** Animate the span of x the rectangles cover. */
69
+ @command({ args: ["domain"] })
70
+ domainTo(args: CommandArgs<{ domain: Interval }> & { duration: number }): Command<RiemannSumProps> {
71
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
72
+ }
73
+
74
+ protected override isClosed(): boolean {
75
+ return true;
76
+ }
77
+
78
+ protected trace(map: CoordinateMap): Vector2[][] {
79
+ const plot = curveOf(this, this.on);
80
+ const [a, b] = this.domain;
81
+ const dx = this.dx;
82
+ if (!plot || !(b > a) || !(dx > 0)) return [];
83
+ const [yMin, yMax] = map.yRange;
84
+ const base = clamp(0, yMin, yMax);
85
+ const at = this.sample === "left" ? 0 : this.sample === "right" ? 1 : 0.5;
86
+ const count = Math.min(MAX_RECTANGLES, Math.ceil((b - a) / dx - 1e-9));
87
+ const runs: Vector2[][] = [];
88
+ for (let i = 0; i < count; i++) {
89
+ const left = a + i * dx;
90
+ const right = Math.min(b, left + dx);
91
+ const value = plot.valueAt(left + (right - left) * at);
92
+ if (!Number.isFinite(value)) continue;
93
+ const height = clamp(value, yMin, yMax);
94
+ runs.push([map.toPoint(left, base), map.toPoint(left, height), map.toPoint(right, height), map.toPoint(right, base)]);
95
+ }
96
+ return runs;
97
+ }
98
+ }