@motionscript/math 0.0.0-stage → 0.1.0-alpha.5
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 +110 -0
- package/LICENSE +201 -0
- package/NOTICE +48 -0
- package/README.md +385 -3
- package/dist/annotate/angle-mark.d.ts +46 -0
- package/dist/annotate/angle-mark.d.ts.map +1 -0
- package/dist/annotate/angle-mark.js +132 -0
- package/dist/annotate/angle-mark.js.map +1 -0
- package/dist/annotate/curly-brace.d.ts +46 -0
- package/dist/annotate/curly-brace.d.ts.map +1 -0
- package/dist/annotate/curly-brace.js +114 -0
- package/dist/annotate/curly-brace.js.map +1 -0
- package/dist/annotate/emphasis-mark.d.ts +20 -0
- package/dist/annotate/emphasis-mark.d.ts.map +1 -0
- package/dist/annotate/emphasis-mark.js +81 -0
- package/dist/annotate/emphasis-mark.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +30 -0
- package/dist/browser/tex.js +2 -0
- package/dist/browser/tex.js.map +7 -0
- package/dist/engine.d.ts +64 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +42 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/brace.d.ts +8 -0
- package/dist/kit/brace.d.ts.map +1 -0
- package/dist/kit/brace.js +36 -0
- package/dist/kit/brace.js.map +1 -0
- package/dist/kit/cache.d.ts +30 -0
- package/dist/kit/cache.d.ts.map +1 -0
- package/dist/kit/cache.js +53 -0
- package/dist/kit/cache.js.map +1 -0
- package/dist/kit/contour.d.ts +8 -0
- package/dist/kit/contour.d.ts.map +1 -0
- package/dist/kit/contour.js +93 -0
- package/dist/kit/contour.js.map +1 -0
- package/dist/kit/coords.d.ts +69 -0
- package/dist/kit/coords.d.ts.map +1 -0
- package/dist/kit/coords.js +224 -0
- package/dist/kit/coords.js.map +1 -0
- package/dist/kit/curve.d.ts +17 -0
- package/dist/kit/curve.d.ts.map +1 -0
- package/dist/kit/curve.js +22 -0
- package/dist/kit/curve.js.map +1 -0
- package/dist/kit/equation.d.ts +61 -0
- package/dist/kit/equation.d.ts.map +1 -0
- package/dist/kit/equation.js +168 -0
- package/dist/kit/equation.js.map +1 -0
- package/dist/kit/equation3d.d.ts +50 -0
- package/dist/kit/equation3d.d.ts.map +1 -0
- package/dist/kit/equation3d.js +103 -0
- package/dist/kit/equation3d.js.map +1 -0
- package/dist/kit/expression.d.ts +35 -0
- package/dist/kit/expression.d.ts.map +1 -0
- package/dist/kit/expression.js +124 -0
- package/dist/kit/expression.js.map +1 -0
- package/dist/kit/field-mark.d.ts +58 -0
- package/dist/kit/field-mark.d.ts.map +1 -0
- package/dist/kit/field-mark.js +129 -0
- package/dist/kit/field-mark.js.map +1 -0
- package/dist/kit/field.d.ts +14 -0
- package/dist/kit/field.d.ts.map +1 -0
- package/dist/kit/field.js +46 -0
- package/dist/kit/field.js.map +1 -0
- package/dist/kit/follow.d.ts +71 -0
- package/dist/kit/follow.d.ts.map +1 -0
- package/dist/kit/follow.js +159 -0
- package/dist/kit/follow.js.map +1 -0
- package/dist/kit/furniture.d.ts +30 -0
- package/dist/kit/furniture.d.ts.map +1 -0
- package/dist/kit/furniture.js +87 -0
- package/dist/kit/furniture.js.map +1 -0
- package/dist/kit/geometry.d.ts +13 -0
- package/dist/kit/geometry.d.ts.map +1 -0
- package/dist/kit/geometry.js +54 -0
- package/dist/kit/geometry.js.map +1 -0
- package/dist/kit/mark.d.ts +47 -0
- package/dist/kit/mark.d.ts.map +1 -0
- package/dist/kit/mark.js +103 -0
- package/dist/kit/mark.js.map +1 -0
- package/dist/kit/part-fills.d.ts +23 -0
- package/dist/kit/part-fills.d.ts.map +1 -0
- package/dist/kit/part-fills.js +46 -0
- package/dist/kit/part-fills.js.map +1 -0
- package/dist/kit/place.d.ts +18 -0
- package/dist/kit/place.d.ts.map +1 -0
- package/dist/kit/place.js +68 -0
- package/dist/kit/place.js.map +1 -0
- package/dist/kit/placement.d.ts +8 -0
- package/dist/kit/placement.d.ts.map +1 -0
- package/dist/kit/placement.js +8 -0
- package/dist/kit/placement.js.map +1 -0
- package/dist/kit/sampler.d.ts +17 -0
- package/dist/kit/sampler.d.ts.map +1 -0
- package/dist/kit/sampler.js +250 -0
- package/dist/kit/sampler.js.map +1 -0
- package/dist/kit/scale.d.ts +12 -0
- package/dist/kit/scale.d.ts.map +1 -0
- package/dist/kit/scale.js +37 -0
- package/dist/kit/scale.js.map +1 -0
- package/dist/kit/source.d.ts +22 -0
- package/dist/kit/source.d.ts.map +1 -0
- package/dist/kit/source.js +45 -0
- package/dist/kit/source.js.map +1 -0
- package/dist/kit/system.d.ts +33 -0
- package/dist/kit/system.d.ts.map +1 -0
- package/dist/kit/system.js +67 -0
- package/dist/kit/system.js.map +1 -0
- package/dist/kit/ticks.d.ts +19 -0
- package/dist/kit/ticks.d.ts.map +1 -0
- package/dist/kit/ticks.js +78 -0
- package/dist/kit/ticks.js.map +1 -0
- package/dist/kit/withdrawn.d.ts +9 -0
- package/dist/kit/withdrawn.d.ts.map +1 -0
- package/dist/kit/withdrawn.js +9 -0
- package/dist/kit/withdrawn.js.map +1 -0
- package/dist/marks/curve-area.d.ts +32 -0
- package/dist/marks/curve-area.d.ts.map +1 -0
- package/dist/marks/curve-area.js +82 -0
- package/dist/marks/curve-area.js.map +1 -0
- package/dist/marks/drop-lines.d.ts +22 -0
- package/dist/marks/drop-lines.d.ts.map +1 -0
- package/dist/marks/drop-lines.js +58 -0
- package/dist/marks/drop-lines.js.map +1 -0
- package/dist/marks/plot-point.d.ts +35 -0
- package/dist/marks/plot-point.d.ts.map +1 -0
- package/dist/marks/plot-point.js +97 -0
- package/dist/marks/plot-point.js.map +1 -0
- package/dist/marks/riemann-sum.d.ts +37 -0
- package/dist/marks/riemann-sum.d.ts.map +1 -0
- package/dist/marks/riemann-sum.js +91 -0
- package/dist/marks/riemann-sum.js.map +1 -0
- package/dist/marks/tangent-line.d.ts +27 -0
- package/dist/marks/tangent-line.d.ts.map +1 -0
- package/dist/marks/tangent-line.js +103 -0
- package/dist/marks/tangent-line.js.map +1 -0
- package/dist/marks/vector-arrow.d.ts +32 -0
- package/dist/marks/vector-arrow.d.ts.map +1 -0
- package/dist/marks/vector-arrow.js +87 -0
- package/dist/marks/vector-arrow.js.map +1 -0
- package/dist/nodes.d.ts +35 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +63 -0
- package/dist/nodes.js.map +1 -0
- package/dist/plots/function-plot.d.ts +67 -0
- package/dist/plots/function-plot.d.ts.map +1 -0
- package/dist/plots/function-plot.js +215 -0
- package/dist/plots/function-plot.js.map +1 -0
- package/dist/plots/points-plot.d.ts +28 -0
- package/dist/plots/points-plot.d.ts.map +1 -0
- package/dist/plots/points-plot.js +99 -0
- package/dist/plots/points-plot.js.map +1 -0
- package/dist/plots/stream-lines.d.ts +35 -0
- package/dist/plots/stream-lines.d.ts.map +1 -0
- package/dist/plots/stream-lines.js +141 -0
- package/dist/plots/stream-lines.js.map +1 -0
- package/dist/plots/traced-path.d.ts +31 -0
- package/dist/plots/traced-path.d.ts.map +1 -0
- package/dist/plots/traced-path.js +56 -0
- package/dist/plots/traced-path.js.map +1 -0
- package/dist/plots/vector-field.d.ts +23 -0
- package/dist/plots/vector-field.d.ts.map +1 -0
- package/dist/plots/vector-field.js +91 -0
- package/dist/plots/vector-field.js.map +1 -0
- package/dist/structures/graph-layout.d.ts +11 -0
- package/dist/structures/graph-layout.d.ts.map +1 -0
- package/dist/structures/graph-layout.js +135 -0
- package/dist/structures/graph-layout.js.map +1 -0
- package/dist/structures/network-graph.d.ts +83 -0
- package/dist/structures/network-graph.d.ts.map +1 -0
- package/dist/structures/network-graph.js +289 -0
- package/dist/structures/network-graph.js.map +1 -0
- package/dist/structures/sample-space.d.ts +62 -0
- package/dist/structures/sample-space.d.ts.map +1 -0
- package/dist/structures/sample-space.js +199 -0
- package/dist/structures/sample-space.js.map +1 -0
- package/dist/systems/axes2d.d.ts +75 -0
- package/dist/systems/axes2d.d.ts.map +1 -0
- package/dist/systems/axes2d.js +193 -0
- package/dist/systems/axes2d.js.map +1 -0
- package/dist/systems/number-line.d.ts +49 -0
- package/dist/systems/number-line.d.ts.map +1 -0
- package/dist/systems/number-line.js +127 -0
- package/dist/systems/number-line.js.map +1 -0
- package/dist/systems/number-plane.d.ts +59 -0
- package/dist/systems/number-plane.d.ts.map +1 -0
- package/dist/systems/number-plane.js +193 -0
- package/dist/systems/number-plane.js.map +1 -0
- package/dist/systems/polar-plane.d.ts +48 -0
- package/dist/systems/polar-plane.d.ts.map +1 -0
- package/dist/systems/polar-plane.js +165 -0
- package/dist/systems/polar-plane.js.map +1 -0
- package/dist/tex/geometry.d.ts +85 -0
- package/dist/tex/geometry.d.ts.map +1 -0
- package/dist/tex/geometry.js +632 -0
- package/dist/tex/geometry.js.map +1 -0
- package/dist/tex/index.d.ts +11 -0
- package/dist/tex/index.d.ts.map +1 -0
- package/dist/tex/index.js +9 -0
- package/dist/tex/index.js.map +1 -0
- package/dist/tex/mathjax.d.ts +33 -0
- package/dist/tex/mathjax.d.ts.map +1 -0
- package/dist/tex/mathjax.js +74 -0
- package/dist/tex/mathjax.js.map +1 -0
- package/dist/tex/matrix.d.ts +31 -0
- package/dist/tex/matrix.d.ts.map +1 -0
- package/dist/tex/matrix.js +73 -0
- package/dist/tex/matrix.js.map +1 -0
- package/dist/tex/node.d.ts +215 -0
- package/dist/tex/node.d.ts.map +1 -0
- package/dist/tex/node.js +595 -0
- package/dist/tex/node.js.map +1 -0
- package/dist/tex/tween.d.ts +50 -0
- package/dist/tex/tween.d.ts.map +1 -0
- package/dist/tex/tween.js +253 -0
- package/dist/tex/tween.js.map +1 -0
- package/dist/three/axes3d.d.ts +87 -0
- package/dist/three/axes3d.d.ts.map +1 -0
- package/dist/three/axes3d.js +228 -0
- package/dist/three/axes3d.js.map +1 -0
- package/dist/three/curve-plot3d.d.ts +60 -0
- package/dist/three/curve-plot3d.d.ts.map +1 -0
- package/dist/three/curve-plot3d.js +143 -0
- package/dist/three/curve-plot3d.js.map +1 -0
- package/dist/three/label3d.d.ts +11 -0
- package/dist/three/label3d.d.ts.map +1 -0
- package/dist/three/label3d.js +40 -0
- package/dist/three/label3d.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +26 -0
- package/dist/three/plot-point3d.d.ts.map +1 -0
- package/dist/three/plot-point3d.js +60 -0
- package/dist/three/plot-point3d.js.map +1 -0
- package/dist/three/space.d.ts +28 -0
- package/dist/three/space.d.ts.map +1 -0
- package/dist/three/space.js +47 -0
- package/dist/three/space.js.map +1 -0
- package/dist/three/surface-plot3d.d.ts +80 -0
- package/dist/three/surface-plot3d.d.ts.map +1 -0
- package/dist/three/surface-plot3d.js +290 -0
- package/dist/three/surface-plot3d.js.map +1 -0
- package/dist/three/vector-arrow3d.d.ts +25 -0
- package/dist/three/vector-arrow3d.d.ts.map +1 -0
- package/dist/three/vector-arrow3d.js +72 -0
- package/dist/three/vector-arrow3d.js.map +1 -0
- package/package.json +79 -3
- package/registry.json +251 -0
- package/src/annotate/angle-mark.ts +147 -0
- package/src/annotate/curly-brace.ts +126 -0
- package/src/annotate/emphasis-mark.ts +89 -0
- package/src/engine.ts +72 -0
- package/src/index.ts +29 -0
- package/src/kit/brace.ts +37 -0
- package/src/kit/cache.ts +61 -0
- package/src/kit/contour.ts +94 -0
- package/src/kit/coords.ts +278 -0
- package/src/kit/curve.ts +29 -0
- package/src/kit/equation.ts +181 -0
- package/src/kit/equation3d.ts +137 -0
- package/src/kit/expression.ts +159 -0
- package/src/kit/field-mark.ts +155 -0
- package/src/kit/field.ts +50 -0
- package/src/kit/follow.ts +203 -0
- package/src/kit/furniture.ts +108 -0
- package/src/kit/geometry.ts +49 -0
- package/src/kit/mark.ts +131 -0
- package/src/kit/part-fills.ts +61 -0
- package/src/kit/place.ts +101 -0
- package/src/kit/placement.ts +8 -0
- package/src/kit/sampler.ts +281 -0
- package/src/kit/scale.ts +39 -0
- package/src/kit/source.ts +57 -0
- package/src/kit/system.ts +99 -0
- package/src/kit/ticks.ts +85 -0
- package/src/kit/withdrawn.ts +8 -0
- package/src/marks/curve-area.ts +89 -0
- package/src/marks/drop-lines.ts +63 -0
- package/src/marks/plot-point.ts +107 -0
- package/src/marks/riemann-sum.ts +98 -0
- package/src/marks/tangent-line.ts +103 -0
- package/src/marks/vector-arrow.ts +98 -0
- package/src/nodes.ts +63 -0
- package/src/plots/function-plot.ts +236 -0
- package/src/plots/points-plot.ts +107 -0
- package/src/plots/stream-lines.ts +151 -0
- package/src/plots/traced-path.ts +52 -0
- package/src/plots/vector-field.ts +88 -0
- package/src/structures/graph-layout.ts +145 -0
- package/src/structures/network-graph.ts +308 -0
- package/src/structures/sample-space.ts +209 -0
- package/src/systems/axes2d.ts +217 -0
- package/src/systems/number-line.ts +131 -0
- package/src/systems/number-plane.ts +203 -0
- package/src/systems/polar-plane.ts +176 -0
- package/src/tex/geometry.ts +703 -0
- package/src/tex/index.ts +14 -0
- package/src/tex/mathjax.ts +82 -0
- package/src/tex/matrix.ts +75 -0
- package/src/tex/node.ts +641 -0
- package/src/tex/tween.ts +308 -0
- package/src/three/axes3d.ts +240 -0
- package/src/three/curve-plot3d.ts +147 -0
- package/src/three/label3d.ts +50 -0
- package/src/three/plot-point3d.ts +66 -0
- package/src/three/space.ts +67 -0
- package/src/three/surface-plot3d.ts +323 -0
- package/src/three/vector-arrow3d.ts +77 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import {
|
|
2
|
+
colorProperty,
|
|
3
|
+
command,
|
|
4
|
+
fillProperty,
|
|
5
|
+
Graphics2D,
|
|
6
|
+
node,
|
|
7
|
+
PaintNode,
|
|
8
|
+
property,
|
|
9
|
+
strokeProperty,
|
|
10
|
+
type AssetScope,
|
|
11
|
+
type Color,
|
|
12
|
+
type Command,
|
|
13
|
+
type CommandArgs,
|
|
14
|
+
type Fill,
|
|
15
|
+
type FillResolved,
|
|
16
|
+
type NodeConfig,
|
|
17
|
+
type NormalizedColor,
|
|
18
|
+
type PaintProps,
|
|
19
|
+
type RenderContext2D,
|
|
20
|
+
type Stroke,
|
|
21
|
+
type StrokeResolved,
|
|
22
|
+
type Vector2,
|
|
23
|
+
} from "@motionscript/sdk";
|
|
24
|
+
import { declareFormulas, drawLabels, type Label } from "../kit/furniture";
|
|
25
|
+
import { lerpPartFills, partPaint, readPartFills, withPartFill, type PartFill, type PartFillResolved } from "../kit/part-fills";
|
|
26
|
+
import { GRAPH_LAYOUTS, layOut, type GraphEdge, type GraphLayout } from "./graph-layout";
|
|
27
|
+
|
|
28
|
+
/** A vertex placed by hand, in the graph's box from -1 to 1 each way, y up. */
|
|
29
|
+
export interface GraphPosition {
|
|
30
|
+
vertex: string;
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface NetworkGraphProps extends PaintProps {
|
|
36
|
+
vertices?: string[];
|
|
37
|
+
edges?: GraphEdge[];
|
|
38
|
+
directed?: boolean;
|
|
39
|
+
arrangement?: GraphLayout;
|
|
40
|
+
root?: string;
|
|
41
|
+
positions?: GraphPosition[];
|
|
42
|
+
vertexRadius?: number;
|
|
43
|
+
vertexStroke?: Stroke;
|
|
44
|
+
labels?: boolean;
|
|
45
|
+
labelSize?: number;
|
|
46
|
+
labelColor?: Color;
|
|
47
|
+
fontFamily?: string;
|
|
48
|
+
partFills?: PartFill[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Every list keeps each entry as it was written, a blank or a repeat included, so
|
|
52
|
+
// an editor showing the list shows the row it just added. What is drawn skips
|
|
53
|
+
// what is not complete yet (`_names`, `_links`).
|
|
54
|
+
|
|
55
|
+
function readVertices(value: readonly unknown[] | undefined): string[] {
|
|
56
|
+
return (Array.isArray(value) ? value : []).map((name) => String(name ?? ""));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// An edge reads from `{ from, to }` or a pair, so a list written by hand is as good as one from a table.
|
|
60
|
+
function readEdges(value: readonly unknown[] | undefined): GraphEdge[] {
|
|
61
|
+
return (Array.isArray(value) ? value : []).map((edge) => {
|
|
62
|
+
const [from, to] = Array.isArray(edge) ? edge : [(edge as GraphEdge)?.from, (edge as GraphEdge)?.to];
|
|
63
|
+
return { from: String(from ?? ""), to: String(to ?? "") };
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readPositions(value: readonly GraphPosition[] | undefined): GraphPosition[] {
|
|
68
|
+
const at = (n: unknown) => (typeof n === "number" && Number.isFinite(n) ? n : 0);
|
|
69
|
+
return (Array.isArray(value) ? value : []).map((entry) => ({ vertex: String(entry?.vertex ?? ""), x: at(entry?.x), y: at(entry?.y) }));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** How far a directed edge and the one back between the same two vertices bow apart, as a share of their length. */
|
|
73
|
+
const BOW = 0.12;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Vertices and the edges between them, arranged by a rule: in a circle, by
|
|
77
|
+
* springs, as a tree from `root`, or in a grid. Its `fill` is the vertices and
|
|
78
|
+
* its `stroke` the edges, with an arrowhead each when `directed`. `arrangeTo`
|
|
79
|
+
* moves every vertex to where another rule places it; `partFillTo` paints a
|
|
80
|
+
* vertex, or an edge written `a-b`. (`layout` is every node's own box.)
|
|
81
|
+
*/
|
|
82
|
+
@node({
|
|
83
|
+
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" }],
|
|
92
|
+
},
|
|
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
|
+
|
|
120
|
+
constructor(props?: NodeConfig<NetworkGraph, NetworkGraphProps>) {
|
|
121
|
+
super(props ?? {});
|
|
122
|
+
this.applyProp("_layoutFrom", "");
|
|
123
|
+
this.applyProp("_layoutMix", 1);
|
|
124
|
+
}
|
|
125
|
+
|
|
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
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
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
|
+
}
|
|
154
|
+
|
|
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
|
+
}
|
|
160
|
+
|
|
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 });
|
|
174
|
+
}
|
|
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);
|
|
184
|
+
}
|
|
185
|
+
|
|
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);
|
|
206
|
+
}
|
|
207
|
+
return placed;
|
|
208
|
+
}
|
|
209
|
+
|
|
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));
|
|
236
|
+
}
|
|
237
|
+
if (drawnPlain) ctx.draw(plain.stroke(stroke));
|
|
238
|
+
}
|
|
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
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
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
|
+
}
|
|
263
|
+
|
|
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
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** An edge from one vertex's rim to the other's, straight or bowed to its left. */
|
|
275
|
+
function edge(a: Vector2, b: Vector2, r: number, bow: number): Vector2[] {
|
|
276
|
+
const dx = b.x - a.x;
|
|
277
|
+
const dy = b.y - a.y;
|
|
278
|
+
const length = Math.hypot(dx, dy);
|
|
279
|
+
if (!(length > 2 * r)) return [];
|
|
280
|
+
if (bow === 0) {
|
|
281
|
+
const ux = dx / length;
|
|
282
|
+
const uy = dy / length;
|
|
283
|
+
return [{ x: a.x + ux * r, y: a.y + uy * r }, { x: b.x - ux * r, y: b.y - uy * r }];
|
|
284
|
+
}
|
|
285
|
+
const mid = { x: (a.x + b.x) / 2 - (dy / length) * bow * length, y: (a.y + b.y) / 2 + (dx / length) * bow * length };
|
|
286
|
+
const run: Vector2[] = [];
|
|
287
|
+
for (let i = 0; i <= 16; i++) {
|
|
288
|
+
const t = i / 16;
|
|
289
|
+
const u = 1 - t;
|
|
290
|
+
run.push({ x: u * u * a.x + 2 * u * t * mid.x + t * t * b.x, y: u * u * a.y + 2 * u * t * mid.y + t * t * b.y });
|
|
291
|
+
}
|
|
292
|
+
// Trimmed back to the rims along the curve.
|
|
293
|
+
const outside = (p: Vector2, c: Vector2) => Math.hypot(p.x - c.x, p.y - c.y) >= r;
|
|
294
|
+
return run.filter((p) => outside(p, a) && outside(p, b));
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** An edge from a vertex to itself: a small loop above it. */
|
|
298
|
+
function loop(c: Vector2, r: number): Vector2[] {
|
|
299
|
+
const run: Vector2[] = [];
|
|
300
|
+
const radius = Math.max(r * 0.8, 6);
|
|
301
|
+
const cy = c.y + r + radius * 0.6;
|
|
302
|
+
for (let i = 0; i <= 24; i++) {
|
|
303
|
+
const angle = -Math.PI / 2 + (2 * Math.PI * i) / 24;
|
|
304
|
+
const p = { x: c.x + Math.cos(angle) * radius, y: cy + Math.sin(angle) * radius };
|
|
305
|
+
if (Math.hypot(p.x - c.x, p.y - c.y) >= r) run.push(p);
|
|
306
|
+
}
|
|
307
|
+
return run;
|
|
308
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
colorProperty,
|
|
3
|
+
command,
|
|
4
|
+
fillOps,
|
|
5
|
+
Graphics2D,
|
|
6
|
+
node,
|
|
7
|
+
PaintNode,
|
|
8
|
+
property,
|
|
9
|
+
strokeProperty,
|
|
10
|
+
withdraw,
|
|
11
|
+
type AssetScope,
|
|
12
|
+
type Color,
|
|
13
|
+
type Command,
|
|
14
|
+
type CommandArgs,
|
|
15
|
+
type Fill,
|
|
16
|
+
type FillResolved,
|
|
17
|
+
type NodeConfig,
|
|
18
|
+
type NormalizedColor,
|
|
19
|
+
type PaintProps,
|
|
20
|
+
type RenderContext2D,
|
|
21
|
+
type Stroke,
|
|
22
|
+
type StrokeResolved,
|
|
23
|
+
type Vector2,
|
|
24
|
+
} from "@motionscript/sdk";
|
|
25
|
+
import { braceOutline } from "../kit/brace";
|
|
26
|
+
import { declareFormulas, drawLabels, type Label } from "../kit/furniture";
|
|
27
|
+
import { lerpFillScale, readFillScale } from "../kit/scale";
|
|
28
|
+
import { formatTick } from "../kit/ticks";
|
|
29
|
+
import { FILL_PAINT, OUTLINE_TRIM } from "../kit/withdrawn";
|
|
30
|
+
|
|
31
|
+
export interface SampleSpaceProps extends PaintProps {
|
|
32
|
+
columnShares?: number[];
|
|
33
|
+
rowShares?: number[];
|
|
34
|
+
columnFills?: Fill[];
|
|
35
|
+
columnLabels?: string[];
|
|
36
|
+
rowLabels?: string[];
|
|
37
|
+
values?: boolean;
|
|
38
|
+
labelSize?: number;
|
|
39
|
+
labelColor?: Color;
|
|
40
|
+
fontFamily?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The parts' paints until told otherwise, a column each, round again past the last. */
|
|
44
|
+
const PALETTE = ["#58a6ff", "#f59e0b", "#4ade80", "#f472b6", "#a78bfa", "#2dd4bf"];
|
|
45
|
+
|
|
46
|
+
/** How strongly the lower part of a split column shows its paint. */
|
|
47
|
+
const LOWER = 0.45;
|
|
48
|
+
|
|
49
|
+
function readShares(value: readonly number[] | undefined): number[] {
|
|
50
|
+
return (Array.isArray(value) ? value : []).map((share) => (Number.isFinite(share) && share > 0 ? share : 0));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Shares tween one by one; a different count has nothing to pair with, and changes at the end.
|
|
54
|
+
function lerpShares(from: number[], to: number[], t: number): number[] {
|
|
55
|
+
if (from.length !== to.length) return t < 1 ? from : to;
|
|
56
|
+
return to.map((share, i) => from[i] + (share - from[i]) * t);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A probability's whole, as a box cut into columns by `columnShares` (shares of
|
|
61
|
+
* the whole, P(A) and P(not A)) and each column cut again by its `rowShares` entry
|
|
62
|
+
* (P(B | A) above, the rest below): each part's area is its probability.
|
|
63
|
+
* Braces name the columns below and the rows on the left; `divideTo` moves the
|
|
64
|
+
* cuts.
|
|
65
|
+
*/
|
|
66
|
+
@node({
|
|
67
|
+
key: "sampleSpace",
|
|
68
|
+
parentKey: "shape",
|
|
69
|
+
forkable: true,
|
|
70
|
+
layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: false },
|
|
71
|
+
seed: { width: 520, height: 420, columnShares: [0.4, 0.6], rowShares: [0.7, 0.25], columnLabels: ["A", "not A"], rowLabels: ["B", "not B"] },
|
|
72
|
+
})
|
|
73
|
+
@withdraw(...FILL_PAINT, ...OUTLINE_TRIM)
|
|
74
|
+
export class SampleSpace extends PaintNode<SampleSpaceProps> {
|
|
75
|
+
/** How the whole is shared across, left to right; any numbers, read as shares of their sum. */
|
|
76
|
+
@property({ default: [1], mapper: readShares, tween: lerpShares, kind: "vector" }) declare columnShares: number[];
|
|
77
|
+
/** Each column's upper share, 0 to 1; a column without one is not cut across. */
|
|
78
|
+
@property({ default: [], mapper: readShares, tween: lerpShares, kind: "vector" }) declare rowShares: number[];
|
|
79
|
+
/** Each column's paint, left to right, round again past the last; its lower part shows it fainter. */
|
|
80
|
+
@property({ default: [], mapper: readFillScale, tween: lerpFillScale, kind: "vector" }) declare columnFills: Fill[];
|
|
81
|
+
@property({ default: [], animatable: false, kind: "vector" }) declare columnLabels: string[];
|
|
82
|
+
/** The upper part's name and the lower's, written on the left. */
|
|
83
|
+
@property({ default: [], animatable: false, kind: "vector" }) declare rowLabels: string[];
|
|
84
|
+
/** Write each part's probability in it. */
|
|
85
|
+
@property({ default: false, animatable: false }) declare values: boolean;
|
|
86
|
+
@property({ default: 20, min: 1, unit: "px" }) declare labelSize: number;
|
|
87
|
+
@colorProperty({ default: "#e5e7eb" }) declare labelColor: Color;
|
|
88
|
+
@property({ default: "", animatable: false }) declare fontFamily: string;
|
|
89
|
+
|
|
90
|
+
@strokeProperty({ default: { weight: 2, fill: "#0b0d12", align: "center" } }) declare stroke: Stroke;
|
|
91
|
+
|
|
92
|
+
constructor(props?: NodeConfig<SampleSpace, SampleSpaceProps>) {
|
|
93
|
+
super(props ?? {});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Move the cuts: the columns' shares, each column's upper share, or both. */
|
|
97
|
+
@command({ args: ["columnShares", "rowShares"] })
|
|
98
|
+
divideTo(args: CommandArgs<{ columnShares?: number[]; rowShares?: number[] }> & { duration: number }): Command<SampleSpaceProps> {
|
|
99
|
+
const data: Partial<SampleSpaceProps> = {};
|
|
100
|
+
if (args.data?.columnShares !== undefined) data.columnShares = args.data.columnShares;
|
|
101
|
+
if (args.data?.rowShares !== undefined) data.rowShares = args.data.rowShares;
|
|
102
|
+
return this.to({ data, duration: args.duration, easing: args.easing });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
override declareAssets(assets: AssetScope): void {
|
|
106
|
+
super.declareAssets(assets);
|
|
107
|
+
if (this.fontFamily !== "") assets.font(this.fontFamily);
|
|
108
|
+
declareFormulas(assets, [...this.columnLabels, ...this.rowLabels]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Each part's box in pixels from the node's centre, y up, and its probability: columns left to right, upper part first. */
|
|
112
|
+
parts(): { x: number; y: number; width: number; height: number; probability: number; column: number; upper: boolean }[] {
|
|
113
|
+
const box = this._box();
|
|
114
|
+
const shares = this.columnShares;
|
|
115
|
+
const total = shares.reduce((sum, share) => sum + share, 0);
|
|
116
|
+
if (!(total > 0) || box.width <= 0 || box.height <= 0) return [];
|
|
117
|
+
const out: ReturnType<SampleSpace["parts"]> = [];
|
|
118
|
+
let left = box.x - box.width / 2;
|
|
119
|
+
shares.forEach((share, column) => {
|
|
120
|
+
const width = (box.width * share) / total;
|
|
121
|
+
const p = share / total;
|
|
122
|
+
const cut = this.rowShares[column];
|
|
123
|
+
const top = box.y + box.height / 2;
|
|
124
|
+
if (cut === undefined) {
|
|
125
|
+
out.push({ x: left + width / 2, y: box.y, width, height: box.height, probability: p, column, upper: true });
|
|
126
|
+
} else {
|
|
127
|
+
const upper = Math.min(1, Math.max(0, cut));
|
|
128
|
+
const h = box.height * upper;
|
|
129
|
+
out.push({ x: left + width / 2, y: top - h / 2, width, height: h, probability: p * upper, column, upper: true });
|
|
130
|
+
out.push({ x: left + width / 2, y: top - h - (box.height - h) / 2, width, height: box.height - h, probability: p * (1 - upper), column, upper: false });
|
|
131
|
+
}
|
|
132
|
+
left += width;
|
|
133
|
+
});
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
protected override renderSelf(ctx: RenderContext2D): void {
|
|
138
|
+
const parts = this.parts();
|
|
139
|
+
if (parts.length === 0) return;
|
|
140
|
+
const fills = this.columnFills as unknown as FillResolved[][];
|
|
141
|
+
const stroke = this.stroke as StrokeResolved[];
|
|
142
|
+
const labels: Label[] = [];
|
|
143
|
+
for (const part of parts) {
|
|
144
|
+
if (!(part.width > 0) || !(part.height > 0)) continue;
|
|
145
|
+
const base = fills.length > 0 ? fills[part.column % fills.length] : fillOps.resolve(PALETTE[part.column % PALETTE.length]);
|
|
146
|
+
const paint = part.upper ? base : base.map((layer) => ({ ...layer, opacity: (layer.opacity ?? 1) * LOWER }));
|
|
147
|
+
const g = new Graphics2D().rect({ x: part.x, y: part.y, width: part.width, height: part.height });
|
|
148
|
+
ctx.draw(g.shadow(this.shadow).fill(paint));
|
|
149
|
+
if (stroke.length > 0) ctx.draw(new Graphics2D().rect({ x: part.x, y: part.y, width: part.width, height: part.height }).stroke(stroke));
|
|
150
|
+
if (this.values && part.height > this.labelSize && part.width > this.labelSize) {
|
|
151
|
+
labels.push({ text: formatTick(Math.round(part.probability * 1000) / 1000, 0.001), at: { x: part.x, y: part.y }, align: "center" });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
labels.push(...this._braces(ctx, parts));
|
|
155
|
+
const style = { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor };
|
|
156
|
+
for (const part of drawLabels(labels, style)) ctx.draw(part);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The outlines are drawn with the parts, and there is no outline of the node's own.
|
|
160
|
+
protected override renderStroke(): void {}
|
|
161
|
+
|
|
162
|
+
/** Room for the braces and their names, below and on the left, taken from the box. */
|
|
163
|
+
private _box(): { x: number; y: number; width: number; height: number } {
|
|
164
|
+
const { width, height } = this.layoutBounds;
|
|
165
|
+
const room = this.labelSize * 2.6;
|
|
166
|
+
const below = this.columnLabels.some((label) => label !== "") ? room : 0;
|
|
167
|
+
const left = this.rowLabels.some((label) => label !== "") ? room * 1.6 : 0;
|
|
168
|
+
return { x: left / 2, y: below / 2, width: Math.max(0, width - left), height: Math.max(0, height - below) };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Braces under each column and beside the first column's parts, and the labels that name them. */
|
|
172
|
+
private _braces(ctx: RenderContext2D, parts: ReturnType<SampleSpace["parts"]>): Label[] {
|
|
173
|
+
const box = this._box();
|
|
174
|
+
const reach = this.labelSize * 0.8;
|
|
175
|
+
const gap = 6;
|
|
176
|
+
const runs = new Graphics2D();
|
|
177
|
+
const labels: Label[] = [];
|
|
178
|
+
let drawn = false;
|
|
179
|
+
const brace = (from: Vector2, to: Vector2, out: Vector2, text: string, align: Label["align"]) => {
|
|
180
|
+
if (text === "") return;
|
|
181
|
+
// A frame centred on the node's own centre, so the path is drawn where it is and not moved onto it.
|
|
182
|
+
runs.path({ data: braceOutline(from, to, out, reach), centerBounds: [-1, -1, 1, 1] });
|
|
183
|
+
drawn = true;
|
|
184
|
+
const mid = { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2 };
|
|
185
|
+
const far = reach + this.labelSize * 0.75;
|
|
186
|
+
labels.push({ text, at: { x: mid.x + out.x * far, y: mid.y + out.y * far }, align });
|
|
187
|
+
};
|
|
188
|
+
const bottom = box.y - box.height / 2 - gap;
|
|
189
|
+
for (const part of parts) {
|
|
190
|
+
if (!part.upper && this.rowShares[part.column] !== undefined) continue;
|
|
191
|
+
const text = this.columnLabels[part.column] ?? "";
|
|
192
|
+
// Left to right along the bottom, so the bulge, to the edge's right, points down.
|
|
193
|
+
brace({ x: part.x + part.width / 2, y: bottom }, { x: part.x - part.width / 2, y: bottom }, { x: 0, y: -1 }, text, "center");
|
|
194
|
+
}
|
|
195
|
+
const first = parts.filter((part) => part.column === 0);
|
|
196
|
+
const left = box.x - box.width / 2 - gap;
|
|
197
|
+
if (this.rowShares[0] !== undefined) {
|
|
198
|
+
first.forEach((part) => {
|
|
199
|
+
const text = this.rowLabels[part.upper ? 0 : 1] ?? "";
|
|
200
|
+
brace({ x: left, y: part.y - part.height / 2 }, { x: left, y: part.y + part.height / 2 }, { x: -1, y: 0 }, text, "right");
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (drawn) {
|
|
204
|
+
const fill = fillOps.resolve({ type: "solid", color: this.labelColor as NormalizedColor });
|
|
205
|
+
ctx.draw(runs.stroke({ weight: 2, fill, align: "center", cap: "round", join: "round" }));
|
|
206
|
+
}
|
|
207
|
+
return labels;
|
|
208
|
+
}
|
|
209
|
+
}
|