@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,72 @@
1
+ import { component2d, Types, } from "@motionscript/sdk";
2
+ import { PAINT, PAINT_COMMANDS } from "../kit/component.js";
3
+ import { PLOT_COLOR } from "../kit/furniture.js";
4
+ import { PLANE_ENTRY } from "../kit/graph.js";
5
+ import { drawMark, inkSize, reading, traced } from "../kit/mark.js";
6
+ // A shorter list is padded with its own last point, so a line grows new points out of its end.
7
+ function lerpPoints(from, to, t) {
8
+ const count = Math.max(from.length, to.length);
9
+ if (from.length === 0 || to.length === 0)
10
+ return t < 1 ? from : to;
11
+ const out = new Array(count);
12
+ for (let i = 0; i < count; i++) {
13
+ const a = from[Math.min(i, from.length - 1)];
14
+ const b = to[Math.min(i, to.length - 1)];
15
+ out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
16
+ }
17
+ return out;
18
+ }
19
+ /** Points on the curve between each pair of a smooth line's points. */
20
+ const SMOOTH_STEPS = 16;
21
+ const state = {
22
+ ...PAINT,
23
+ points: Types.custom([], { lerp: lerpPoints }),
24
+ /** Join the last point to the first. */
25
+ closed: Types.boolean(false, { animatable: false }),
26
+ /** Curve through the points instead of turning a corner at each. */
27
+ smooth: Types.boolean(false, { animatable: false }),
28
+ graph: PLANE_ENTRY,
29
+ };
30
+ /** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
31
+ export const PointsPlot = traced(component2d({
32
+ key: "pointsPlot",
33
+ state,
34
+ defaults: { stroke: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
35
+ commands: {
36
+ ...PAINT_COMMANDS,
37
+ /** Move the points to new places; extra ones grow from the line's end. */
38
+ pointsTo: { args: ["points"] },
39
+ },
40
+ measure: (_measurer, s) => inkSize(tracePoints(s, reading(s))),
41
+ // An open line has no inside, so its fill waits until it is closed.
42
+ render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),
43
+ }), tracePoints);
44
+ function tracePoints(s, { map, shift }) {
45
+ const pixels = s.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
46
+ if (pixels.length < 2)
47
+ return [];
48
+ return [s.smooth && pixels.length > 2 ? spline(pixels, s.closed) : pixels];
49
+ }
50
+ // Catmull–Rom, so the curve passes through every point it was given.
51
+ function spline(points, closed) {
52
+ const n = points.length;
53
+ const at = (i) => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);
54
+ const out = [];
55
+ const spans = closed ? n : n - 1;
56
+ for (let i = 0; i < spans; i++) {
57
+ const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
58
+ for (let s = 0; s < SMOOTH_STEPS; s++) {
59
+ const t = s / SMOOTH_STEPS;
60
+ const t2 = t * t;
61
+ const t3 = t2 * t;
62
+ out.push({
63
+ x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),
64
+ y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),
65
+ });
66
+ }
67
+ }
68
+ if (!closed)
69
+ out.push(points[n - 1]);
70
+ return out;
71
+ }
72
+ //# sourceMappingURL=points-plot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"points-plot.js","sourceRoot":"","sources":["../../src/plots/points-plot.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,KAAK,GAaR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OA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CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;aACpI,CAAC,CAAC;QACP,CAAC;IACL,CAAC;IACD,IAAI,CAAC,MAAM;QAAE,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACrC,OAAO,GAAG,CAAC;AACf,CAAC","sourcesContent":["import {\n component2d,\n Types,\n type NodeOf,\n type PaintProps,\n type Vector2,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Fill as _Fill,\n type FillResolved as _FillResolved,\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type Shadow as _Shadow,\n type ShadowResolved as _ShadowResolved,\n type Stroke as _Stroke,\n type StrokeResolved as _StrokeResolved,\n} from \"@motionscript/sdk\";\nimport { PAINT, PAINT_COMMANDS } from \"../kit/component\";\nimport { PLOT_COLOR } from \"../kit/furniture\";\nimport { PLANE_ENTRY } from \"../kit/graph\";\nimport { drawMark, inkSize, reading, traced, type MarkReading } from \"../kit/mark\";\n\nexport interface PointsPlotProps extends PaintProps {\n points?: Vector2[];\n closed?: boolean;\n smooth?: boolean;\n}\n\n// A shorter list is padded with its own last point, so a line grows new points out of its end.\nfunction lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {\n const count = Math.max(from.length, to.length);\n if (from.length === 0 || to.length === 0) return t < 1 ? from : to;\n const out = new Array<Vector2>(count);\n for (let i = 0; i < count; i++) {\n const a = from[Math.min(i, from.length - 1)];\n const b = to[Math.min(i, to.length - 1)];\n out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };\n }\n return out;\n}\n\n/** Points on the curve between each pair of a smooth line's points. */\nconst SMOOTH_STEPS = 16;\n\nconst state = {\n ...PAINT,\n points: Types.custom<Vector2[]>([], { lerp: lerpPoints }),\n /** Join the last point to the first. */\n closed: Types.boolean(false, { animatable: false }),\n /** Curve through the points instead of turning a corner at each. */\n smooth: Types.boolean(false, { animatable: false }),\n graph: PLANE_ENTRY,\n};\n\ninterface PointsValues {\n readonly points: Vector2[];\n readonly closed: boolean;\n readonly smooth: boolean;\n}\n\n/** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */\nexport const PointsPlot = traced(component2d({\n key: \"pointsPlot\",\n state,\n defaults: { stroke: { weight: 3, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" }, width: \"hug\", height: \"hug\" },\n commands: {\n ...PAINT_COMMANDS,\n /** Move the points to new places; extra ones grow from the line's end. */\n pointsTo: { args: [\"points\"] },\n },\n measure: (_measurer, s) => inkSize(tracePoints(s, reading(s))),\n // An open line has no inside, so its fill waits until it is closed.\n render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),\n}), tracePoints);\n\nexport type PointsPlot = NodeOf<typeof PointsPlot>;\n\nfunction tracePoints(s: PointsValues, { map, shift }: MarkReading): Vector2[][] {\n const pixels = s.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));\n if (pixels.length < 2) return [];\n return [s.smooth && pixels.length > 2 ? spline(pixels, s.closed) : pixels];\n}\n\n// Catmull–Rom, so the curve passes through every point it was given.\nfunction spline(points: readonly Vector2[], closed: boolean): Vector2[] {\n const n = points.length;\n const at = (i: number): Vector2 => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);\n const out: Vector2[] = [];\n const spans = closed ? n : n - 1;\n for (let i = 0; i < spans; i++) {\n const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);\n for (let s = 0; s < SMOOTH_STEPS; s++) {\n const t = s / SMOOTH_STEPS;\n const t2 = t * t;\n const t3 = t2 * t;\n out.push({\n x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),\n y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),\n });\n }\n }\n if (!closed) out.push(points[n - 1]);\n return out;\n}\n"]}
@@ -0,0 +1,76 @@
1
+ import { type NodeOf, type Fill as _Fill, type FillResolved as _FillResolved, type Node2D as _Node2D, type Node2DProps as _Node2DProps, type Shadow as _Shadow, type ShadowResolved as _ShadowResolved, type Stroke as _Stroke, type StrokeResolved as _StrokeResolved } from "@motionscript/sdk";
2
+ import { type FieldProps } from "../kit/field-mark.js";
3
+ export interface StreamLinesProps extends FieldProps {
4
+ spacing?: number;
5
+ length?: number;
6
+ window?: number;
7
+ phase?: number;
8
+ }
9
+ /**
10
+ * The lines a vector field carries a point along, one from each point of a
11
+ * grid: `(-y, x)` draws circles. With a `window` below one, each line shows
12
+ * only a stretch of itself, and `flowTo` sends those stretches along.
13
+ */
14
+ export declare const StreamLines: import("@motionscript/sdk").Component<"streamLines", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
15
+ /** How far apart the lines start, in graph units. */
16
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
17
+ /** How far each line runs from where it starts, in graph units along itself. */
18
+ length: import("@motionscript/sdk").Type<number, number, false>;
19
+ /** The share of each line that is shown at once; one shows all of it. */
20
+ window: import("@motionscript/sdk").Type<number, number, false>;
21
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
22
+ phase: import("@motionscript/sdk").Type<number, number, false>;
23
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
24
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
25
+ strengthFills: import("@motionscript/sdk").Type<_Fill[], _FillResolved[][], false>;
26
+ maxMagnitude: import("@motionscript/sdk").Type<number, number, false>;
27
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
28
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
29
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
30
+ }>, {
31
+ readonly flowTo: readonly ["phase"];
32
+ readonly morphTo: readonly ["expression"];
33
+ readonly paramsTo: readonly ["params"];
34
+ readonly strokeTo: readonly ["stroke"];
35
+ readonly shadowTo: readonly ["shadow"];
36
+ }, null, _Node2D<_Node2DProps>, import("@motionscript/sdk").CallProps<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
37
+ /** How far apart the lines start, in graph units. */
38
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
39
+ /** How far each line runs from where it starts, in graph units along itself. */
40
+ length: import("@motionscript/sdk").Type<number, number, false>;
41
+ /** The share of each line that is shown at once; one shows all of it. */
42
+ window: import("@motionscript/sdk").Type<number, number, false>;
43
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
44
+ phase: import("@motionscript/sdk").Type<number, number, false>;
45
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
46
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
47
+ strengthFills: import("@motionscript/sdk").Type<_Fill[], _FillResolved[][], false>;
48
+ maxMagnitude: import("@motionscript/sdk").Type<number, number, false>;
49
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
50
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
51
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
52
+ }>, null, import("@motionscript/sdk").BuiltNode<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
53
+ /** How far apart the lines start, in graph units. */
54
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
55
+ /** How far each line runs from where it starts, in graph units along itself. */
56
+ length: import("@motionscript/sdk").Type<number, number, false>;
57
+ /** The share of each line that is shown at once; one shows all of it. */
58
+ window: import("@motionscript/sdk").Type<number, number, false>;
59
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
60
+ phase: import("@motionscript/sdk").Type<number, number, false>;
61
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
62
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
63
+ strengthFills: import("@motionscript/sdk").Type<_Fill[], _FillResolved[][], false>;
64
+ maxMagnitude: import("@motionscript/sdk").Type<number, number, false>;
65
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
66
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
67
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
68
+ }>, {
69
+ readonly flowTo: readonly ["phase"];
70
+ readonly morphTo: readonly ["expression"];
71
+ readonly paramsTo: readonly ["params"];
72
+ readonly strokeTo: readonly ["stroke"];
73
+ readonly shadowTo: readonly ["shadow"];
74
+ }, _Node2D<_Node2DProps>>>>;
75
+ export type StreamLines = NodeOf<typeof StreamLines>;
76
+ //# sourceMappingURL=stream-lines.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-lines.d.ts","sourceRoot":"","sources":["../../src/plots/stream-lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,MAAM,EAGX,KAAK,IAAI,IAAI,KAAK,EAClB,KAAK,YAAY,IAAI,aAAa,EAClC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,IAAI,YAAY,EAChC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACzC,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAyE,KAAK,UAAU,EAAyD,MAAM,mBAAmB,CAAC;AAKlL,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB;AAiCD;;;;GAIG;AACH,eAAO,MAAM,WAAW;IA7BpB,qDAAqD;;IAErD,gFAAgF;;IAEhF,yEAAyE;;IAEzE,kGAAkG;;;;;;;;;;;;;;;;IANlG,qDAAqD;;IAErD,gFAAgF;;IAEhF,yEAAyE;;IAEzE,kGAAkG;;;;;;;;;;IANlG,qDAAqD;;IAErD,gFAAgF;;IAEhF,yEAAyE;;IAEzE,kGAAkG;;;;;;;;;;;;;;;2BAkClC,CAAC;AAErE,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,WAAW,CAAC,CAAC"}
@@ -0,0 +1,122 @@
1
+ import { component2d, Types, } from "@motionscript/sdk";
2
+ import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField } from "../kit/field-mark.js";
3
+ import { PLOT_COLOR } from "../kit/furniture.js";
4
+ import { clamp } from "../kit/geometry.js";
5
+ import { inkSize, reading, traced } from "../kit/mark.js";
6
+ const MAX_LINES = 1600;
7
+ /** Steps a line takes across one spacing: what keeps a tight swirl round. */
8
+ const STEPS_PER_SPACING = 8;
9
+ const MAX_STEPS = 600;
10
+ const state = {
11
+ ...FIELD_STATE,
12
+ /** How far apart the lines start, in graph units. */
13
+ spacing: Types.number(1, { min: 0.01, step: 0.1 }),
14
+ /** How far each line runs from where it starts, in graph units along itself. */
15
+ length: Types.number(3, { min: 0, step: 0.1 }),
16
+ /** The share of each line that is shown at once; one shows all of it. */
17
+ window: Types.number(1, { min: 0.01, max: 1, step: 0.01 }),
18
+ /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
19
+ phase: Types.number(0, { step: 0.01 }),
20
+ };
21
+ const LINES = {
22
+ own: (s) => `${s.spacing}|${s.length}|${s.window}|${s.phase}`,
23
+ strokes: (s, map, field) => lines(s, map, field),
24
+ };
25
+ const traceLines = (s, read) => traceField("streamLines", LINES, s, read);
26
+ /**
27
+ * The lines a vector field carries a point along, one from each point of a
28
+ * grid: `(-y, x)` draws circles. With a `window` below one, each line shows
29
+ * only a stretch of itself, and `flowTo` sends those stretches along.
30
+ */
31
+ export const StreamLines = traced(component2d({
32
+ key: "streamLines",
33
+ state,
34
+ defaults: { stroke: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
35
+ commands: {
36
+ ...FIELD_COMMANDS,
37
+ /** Send the shown stretches along their lines: a whole number runs each line that many times over. */
38
+ flowTo: { args: ["phase"] },
39
+ },
40
+ measure: (_measurer, s) => inkSize(traceLines(s, reading(s)).runs),
41
+ render: (ctx, s) => drawField(ctx, s, traceLines(s, reading(s))),
42
+ }), (s, read) => traceLines(s, read).runs);
43
+ function lines(s, map, field) {
44
+ const [x0, x1] = map.xRange;
45
+ const [y0, y1] = map.yRange;
46
+ let spacing = Math.max(s.spacing, 1e-6);
47
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
48
+ while (count() > MAX_LINES)
49
+ spacing *= 2;
50
+ const step = spacing / STEPS_PER_SPACING;
51
+ const steps = Math.min(MAX_STEPS, Math.round(s.length / step));
52
+ if (steps < 1)
53
+ return [];
54
+ // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
55
+ const inside = (x, y) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
56
+ const window = clamp(s.window, 0, 1);
57
+ const drawn = [];
58
+ let index = 0;
59
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
60
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
61
+ const line = follow(field, i * spacing, j * spacing, step, steps, inside);
62
+ index++;
63
+ if (line.points.length < 2)
64
+ continue;
65
+ const shown = window >= 1 ? line.points : stretch(line.points, window, s.phase + stagger(index));
66
+ if (shown.length < 2)
67
+ continue;
68
+ drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
69
+ }
70
+ }
71
+ return drawn;
72
+ }
73
+ /** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
74
+ function follow(field, x, y, step, steps, inside) {
75
+ const points = [{ x, y }];
76
+ const v = { x: 0, y: 0 };
77
+ let strength = 0;
78
+ let read = 0;
79
+ // The field's direction at a point, as a unit step; `false` where it has none or is at rest.
80
+ const heading = (px, py, out) => {
81
+ if (!field(px, py, v))
82
+ return false;
83
+ const magnitude = Math.hypot(v.x, v.y);
84
+ if (!(magnitude > 1e-9))
85
+ return false;
86
+ out.x = v.x / magnitude;
87
+ out.y = v.y / magnitude;
88
+ strength += magnitude;
89
+ read++;
90
+ return true;
91
+ };
92
+ const a = { x: 0, y: 0 };
93
+ const b = { x: 0, y: 0 };
94
+ for (let i = 0; i < steps; i++) {
95
+ // Midpoint rule: the direction half a step on is the one the whole step takes.
96
+ if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b))
97
+ break;
98
+ x += b.x * step;
99
+ y += b.y * step;
100
+ if (!inside(x, y))
101
+ break;
102
+ points.push({ x, y });
103
+ }
104
+ return { points, magnitude: read > 0 ? strength / read : 0 };
105
+ }
106
+ /** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */
107
+ function stretch(points, share, phase) {
108
+ const turn = phase - Math.floor(phase);
109
+ const end = turn * (1 + share);
110
+ const from = Math.max(0, end - share);
111
+ const to = Math.min(1, end);
112
+ if (!(to > from))
113
+ return [];
114
+ const last = points.length - 1;
115
+ return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);
116
+ }
117
+ /** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */
118
+ function stagger(index) {
119
+ const mixed = Math.sin(index * 12.9898) * 43758.5453;
120
+ return mixed - Math.floor(mixed);
121
+ }
122
+ //# sourceMappingURL=stream-lines.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-lines.js","sourceRoot":"","sources":["../../src/plots/stream-lines.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,KAAK,GAYR,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAA6F,MAAM,mBAAmB,CAAC;AAClL,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAoB,MAAM,aAAa,CAAC;AASzE,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,6EAA6E;AAC7E,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,SAAS,GAAG,GAAG,CAAC;AAEtB,MAAM,KAAK,GAAG;IACV,GAAG,WAAW;IACd,qDAAqD;IACrD,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;IAClD,gFAAgF;IAChF,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;IAC9C,yEAAyE;IACzE,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAC1D,kGAAkG;IAClG,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;CACzC,CAAC;AASF,MAAM,KAAK,GAA8B;IACrC,GAAG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AAG,CAAC,CAAC,CAAC;AAC3E,CAAC;AAED,6HAA6H;AAC7H,SAAS,OAAO,CAAC,KAAa;IAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,UAAU,CAAC;IACrD,OAAO,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import {\n component2d,\n Types,\n type NodeOf,\n type Vector2,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Fill as _Fill,\n type FillResolved as _FillResolved,\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type Shadow as _Shadow,\n type ShadowResolved as _ShadowResolved,\n type Stroke as _Stroke,\n type StrokeResolved as _StrokeResolved,\n} from \"@motionscript/sdk\";\nimport type { CoordinateMap } from \"../kit/coords\";\nimport { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldSampler, type FieldStroke, type FieldValues } from \"../kit/field-mark\";\nimport { PLOT_COLOR } from \"../kit/furniture\";\nimport { clamp } from \"../kit/geometry\";\nimport { inkSize, reading, traced, type MarkReading } from \"../kit/mark\";\n\nexport interface StreamLinesProps extends FieldProps {\n spacing?: number;\n length?: number;\n window?: number;\n phase?: number;\n}\n\nconst MAX_LINES = 1600;\n/** Steps a line takes across one spacing: what keeps a tight swirl round. */\nconst STEPS_PER_SPACING = 8;\nconst MAX_STEPS = 600;\n\nconst state = {\n ...FIELD_STATE,\n /** How far apart the lines start, in graph units. */\n spacing: Types.number(1, { min: 0.01, step: 0.1 }),\n /** How far each line runs from where it starts, in graph units along itself. */\n length: Types.number(3, { min: 0, step: 0.1 }),\n /** The share of each line that is shown at once; one shows all of it. */\n window: Types.number(1, { min: 0.01, max: 1, step: 0.01 }),\n /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */\n phase: Types.number(0, { step: 0.01 }),\n};\n\ninterface LinesValues extends FieldValues {\n readonly spacing: number;\n readonly length: number;\n readonly window: number;\n readonly phase: number;\n}\n\nconst LINES: FieldDrawing<LinesValues> = {\n own: (s) => `${s.spacing}|${s.length}|${s.window}|${s.phase}`,\n strokes: (s, map, field) => lines(s, map, field),\n};\n\nconst traceLines = (s: LinesValues, read: MarkReading) => traceField(\"streamLines\", LINES, s, read);\n\n/**\n * The lines a vector field carries a point along, one from each point of a\n * grid: `(-y, x)` draws circles. With a `window` below one, each line shows\n * only a stretch of itself, and `flowTo` sends those stretches along.\n */\nexport const StreamLines = traced(component2d({\n key: \"streamLines\",\n state,\n defaults: { stroke: { weight: 2, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" }, width: \"hug\", height: \"hug\" },\n commands: {\n ...FIELD_COMMANDS,\n /** Send the shown stretches along their lines: a whole number runs each line that many times over. */\n flowTo: { args: [\"phase\"] },\n },\n measure: (_measurer, s) => inkSize(traceLines(s, reading(s)).runs),\n render: (ctx, s) => drawField(ctx, s, traceLines(s, reading(s))),\n}), (s: LinesValues, read: MarkReading) => traceLines(s, read).runs);\n\nexport type StreamLines = NodeOf<typeof StreamLines>;\n\nfunction lines(s: LinesValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {\n const [x0, x1] = map.xRange;\n const [y0, y1] = map.yRange;\n let spacing = Math.max(s.spacing, 1e-6);\n const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);\n while (count() > MAX_LINES) spacing *= 2;\n const step = spacing / STEPS_PER_SPACING;\n const steps = Math.min(MAX_STEPS, Math.round(s.length / step));\n if (steps < 1) return [];\n // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.\n const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;\n const window = clamp(s.window, 0, 1);\n\n const drawn: FieldStroke[] = [];\n let index = 0;\n for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {\n for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {\n const line = follow(field, i * spacing, j * spacing, step, steps, inside);\n index++;\n if (line.points.length < 2) continue;\n const shown = window >= 1 ? line.points : stretch(line.points, window, s.phase + stagger(index));\n if (shown.length < 2) continue;\n drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });\n }\n }\n return drawn;\n}\n\n/** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */\nfunction follow(\n field: FieldSampler,\n x: number,\n y: number,\n step: number,\n steps: number,\n inside: (x: number, y: number) => boolean,\n): { points: Vector2[]; magnitude: number } {\n const points: Vector2[] = [{ x, y }];\n const v: Vector2 = { x: 0, y: 0 };\n let strength = 0;\n let read = 0;\n // The field's direction at a point, as a unit step; `false` where it has none or is at rest.\n const heading = (px: number, py: number, out: Vector2): boolean => {\n if (!field(px, py, v)) return false;\n const magnitude = Math.hypot(v.x, v.y);\n if (!(magnitude > 1e-9)) return false;\n out.x = v.x / magnitude;\n out.y = v.y / magnitude;\n strength += magnitude;\n read++;\n return true;\n };\n const a: Vector2 = { x: 0, y: 0 };\n const b: Vector2 = { x: 0, y: 0 };\n for (let i = 0; i < steps; i++) {\n // Midpoint rule: the direction half a step on is the one the whole step takes.\n if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b)) break;\n x += b.x * step;\n y += b.y * step;\n if (!inside(x, y)) break;\n points.push({ x, y });\n }\n return { points, magnitude: read > 0 ? strength / read : 0 };\n}\n\n/** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */\nfunction stretch(points: Vector2[], share: number, phase: number): Vector2[] {\n const turn = phase - Math.floor(phase);\n const end = turn * (1 + share);\n const from = Math.max(0, end - share);\n const to = Math.min(1, end);\n if (!(to > from)) return [];\n const last = points.length - 1;\n return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);\n}\n\n/** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */\nfunction stagger(index: number): number {\n const mixed = Math.sin(index * 12.9898) * 43758.5453;\n return mixed - Math.floor(mixed);\n}\n"]}
@@ -0,0 +1,70 @@
1
+ import { type NodeOf, type Node2D as _Node2D, type Node2DProps as _Node2DProps, type Shadow as _Shadow, type ShadowResolved as _ShadowResolved, type Stroke as _Stroke, type StrokeResolved as _StrokeResolved } from "@motionscript/sdk";
2
+ import type { Interval } from "../kit/coords.js";
3
+ import { type FunctionPlotProps } from "./function-plot.js";
4
+ export interface TracedPathProps extends FunctionPlotProps {
5
+ from?: number;
6
+ value?: number;
7
+ tail?: number;
8
+ }
9
+ /**
10
+ * The trail a point leaves as a value changes: the curve of its equation from
11
+ * where the value began to where it is now. Bind `value` to a variable
12
+ * (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
13
+ * `traceTo`; a `tail` keeps only its most recent stretch.
14
+ */
15
+ export declare const TracedPath: import("@motionscript/sdk").Component<"tracedPath", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
16
+ /** The value the trail starts at. */
17
+ from: import("@motionscript/sdk").Type<number, number, false>;
18
+ /** The value the point is at now: where the trail ends. */
19
+ value: import("@motionscript/sdk").Type<number, number, false>;
20
+ /** How much of the trail is kept, in the value's own units; zero keeps all of it. */
21
+ tail: import("@motionscript/sdk").Type<number, number, false>;
22
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
23
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
24
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
25
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
26
+ domain: import("@motionscript/sdk").Type<Interval | null, Interval | null, false>;
27
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
28
+ }>, {
29
+ readonly traceTo: readonly ["value"];
30
+ readonly strokeTo: readonly ["stroke"];
31
+ readonly shadowTo: readonly ["shadow"];
32
+ readonly morphTo: readonly ["expression"];
33
+ readonly paramsTo: readonly ["params"];
34
+ readonly domainTo: readonly ["domain"];
35
+ }, null, _Node2D<_Node2DProps>, import("@motionscript/sdk").CallProps<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
36
+ /** The value the trail starts at. */
37
+ from: import("@motionscript/sdk").Type<number, number, false>;
38
+ /** The value the point is at now: where the trail ends. */
39
+ value: import("@motionscript/sdk").Type<number, number, false>;
40
+ /** How much of the trail is kept, in the value's own units; zero keeps all of it. */
41
+ tail: import("@motionscript/sdk").Type<number, number, false>;
42
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
43
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
44
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
45
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
46
+ domain: import("@motionscript/sdk").Type<Interval | null, Interval | null, false>;
47
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
48
+ }>, null, import("@motionscript/sdk").BuiltNode<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
49
+ /** The value the trail starts at. */
50
+ from: import("@motionscript/sdk").Type<number, number, false>;
51
+ /** The value the point is at now: where the trail ends. */
52
+ value: import("@motionscript/sdk").Type<number, number, false>;
53
+ /** How much of the trail is kept, in the value's own units; zero keeps all of it. */
54
+ tail: import("@motionscript/sdk").Type<number, number, false>;
55
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
56
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
57
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
58
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
59
+ domain: import("@motionscript/sdk").Type<Interval | null, Interval | null, false>;
60
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
61
+ }>, {
62
+ readonly traceTo: readonly ["value"];
63
+ readonly strokeTo: readonly ["stroke"];
64
+ readonly shadowTo: readonly ["shadow"];
65
+ readonly morphTo: readonly ["expression"];
66
+ readonly paramsTo: readonly ["params"];
67
+ readonly domainTo: readonly ["domain"];
68
+ }, _Node2D<_Node2DProps>>>>;
69
+ export type TracedPath = NodeOf<typeof TracedPath>;
70
+ //# sourceMappingURL=traced-path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traced-path.d.ts","sourceRoot":"","sources":["../../src/plots/traced-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,MAAM,EAEX,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,IAAI,YAAY,EAChC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACzC,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,OAAO,EAAqD,KAAK,iBAAiB,EAAmB,MAAM,iBAAiB,CAAC;AAE7H,MAAM,WAAW,eAAgB,SAAQ,iBAAiB;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAmBD;;;;;GAKG;AACH,eAAO,MAAM,UAAU;IApBnB,qCAAqC;;IAErC,2DAA2D;;IAE3D,qFAAqF;;;;;;;;;;;;;;;;IAJrF,qCAAqC;;IAErC,2DAA2D;;IAE3D,qFAAqF;;;;;;;;;IAJrF,qCAAqC;;IAErC,2DAA2D;;IAE3D,qFAAqF;;;;;;;;;;;;;;;2BA2B1E,CAAC;AAEhB,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC"}
@@ -0,0 +1,41 @@
1
+ import { component2d, Types, } from "@motionscript/sdk";
2
+ import { omit } from "../kit/component.js";
3
+ import { drawMark, inkSize, reading, traced } from "../kit/mark.js";
4
+ import { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot } from "./function-plot.js";
5
+ const state = {
6
+ // It reveals itself by its value, so it trims no outline of its own.
7
+ ...omit(PLOT_STATE, "start", "end"),
8
+ /** The value the trail starts at. */
9
+ from: Types.number(0, { step: 0.1 }),
10
+ /** The value the point is at now: where the trail ends. */
11
+ value: Types.number(0, { step: 0.1 }),
12
+ /** How much of the trail is kept, in the value's own units; zero keeps all of it. */
13
+ tail: Types.number(0, { min: 0, step: 0.1 }),
14
+ };
15
+ /**
16
+ * The trail a point leaves as a value changes: the curve of its equation from
17
+ * where the value began to where it is now. Bind `value` to a variable
18
+ * (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
19
+ * `traceTo`; a `tail` keeps only its most recent stretch.
20
+ */
21
+ export const TracedPath = traced(component2d({
22
+ key: "tracedPath",
23
+ state,
24
+ defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
25
+ commands: {
26
+ ...omit(PLOT_COMMANDS, "progressTo", "flash"),
27
+ /** Carry the point on to another value, the trail following it. */
28
+ traceTo: { args: ["value"] },
29
+ },
30
+ measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),
31
+ render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),
32
+ }), traceTrail);
33
+ function traceTrail(s, read) {
34
+ return tracePlot(s, read, span(s));
35
+ }
36
+ /** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */
37
+ function span({ from, value, tail }) {
38
+ const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
39
+ return value >= from ? [value - kept, value] : [value, value + kept];
40
+ }
41
+ //# sourceMapp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AAC,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7D,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;CAClE,CAAC,EAAE,UAAU,CAAC,CAAC;AAIhB,SAAS,UAAU,CAAC,CAAc,EAAE,IAAiB;IACjD,OAAO,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC;AAED,+GAA+G;AAC/G,SAAS,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAe;IAC5C,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;IACxF,OAAO,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC,CAAC;AACzE,CAAC","sourcesContent":["import {\n component2d,\n Types,\n type NodeOf,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type Shadow as _Shadow,\n type ShadowResolved as _ShadowResolved,\n type Stroke as _Stroke,\n type StrokeResolved as _StrokeResolved,\n} from \"@motionscript/sdk\";\nimport { omit } from \"../kit/component\";\nimport type { Interval } from \"../kit/coords\";\nimport { drawMark, inkSize, reading, traced, type MarkReading } from \"../kit/mark\";\nimport { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot, type FunctionPlotProps, type PlotValues } from \"./function-plot\";\n\nexport interface TracedPathProps extends FunctionPlotProps {\n from?: number;\n value?: number;\n tail?: number;\n}\n\nconst state = {\n // It reveals itself by its value, so it trims no outline of its own.\n ...omit(PLOT_STATE, \"start\", \"end\"),\n /** The value the trail starts at. */\n from: Types.number(0, { step: 0.1 }),\n /** The value the point is at now: where the trail ends. */\n value: Types.number(0, { step: 0.1 }),\n /** How much of the trail is kept, in the value's own units; zero keeps all of it. */\n tail: Types.number(0, { min: 0, step: 0.1 }),\n};\n\ninterface TrailValues extends PlotValues {\n readonly from: number;\n readonly value: number;\n readonly tail: number;\n}\n\n/**\n * The trail a point leaves as a value changes: the curve of its equation from\n * where the value began to where it is now. Bind `value` to a variable\n * (`\"=[t]\"`) and the trail grows as the variable is driven, or drive it with\n * `traceTo`; a `tail` keeps only its most recent stretch.\n */\nexport const TracedPath = traced(component2d({\n key: \"tracedPath\",\n state,\n defaults: { stroke: PLOT_STROKE, width: \"hug\", height: \"hug\" },\n commands: {\n ...omit(PLOT_COMMANDS, \"progressTo\", \"flash\"),\n /** Carry the point on to another value, the trail following it. */\n traceTo: { args: [\"value\"] },\n },\n measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),\n render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),\n}), traceTrail);\n\nexport type TracedPath = NodeOf<typeof TracedPath>;\n\nfunction traceTrail(s: TrailValues, read: MarkReading) {\n return tracePlot(s, read, span(s));\n}\n\n/** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */\nfunction span({ from, value, tail }: TrailValues): Interval {\n const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);\n return value >= from ? [value - kept, value] : [value, value + kept];\n}\n"]}
@@ -0,0 +1,60 @@
1
+ import { type NodeOf, type Fill as _Fill, type FillResolved as _FillResolved, type Node2D as _Node2D, type Node2DProps as _Node2DProps, type Shadow as _Shadow, type ShadowResolved as _ShadowResolved, type Stroke as _Stroke, type StrokeResolved as _StrokeResolved } from "@motionscript/sdk";
2
+ import { type FieldProps } from "../kit/field-mark.js";
3
+ export interface VectorFieldProps extends FieldProps {
4
+ spacing?: number;
5
+ arrowScale?: number;
6
+ }
7
+ /**
8
+ * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
9
+ * Each arrow is centred on its point and is as long as the field is strong
10
+ * there, up to the spacing.
11
+ */
12
+ export declare const VectorField: import("@motionscript/sdk").Component<"vectorField", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
13
+ /** How far apart the arrows stand, in graph units. */
14
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
15
+ /** How long an arrow is drawn for a vector of length one, in graph units. */
16
+ arrowScale: import("@motionscript/sdk").Type<number, number, false>;
17
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
18
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
19
+ strengthFills: import("@motionscript/sdk").Type<_Fill[], _FillResolved[][], false>;
20
+ maxMagnitude: import("@motionscript/sdk").Type<number, number, false>;
21
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
22
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
23
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
24
+ }>, {
25
+ morphTo: readonly ["expression"];
26
+ paramsTo: readonly ["params"];
27
+ strokeTo: readonly ["stroke"];
28
+ shadowTo: readonly ["shadow"];
29
+ }, null, _Node2D<_Node2DProps>, import("@motionscript/sdk").CallProps<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
30
+ /** How far apart the arrows stand, in graph units. */
31
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
32
+ /** How long an arrow is drawn for a vector of length one, in graph units. */
33
+ arrowScale: import("@motionscript/sdk").Type<number, number, false>;
34
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
35
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
36
+ strengthFills: import("@motionscript/sdk").Type<_Fill[], _FillResolved[][], false>;
37
+ maxMagnitude: import("@motionscript/sdk").Type<number, number, false>;
38
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
39
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
40
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
41
+ }>, null, import("@motionscript/sdk").BuiltNode<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
42
+ /** How far apart the arrows stand, in graph units. */
43
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
44
+ /** How long an arrow is drawn for a vector of length one, in graph units. */
45
+ arrowScale: import("@motionscript/sdk").Type<number, number, false>;
46
+ expression: import("@motionscript/sdk").Type<string | import("../index.js").PlotSource, import("../index.js").PlotSource, false>;
47
+ params: import("@motionscript/sdk").Type<Readonly<Record<string, number>>, Readonly<Record<string, number>>, false>;
48
+ strengthFills: import("@motionscript/sdk").Type<_Fill[], _FillResolved[][], false>;
49
+ maxMagnitude: import("@motionscript/sdk").Type<number, number, false>;
50
+ graph: import("@motionscript/sdk").Type<import("../index.js").GraphPlane | null, import("../index.js").GraphPlane | null, true>;
51
+ stroke: import("@motionscript/sdk").Attribute<_Stroke, _StrokeResolved[]>;
52
+ shadow: import("@motionscript/sdk").Attribute<_Shadow, _ShadowResolved[]>;
53
+ }>, {
54
+ morphTo: readonly ["expression"];
55
+ paramsTo: readonly ["params"];
56
+ strokeTo: readonly ["stroke"];
57
+ shadowTo: readonly ["shadow"];
58
+ }, _Node2D<_Node2DProps>>>>;
59
+ export type VectorField = NodeOf<typeof VectorField>;
60
+ //# sourceMappingURL=vector-field.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-field.d.ts","sourceRoot":"","sources":["../../src/plots/vector-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,MAAM,EAGX,KAAK,IAAI,IAAI,KAAK,EAClB,KAAK,YAAY,IAAI,aAAa,EAClC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,IAAI,YAAY,EAChC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACzC,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAyE,KAAK,UAAU,EAAyD,MAAM,mBAAmB,CAAC;AAIlL,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AA6BD;;;;GAIG;AACH,eAAO,MAAM,WAAW;IAvBpB,sDAAsD;;IAEtD,6EAA6E;;;;;;;;;;;;;;;IAF7E,sDAAsD;;IAEtD,6EAA6E;;;;;;;;;;IAF7E,sDAAsD;;IAEtD,6EAA6E;;;;;;;;;;;;;;2BA4BX,CAAC;AAEvE,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,WAAW,CAAC,CAAC"}
@@ -0,0 +1,77 @@
1
+ import { component2d, Types, } from "@motionscript/sdk";
2
+ import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField } from "../kit/field-mark.js";
3
+ import { PLOT_COLOR } from "../kit/furniture.js";
4
+ import { inkSize, reading, traced } from "../kit/mark.js";
5
+ /** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */
6
+ const MAX_ARROWS = 2400;
7
+ /** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */
8
+ const HEAD_SHARE = 0.38;
9
+ const HEAD_MAX = 11;
10
+ const HEAD_ANGLE = (26 * Math.PI) / 180;
11
+ const state = {
12
+ ...FIELD_STATE,
13
+ /** How far apart the arrows stand, in graph units. */
14
+ spacing: Types.number(1, { min: 0.01, step: 0.1 }),
15
+ /** How long an arrow is drawn for a vector of length one, in graph units. */
16
+ arrowScale: Types.number(0.4, { min: 0, step: 0.05 }),
17
+ };
18
+ const ARROWS = {
19
+ own: (s) => `${s.spacing}|${s.arrowScale}`,
20
+ strokes: (s, map, field) => arrows(s, map, field),
21
+ };
22
+ const traceArrows = (s, read) => traceField("vectorField", ARROWS, s, read);
23
+ /**
24
+ * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
25
+ * Each arrow is centred on its point and is as long as the field is strong
26
+ * there, up to the spacing.
27
+ */
28
+ export const VectorField = traced(component2d({
29
+ key: "vectorField",
30
+ state,
31
+ defaults: { stroke: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
32
+ commands: FIELD_COMMANDS,
33
+ measure: (_measurer, s) => inkSize(traceArrows(s, reading(s)).runs),
34
+ render: (ctx, s) => drawField(ctx, s, traceArrows(s, reading(s))),
35
+ }), (s, read) => traceArrows(s, read).runs);
36
+ function arrows(s, map, field) {
37
+ const [x0, x1] = map.xRange;
38
+ const [y0, y1] = map.yRange;
39
+ let spacing = Math.max(s.spacing, 1e-6);
40
+ const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
41
+ while (count() > MAX_ARROWS)
42
+ spacing *= 2;
43
+ const drawn = [];
44
+ const v = { x: 0, y: 0 };
45
+ const longest = spacing * 0.9;
46
+ for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
47
+ for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
48
+ const gx = i * spacing;
49
+ const gy = j * spacing;
50
+ if (!field(gx, gy, v))
51
+ continue;
52
+ const magnitude = Math.hypot(v.x, v.y);
53
+ const length = Math.min(magnitude * s.arrowScale, longest);
54
+ if (!(length > 0))
55
+ continue;
56
+ const hx = ((v.x / magnitude) * length) / 2;
57
+ const hy = ((v.y / magnitude) * length) / 2;
58
+ const tail = map.toPoint(gx - hx, gy - hy);
59
+ const head = map.toPoint(gx + hx, gy + hy);
60
+ drawn.push({ runs: arrow(tail, head), magnitude });
61
+ }
62
+ }
63
+ return drawn;
64
+ }
65
+ /** A shaft and the two barbs of its head, in pixels. */
66
+ function arrow(tail, head) {
67
+ const dx = head.x - tail.x;
68
+ const dy = head.y - tail.y;
69
+ const length = Math.hypot(dx, dy);
70
+ if (!(length > 0))
71
+ return [];
72
+ const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);
73
+ const back = Math.atan2(dy, dx) + Math.PI;
74
+ const at = (angle) => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });
75
+ return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];
76
+ }
77
+ //# sourceMappingURL=vector-field.js.map