@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,126 @@
1
+ import {
2
+ colorProperty,
3
+ node,
4
+ property,
5
+ strokeProperty,
6
+ vector2Property,
7
+ type AssetScope,
8
+ type Color,
9
+ type NodeConfig,
10
+ type NodeHandle,
11
+ type NormalizedColor,
12
+ type PathCommand,
13
+ type RenderContext2D,
14
+ type Stroke,
15
+ type Vector2,
16
+ } from "@motionscript/sdk";
17
+ import { braceOutline } from "../kit/brace";
18
+ import { add, between, followed, FollowMark, heading, type FollowProps, type Quad } from "../kit/follow";
19
+ import { declareFormulas, drawLabels } from "../kit/furniture";
20
+
21
+ export type BraceSide = "below" | "above" | "left" | "right";
22
+
23
+ export interface CurlyBraceProps extends FollowProps {
24
+ side?: BraceSide;
25
+ tail?: Vector2;
26
+ head?: Vector2;
27
+ clearance?: number;
28
+ reach?: number;
29
+ label?: string;
30
+ labelSize?: number;
31
+ labelColor?: Color;
32
+ fontFamily?: string;
33
+ }
34
+
35
+ /** The edge of a target a brace runs along, as two corner indexes of its quad, read with the brace's bulge to the left. */
36
+ const EDGE: Record<BraceSide, [number, number]> = {
37
+ below: [2, 3],
38
+ above: [0, 1],
39
+ left: [3, 0],
40
+ right: [1, 2],
41
+ };
42
+
43
+ /**
44
+ * A curly brace along one side of a node or of a part of a formula, or between
45
+ * two points when it names neither, with an optional label at its tip. A label
46
+ * written between dollar signs is typeset.
47
+ */
48
+ @node({
49
+ key: "curlyBrace",
50
+ parentKey: "shape",
51
+ forkable: true,
52
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
53
+ })
54
+ export class CurlyBrace extends FollowMark<CurlyBraceProps> {
55
+ @property({ default: "below", options: ["below", "above", "left", "right"], animatable: false }) declare side: BraceSide;
56
+ /** The two ends of a brace that follows nothing, from the brace's own position. It bulges to the left of tail to head. */
57
+ @vector2Property({ default: { x: -80, y: 0 } }) declare tail: Vector2;
58
+ @vector2Property({ default: { x: 80, y: 0 } }) declare head: Vector2;
59
+ /** The room between the target and the brace. (`gap` is a layout's.) */
60
+ @property({ default: 8, min: 0, unit: "px" }) declare clearance: number;
61
+ /** How far the brace reaches from its ends to its tip. (`depth` is a node's place in 3D.) */
62
+ @property({ default: 18, min: 2, unit: "px" }) declare reach: number;
63
+
64
+ @property({ default: "", animatable: false }) declare label: string;
65
+ @property({ default: 28, min: 1, unit: "px" }) declare labelSize: number;
66
+ @colorProperty({ default: "#e5e7eb" }) declare labelColor: Color;
67
+ @property({ default: "", animatable: false }) declare fontFamily: string;
68
+
69
+ @strokeProperty({ default: { weight: 3, fill: "#e5e7eb", align: "center", cap: "round", join: "round" } })
70
+ declare stroke: Stroke;
71
+
72
+ constructor(props?: NodeConfig<CurlyBrace, CurlyBraceProps>) {
73
+ super(props ?? {});
74
+ }
75
+
76
+ override declareAssets(assets: AssetScope): void {
77
+ super.declareAssets(assets);
78
+ if (this.label !== "" && this.fontFamily !== "") assets.font(this.fontFamily);
79
+ declareFormulas(assets, [this.label]);
80
+ }
81
+
82
+ protected override quad(): Quad | null {
83
+ // No target: the span itself, as an edge with nothing above it.
84
+ return this._spans() ? [this.head, this.tail, this.tail, this.head] : super.quad();
85
+ }
86
+
87
+ /** Whether the brace follows nothing, and so spans its own two points. */
88
+ private _spans(): boolean {
89
+ return !this.retarget && followed(this, this.on) === null;
90
+ }
91
+
92
+ protected outline(quad: Quad): PathCommand[] {
93
+ const { from, to, out } = this._span(quad);
94
+ return braceOutline(from, to, out, this.reach);
95
+ }
96
+
97
+ protected override renderSelf(ctx: RenderContext2D): void {
98
+ super.renderSelf(ctx);
99
+ if (this.label === "") return;
100
+ const quad = this.quad();
101
+ if (!quad) return;
102
+ const { from, to, out } = this._span(quad);
103
+ const reach = Math.min(this.reach, heading(from, to).length / 2) + this.labelSize * 0.75;
104
+ const at = add(between(from, to), out, reach);
105
+ const align = out.x > 0.5 ? "left" : out.x < -0.5 ? "right" : "center";
106
+ const style = { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor };
107
+ for (const part of drawLabels([{ text: this.label, at, align }], style)) ctx.draw(part);
108
+ }
109
+
110
+ /** The edge the brace runs along, pushed out by the gap, and the way it bulges. */
111
+ private _span(quad: Quad): { from: Vector2; to: Vector2; out: Vector2 } {
112
+ const [i, j] = this._spans() ? [1, 0] : EDGE[this.side];
113
+ const { unit } = heading(quad[i], quad[j]);
114
+ // Left of the edge's own direction, in a y-up frame.
115
+ const out = { x: -unit.y, y: unit.x };
116
+ return { from: add(quad[i], out, this.clearance), to: add(quad[j], out, this.clearance), out };
117
+ }
118
+
119
+ override _handles(): readonly NodeHandle[] {
120
+ return this._spans() ? [{ prop: "tail", at: this.tail }, { prop: "head", at: this.head }] : [];
121
+ }
122
+
123
+ override _handleValue(prop: string, at: Vector2): Vector2 | null {
124
+ return this._spans() && (prop === "tail" || prop === "head") ? { x: at.x, y: at.y } : null;
125
+ }
126
+ }
@@ -0,0 +1,89 @@
1
+ import {
2
+ node,
3
+ property,
4
+ strokeProperty,
5
+ type NodeConfig,
6
+ type PathCommand,
7
+ type Stroke,
8
+ type Vector2,
9
+ } from "@motionscript/sdk";
10
+ import { add, between, FollowMark, grown, heading, type FollowProps, type Quad } from "../kit/follow";
11
+
12
+ export type EmphasisShape = "box" | "circle" | "underline" | "cross";
13
+
14
+ export interface EmphasisMarkProps extends FollowProps {
15
+ shape?: EmphasisShape;
16
+ clearance?: number;
17
+ }
18
+
19
+ /** How far a cubic's handle reaches to draw a quarter of an ellipse. */
20
+ const QUARTER = 0.5523;
21
+
22
+ const line = (a: Vector2, b: Vector2): PathCommand[] => [
23
+ { type: "M", x: a.x, y: a.y },
24
+ { type: "L", x: b.x, y: b.y },
25
+ ];
26
+
27
+ const SHAPES: Record<EmphasisShape, (quad: Quad, gap: number) => PathCommand[]> = {
28
+ box: (quad, gap) => {
29
+ const [tl, tr, br, bl] = grown(quad, gap);
30
+ return [{ type: "M", x: tl.x, y: tl.y }, ...[tr, br, bl].map((p): PathCommand => ({ type: "L", x: p.x, y: p.y })), { type: "Z" }];
31
+ },
32
+ // The ellipse through the grown box's corners.
33
+ circle: (quad, gap) => {
34
+ const [tl, tr, br, bl] = grown(quad, gap);
35
+ const centre = between(tl, br);
36
+ const across = { x: ((tr.x - tl.x) / 2) * Math.SQRT2, y: ((tr.y - tl.y) / 2) * Math.SQRT2 };
37
+ const up = { x: ((tl.x - bl.x) / 2) * Math.SQRT2, y: ((tl.y - bl.y) / 2) * Math.SQRT2 };
38
+ const at = (a: number, u: number) => add(add(centre, across, a), up, u);
39
+ const arc = (c1: Vector2, c2: Vector2, end: Vector2): PathCommand => ({
40
+ type: "C", x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: end.x, y: end.y,
41
+ });
42
+ const k = QUARTER;
43
+ const start = at(0, 1);
44
+ return [
45
+ { type: "M", x: start.x, y: start.y },
46
+ arc(at(k, 1), at(1, k), at(1, 0)),
47
+ arc(at(1, -k), at(k, -1), at(0, -1)),
48
+ arc(at(-k, -1), at(-1, -k), at(-1, 0)),
49
+ arc(at(-1, k), at(-k, 1), start),
50
+ { type: "Z" },
51
+ ];
52
+ },
53
+ underline: (quad, gap) => {
54
+ const [, , br, bl] = quad;
55
+ const down = heading(quad[0], bl).unit;
56
+ return line(add(bl, down, gap), add(br, down, gap));
57
+ },
58
+ cross: (quad, gap) => {
59
+ const [tl, tr, br, bl] = grown(quad, gap);
60
+ return [...line(tl, br), ...line(bl, tr)];
61
+ },
62
+ };
63
+
64
+ /**
65
+ * A box, a circle, an underline or a cross drawn on a node, or on one part of
66
+ * a formula. `progressTo` draws it on, and `retargetTo` slides it to another.
67
+ */
68
+ @node({
69
+ key: "emphasisMark",
70
+ parentKey: "shape",
71
+ forkable: true,
72
+ layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
73
+ })
74
+ export class EmphasisMark extends FollowMark<EmphasisMarkProps> {
75
+ @property({ default: "box", options: ["box", "circle", "underline", "cross"], animatable: false }) declare shape: EmphasisShape;
76
+ /** The room between the target and the mark. (`gap` is a layout's.) */
77
+ @property({ default: 10, unit: "px" }) declare clearance: number;
78
+
79
+ @strokeProperty({ default: { weight: 3, fill: "#facc15", align: "center", cap: "round", join: "round" } })
80
+ declare stroke: Stroke;
81
+
82
+ constructor(props?: NodeConfig<EmphasisMark, EmphasisMarkProps>) {
83
+ super(props ?? {});
84
+ }
85
+
86
+ protected outline(quad: Quad): PathCommand[] {
87
+ return SHAPES[this.shape](quad, this.clearance);
88
+ }
89
+ }
package/src/engine.ts ADDED
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The math **kit**: what the nodes share, and what a copied one does not copy.
3
+ *
4
+ * `ms add` writes a node's own files into a project and rewrites its reach for
5
+ * these to `@motionscript/math`, so MathJax, the morph and every shared class
6
+ * stay a real dependency with one identity.
7
+ */
8
+ export {
9
+ blankLatex, buildLatexPath, findLatexParts, latexPartNames, LatexError, LatexNotLoaded, loadMathJax, mathJaxLoaded, MATHJAX_LOADER,
10
+ } from './tex/geometry';
11
+ export type { LatexPart, LatexToken, LatexPathResult } from './tex/geometry';
12
+ export { defaultLatexMorph } from './tex/tween';
13
+ export type { AnimatedToken, LatexMorphStrategy } from './tex/tween';
14
+ export { Latex } from './tex/node';
15
+ export type { LatexProps, LatexStrategies } from './tex/node';
16
+ export { lerpPartFills, partPaint, readPartFills, withPartFill } from './kit/part-fills';
17
+ export type { PartFill, PartFillResolved } from './kit/part-fills';
18
+ export { FILL_PAINT, OUTLINE_TRIM, STROKE_PAINT } from './kit/withdrawn';
19
+ export { createBoundedCache } from './kit/cache';
20
+ export type { BoundedCache } from './kit/cache';
21
+
22
+ export { CoordinateSystem } from './kit/system';
23
+ // A number plane is axes with a grid, so a copied one extends these rather than a copy of them.
24
+ export { Axes2d } from './systems/axes2d';
25
+ export type { Axes2dProps, AxisLayout } from './systems/axes2d';
26
+ export type { Anchored } from './kit/place';
27
+ export { openPlane, placeOnMap } from './kit/place';
28
+ export { Mark } from './kit/mark';
29
+ export { axisScale, cartesianMap, resolveInterval, PIXEL_MAP } from './kit/coords';
30
+ export type { AxisScale, AxisScaling, CartesianOptions, CoordinateMap, Interval } from './kit/coords';
31
+ export { compileExpression, compileExpressionCached, expressionVocabulary, MATH_DIALECT } from './kit/expression';
32
+ export type { CompiledExpression, ExpressionScope, ExpressionVocabulary } from './kit/expression';
33
+ export { equationError, equationParams, parseEquation, EQUATION_VARIABLES } from './kit/equation';
34
+ export type { Equation } from './kit/equation';
35
+ export { lerpDomain, lerpParams, lerpSource, readSource, revisionOf, scopeFor, UNSET_PARAM } from './kit/source';
36
+ export type { PlotSource } from './kit/source';
37
+ export { sampleCurve } from './kit/sampler';
38
+ export type { CurveProjection, SampleOptions } from './kit/sampler';
39
+ export { formatTick, ticksFor } from './kit/ticks';
40
+ export type { Ticks } from './kit/ticks';
41
+ export { boundsOfRuns, clamp, inflate, runsToPath, stated } from './kit/geometry';
42
+ export { curveNamed, curveOf } from './kit/curve';
43
+ export type { Curve } from './kit/curve';
44
+ export { along, AXIS_COLOR, drawLabels, drawn, lerpInterval, PLOT_COLOR, readInterval, withoutTips } from './kit/furniture';
45
+ export type { Label, LabelStyle } from './kit/furniture';
46
+ export { add, between, followed, FollowMark, grown, heading, quadOf } from './kit/follow';
47
+ export type { FollowProps, Parted, Quad, Retarget, Target } from './kit/follow';
48
+ export {
49
+ parseSpaceCurve, parseSurface, spaceCurveError, spaceCurveParams, surfaceError, surfaceParams, EQUATION_KINDS, EQUATION_VARIABLES_3D,
50
+ } from './kit/equation3d';
51
+ export type { EquationLanguage, SpaceCurveEquation, SurfaceEquation } from './kit/equation3d';
52
+ export { aim, graphSpace, spaceOf } from './three/space';
53
+ export type { GraphSpace } from './three/space';
54
+ export { drawLabel3D } from './three/label3d';
55
+ export type { LabelStyle3D } from './three/label3d';
56
+ // A traced path is a plot whose span follows a value, so a copied one extends this rather than a copy of it.
57
+ export { FunctionPlot } from './plots/function-plot';
58
+ export type { FunctionPlotProps } from './plots/function-plot';
59
+ export { FieldMark } from './kit/field-mark';
60
+ export type { FieldProps, FieldSampler, FieldStroke } from './kit/field-mark';
61
+ export { fieldError, fieldParams, parseField, FIELD_VARIABLES } from './kit/field';
62
+ export type { FieldEquation } from './kit/field';
63
+ export { traceContour } from './kit/contour';
64
+ export { colorAt, fillAt, lerpFillScale, lerpScale, readFillScale, readScale } from './kit/scale';
65
+ export { SYSTEMS, SYSTEMS_AND_PLOT } from './kit/placement';
66
+ export {
67
+ IDENTITY_MATRIX, isIdentityMatrix, lerpMatrix, lineThrough, readMatrix, transformedMap, warpedMap,
68
+ } from './kit/coords';
69
+ export type { Matrix2, Warp } from './kit/coords';
70
+ export { braceOutline } from './kit/brace';
71
+ export { GRAPH_LAYOUTS, layOut } from './structures/graph-layout';
72
+ export type { GraphEdge, GraphLayout } from './structures/graph-layout';
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ export * from './tex/node';
2
+ export * from './tex/matrix';
3
+ export * from './systems/number-line';
4
+ export * from './systems/axes2d';
5
+ export * from './systems/number-plane';
6
+ export * from './plots/function-plot';
7
+ export * from './plots/points-plot';
8
+ export * from './marks/plot-point';
9
+ export * from './marks/vector-arrow';
10
+ export * from './marks/curve-area';
11
+ export * from './marks/riemann-sum';
12
+ export * from './marks/tangent-line';
13
+ export * from './marks/drop-lines';
14
+ export * from './annotate/curly-brace';
15
+ export * from './annotate/angle-mark';
16
+ export * from './annotate/emphasis-mark';
17
+ export * from './three/axes3d';
18
+ export * from './three/surface-plot3d';
19
+ export * from './three/curve-plot3d';
20
+ export * from './three/plot-point3d';
21
+ export * from './three/vector-arrow3d';
22
+ export * from './systems/polar-plane';
23
+ export * from './plots/vector-field';
24
+ export * from './plots/stream-lines';
25
+ export * from './plots/traced-path';
26
+ export * from './structures/network-graph';
27
+ export * from './structures/sample-space';
28
+ export * from './engine';
29
+ export { NODES } from "./nodes";
@@ -0,0 +1,37 @@
1
+ import type { PathCommand, Vector2 } from "@motionscript/sdk";
2
+ import { add, between, heading } from "./follow";
3
+
4
+ /** How far a quarter circle's control points sit along its tangents, as a share of its radius. */
5
+ const QUARTER = 0.5523;
6
+
7
+ /**
8
+ * A curly brace from `from` to `to`, bulging toward `out` (a unit vector) and
9
+ * `reach` deep at its tip: what a brace node draws, and a sample space beside
10
+ * its parts. Path commands, y-up.
11
+ */
12
+ export function braceOutline(from: Vector2, to: Vector2, out: Vector2, reach: number): PathCommand[] {
13
+ const { unit, length } = heading(from, to);
14
+ if (length === 0) return [];
15
+ const r = Math.min(reach / 2, length / 4);
16
+ const mid = between(from, to);
17
+ const lift = (p: Vector2, k: number) => add(p, out, k * r);
18
+ const along = (p: Vector2, k: number) => add(p, unit, k * r);
19
+ const h = QUARTER;
20
+ const curve = (c1: Vector2, c2: Vector2, end: Vector2): PathCommand => ({
21
+ type: "C", x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: end.x, y: end.y,
22
+ });
23
+ const a = lift(along(from, 1), 1);
24
+ const b = lift(along(mid, -1), 1);
25
+ const tip = lift(mid, 2);
26
+ const c = lift(along(mid, 1), 1);
27
+ const d = lift(along(to, -1), 1);
28
+ return [
29
+ { type: "M", x: from.x, y: from.y },
30
+ curve(lift(from, h), lift(along(from, 1 - h), 1), a),
31
+ { type: "L", x: b.x, y: b.y },
32
+ curve(lift(along(mid, -1 + h), 1), lift(mid, 2 - h), tip),
33
+ curve(lift(mid, 2 - h), lift(along(mid, 1 - h), 1), c),
34
+ { type: "L", x: d.x, y: d.y },
35
+ curve(lift(along(to, -1 + h), 1), lift(to, h), to),
36
+ ];
37
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * A bounded, least-recently-used memo.
3
+ *
4
+ * Sits under `geometry.ts`, and imports nothing, so it is testable in bare Node:
5
+ * everything else in this package reaches MathJax's browser adaptor at module
6
+ * scope, which means a test that touches it needs a DOM. The one piece of logic
7
+ * here worth being sure about — what gets thrown away when the memo is full —
8
+ * is exactly the piece that does not need one.
9
+ *
10
+ * Least-recently-used rather than oldest-first, and that matters for the shape
11
+ * of work this sees. A `Latex` node re-resolves its formula whenever `fontSize`
12
+ * changes, so animating the size of one formula inserts a fresh key every frame
13
+ * and would, under a first-in-first-out rule, walk the whole memo out of it in a
14
+ * couple of seconds — including the entries for every *other* formula in the
15
+ * scene, none of which had done anything wrong. Touching an entry on read keeps
16
+ * the formulas that are actually being asked for ahead of the ones streaming
17
+ * past.
18
+ */
19
+
20
+ export interface BoundedCache<T> {
21
+ get(key: string): T | undefined;
22
+ set(key: string, value: T): void;
23
+ /** Entries held. For tests and for anyone measuring. */
24
+ readonly size: number;
25
+ }
26
+
27
+ /**
28
+ * @param limit Most entries to hold. One more arriving evicts the least
29
+ * recently read or written.
30
+ */
31
+ export function createBoundedCache<T>(limit: number): BoundedCache<T> {
32
+ // Insertion order is the eviction order, which is what makes a plain `Map`
33
+ // an LRU as long as every read re-inserts.
34
+ const entries = new Map<string, T>();
35
+
36
+ return {
37
+ get(key) {
38
+ if (!entries.has(key)) return undefined;
39
+ const value = entries.get(key) as T;
40
+ // Delete-then-set moves the key to the end, so the next eviction
41
+ // takes the genuinely coldest entry rather than the oldest one.
42
+ entries.delete(key);
43
+ entries.set(key, value);
44
+ return value;
45
+ },
46
+
47
+ set(key, value) {
48
+ entries.delete(key);
49
+ entries.set(key, value);
50
+ while (entries.size > limit) {
51
+ const coldest = entries.keys().next().value;
52
+ if (coldest === undefined) break;
53
+ entries.delete(coldest);
54
+ }
55
+ },
56
+
57
+ get size() {
58
+ return entries.size;
59
+ },
60
+ };
61
+ }
@@ -0,0 +1,94 @@
1
+ import type { Vector2 } from "@motionscript/sdk";
2
+
3
+ /**
4
+ * The lines along which a function of two variables is zero, over a box of
5
+ * graph values: marching squares, its crossings placed by linear interpolation
6
+ * and joined end to end into runs. Points are graph coordinates.
7
+ */
8
+ export function traceContour(
9
+ f: (x: number, y: number) => number,
10
+ [x0, x1]: readonly [number, number],
11
+ [y0, y1]: readonly [number, number],
12
+ columns: number,
13
+ rows: number,
14
+ ): Vector2[][] {
15
+ if (!(x1 > x0) || !(y1 > y0) || columns < 1 || rows < 1) return [];
16
+ const dx = (x1 - x0) / columns;
17
+ const dy = (y1 - y0) / rows;
18
+ const stride = columns + 1;
19
+ const values = new Float64Array(stride * (rows + 1));
20
+ for (let j = 0; j <= rows; j++) {
21
+ for (let i = 0; i <= columns; i++) values[j * stride + i] = f(x0 + i * dx, y0 + j * dy);
22
+ }
23
+
24
+ // A crossing lives on a grid edge, which is its name: two cells sharing the edge share the point.
25
+ const points = new Map<number, Vector2>();
26
+ const edgeCount = stride * (rows + 1);
27
+ const crossing = (i: number, j: number, vertical: boolean): number | null => {
28
+ const a = values[j * stride + i];
29
+ const b = vertical ? values[(j + 1) * stride + i] : values[j * stride + i + 1];
30
+ if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 === b < 0) return null;
31
+ const key = (vertical ? edgeCount : 0) + j * stride + i;
32
+ if (!points.has(key)) {
33
+ const t = a / (a - b);
34
+ points.set(key, vertical ? { x: x0 + i * dx, y: y0 + (j + t) * dy } : { x: x0 + (i + t) * dx, y: y0 + j * dy });
35
+ }
36
+ return key;
37
+ };
38
+
39
+ const next = new Map<number, number[]>();
40
+ const join = (a: number | null, b: number | null): void => {
41
+ if (a === null || b === null) return;
42
+ (next.get(a) ?? next.set(a, []).get(a)!).push(b);
43
+ (next.get(b) ?? next.set(b, []).get(b)!).push(a);
44
+ };
45
+ for (let j = 0; j < rows; j++) {
46
+ for (let i = 0; i < columns; i++) {
47
+ const bottom = crossing(i, j, false);
48
+ const top = crossing(i, j + 1, false);
49
+ const left = crossing(i, j, true);
50
+ const right = crossing(i + 1, j, true);
51
+ const found = [bottom, right, top, left].filter((edge) => edge !== null);
52
+ if (found.length === 2) join(found[0], found[1]);
53
+ else if (found.length === 4) {
54
+ // A saddle: the centre's sign says which pairs of edges the curve joins.
55
+ const centre = f(x0 + (i + 0.5) * dx, y0 + (j + 0.5) * dy);
56
+ const corner = values[j * stride + i];
57
+ if (centre < 0 === corner < 0) {
58
+ join(bottom, right);
59
+ join(top, left);
60
+ } else {
61
+ join(bottom, left);
62
+ join(top, right);
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ const runs: Vector2[][] = [];
69
+ const used = new Set<number>();
70
+ const walk = (from: number): Vector2[] => {
71
+ const run: Vector2[] = [points.get(from)!];
72
+ used.add(from);
73
+ let at = from;
74
+ for (;;) {
75
+ const onward = (next.get(at) ?? []).find((key) => !used.has(key));
76
+ if (onward === undefined) break;
77
+ used.add(onward);
78
+ run.push(points.get(onward)!);
79
+ at = onward;
80
+ }
81
+ return run;
82
+ };
83
+ // Open runs first, from their ends, so each is one run and not two halves.
84
+ for (const [key, links] of next) {
85
+ if (links.length === 1 && !used.has(key)) runs.push(walk(key));
86
+ }
87
+ for (const key of next.keys()) {
88
+ if (used.has(key)) continue;
89
+ const run = walk(key);
90
+ run.push(run[0]);
91
+ runs.push(run);
92
+ }
93
+ return runs.filter((run) => run.length > 1);
94
+ }