@motionscript/math 0.1.0-alpha.5 → 0.1.0-alpha.7

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 (233) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +77 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +94 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +69 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +56 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +37 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +45 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +56 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +47 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +58 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +48 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +135 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +55 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +60 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +54 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +48 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +140 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +90 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +159 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +80 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +140 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +76 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +88 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +84 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +214 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +163 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +108 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +85 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +176 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +87 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +187 -160
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
@@ -1,56 +1,41 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
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 }),
6
14
  };
7
- import { command, node, property, withdraw } from "@motionscript/sdk";
8
- import { stated } from "../kit/geometry.js";
9
- import { SYSTEMS } from "../kit/placement.js";
10
- import { FunctionPlot } from "./function-plot.js";
11
- import { OUTLINE_TRIM } from "../kit/withdrawn.js";
12
15
  /**
13
16
  * The trail a point leaves as a value changes: the curve of its equation from
14
17
  * where the value began to where it is now. Bind `value` to a variable
15
18
  * (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
16
19
  * `traceTo`; a `tail` keeps only its most recent stretch.
17
20
  */
18
- let TracedPath = class TracedPath extends FunctionPlot {
19
- constructor(props) {
20
- super((props ?? {}));
21
- }
22
- /** Carry the point on to another value, the trail following it. */
23
- traceTo(args) {
24
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
25
- }
26
- span() {
27
- const { from, value, tail } = this;
28
- const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
29
- return value >= from ? [value - kept, value] : [value, value + kept];
30
- }
31
- };
32
- __decorate([
33
- property({ default: 0, step: 0.1 })
34
- ], TracedPath.prototype, "from", void 0);
35
- __decorate([
36
- property({ default: 0, step: 0.1 })
37
- ], TracedPath.prototype, "value", void 0);
38
- __decorate([
39
- property({ default: 0, min: 0, step: 0.1 })
40
- ], TracedPath.prototype, "tail", void 0);
41
- __decorate([
42
- command({ args: ["value"] })
43
- ], TracedPath.prototype, "traceTo", null);
44
- TracedPath = __decorate([
45
- node({
46
- key: "tracedPath",
47
- within: SYSTEMS,
48
- parentKey: "functionPlot",
49
- forkable: true,
50
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
51
- seed: { expression: "(2cos(t), sin(2t))", value: 6.28 },
52
- }),
53
- withdraw(...OUTLINE_TRIM)
54
- ], TracedPath);
55
- export { TracedPath };
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
+ }
56
41
  //# sourceMappingURL=traced-path.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"traced-path.js","sourceRoot":"","sources":["../../src/plots/traced-path.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAmD,MAAM,mBAAmB,CAAC;AAEvH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAA0B,MAAM,iBAA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GAAG,EAAE,CAAC;wCAAsB;AAQlE;IADC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC;yCAG5B;AAhBQ,UAAU;IATtB,IAAI,CAAC;QACF,GAAG,EAAE,YAAY;QACjB,MAAM,EAAE,OAAO;QACf,SAAS,EAAE,cAAc;QACzB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,gBAAgB,EAAE,KAAK,EAAE,iBAAiB,EAAE,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE;QAC3G,IAAI,EAAE,EAAE,UAAU,EAAE,oBAAoB,EAAE,KAAK,EAAE,IAAI,EAAE;KAC1D,CAAC;IACD,QAAQ,CAAC,GAAG,YAAY,CAAC;GACb,UAAU,CAuBtB","sourcesContent":["import { command, node, property, withdraw, type Command, type CommandArgs, type NodeConfig } from \"@motionscript/sdk\";\r\nimport type { Interval } from \"../kit/coords\";\r\nimport { stated } from \"../kit/geometry\";\r\nimport { SYSTEMS } from \"../kit/placement\";\r\nimport { FunctionPlot, type FunctionPlotProps } from \"./function-plot\";\r\nimport { OUTLINE_TRIM } from \"../kit/withdrawn\";\r\n\r\nexport interface TracedPathProps extends FunctionPlotProps {\r\n from?: number;\r\n value?: number;\r\n tail?: number;\r\n}\r\n\r\n/**\r\n * The trail a point leaves as a value changes: the curve of its equation from\r\n * where the value began to where it is now. Bind `value` to a variable\r\n * (`\"=[t]\"`) and the trail grows as the variable is driven, or drive it with\r\n * `traceTo`; a `tail` keeps only its most recent stretch.\r\n */\r\n@node({\r\n key: \"tracedPath\",\r\n within: SYSTEMS,\r\n parentKey: \"functionPlot\",\r\n forkable: true,\r\n layout: { children: \"freeform\", defaultWidthMode: \"hug\", defaultHeightMode: \"hug\", acceptsChildren: false },\r\n seed: { expression: \"(2cos(t), sin(2t))\", value: 6.28 },\r\n})\r\n@withdraw(...OUTLINE_TRIM)\r\nexport class TracedPath extends FunctionPlot<TracedPathProps> {\r\n /** The value the trail starts at. */\r\n @property({ default: 0, step: 0.1 }) declare from: number;\r\n /** The value the point is at now: where the trail ends. */\r\n @property({ default: 0, step: 0.1 }) declare value: number;\r\n /** How much of the trail is kept, in the value's own units; zero keeps all of it. */\r\n @property({ default: 0, min: 0, step: 0.1 }) declare tail: number;\r\n\r\n constructor(props?: NodeConfig<TracedPath, TracedPathProps>) {\r\n super((props ?? {}) as NodeConfig<FunctionPlot<TracedPathProps>, TracedPathProps>);\r\n }\r\n\r\n /** Carry the point on to another value, the trail following it. */\r\n @command({ args: [\"value\"] })\r\n traceTo(args: CommandArgs<{ value: number }> & { duration: number }): Command<TracedPathProps> {\r\n return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });\r\n }\r\n\r\n protected override span(): Interval | null {\r\n const { from, value, tail } = this;\r\n const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);\r\n return value >= from ? [value - kept, value] : [value, value + kept];\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"traced-path.js","sourceRoot":"","sources":["../../src/plots/traced-path.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,KAAK,GASR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAExC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAoB,MAAM,aAAa,CAAC;AACnF,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAA2C,MAAM,iBAAiB,CAAC;AAQ7H,MAAM,KAAK,GAAG;IACV,qEAAqE;IACrE,GAAG,IAAI,CAAC,UAAU,EAAE,OAAO,EAAE,KAAK,CAAC;IACnC,qCAAqC;IACrC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;IACpC,2DAA2D;IAC3D,KAAK,EAAE,KAAK,CAAC,MAAM,CAA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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 {\r\n component2d,\r\n Types,\r\n type NodeOf,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n type Shadow as _Shadow,\r\n type ShadowResolved as _ShadowResolved,\r\n type Stroke as _Stroke,\r\n type StrokeResolved as _StrokeResolved,\r\n} from \"@motionscript/sdk\";\r\nimport { omit } from \"../kit/component\";\r\nimport type { Interval } from \"../kit/coords\";\r\nimport { drawMark, inkSize, reading, traced, type MarkReading } from \"../kit/mark\";\r\nimport { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot, type FunctionPlotProps, type PlotValues } from \"./function-plot\";\r\n\r\nexport interface TracedPathProps extends FunctionPlotProps {\r\n from?: number;\r\n value?: number;\r\n tail?: number;\r\n}\r\n\r\nconst state = {\r\n // It reveals itself by its value, so it trims no outline of its own.\r\n ...omit(PLOT_STATE, \"start\", \"end\"),\r\n /** The value the trail starts at. */\r\n from: Types.number(0, { step: 0.1 }),\r\n /** The value the point is at now: where the trail ends. */\r\n value: Types.number(0, { step: 0.1 }),\r\n /** How much of the trail is kept, in the value's own units; zero keeps all of it. */\r\n tail: Types.number(0, { min: 0, step: 0.1 }),\r\n};\r\n\r\ninterface TrailValues extends PlotValues {\r\n readonly from: number;\r\n readonly value: number;\r\n readonly tail: number;\r\n}\r\n\r\n/**\r\n * The trail a point leaves as a value changes: the curve of its equation from\r\n * where the value began to where it is now. Bind `value` to a variable\r\n * (`\"=[t]\"`) and the trail grows as the variable is driven, or drive it with\r\n * `traceTo`; a `tail` keeps only its most recent stretch.\r\n */\r\nexport const TracedPath = traced(component2d({\r\n key: \"tracedPath\",\r\n state,\r\n defaults: { stroke: PLOT_STROKE, width: \"hug\", height: \"hug\" },\r\n commands: {\r\n ...omit(PLOT_COMMANDS, \"progressTo\", \"flash\"),\r\n /** Carry the point on to another value, the trail following it. */\r\n traceTo: { args: [\"value\"] },\r\n },\r\n measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),\r\n render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),\r\n}), traceTrail);\r\n\r\nexport type TracedPath = NodeOf<typeof TracedPath>;\r\n\r\nfunction traceTrail(s: TrailValues, read: MarkReading) {\r\n return tracePlot(s, read, span(s));\r\n}\r\n\r\n/** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */\r\nfunction span({ from, value, tail }: TrailValues): Interval {\r\n const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);\r\n return value >= from ? [value - kept, value] : [value, value + kept];\r\n}\r\n"]}
@@ -1,6 +1,5 @@
1
- import { type NodeConfig, type Stroke } from "@motionscript/sdk";
2
- import type { CoordinateMap } from "../kit/coords.js";
3
- import { FieldMark, type FieldProps, type FieldSampler, type FieldStroke } from "../kit/field-mark.js";
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";
4
3
  export interface VectorFieldProps extends FieldProps {
5
4
  spacing?: number;
6
5
  arrowScale?: number;
@@ -10,14 +9,52 @@ export interface VectorFieldProps extends FieldProps {
10
9
  * Each arrow is centred on its point and is as long as the field is strong
11
10
  * there, up to the spacing.
12
11
  */
13
- export declare class VectorField extends FieldMark<VectorFieldProps> {
12
+ export declare const VectorField: import("@motionscript/sdk").Component<"vectorField", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
14
13
  /** How far apart the arrows stand, in graph units. */
15
- spacing: number;
14
+ spacing: import("@motionscript/sdk").Type<number, number, false>;
16
15
  /** How long an arrow is drawn for a vector of length one, in graph units. */
17
- arrowScale: number;
18
- stroke: Stroke;
19
- constructor(props?: NodeConfig<VectorField, VectorFieldProps>);
20
- protected own(): string;
21
- protected strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[];
22
- }
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>;
23
60
  //# sourceMappingURL=vector-field.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"vector-field.d.ts","sourceRoot":"","sources":["../../src/plots/vector-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkC,KAAK,UAAU,EAAE,KAAK,MAAM,EAAgB,MAAM,mBAAmB,CAA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;;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"}
@@ -1,81 +1,67 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { node, property, strokeProperty } from "@motionscript/sdk";
8
- import { FieldMark } from "../kit/field-mark.js";
1
+ import { component2d, Types, } from "@motionscript/sdk";
2
+ import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField } from "../kit/field-mark.js";
9
3
  import { PLOT_COLOR } from "../kit/furniture.js";
10
- import { SYSTEMS } from "../kit/placement.js";
4
+ import { inkSize, reading, traced } from "../kit/mark.js";
11
5
  /** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */
12
6
  const MAX_ARROWS = 2400;
13
7
  /** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */
14
8
  const HEAD_SHARE = 0.38;
15
9
  const HEAD_MAX = 11;
16
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);
17
23
  /**
18
24
  * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
19
25
  * Each arrow is centred on its point and is as long as the field is strong
20
26
  * there, up to the spacing.
21
27
  */
22
- let VectorField = class VectorField extends FieldMark {
23
- constructor(props) {
24
- super(props ?? {});
25
- }
26
- own() {
27
- return `${this.spacing}|${this.arrowScale}`;
28
- }
29
- strokes(map, field) {
30
- const [x0, x1] = map.xRange;
31
- const [y0, y1] = map.yRange;
32
- let spacing = Math.max(this.spacing, 1e-6);
33
- const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
34
- while (count() > MAX_ARROWS)
35
- spacing *= 2;
36
- const drawn = [];
37
- const v = { x: 0, y: 0 };
38
- const longest = spacing * 0.9;
39
- for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
40
- for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
41
- const gx = i * spacing;
42
- const gy = j * spacing;
43
- if (!field(gx, gy, v))
44
- continue;
45
- const magnitude = Math.hypot(v.x, v.y);
46
- const length = Math.min(magnitude * this.arrowScale, longest);
47
- if (!(length > 0))
48
- continue;
49
- const hx = ((v.x / magnitude) * length) / 2;
50
- const hy = ((v.y / magnitude) * length) / 2;
51
- const tail = map.toPoint(gx - hx, gy - hy);
52
- const head = map.toPoint(gx + hx, gy + hy);
53
- drawn.push({ runs: arrow(tail, head), magnitude });
54
- }
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 });
55
61
  }
56
- return drawn;
57
62
  }
58
- };
59
- __decorate([
60
- property({ default: 1, min: 0.01, step: 0.1 })
61
- ], VectorField.prototype, "spacing", void 0);
62
- __decorate([
63
- property({ default: 0.4, min: 0, step: 0.05 })
64
- ], VectorField.prototype, "arrowScale", void 0);
65
- __decorate([
66
- strokeProperty({ default: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
67
- ], VectorField.prototype, "stroke", void 0);
68
- VectorField = __decorate([
69
- node({
70
- key: "vectorField",
71
- within: SYSTEMS,
72
- parentKey: "shape",
73
- forkable: true,
74
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
75
- seed: { expression: "(-y, x)" },
76
- })
77
- ], VectorField);
78
- export { VectorField };
63
+ return drawn;
64
+ }
79
65
  /** A shaft and the two barbs of its head, in pixels. */
80
66
  function arrow(tail, head) {
81
67
  const dx = head.x - tail.x;
@@ -1 +1 @@
1
- {"version":3,"file":"vector-field.js","sourceRoot":"","sources":["../../src/plots/vector-field.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,cAAc,EAA8C,MAAM,mBAAmB,CAAC;AAE/G,OAAO,EAAE,SAAS,EAAwD,MAAM,mBAAmB,CAAC;AACpG,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAO3C,kHAAkH;AAClH,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,uGAAuG;AACvG,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,MAAM,UAAU,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC;AAExC;;;;GAIG;AASI,IAAM,WAAW,GAAjB,MAAM,WAAY,SAAQ,SAA2B;IASxD,YAAY,KAAiD;QACzD,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IACvB,CAAC;IAES,GAAG;QACT,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;IAChD,CAAC;IAES,OAAO,CAAC,GAAkB,EAAE,KAAmB;QACrD,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC;QAC5B,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC;QAC5B,IAAI,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,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stroke, type Vector2 } from \"@motionscript/sdk\";\r\nimport type { CoordinateMap } from \"../kit/coords\";\r\nimport { FieldMark, type FieldProps, type FieldSampler, type FieldStroke } from \"../kit/field-mark\";\r\nimport { PLOT_COLOR } from \"../kit/furniture\";\r\nimport { SYSTEMS } from \"../kit/placement\";\r\n\r\nexport interface VectorFieldProps extends FieldProps {\r\n spacing?: number;\r\n arrowScale?: number;\r\n}\r\n\r\n/** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */\r\nconst MAX_ARROWS = 2400;\r\n/** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */\r\nconst HEAD_SHARE = 0.38;\r\nconst HEAD_MAX = 11;\r\nconst HEAD_ANGLE = (26 * Math.PI) / 180;\r\n\r\n/**\r\n * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.\r\n * Each arrow is centred on its point and is as long as the field is strong\r\n * there, up to the spacing.\r\n */\r\n@node({\r\n key: \"vectorField\",\r\n within: SYSTEMS,\r\n parentKey: \"shape\",\r\n forkable: true,\r\n layout: { children: \"freeform\", defaultWidthMode: \"hug\", defaultHeightMode: \"hug\", acceptsChildren: false },\r\n seed: { expression: \"(-y, x)\" },\r\n})\r\nexport class VectorField extends FieldMark<VectorFieldProps> {\r\n /** How far apart the arrows stand, in graph units. */\r\n @property({ default: 1, min: 0.01, step: 0.1 }) declare spacing: number;\r\n /** How long an arrow is drawn for a vector of length one, in graph units. */\r\n @property({ default: 0.4, min: 0, step: 0.05 }) declare arrowScale: number;\r\n\r\n @strokeProperty({ default: { weight: 2.5, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" } })\r\n declare stroke: Stroke;\r\n\r\n constructor(props?: NodeConfig<VectorField, VectorFieldProps>) {\r\n super(props ?? {});\r\n }\r\n\r\n protected own(): string {\r\n return `${this.spacing}|${this.arrowScale}`;\r\n }\r\n\r\n protected strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[] {\r\n const [x0, x1] = map.xRange;\r\n const [y0, y1] = map.yRange;\r\n let spacing = Math.max(this.spacing, 1e-6);\r\n const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);\r\n while (count() > MAX_ARROWS) spacing *= 2;\r\n\r\n const drawn: FieldStroke[] = [];\r\n const v: Vector2 = { x: 0, y: 0 };\r\n const longest = spacing * 0.9;\r\n for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {\r\n for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {\r\n const gx = i * spacing;\r\n const gy = j * spacing;\r\n if (!field(gx, gy, v)) continue;\r\n const magnitude = Math.hypot(v.x, v.y);\r\n const length = Math.min(magnitude * this.arrowScale, longest);\r\n if (!(length > 0)) continue;\r\n const hx = ((v.x / magnitude) * length) / 2;\r\n const hy = ((v.y / magnitude) * length) / 2;\r\n const tail = map.toPoint(gx - hx, gy - hy);\r\n const head = map.toPoint(gx + hx, gy + hy);\r\n drawn.push({ runs: arrow(tail, head), magnitude });\r\n }\r\n }\r\n return drawn;\r\n }\r\n}\r\n\r\n/** A shaft and the two barbs of its head, in pixels. */\r\nfunction arrow(tail: Vector2, head: Vector2): Vector2[][] {\r\n const dx = head.x - tail.x;\r\n const dy = head.y - tail.y;\r\n const length = Math.hypot(dx, dy);\r\n if (!(length > 0)) return [];\r\n const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);\r\n const back = Math.atan2(dy, dx) + Math.PI;\r\n const at = (angle: number): Vector2 => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });\r\n return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];\r\n}\r\n"]}
1
+ {"version":3,"file":"vector-field.js","sourceRoot":"","sources":["../../src/plots/vector-field.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,KAAK,GAYR,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,SAAS,EAA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C,IAAI,GAAG,UAAU,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAChF,CAAC","sourcesContent":["import {\r\n component2d,\r\n Types,\r\n type NodeOf,\r\n type Vector2,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Fill as _Fill,\r\n type FillResolved as _FillResolved,\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n type Shadow as _Shadow,\r\n type ShadowResolved as _ShadowResolved,\r\n type Stroke as _Stroke,\r\n type StrokeResolved as _StrokeResolved,\r\n} from \"@motionscript/sdk\";\r\nimport type { CoordinateMap } from \"../kit/coords\";\r\nimport { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldValues, type FieldSampler, type FieldStroke } from \"../kit/field-mark\";\r\nimport { PLOT_COLOR } from \"../kit/furniture\";\r\nimport { inkSize, reading, traced, type MarkReading } from \"../kit/mark\";\r\n\r\nexport interface VectorFieldProps extends FieldProps {\r\n spacing?: number;\r\n arrowScale?: number;\r\n}\r\n\r\n/** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */\r\nconst MAX_ARROWS = 2400;\r\n/** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */\r\nconst HEAD_SHARE = 0.38;\r\nconst HEAD_MAX = 11;\r\nconst HEAD_ANGLE = (26 * Math.PI) / 180;\r\n\r\nconst state = {\r\n ...FIELD_STATE,\r\n /** How far apart the arrows stand, in graph units. */\r\n spacing: Types.number(1, { min: 0.01, step: 0.1 }),\r\n /** How long an arrow is drawn for a vector of length one, in graph units. */\r\n arrowScale: Types.number(0.4, { min: 0, step: 0.05 }),\r\n};\r\n\r\ninterface ArrowsValues extends FieldValues {\r\n readonly spacing: number;\r\n readonly arrowScale: number;\r\n}\r\n\r\nconst ARROWS: FieldDrawing<ArrowsValues> = {\r\n own: (s) => `${s.spacing}|${s.arrowScale}`,\r\n strokes: (s, map, field) => arrows(s, map, field),\r\n};\r\n\r\nconst traceArrows = (s: ArrowsValues, read: MarkReading) => traceField(\"vectorField\", ARROWS, s, read);\r\n\r\n/**\r\n * A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.\r\n * Each arrow is centred on its point and is as long as the field is strong\r\n * there, up to the spacing.\r\n */\r\nexport const VectorField = traced(component2d({\r\n key: \"vectorField\",\r\n state,\r\n defaults: { stroke: { weight: 2.5, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" }, width: \"hug\", height: \"hug\" },\r\n commands: FIELD_COMMANDS,\r\n measure: (_measurer, s) => inkSize(traceArrows(s, reading(s)).runs),\r\n render: (ctx, s) => drawField(ctx, s, traceArrows(s, reading(s))),\r\n}), (s: ArrowsValues, read: MarkReading) => traceArrows(s, read).runs);\r\n\r\nexport type VectorField = NodeOf<typeof VectorField>;\r\n\r\nfunction arrows(s: ArrowsValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {\r\n const [x0, x1] = map.xRange;\r\n const [y0, y1] = map.yRange;\r\n let spacing = Math.max(s.spacing, 1e-6);\r\n const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);\r\n while (count() > MAX_ARROWS) spacing *= 2;\r\n\r\n const drawn: FieldStroke[] = [];\r\n const v: Vector2 = { x: 0, y: 0 };\r\n const longest = spacing * 0.9;\r\n for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {\r\n for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {\r\n const gx = i * spacing;\r\n const gy = j * spacing;\r\n if (!field(gx, gy, v)) continue;\r\n const magnitude = Math.hypot(v.x, v.y);\r\n const length = Math.min(magnitude * s.arrowScale, longest);\r\n if (!(length > 0)) continue;\r\n const hx = ((v.x / magnitude) * length) / 2;\r\n const hy = ((v.y / magnitude) * length) / 2;\r\n const tail = map.toPoint(gx - hx, gy - hy);\r\n const head = map.toPoint(gx + hx, gy + hy);\r\n drawn.push({ runs: arrow(tail, head), magnitude });\r\n }\r\n }\r\n return drawn;\r\n}\r\n\r\n/** A shaft and the two barbs of its head, in pixels. */\r\nfunction arrow(tail: Vector2, head: Vector2): Vector2[][] {\r\n const dx = head.x - tail.x;\r\n const dy = head.y - tail.y;\r\n const length = Math.hypot(dx, dy);\r\n if (!(length > 0)) return [];\r\n const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);\r\n const back = Math.atan2(dy, dx) + Math.PI;\r\n const at = (angle: number): Vector2 => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });\r\n return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];\r\n}\r\n"]}