@motionscript/math 0.1.0-alpha.4 → 0.1.0-alpha.6

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 +132 -92
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +52 -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 +59 -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 +43 -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 +58 -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 +24 -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 +28 -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 +37 -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 +30 -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 +38 -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 +30 -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 +107 -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 +34 -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 +38 -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 +34 -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 +31 -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 +99 -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 +69 -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 +126 -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 +51 -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 +92 -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 +47 -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 +56 -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 +55 -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 +140 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +159 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +70 -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 +57 -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 +131 -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 +58 -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 +182 -161
  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,27 +1,28 @@
1
1
  import {
2
- colorProperty,
3
- command,
4
- fillProperty,
2
+ component2d,
5
3
  Graphics2D,
6
- node,
7
- PaintNode,
8
- property,
9
- strokeProperty,
10
- type AssetScope,
4
+ Types,
11
5
  type Color,
12
- type Command,
13
- type CommandArgs,
14
6
  type Fill,
15
7
  type FillResolved,
16
- type NodeConfig,
8
+ type NodeOf,
17
9
  type NormalizedColor,
18
10
  type PaintProps,
19
- type RenderContext2D,
11
+ type RenderContext,
12
+ type ShadowResolved,
13
+ type Size2D,
20
14
  type Stroke,
21
15
  type StrokeResolved,
16
+ type Values,
22
17
  type Vector2,
18
+ // Named so the emitted declaration reaches these through the sdk, not core.
19
+ type Node2D as _Node2D,
20
+ type Node2DProps as _Node2DProps,
21
+ type Shadow as _Shadow,
23
22
  } from "@motionscript/sdk";
24
- import { declareFormulas, drawLabels, type Label } from "../kit/furniture";
23
+ import { createBoundedCache } from "../kit/cache";
24
+ import { listed, PAINT, PAINT_COMMANDS, painted } from "../kit/component";
25
+ import { drawLabels, loadFormulas, type Label } from "../kit/furniture";
25
26
  import { lerpPartFills, partPaint, readPartFills, withPartFill, type PartFill, type PartFillResolved } from "../kit/part-fills";
26
27
  import { GRAPH_LAYOUTS, layOut, type GraphEdge, type GraphLayout } from "./graph-layout";
27
28
 
@@ -50,7 +51,7 @@ export interface NetworkGraphProps extends PaintProps {
50
51
 
51
52
  // Every list keeps each entry as it was written, a blank or a repeat included, so
52
53
  // an editor showing the list shows the row it just added. What is drawn skips
53
- // what is not complete yet (`_names`, `_links`).
54
+ // what is not complete yet (`names`, `links`).
54
55
 
55
56
  function readVertices(value: readonly unknown[] | undefined): string[] {
56
57
  return (Array.isArray(value) ? value : []).map((name) => String(name ?? ""));
@@ -72,6 +73,35 @@ function readPositions(value: readonly GraphPosition[] | undefined): GraphPositi
72
73
  /** How far a directed edge and the one back between the same two vertices bow apart, as a share of their length. */
73
74
  const BOW = 0.12;
74
75
 
76
+ const NUL = String.fromCharCode(0);
77
+
78
+ const state = {
79
+ ...PAINT,
80
+ vertices: Types.custom<string[]>([], { resolve: readVertices, animatable: false, kind: "vector" }),
81
+ edges: Types.custom<GraphEdge[]>([], { resolve: readEdges, animatable: false, kind: "vector" }),
82
+ /** Draw each edge as an arrow from its `from` to its `to`. */
83
+ directed: Types.boolean(false, { animatable: false }),
84
+ arrangement: listed(GRAPH_LAYOUTS, "circular", { animatable: false }),
85
+ /** The vertex a tree hangs from; left empty, the first. */
86
+ root: Types.text("", { animatable: false }),
87
+ /** Vertices placed by hand, whatever the layout says, in the box from -1 to 1 each way. */
88
+ positions: Types.custom<GraphPosition[]>([], { resolve: readPositions, animatable: false, kind: "vector" }),
89
+ vertexRadius: Types.number(18, { min: 0, unit: "px" }),
90
+ vertexStroke: Types.stroke([]),
91
+ labels: Types.boolean(true, { animatable: false }),
92
+ labelSize: Types.number(18, { min: 1, unit: "px" }),
93
+ labelColor: Types.color("#111827"),
94
+ fontFamily: Types.text("", { animatable: false }),
95
+ /** Paints for a vertex by its name, or an edge written `a-b`, over the graph's own. */
96
+ partFills: Types.custom<PartFill[], PartFillResolved[]>([], { resolve: readPartFills, lerp: lerpPartFills, kind: "vector" }),
97
+ /** The arrangement an `arrangeTo` is leaving, and how far it is towards the new one. */
98
+ layoutFrom: Types.text("", { private: true }),
99
+ layoutMix: Types.number(1, { private: true }),
100
+ };
101
+
102
+ /** What a graph is drawn from: its own entries, as its fields and its node both read them. */
103
+ export type GraphValues = Values<typeof state>;
104
+
75
105
  /**
76
106
  * Vertices and the edges between them, arranged by a rule: in a circle, by
77
107
  * springs, as a tree from `root`, or in a grid. Its `fill` is the vertices and
@@ -79,196 +109,149 @@ const BOW = 0.12;
79
109
  * moves every vertex to where another rule places it; `partFillTo` paints a
80
110
  * vertex, or an edge written `a-b`. (`layout` is every node's own box.)
81
111
  */
82
- @node({
112
+ export const NetworkGraph = component2d({
83
113
  key: "networkGraph",
84
- parentKey: "shape",
85
- forkable: true,
86
- layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: false },
87
- seed: {
88
- width: 560,
89
- height: 400,
90
- vertices: ["1", "2", "3", "4", "5"],
91
- edges: [{ from: "1", to: "2" }, { from: "2", to: "3" }, { from: "3", to: "4" }, { from: "4", to: "5" }, { from: "5", to: "1" }, { from: "1", to: "3" }],
114
+ state,
115
+ defaults: {
116
+ fill: "#e5e7eb",
117
+ stroke: { weight: 3, fill: "#9ca3af", align: "center", cap: "round" },
92
118
  },
93
- })
94
- export class NetworkGraph extends PaintNode<NetworkGraphProps> {
95
- @property({ default: [], mapper: readVertices, animatable: false, kind: "vector" }) declare vertices: string[];
96
- @property({ default: [], mapper: readEdges, animatable: false, kind: "vector" }) declare edges: GraphEdge[];
97
- /** Draw each edge as an arrow from its `from` to its `to`. */
98
- @property({ default: false, animatable: false }) declare directed: boolean;
99
- @property({ default: "circular", options: GRAPH_LAYOUTS, animatable: false }) declare arrangement: GraphLayout;
100
- /** The vertex a tree hangs from; left empty, the first. */
101
- @property({ default: "", animatable: false }) declare root: string;
102
- /** Vertices placed by hand, whatever the layout says, in the box from -1 to 1 each way. */
103
- @property({ default: [], mapper: readPositions, animatable: false, kind: "vector" }) declare positions: GraphPosition[];
104
-
105
- @property({ default: 18, min: 0, unit: "px" }) declare vertexRadius: number;
106
- @strokeProperty({ default: [] }) declare vertexStroke: Stroke;
107
- @property({ default: true, animatable: false }) declare labels: boolean;
108
- @property({ default: 18, min: 1, unit: "px" }) declare labelSize: number;
109
- @colorProperty({ default: "#111827" }) declare labelColor: Color;
110
- @property({ default: "", animatable: false }) declare fontFamily: string;
111
- /** Paints for a vertex by its name, or an edge written `a-b`, over the graph's own. */
112
- @property({ default: [], mapper: readPartFills, tween: lerpPartFills, kind: "vector" }) declare partFills: PartFill[];
113
-
114
- @fillProperty({ default: "#e5e7eb" }) declare fill: Fill;
115
- @strokeProperty({ default: { weight: 3, fill: "#9ca3af", align: "center", cap: "round" } }) declare stroke: Stroke;
116
-
117
- declare private _layoutFrom: string;
118
- declare private _layoutMix: number;
119
+ load: (loader, s) => {
120
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
121
+ if (s.labels) loadFormulas(loader, names(s));
122
+ },
123
+ commands: {
124
+ ...PAINT_COMMANDS,
125
+ /** Move every vertex to where another arrangement places it. */
126
+ arrangeTo: {
127
+ // No default: one left out keeps the arrangement the graph has.
128
+ args: { arrangement: listed(GRAPH_LAYOUTS, undefined) },
129
+ run: (timeline, s) => {
130
+ const from = s.arrangement;
131
+ const to = timeline.args.arrangement ?? from;
132
+ return timeline.animate((t) => ({ arrangement: to, layoutFrom: from, layoutMix: t }));
133
+ },
134
+ },
135
+ /** Paint one part of the graph: a vertex by its name, or an edge written `a-b`. */
136
+ partFillTo: {
137
+ args: { part: Types.text(""), fill: Types.custom<Fill>("#facc15", { kind: "fills" }) },
138
+ run: (timeline, s) => {
139
+ const { part, fill } = timeline.args;
140
+ if (!part || fill == null) return timeline.tween({});
141
+ return timeline.tween({ partFills: withPartFill(s.partFills, { part, fill }) });
142
+ },
143
+ },
144
+ },
145
+ render: (ctx, s) => painted(ctx, () => drawGraph(ctx, s)),
146
+ });
119
147
 
120
- constructor(props?: NodeConfig<NetworkGraph, NetworkGraphProps>) {
121
- super(props ?? {});
122
- this.applyProp("_layoutFrom", "");
123
- this.applyProp("_layoutMix", 1);
124
- }
148
+ export type NetworkGraph = NodeOf<typeof NetworkGraph>;
125
149
 
126
- /** Move every vertex to where another arrangement places it. */
127
- @command({ args: ["arrangement"] })
128
- arrangeTo(args: CommandArgs<{ arrangement: GraphLayout }> & { duration: number }): Command<NetworkGraphProps> {
129
- const from = this.arrangement;
130
- const to = args.data?.arrangement ?? from;
131
- return this.command<NetworkGraphProps>(
132
- (t) => ({ arrangement: to, _layoutFrom: from, _layoutMix: t }) as Partial<NetworkGraphProps>,
133
- args.duration,
134
- args.easing,
135
- );
150
+ /** Where each vertex is drawn in a graph of `size`, in pixels from its centre, y up. */
151
+ export function vertexPoints(graph: GraphValues, size: Size2D): Map<string, Vector2> {
152
+ const room = graph.vertexRadius + Math.max(2, graph.labelSize * 0.2);
153
+ const halfX = Math.max(0, size.width / 2 - room);
154
+ const halfY = Math.max(0, size.height / 2 - room);
155
+ const to = placed(graph, graph.arrangement);
156
+ const from = graph.layoutMix < 1 && graph.layoutFrom !== "" ? placed(graph, graph.layoutFrom as GraphLayout) : null;
157
+ const t = graph.layoutMix;
158
+ const out = new Map<string, Vector2>();
159
+ for (const [name, p] of to) {
160
+ const q = from?.get(name) ?? p;
161
+ out.set(name, { x: (q.x + (p.x - q.x) * t) * halfX, y: (q.y + (p.y - q.y) * t) * halfY });
136
162
  }
163
+ return out;
164
+ }
137
165
 
138
- /** Paint one part of the graph: a vertex by its name, or an edge written `a-b`. */
139
- @command({
140
- args: [
141
- { key: "part", kind: "text", default: "" },
142
- { key: "fill", kind: "fills", default: "#facc15" },
143
- ],
144
- })
145
- partFillTo(args: CommandArgs<{ part: string; fill: Fill }> & { duration: number }): Command<NetworkGraphProps> {
146
- const { part, fill } = args.data ?? {};
147
- if (!part || fill == null) return this.to({ data: {}, duration: args.duration, easing: args.easing });
148
- return this.to({
149
- data: { partFills: withPartFill(this.partFills as unknown as PartFillResolved[], { part, fill }) } as Partial<NetworkGraphProps>,
150
- duration: args.duration,
151
- easing: args.easing,
152
- });
153
- }
166
+ function drawGraph(ctx: RenderContext, graph: GraphValues): void {
167
+ const points = vertexPoints(graph, ctx.size);
168
+ if (points.size === 0) return;
169
+ const parts = new Map((graph.partFills as PartFillResolved[]).map((entry) => [entry.part, entry]));
170
+ drawEdges(ctx, graph, points, parts);
171
+ drawVertices(ctx, graph, points, parts);
172
+ }
154
173
 
155
- override declareAssets(assets: AssetScope): void {
156
- super.declareAssets(assets);
157
- if (this.fontFamily !== "") assets.font(this.fontFamily);
158
- if (this.labels) declareFormulas(assets, this._names());
159
- }
174
+ // Two layouts are held at once while one becomes the other, and a scene may hold several graphs.
175
+ const layouts = createBoundedCache<Map<string, Vector2>>(32);
160
176
 
161
- /** Where each vertex is drawn, in pixels from the node's centre, y up. */
162
- vertexPoints(): Map<string, Vector2> {
163
- const { width, height } = this.layoutBounds;
164
- const room = this.vertexRadius + Math.max(2, this.labelSize * 0.2);
165
- const halfX = Math.max(0, width / 2 - room);
166
- const halfY = Math.max(0, height / 2 - room);
167
- const to = this._placed(this.arrangement);
168
- const from = this._layoutMix < 1 && this._layoutFrom !== "" ? this._placed(this._layoutFrom as GraphLayout) : null;
169
- const t = this._layoutMix;
170
- const out = new Map<string, Vector2>();
171
- for (const [name, p] of to) {
172
- const q = from?.get(name) ?? p;
173
- out.set(name, { x: (q.x + (p.x - q.x) * t) * halfX, y: (q.y + (p.y - q.y) * t) * halfY });
177
+ /** Each vertex's place under `layout`, from -1 to 1, a hand-placed one where it is. */
178
+ function placed(graph: GraphValues, layout: GraphLayout): Map<string, Vector2> {
179
+ const vertices = names(graph);
180
+ const edges = links(graph);
181
+ const key = [layout, graph.root, vertices.join(NUL), JSON.stringify(edges), JSON.stringify(graph.positions)].join("|");
182
+ let at = layouts.get(key);
183
+ if (!at) {
184
+ at = layOut(layout, vertices, edges, graph.root.trim());
185
+ for (const { vertex, x, y } of graph.positions) {
186
+ const name = vertex.trim();
187
+ if (at.has(name)) at.set(name, { x, y });
174
188
  }
175
- return out;
176
- }
177
-
178
- protected override renderSelf(ctx: RenderContext2D): void {
179
- const points = this.vertexPoints();
180
- if (points.size === 0) return;
181
- const parts = new Map((this.partFills as unknown as PartFillResolved[]).map((entry) => [entry.part, entry]));
182
- this._drawEdges(ctx, points, parts);
183
- this._drawVertices(ctx, points, parts);
189
+ layouts.set(key, at);
184
190
  }
191
+ return at;
192
+ }
185
193
 
186
- // The edges are drawn under the vertices, in `renderSelf`; there is no outline of the node's own.
187
- protected override renderStroke(): void {}
188
-
189
- private _layouts = new Map<string, Map<string, Vector2>>();
190
-
191
- /** Each vertex's place under `layout`, from -1 to 1, a hand-placed one where it is. */
192
- private _placed(layout: GraphLayout): Map<string, Vector2> {
193
- const vertices = this._names();
194
- const edges = this._links();
195
- const key = [layout, this.root, vertices.join("\u0000"), JSON.stringify(edges), JSON.stringify(this.positions)].join("|");
196
- let placed = this._layouts.get(key);
197
- if (!placed) {
198
- placed = layOut(layout, vertices, edges, this.root.trim());
199
- for (const { vertex, x, y } of this.positions) {
200
- const name = vertex.trim();
201
- if (placed.has(name)) placed.set(name, { x, y });
202
- }
203
- // Two layouts are held at once while one becomes the other.
204
- if (this._layouts.size > 4) this._layouts.clear();
205
- this._layouts.set(key, placed);
194
+ function drawEdges(ctx: RenderContext, graph: GraphValues, points: Map<string, Vector2>, parts: Map<string, PartFillResolved>): void {
195
+ const stroke = graph.stroke as StrokeResolved[];
196
+ if (stroke.length === 0) return;
197
+ const r = graph.vertexRadius;
198
+ const all = links(graph);
199
+ const pairs = new Set(all.map(({ from, to }) => `${from}${NUL}${to}`));
200
+ const plain = new Graphics2D();
201
+ let drawnPlain = false;
202
+ for (const { from, to } of all) {
203
+ const a = points.get(from);
204
+ const b = points.get(to);
205
+ if (!a || !b) continue;
206
+ const run = from === to ? loop(a, r) : edge(a, b, r, graph.directed && pairs.has(`${to}${NUL}${from}`) ? BOW : 0);
207
+ if (run.length < 2) continue;
208
+ const paint = parts.get(`${from}-${to}`) ?? (graph.directed ? undefined : parts.get(`${to}-${from}`));
209
+ const own = graph.directed || paint !== undefined;
210
+ const g = own ? new Graphics2D() : plain;
211
+ g.line({ points: run, radius: 0, start: graph.start, end: graph.end });
212
+ if (!own) {
213
+ drawnPlain = true;
214
+ continue;
206
215
  }
207
- return placed;
216
+ let layers = stroke;
217
+ if (paint) layers = layers.map((layer) => ({ ...layer, fill: partPaint(layer.fill, paint) }));
218
+ if (graph.directed && from !== to) layers = layers.map((layer) => ({ ...layer, endCap: layer.endCap ?? "triangle" }));
219
+ ctx.draw(g.stroke(layers));
208
220
  }
221
+ if (drawnPlain) ctx.draw(plain.stroke(stroke));
222
+ }
209
223
 
210
- private _drawEdges(ctx: RenderContext2D, points: Map<string, Vector2>, parts: Map<string, PartFillResolved>): void {
211
- const stroke = this.stroke as StrokeResolved[];
212
- if (stroke.length === 0) return;
213
- const r = this.vertexRadius;
214
- const links = this._links();
215
- const pairs = new Set(links.map(({ from, to }) => `${from}\u0000${to}`));
216
- const plain = new Graphics2D();
217
- let drawnPlain = false;
218
- for (const { from, to } of links) {
219
- const a = points.get(from);
220
- const b = points.get(to);
221
- if (!a || !b) continue;
222
- const run = from === to ? loop(a, r) : edge(a, b, r, this.directed && pairs.has(`${to}\u0000${from}`) ? BOW : 0);
223
- if (run.length < 2) continue;
224
- const paint = parts.get(`${from}-${to}`) ?? (this.directed ? undefined : parts.get(`${to}-${from}`));
225
- const own = this.directed || paint !== undefined;
226
- const g = own ? new Graphics2D() : plain;
227
- g.line({ points: run, radius: 0, start: this.start, end: this.end });
228
- if (!own) {
229
- drawnPlain = true;
230
- continue;
231
- }
232
- let layers = stroke;
233
- if (paint) layers = layers.map((layer) => ({ ...layer, fill: partPaint(layer.fill, paint) }));
234
- if (this.directed && from !== to) layers = layers.map((layer) => ({ ...layer, endCap: layer.endCap ?? "triangle" }));
235
- ctx.draw(g.stroke(layers));
224
+ function drawVertices(ctx: RenderContext, graph: GraphValues, points: Map<string, Vector2>, parts: Map<string, PartFillResolved>): void {
225
+ const r = graph.vertexRadius;
226
+ const fill = graph.fill as FillResolved[];
227
+ const outline = graph.vertexStroke as StrokeResolved[];
228
+ const labels: Label[] = [];
229
+ for (const [name, p] of points) {
230
+ if (r > 0) {
231
+ const paint = partPaint(fill, parts.get(name));
232
+ const disc = new Graphics2D().ellipse({ x: p.x, y: p.y, width: r * 2, height: r * 2 });
233
+ ctx.draw(disc.shadow(graph.shadow as ShadowResolved[]).fill(paint));
234
+ if (outline.length > 0) ctx.draw(new Graphics2D().ellipse({ x: p.x, y: p.y, width: r * 2, height: r * 2 }).stroke(outline));
236
235
  }
237
- if (drawnPlain) ctx.draw(plain.stroke(stroke));
236
+ if (graph.labels) labels.push({ text: name, at: { x: p.x, y: p.y }, align: "center" });
238
237
  }
239
-
240
- private _drawVertices(ctx: RenderContext2D, points: Map<string, Vector2>, parts: Map<string, PartFillResolved>): void {
241
- const r = this.vertexRadius;
242
- const fill = this.fill as FillResolved[];
243
- const outline = this.vertexStroke as StrokeResolved[];
244
- const labels: Label[] = [];
245
- for (const [name, p] of points) {
246
- if (r > 0) {
247
- const paint = partPaint(fill, parts.get(name));
248
- const disc = new Graphics2D().ellipse({ x: p.x, y: p.y, width: r * 2, height: r * 2 });
249
- ctx.draw(disc.shadow(this.shadow).fill(paint));
250
- if (outline.length > 0) ctx.draw(new Graphics2D().ellipse({ x: p.x, y: p.y, width: r * 2, height: r * 2 }).stroke(outline));
251
- }
252
- if (this.labels) labels.push({ text: name, at: { x: p.x, y: p.y }, align: "center" });
253
- }
254
- for (const part of drawLabels(labels, { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor })) {
255
- ctx.draw(part);
256
- }
238
+ for (const part of drawLabels(labels, { fontSize: graph.labelSize, fontFamily: graph.fontFamily, color: graph.labelColor as NormalizedColor })) {
239
+ ctx.draw(part);
257
240
  }
241
+ }
258
242
 
259
- /** The vertices drawn: each name once, trimmed, the blank ones left out. */
260
- private _names(): string[] {
261
- return [...new Set(this.vertices.map((name) => name.trim()).filter((name) => name !== ""))];
262
- }
243
+ /** The vertices drawn: each name once, trimmed, the blank ones left out. */
244
+ function names(graph: Pick<GraphValues, "vertices">): string[] {
245
+ return [...new Set(graph.vertices.map((name) => name.trim()).filter((name) => name !== ""))];
246
+ }
263
247
 
264
- /** The edges drawn: both ends named. */
265
- private _links(): GraphEdge[] {
266
- return this.edges.flatMap(({ from, to }) => {
267
- const a = from.trim();
268
- const b = to.trim();
269
- return a === "" || b === "" ? [] : [{ from: a, to: b }];
270
- });
271
- }
248
+ /** The edges drawn: both ends named. */
249
+ function links(graph: Pick<GraphValues, "edges">): GraphEdge[] {
250
+ return graph.edges.flatMap(({ from, to }) => {
251
+ const a = from.trim();
252
+ const b = to.trim();
253
+ return a === "" || b === "" ? [] : [{ from: a, to: b }];
254
+ });
272
255
  }
273
256
 
274
257
  /** An edge from one vertex's rim to the other's, straight or bowed to its left. */