@motionscript/math 0.1.0-alpha.5 → 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.
- package/CHANGELOG.md +22 -0
- package/README.md +39 -33
- package/dist/annotate/angle-mark.d.ts +52 -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 +59 -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 +43 -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 +58 -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 +24 -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 +28 -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 +37 -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 +30 -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 +38 -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 +30 -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 +107 -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 +34 -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 +38 -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 +34 -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 +31 -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 +99 -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 +69 -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 +126 -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 +51 -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 +92 -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 +47 -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 +56 -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 +55 -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 +140 -54
- package/dist/three/axes3d.d.ts.map +1 -1
- package/dist/three/axes3d.js +159 -204
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +70 -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 +57 -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 +131 -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 +58 -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 +182 -161
- 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
package/src/kit/mark.ts
CHANGED
|
@@ -1,131 +1,152 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Graphics2D,
|
|
3
|
-
|
|
4
|
-
type BoxBounds,
|
|
5
|
-
type ChildPlane,
|
|
3
|
+
type ChildView,
|
|
6
4
|
type Extents,
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
type
|
|
10
|
-
type PathCommand,
|
|
11
|
-
type RenderContext2D,
|
|
5
|
+
type Fill,
|
|
6
|
+
type RenderContext,
|
|
7
|
+
type Shadow,
|
|
12
8
|
type Size2D,
|
|
9
|
+
type Stroke,
|
|
10
|
+
type StrokeResolved,
|
|
13
11
|
type Vector2,
|
|
14
12
|
} from "@motionscript/sdk";
|
|
13
|
+
import { painted } from "./component";
|
|
15
14
|
import { PIXEL_MAP, type CoordinateMap } from "./coords";
|
|
16
15
|
import { boundsOfRuns, runsToPath } from "./geometry";
|
|
17
|
-
import {
|
|
16
|
+
import type { GraphPlane } from "./graph";
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
|
-
* A node drawn in graph coordinates: a plot, an area, an arrow
|
|
21
|
-
* graph points. It answers with runs of pixel points and is
|
|
22
|
-
*
|
|
23
|
-
* is the tight box of its ink.
|
|
19
|
+
* A mark is a node drawn in graph coordinates: a plot, an area, an arrow
|
|
20
|
+
* between two graph points. It answers with runs of pixel points and is
|
|
21
|
+
* otherwise a shape, so its box is the tight box of its ink.
|
|
24
22
|
*
|
|
25
|
-
* Inside a coordinate system the
|
|
26
|
-
*
|
|
27
|
-
* Outside one a graph unit is a pixel and `x`/`y` are the usual
|
|
28
|
-
*
|
|
29
|
-
* A mark that takes children holds them on the same map it is drawn through: a
|
|
30
|
-
* plot holding the area under it and the point that rides it.
|
|
23
|
+
* Inside a coordinate system (or a plot) the holder lays it out on that ink,
|
|
24
|
+
* calling the mark's own trace with its map, and the mark's `x`/`y` are a shift
|
|
25
|
+
* in graph units. Outside one a graph unit is a pixel and `x`/`y` are the usual
|
|
26
|
+
* offset.
|
|
31
27
|
*/
|
|
32
|
-
export abstract class Mark<P extends PaintProps = PaintProps> extends PaintNode<P> {
|
|
33
|
-
/** The runs this mark draws, in pixels, y-up from the system's centre. */
|
|
34
|
-
protected abstract trace(map: CoordinateMap): Vector2[][];
|
|
35
28
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return this._inSystem ? { x: this.x, y: this.y } : NO_SHIFT;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** A graph point in this mark's own frame, where a handle on it sits. */
|
|
47
|
-
protected ownPoint(point: Vector2): Vector2 {
|
|
48
|
-
const shift = this.shift();
|
|
49
|
-
const at = this._map.toPoint(point.x + shift.x, point.y + shift.y);
|
|
50
|
-
const centre = this._centre();
|
|
51
|
-
return { x: at.x - centre.x, y: at.y - centre.y };
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** The graph point at `at` in this mark's own frame: {@link ownPoint} undone. */
|
|
55
|
-
protected graphPoint(at: Vector2): Vector2 {
|
|
56
|
-
const shift = this.shift();
|
|
57
|
-
const centre = this._centre();
|
|
58
|
-
const point = this._map.toCoords(at.x + centre.x, at.y + centre.y);
|
|
59
|
-
return { x: point.x - shift.x, y: point.y - shift.y };
|
|
60
|
-
}
|
|
29
|
+
/** What a mark is traced through: the map, its own `x`/`y` as a shift, and the plane it reads curves from. */
|
|
30
|
+
export interface MarkReading {
|
|
31
|
+
readonly map: CoordinateMap;
|
|
32
|
+
/** Zero outside a system, where `x`/`y` are pixels. */
|
|
33
|
+
readonly shift: Vector2;
|
|
34
|
+
readonly plane: GraphPlane | null;
|
|
35
|
+
}
|
|
61
36
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
protected override shapeGraphics(): Graphics2D | null {
|
|
73
|
-
const ink = this._ink;
|
|
74
|
-
if (!ink || this._data.length === 0) return null;
|
|
75
|
-
return new Graphics2D().path({
|
|
76
|
-
data: this._data,
|
|
77
|
-
centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY],
|
|
78
|
-
start: this.start,
|
|
79
|
-
end: this.end,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// A mark's stroke is its ink, so it is painted before its children rather
|
|
84
|
-
// than over them: a point or a tangent riding a plot sits on the curve.
|
|
85
|
-
protected override renderSelf(ctx: RenderContext2D): void {
|
|
86
|
-
if (this.paintsFill()) super.renderSelf(ctx);
|
|
87
|
-
this.renderInk(ctx);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** The stroke along the ink. A mark that strokes it differently overrides this, not `renderStroke`. */
|
|
91
|
-
protected renderInk(ctx: RenderContext2D): void {
|
|
92
|
-
super.renderStroke(ctx);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
protected override renderStroke(): void {}
|
|
96
|
-
|
|
97
|
-
protected override layoutChildren(rect: BoxBounds, scope: MeasureContext2D): void {
|
|
98
|
-
const children = this.flowChildren();
|
|
99
|
-
if (children.length === 0) return;
|
|
100
|
-
const room = { x: 0, y: 0, width: rect.width, height: rect.height };
|
|
101
|
-
this._planes = placeOnMap(children, this._map, this._centre(), room, scope);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
override _planeOf(child: Node2D | null): ChildPlane | null {
|
|
105
|
-
return child ? (this._planes.get(child) ?? null) : openPlane(this._map, this._centre());
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private _data: PathCommand[] = [];
|
|
109
|
-
private _ink: Extents | null = null;
|
|
110
|
-
private _inSystem = false;
|
|
111
|
-
private _map: CoordinateMap = PIXEL_MAP;
|
|
112
|
-
private _planes = new WeakMap<Node2D, ChildPlane>();
|
|
113
|
-
|
|
114
|
-
/** Its own centre in the map's pixels: where its children's boxes are placed from. */
|
|
115
|
-
private _centre(): Vector2 {
|
|
116
|
-
const ink = this._ink;
|
|
117
|
-
return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** @internal What a system lays this mark out on: its ink, drawn through `map`. */
|
|
121
|
-
_resolve(map: CoordinateMap, inSystem = true): Extents | null {
|
|
122
|
-
this._inSystem = inSystem;
|
|
123
|
-
this._map = map;
|
|
124
|
-
const runs = this.trace(map);
|
|
125
|
-
this._data = runsToPath(runs, this.isClosed());
|
|
126
|
-
this._ink = boundsOfRuns(runs);
|
|
127
|
-
return this._ink;
|
|
128
|
-
}
|
|
37
|
+
/** The runs a mark draws, in pixels, y-up from the system's centre. */
|
|
38
|
+
export type Tracer<V> = (state: V, read: MarkReading) => Vector2[][];
|
|
39
|
+
|
|
40
|
+
/** What every mark's state reads: its place, and the plane it is laid out on. */
|
|
41
|
+
export interface MarkState {
|
|
42
|
+
readonly x: number;
|
|
43
|
+
readonly y: number;
|
|
44
|
+
readonly graph: GraphPlane | null;
|
|
129
45
|
}
|
|
130
46
|
|
|
131
47
|
const NO_SHIFT: Vector2 = { x: 0, y: 0 };
|
|
48
|
+
|
|
49
|
+
const tracers: { readonly is: (child: ChildView) => boolean; readonly trace: Tracer<never> }[] = [];
|
|
50
|
+
|
|
51
|
+
/** `component`, declared a mark: a holder lays it out on the ink `trace` draws. Hand back the component. */
|
|
52
|
+
export function traced<C extends { is(child: ChildView): boolean }>(component: C, trace: Tracer<never>): C {
|
|
53
|
+
tracers.push({ is: (child) => component.is(child), trace });
|
|
54
|
+
return component;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The runs `child` draws on `plane`, or `undefined` for a child that is no mark. */
|
|
58
|
+
export function markRuns(child: ChildView, map: CoordinateMap, plane: GraphPlane): Vector2[][] | undefined {
|
|
59
|
+
const mark = tracers.find((entry) => entry.is(child));
|
|
60
|
+
if (!mark) return undefined;
|
|
61
|
+
const state = child.state as unknown as MarkState;
|
|
62
|
+
return (mark.trace as Tracer<MarkState>)(state, { map, shift: { x: state.x, y: state.y }, plane });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** How a mark's own fields read it: through its plane's map when one lays it out, through pixels when none does. */
|
|
66
|
+
export function reading(state: MarkState): MarkReading {
|
|
67
|
+
const plane = state.graph;
|
|
68
|
+
return plane ? { map: plane.map, shift: { x: state.x, y: state.y }, plane } : { map: PIXEL_MAP, shift: NO_SHIFT, plane: null };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The size of a mark's ink: its box when a node other than a system lays it out. */
|
|
72
|
+
export function inkSize(runs: readonly (readonly Vector2[])[]): Size2D {
|
|
73
|
+
const ink = boundsOfRuns(runs);
|
|
74
|
+
return ink ? { width: ink.maxX - ink.minX, height: ink.maxY - ink.minY } : { width: 0, height: 0 };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The centre of `runs`' ink in the map's pixels: where a mark's children are placed from. */
|
|
78
|
+
export function inkCentre(runs: readonly (readonly Vector2[])[]): Vector2 {
|
|
79
|
+
const ink = boundsOfRuns(runs);
|
|
80
|
+
return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A graph point in a mark's own frame, where a handle on it sits. */
|
|
84
|
+
export function ownPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], point: Vector2): Vector2 {
|
|
85
|
+
const at = read.map.toPoint(point.x + read.shift.x, point.y + read.shift.y);
|
|
86
|
+
const centre = inkCentre(runs);
|
|
87
|
+
return { x: at.x - centre.x, y: at.y - centre.y };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The graph point at `at` in a mark's own frame: {@link ownPoint} undone. */
|
|
91
|
+
export function graphPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], at: Vector2): Vector2 {
|
|
92
|
+
const centre = inkCentre(runs);
|
|
93
|
+
const point = read.map.toCoords(at.x + centre.x, at.y + centre.y);
|
|
94
|
+
return { x: point.x - read.shift.x, y: point.y - read.shift.y };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** What a mark paints with: a shape's paints, any it withdrew left out. */
|
|
98
|
+
export interface MarkPaint {
|
|
99
|
+
readonly fill?: Fill;
|
|
100
|
+
readonly stroke?: Stroke;
|
|
101
|
+
readonly shadow: Shadow;
|
|
102
|
+
readonly start?: number;
|
|
103
|
+
readonly end?: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface MarkOptions {
|
|
107
|
+
/** Whether each run is closed into an outline. */
|
|
108
|
+
readonly closed?: boolean;
|
|
109
|
+
/** Whether the fill is painted: a mark whose ink has no inside yet (an open line) says no. */
|
|
110
|
+
readonly fills?: boolean;
|
|
111
|
+
/** The stroke along the ink, when it is not the plain one. */
|
|
112
|
+
readonly ink?: (ctx: RenderContext, ink: Extents) => void;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const NONE: never[] = [];
|
|
116
|
+
|
|
117
|
+
/** One mark's outline, the same commands for each paint. */
|
|
118
|
+
export function markShape(runs: readonly (readonly Vector2[])[], s: MarkPaint, closed = false): (() => Graphics2D) | null {
|
|
119
|
+
const ink = boundsOfRuns(runs);
|
|
120
|
+
const data = runsToPath(runs, closed);
|
|
121
|
+
if (!ink || data.length === 0) return null;
|
|
122
|
+
const centerBounds: [number, number, number, number] = [ink.minX, ink.minY, ink.maxX, ink.maxY];
|
|
123
|
+
return () => new Graphics2D().path({ data, centerBounds, start: s.start ?? 0, end: s.end ?? 1 });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A mark's paint: the fill under the stroke along its ink, both before its
|
|
128
|
+
* children, so a point or a tangent riding a plot sits on the curve.
|
|
129
|
+
*/
|
|
130
|
+
export function drawMark(ctx: RenderContext, s: MarkPaint, runs: readonly (readonly Vector2[])[], options: MarkOptions = {}): void {
|
|
131
|
+
const shape = markShape(runs, s, options.closed);
|
|
132
|
+
const fill = s.fill ?? NONE;
|
|
133
|
+
painted(ctx, () => {
|
|
134
|
+
if (shape && options.fills !== false) ctx.draw(shape().shadow(s.shadow).fill(fill));
|
|
135
|
+
const ink = options.ink ? boundsOfRuns(runs) : null;
|
|
136
|
+
if (ink && options.ink) options.ink(ctx, ink);
|
|
137
|
+
else if (shape) strokeShape(ctx, shape, s);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The stroke along a shape, casting the shadow when nothing fills it. */
|
|
142
|
+
export function strokeShape(ctx: RenderContext, shape: () => Graphics2D, s: MarkPaint): void {
|
|
143
|
+
const stroke = (s.stroke ?? NONE) as StrokeResolved[];
|
|
144
|
+
if (stroke.length === 0) return;
|
|
145
|
+
ctx.draw(castsShadow(s) ? shape().shadow(s.shadow).stroke(stroke) : shape().stroke(stroke));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A shadow is cast by the stroke when nothing visible fills the shape. */
|
|
149
|
+
export function castsShadow(s: MarkPaint): boolean {
|
|
150
|
+
if ((s.shadow as unknown[]).length === 0) return false;
|
|
151
|
+
return !((s.fill ?? NONE) as { opacity?: number }[]).some((layer) => (layer.opacity ?? 1) > 0);
|
|
152
|
+
}
|
package/src/kit/place.ts
CHANGED
|
@@ -1,31 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
flowOps,
|
|
3
|
-
type BoxBounds,
|
|
4
|
-
type ChildPlane,
|
|
5
|
-
type Extents,
|
|
6
|
-
type MeasureContext2D,
|
|
7
|
-
type Node2D,
|
|
8
|
-
type Vector2,
|
|
9
|
-
} from "@motionscript/sdk";
|
|
1
|
+
import type { ChildPlane, ChildView, Placement2D, Size2D, Vector2 } from "@motionscript/sdk";
|
|
10
2
|
import type { CoordinateMap } from "./coords";
|
|
3
|
+
import { boundsOfRuns } from "./geometry";
|
|
4
|
+
import type { GraphPlane } from "./graph";
|
|
5
|
+
import { markRuns } from "./mark";
|
|
11
6
|
|
|
12
|
-
/**
|
|
13
|
-
export
|
|
14
|
-
/** The graph coordinates the child is at, given its own `x`/`y`. */
|
|
15
|
-
anchorAt(x: number, y: number): Vector2;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function isAnchored(node: object): node is Anchored {
|
|
19
|
-
return typeof (node as Partial<Anchored>).anchorAt === "function";
|
|
20
|
-
}
|
|
7
|
+
/** Where a child that sits somewhere other than its own `x`/`y` is (a point riding a curve), given its own `x`/`y`. */
|
|
8
|
+
export type Anchor = (x: number, y: number) => Vector2;
|
|
21
9
|
|
|
22
|
-
|
|
23
|
-
interface Traced {
|
|
24
|
-
_resolve(map: CoordinateMap, inSystem?: boolean): Extents | null;
|
|
25
|
-
}
|
|
10
|
+
const anchors: { readonly is: (child: ChildView) => boolean; readonly anchor: (state: never, plane: GraphPlane) => Anchor | null }[] = [];
|
|
26
11
|
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
/** `component`, declared to sit at `anchor` of its own `x`/`y` on a graph. Hand back the component. */
|
|
13
|
+
export function anchored<C extends { is(child: ChildView): boolean }>(component: C, anchor: (state: never, plane: GraphPlane) => Anchor | null): C {
|
|
14
|
+
anchors.push({ is: (child) => component.is(child), anchor });
|
|
15
|
+
return component;
|
|
29
16
|
}
|
|
30
17
|
|
|
31
18
|
/**
|
|
@@ -33,39 +20,29 @@ function isTraced(node: object): node is Traced {
|
|
|
33
20
|
* system, or a plot holding the marks that read it. A mark goes on its ink;
|
|
34
21
|
* anything else keeps its own size and has its `x`/`y` read as graph
|
|
35
22
|
* coordinates. `origin` is the holder's own centre in the map's pixels (y-up),
|
|
36
|
-
* since a child's box is placed from there.
|
|
23
|
+
* since a child's box is placed from there.
|
|
37
24
|
*/
|
|
38
|
-
export function placeOnMap(
|
|
39
|
-
children
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
25
|
+
export function placeOnMap(children: readonly ChildView[], map: CoordinateMap, origin: Vector2, room: Size2D, plane: GraphPlane): Placement2D[] {
|
|
26
|
+
return children.map((child) => {
|
|
27
|
+
const runs = markRuns(child, map, plane);
|
|
28
|
+
if (runs) {
|
|
29
|
+
const ink = boundsOfRuns(runs);
|
|
30
|
+
const cx = ink ? (ink.minX + ink.maxX) / 2 : origin.x;
|
|
31
|
+
const cy = ink ? (ink.minY + ink.maxY) / 2 : origin.y;
|
|
32
|
+
return {
|
|
33
|
+
x: cx - origin.x,
|
|
34
|
+
y: cy - origin.y,
|
|
35
|
+
width: ink ? ink.maxX - ink.minX : 0,
|
|
36
|
+
height: ink ? ink.maxY - ink.minY : 0,
|
|
37
|
+
plane: markPlane(map, cx, cy),
|
|
38
|
+
};
|
|
51
39
|
}
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
child.layout(
|
|
56
|
-
{ x: cx - origin.x, y: origin.y - cy, width: ink ? ink.maxX - ink.minX : 0, height: ink ? ink.maxY - ink.minY : 0 },
|
|
57
|
-
scope,
|
|
58
|
-
);
|
|
59
|
-
planes.set(child, markPlane(map, cx, cy));
|
|
60
|
-
}
|
|
61
|
-
if (others.length === 0) return planes;
|
|
62
|
-
const sizes = flowOps.measure({}, others, room, scope).sizes;
|
|
63
|
-
others.forEach((child, i) => {
|
|
40
|
+
const size = child.measure({ width: room.width, height: room.height });
|
|
41
|
+
const entry = anchors.find((a) => a.is(child));
|
|
42
|
+
const anchor = entry ? (entry.anchor as (state: unknown, plane: GraphPlane) => Anchor | null)(child.state, plane) : null;
|
|
64
43
|
// Its box is centred on the map's own centre, so its offset from there is its point.
|
|
65
|
-
|
|
66
|
-
planes.set(child, pointPlane(map, child));
|
|
44
|
+
return { x: -origin.x, y: -origin.y, width: size.width, height: size.height, plane: pointPlane(map, anchor) };
|
|
67
45
|
});
|
|
68
|
-
return planes;
|
|
69
46
|
}
|
|
70
47
|
|
|
71
48
|
/** The plane of a child that is not there yet, under a holder centred on `origin`: where a host would drop one. */
|
|
@@ -79,12 +56,11 @@ export function openPlane(map: CoordinateMap, origin: Vector2): ChildPlane {
|
|
|
79
56
|
};
|
|
80
57
|
}
|
|
81
58
|
|
|
82
|
-
function pointPlane(map: CoordinateMap,
|
|
59
|
+
function pointPlane(map: CoordinateMap, anchor: Anchor | null): ChildPlane {
|
|
83
60
|
const point: Vector2 = { x: 0, y: 0 };
|
|
84
|
-
const anchored = isAnchored(child) ? child : null;
|
|
85
61
|
return {
|
|
86
62
|
toOffset: (x, y) => {
|
|
87
|
-
const at =
|
|
63
|
+
const at = anchor?.(x, y);
|
|
88
64
|
return at ? map.toPoint(at.x, at.y, point) : map.toPoint(x, y, point);
|
|
89
65
|
},
|
|
90
66
|
toCoords: (x, y) => map.toCoords(x, y),
|
package/src/kit/system.ts
CHANGED
|
@@ -1,99 +1,105 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Clip,
|
|
3
|
-
flowOps,
|
|
4
2
|
Graphics2D,
|
|
5
3
|
insetsOps,
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
Paint,
|
|
5
|
+
provides,
|
|
8
6
|
type ChildPlane,
|
|
7
|
+
type ChildView,
|
|
9
8
|
type Extents,
|
|
10
|
-
type
|
|
11
|
-
type
|
|
12
|
-
type
|
|
13
|
-
type
|
|
9
|
+
type InsetsResolved,
|
|
10
|
+
type Placement2D,
|
|
11
|
+
type Provided,
|
|
12
|
+
type RenderContext,
|
|
13
|
+
type Shadow,
|
|
14
|
+
type Fill,
|
|
14
15
|
type Size2D,
|
|
15
|
-
type SizeConstraints,
|
|
16
16
|
type Vector2,
|
|
17
17
|
} from "@motionscript/sdk";
|
|
18
18
|
import type { CoordinateMap } from "./coords";
|
|
19
|
+
import { settle, SYSTEM, systemPlane } from "./graph";
|
|
19
20
|
import { openPlane, placeOnMap } from "./place";
|
|
20
21
|
|
|
21
|
-
export type { Anchored } from "./place";
|
|
22
|
-
|
|
23
|
-
const NO_CONTENT: Size2D = { width: 0, height: 0 };
|
|
24
|
-
const CENTRE: Vector2 = { x: 0, y: 0 };
|
|
25
|
-
|
|
26
22
|
/**
|
|
27
|
-
*
|
|
28
|
-
* the system's units and the child keeps
|
|
29
|
-
* on a graph: a dot, a label, an image. A
|
|
30
|
-
* system's map instead and laid out on its ink.
|
|
23
|
+
* What a coordinate system shares: a node whose children sit in its graph
|
|
24
|
+
* coordinates. A child's `x`/`y` are in the system's units and the child keeps
|
|
25
|
+
* its own size, so anything can be put on a graph: a dot, a label, an image. A
|
|
26
|
+
* mark is drawn through the system's map instead and laid out on its ink.
|
|
31
27
|
*
|
|
32
28
|
* The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
|
|
33
29
|
* the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
|
|
34
30
|
* document's children would otherwise share their indices with them.
|
|
35
31
|
*/
|
|
36
|
-
export abstract class CoordinateSystem<P extends PaintProps = PaintProps> extends PaintNode<P> {
|
|
37
|
-
/** How graph values land on a plot area of this size. */
|
|
38
|
-
protected abstract mapFor(area: Extents): CoordinateMap;
|
|
39
|
-
|
|
40
|
-
/** What the system draws of itself, under its children. */
|
|
41
|
-
protected abstract furniture(map: CoordinateMap): Graphics2D[];
|
|
42
32
|
|
|
43
|
-
|
|
44
|
-
get coordinates(): CoordinateMap {
|
|
45
|
-
return this._map ?? (this._map = this.mapFor(this._area(this.layoutBounds)));
|
|
46
|
-
}
|
|
33
|
+
const CENTRE: Vector2 = { x: 0, y: 0 };
|
|
47
34
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
/** What a system's helpers read of its state. */
|
|
36
|
+
export interface SystemState {
|
|
37
|
+
readonly padding: InsetsResolved;
|
|
38
|
+
readonly fill: Fill;
|
|
39
|
+
readonly shadow: Shadow;
|
|
40
|
+
readonly clip: boolean;
|
|
41
|
+
}
|
|
52
42
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this._map = map;
|
|
56
|
-
const inner = insetsOps.insetBounds(rect.width, rect.height, this.effectivePadding());
|
|
57
|
-
this._planes = placeOnMap(this.flowChildren(), map, CENTRE, inner, scope);
|
|
58
|
-
}
|
|
43
|
+
/** How a system's graph values land on its plot area. */
|
|
44
|
+
export type MapFor<S> = (area: Extents, state: S) => CoordinateMap;
|
|
59
45
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
46
|
+
/** The plot area: the box less its padding, y-up from the box's centre. */
|
|
47
|
+
export function plotArea(size: Size2D, padding: InsetsResolved): Extents {
|
|
48
|
+
const inner = insetsOps.insetBounds(size.width, size.height, padding);
|
|
49
|
+
return {
|
|
50
|
+
minX: inner.x - inner.width / 2,
|
|
51
|
+
maxX: inner.x + inner.width / 2,
|
|
52
|
+
minY: -inner.y - inner.height / 2,
|
|
53
|
+
maxY: -inner.y + inner.height / 2,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
63
56
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
/** A system's map at a laid-out `size`. */
|
|
58
|
+
export function systemMap<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): CoordinateMap {
|
|
59
|
+
return mapFor(plotArea(size, state.padding), state);
|
|
60
|
+
}
|
|
68
61
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
for (const part of this.furniture(map)) ctx.draw(part);
|
|
75
|
-
if (clip) ctx.endClip();
|
|
76
|
-
}
|
|
62
|
+
/** A system is the size it is given: its children are placed in it, not measured into it. */
|
|
63
|
+
export function systemSize(state: SystemState): Size2D {
|
|
64
|
+
const { left, right, top, bottom } = state.padding;
|
|
65
|
+
return { width: left + right, height: top + bottom };
|
|
66
|
+
}
|
|
77
67
|
|
|
78
|
-
|
|
79
|
-
|
|
68
|
+
/** What a system hands its subtree: its plane, which its layout keeps current. */
|
|
69
|
+
export function systemProvides(state: object): readonly Provided[] {
|
|
70
|
+
return [provides(SYSTEM, systemPlane(state))];
|
|
71
|
+
}
|
|
80
72
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
73
|
+
/** Lay a system's children out on its map. */
|
|
74
|
+
export function systemLayout<S extends SystemState>(size: Size2D, state: S, children: readonly ChildView[], mapFor: MapFor<S>): Placement2D[] {
|
|
75
|
+
const map = systemMap(size, state, mapFor);
|
|
76
|
+
const plane = systemPlane(state);
|
|
77
|
+
settle(plane, map, children);
|
|
78
|
+
const inner = insetsOps.insetBounds(size.width, size.height, state.padding);
|
|
79
|
+
return placeOnMap(children, map, CENTRE, inner, plane);
|
|
80
|
+
}
|
|
85
81
|
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
/** Where a child dropped on a system would sit. */
|
|
83
|
+
export function systemOpenPlane<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): ChildPlane {
|
|
84
|
+
return openPlane(systemMap(size, state, mapFor), CENTRE);
|
|
85
|
+
}
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
87
|
+
/**
|
|
88
|
+
* A system's paint: the backdrop, then the furniture under its children, kept
|
|
89
|
+
* in the plot area where the map spills from it, then the children, confined
|
|
90
|
+
* to the plot area when `clip` is set. The stroke is the axis line, drawn with
|
|
91
|
+
* the furniture: painted after the children it would land over them.
|
|
92
|
+
*/
|
|
93
|
+
export function drawSystem(ctx: RenderContext, state: SystemState, map: CoordinateMap, furniture: (map: CoordinateMap) => Graphics2D[]): void {
|
|
94
|
+
const { width, height } = ctx.size;
|
|
95
|
+
const { minX, minY, maxX, maxY } = map.area;
|
|
96
|
+
const area = new Graphics2D().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
|
|
97
|
+
ctx.inFront(() => {
|
|
98
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).shadow(state.shadow).fill(state.fill));
|
|
99
|
+
const parts = furniture(map);
|
|
100
|
+
if (map.spills) ctx.clip(area, () => ctx.draw(parts));
|
|
101
|
+
else ctx.draw(parts);
|
|
102
|
+
});
|
|
103
|
+
// No shape of its own to paint here; the plot area is what clips the children and what picking tests.
|
|
104
|
+
Paint.draw(ctx, state as unknown as Paint.PaintState, null, { clipShape: area });
|
|
99
105
|
}
|
package/src/kit/withdrawn.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* The paint entries and commands a math node may have no use for: a node that
|
|
3
|
+
* is only a line has no fill, an area no outline, and one that reveals itself
|
|
4
|
+
* in its own way no outline trim.
|
|
5
5
|
*/
|
|
6
6
|
export const FILL_PAINT = ["fill", "fillTo"] as const;
|
|
7
7
|
export const STROKE_PAINT = ["stroke", "strokeTo"] as const;
|