@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.
- package/CHANGELOG.md +28 -0
- package/README.md +39 -33
- package/dist/annotate/angle-mark.d.ts +77 -27
- package/dist/annotate/angle-mark.d.ts.map +1 -1
- package/dist/annotate/angle-mark.js +80 -115
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +94 -25
- package/dist/annotate/curly-brace.d.ts.map +1 -1
- package/dist/annotate/curly-brace.js +85 -100
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +69 -9
- package/dist/annotate/emphasis-mark.d.ts.map +1 -1
- package/dist/annotate/emphasis-mark.js +33 -34
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
- package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
- package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +2 -2
- package/dist/engine.d.ts +26 -19
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +17 -14
- package/dist/engine.js.map +1 -1
- package/dist/kit/component.d.ts +51 -0
- package/dist/kit/component.d.ts.map +1 -0
- package/dist/kit/component.js +30 -0
- package/dist/kit/component.js.map +1 -0
- package/dist/kit/curve.d.ts +22 -11
- package/dist/kit/curve.d.ts.map +1 -1
- package/dist/kit/curve.js +25 -18
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/field-mark.d.ts +56 -35
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js +100 -120
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/follow.d.ts +67 -29
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js +114 -110
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.d.ts +3 -1
- package/dist/kit/furniture.d.ts.map +1 -1
- package/dist/kit/furniture.js +5 -0
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/graph.d.ts +46 -0
- package/dist/kit/graph.d.ts.map +1 -0
- package/dist/kit/graph.js +90 -0
- package/dist/kit/graph.js.map +1 -0
- package/dist/kit/mark.d.ts +64 -39
- package/dist/kit/mark.d.ts.map +1 -1
- package/dist/kit/mark.js +80 -96
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/place.d.ts +10 -8
- package/dist/kit/place.d.ts.map +1 -1
- package/dist/kit/place.js +28 -30
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/system.d.ts +27 -29
- package/dist/kit/system.d.ts.map +1 -1
- package/dist/kit/system.js +62 -58
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/withdrawn.d.ts +3 -3
- package/dist/kit/withdrawn.js +3 -3
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.d.ts +37 -18
- package/dist/marks/curve-area.d.ts.map +1 -1
- package/dist/marks/curve-area.js +50 -73
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.d.ts +45 -10
- package/dist/marks/drop-lines.d.ts.map +1 -1
- package/dist/marks/drop-lines.js +37 -52
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +56 -20
- package/dist/marks/plot-point.d.ts.map +1 -1
- package/dist/marks/plot-point.js +65 -91
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +47 -26
- package/dist/marks/riemann-sum.d.ts.map +1 -1
- package/dist/marks/riemann-sum.js +57 -85
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +58 -14
- package/dist/marks/tangent-line.d.ts.map +1 -1
- package/dist/marks/tangent-line.js +73 -87
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +48 -20
- package/dist/marks/vector-arrow.d.ts.map +1 -1
- package/dist/marks/vector-arrow.js +58 -78
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.d.ts +13 -5
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +0 -6
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +135 -43
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js +146 -177
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.d.ts +55 -19
- package/dist/plots/points-plot.d.ts.map +1 -1
- package/dist/plots/points-plot.js +31 -58
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.d.ts +60 -19
- package/dist/plots/stream-lines.d.ts.map +1 -1
- package/dist/plots/stream-lines.js +60 -79
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.d.ts +54 -15
- package/dist/plots/traced-path.d.ts.map +1 -1
- package/dist/plots/traced-path.js +33 -48
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.d.ts +48 -11
- package/dist/plots/vector-field.d.ts.map +1 -1
- package/dist/plots/vector-field.js +50 -64
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/network-graph.d.ts +140 -49
- package/dist/structures/network-graph.d.ts.map +1 -1
- package/dist/structures/network-graph.js +168 -217
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.d.ts +90 -38
- package/dist/structures/sample-space.d.ts.map +1 -1
- package/dist/structures/sample-space.js +132 -170
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts +159 -37
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js +147 -183
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.d.ts +80 -28
- package/dist/systems/number-line.d.ts.map +1 -1
- package/dist/systems/number-line.js +82 -118
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.d.ts +140 -31
- package/dist/systems/number-plane.d.ts.map +1 -1
- package/dist/systems/number-plane.js +142 -149
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.d.ts +76 -26
- package/dist/systems/polar-plane.d.ts.map +1 -1
- package/dist/systems/polar-plane.js +107 -133
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/index.d.ts.map +1 -1
- package/dist/tex/index.js +1 -0
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/matrix.d.ts +88 -18
- package/dist/tex/matrix.d.ts.map +1 -1
- package/dist/tex/matrix.js +33 -50
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.d.ts +84 -201
- package/dist/tex/node.d.ts.map +1 -1
- package/dist/tex/node.js +23 -593
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/typeset.d.ts +133 -0
- package/dist/tex/typeset.d.ts.map +1 -0
- package/dist/tex/typeset.js +403 -0
- package/dist/tex/typeset.js.map +1 -0
- package/dist/three/axes3d.d.ts +214 -54
- package/dist/three/axes3d.d.ts.map +1 -1
- package/dist/three/axes3d.js +163 -204
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +108 -42
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js +97 -132
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.d.ts +17 -0
- package/dist/three/entries.d.ts.map +1 -0
- package/dist/three/entries.js +15 -0
- package/dist/three/entries.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +85 -12
- package/dist/three/plot-point3d.d.ts.map +1 -1
- package/dist/three/plot-point3d.js +27 -53
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.d.ts +17 -2
- package/dist/three/space.d.ts.map +1 -1
- package/dist/three/space.js +19 -8
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +176 -49
- package/dist/three/surface-plot3d.d.ts.map +1 -1
- package/dist/three/surface-plot3d.js +227 -261
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +87 -16
- package/dist/three/vector-arrow3d.d.ts.map +1 -1
- package/dist/three/vector-arrow3d.js +48 -67
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/src/annotate/angle-mark.ts +95 -105
- package/src/annotate/curly-brace.ts +107 -85
- package/src/annotate/emphasis-mark.ts +55 -26
- package/src/engine.ts +35 -21
- package/src/kit/component.ts +57 -0
- package/src/kit/curve.ts +38 -20
- package/src/kit/field-mark.ts +124 -108
- package/src/kit/follow.ts +146 -120
- package/src/kit/furniture.ts +6 -1
- package/src/kit/graph.ts +116 -0
- package/src/kit/mark.ts +135 -114
- package/src/kit/place.ts +32 -56
- package/src/kit/system.ts +77 -71
- package/src/kit/withdrawn.ts +3 -3
- package/src/marks/curve-area.ts +65 -65
- package/src/marks/drop-lines.ts +50 -42
- package/src/marks/plot-point.ts +77 -82
- package/src/marks/riemann-sum.ts +73 -72
- package/src/marks/tangent-line.ts +87 -71
- package/src/marks/vector-arrow.ts +73 -73
- package/src/nodes.ts +41 -5
- package/src/plots/function-plot.ts +155 -171
- package/src/plots/points-plot.ts +46 -50
- package/src/plots/stream-lines.ts +75 -66
- package/src/plots/traced-path.ts +51 -33
- package/src/plots/vector-field.ts +68 -49
- package/src/structures/network-graph.ts +168 -185
- package/src/structures/sample-space.ts +149 -148
- package/src/systems/axes2d.ts +165 -156
- package/src/systems/number-line.ts +91 -92
- package/src/systems/number-plane.ts +149 -120
- package/src/systems/polar-plane.ts +117 -116
- package/src/tex/index.ts +2 -1
- package/src/tex/matrix.ts +62 -40
- package/src/tex/node.ts +54 -621
- package/src/tex/typeset.ts +461 -0
- package/src/three/axes3d.ts +187 -160
- package/src/three/curve-plot3d.ts +107 -110
- package/src/three/entries.ts +42 -0
- package/src/three/plot-point3d.ts +42 -45
- package/src/three/space.ts +34 -9
- package/src/three/surface-plot3d.ts +312 -323
- package/src/three/vector-arrow3d.ts +65 -54
- package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
- package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
- package/src/kit/placement.ts +0 -8
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
command,
|
|
4
|
-
fillProperty,
|
|
2
|
+
component2d,
|
|
5
3
|
Graphics2D,
|
|
6
|
-
|
|
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
|
|
8
|
+
type NodeOf,
|
|
17
9
|
type NormalizedColor,
|
|
18
10
|
type PaintProps,
|
|
19
|
-
type
|
|
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 {
|
|
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 (`
|
|
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
|
-
|
|
112
|
+
export const NetworkGraph = component2d({
|
|
83
113
|
key: "networkGraph",
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
121
|
-
super(props ?? {});
|
|
122
|
-
this.applyProp("_layoutFrom", "");
|
|
123
|
-
this.applyProp("_layoutMix", 1);
|
|
124
|
-
}
|
|
148
|
+
export type NetworkGraph = NodeOf<typeof NetworkGraph>;
|
|
125
149
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
156
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
if (!
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
-
|
|
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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 (
|
|
236
|
+
if (graph.labels) labels.push({ text: name, at: { x: p.x, y: p.y }, align: "center" });
|
|
238
237
|
}
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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. */
|