@motionscript/math 0.1.0-alpha.7 → 0.1.0-alpha.9
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 +4 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -48
- package/README.md +390 -391
- package/dist/annotate/angle-mark.d.ts +6 -6
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +9 -9
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +9 -9
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-ABYBW64J.js → chunk-IRZCG6PM.js} +2 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
- package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
- 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 +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts +2 -2
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/graph.js.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/part-fills.js.map +1 -1
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.js +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/scale.js.map +1 -1
- package/dist/kit/source.js.map +1 -1
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/ticks.js.map +1 -1
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.d.ts +6 -6
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +3 -3
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +3 -3
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +6 -6
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +6 -6
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +8 -8
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/graph-layout.js.map +1 -1
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.d.ts +2 -2
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.d.ts +2 -2
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts +4 -4
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +27 -27
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js +3 -2
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.d.ts.map +1 -1
- package/dist/three/label3d.js +3 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.d.ts +27 -27
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +36 -36
- package/dist/three/surface-plot3d.js +6 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +27 -27
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +5 -4
- package/registry.json +251 -251
- package/src/annotate/angle-mark.ts +137 -137
- package/src/annotate/curly-brace.ts +148 -148
- package/src/annotate/emphasis-mark.ts +118 -118
- package/src/engine.ts +86 -86
- package/src/index.ts +29 -29
- package/src/kit/brace.ts +37 -37
- package/src/kit/cache.ts +61 -61
- package/src/kit/component.ts +57 -57
- package/src/kit/contour.ts +94 -94
- package/src/kit/coords.ts +278 -278
- package/src/kit/curve.ts +47 -47
- package/src/kit/equation.ts +181 -181
- package/src/kit/equation3d.ts +137 -137
- package/src/kit/expression.ts +159 -159
- package/src/kit/field-mark.ts +171 -171
- package/src/kit/field.ts +50 -50
- package/src/kit/follow.ts +229 -229
- package/src/kit/furniture.ts +113 -113
- package/src/kit/geometry.ts +49 -49
- package/src/kit/graph.ts +116 -116
- package/src/kit/mark.ts +152 -152
- package/src/kit/part-fills.ts +61 -61
- package/src/kit/place.ts +77 -77
- package/src/kit/sampler.ts +281 -281
- package/src/kit/scale.ts +39 -39
- package/src/kit/source.ts +57 -57
- package/src/kit/system.ts +105 -105
- package/src/kit/ticks.ts +85 -85
- package/src/kit/withdrawn.ts +8 -8
- package/src/marks/curve-area.ts +89 -89
- package/src/marks/drop-lines.ts +71 -71
- package/src/marks/plot-point.ts +102 -102
- package/src/marks/riemann-sum.ts +99 -99
- package/src/marks/tangent-line.ts +119 -119
- package/src/marks/vector-arrow.ts +98 -98
- package/src/nodes.ts +99 -99
- package/src/plots/function-plot.ts +220 -220
- package/src/plots/points-plot.ts +103 -103
- package/src/plots/stream-lines.ts +160 -160
- package/src/plots/traced-path.ts +70 -70
- package/src/plots/vector-field.ts +107 -107
- package/src/structures/graph-layout.ts +145 -145
- package/src/structures/network-graph.ts +291 -291
- package/src/structures/sample-space.ts +210 -210
- package/src/systems/axes2d.ts +226 -226
- package/src/systems/number-line.ts +130 -130
- package/src/systems/number-plane.ts +232 -232
- package/src/systems/polar-plane.ts +177 -177
- package/src/tex/geometry.ts +703 -703
- package/src/tex/index.ts +15 -15
- package/src/tex/mathjax.ts +82 -82
- package/src/tex/matrix.ts +97 -97
- package/src/tex/node.ts +74 -74
- package/src/tex/tween.ts +308 -308
- package/src/tex/typeset.ts +461 -461
- package/src/three/axes3d.ts +267 -267
- package/src/three/curve-plot3d.ts +146 -144
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -50
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -312
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-ABYBW64J.js.map +0 -7
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
- package/dist/kit/placement.d.ts +0 -8
- package/dist/kit/placement.d.ts.map +0 -1
- package/dist/kit/placement.js +0 -8
- package/dist/kit/placement.js.map +0 -1
package/src/kit/furniture.ts
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
import { Fills, Graphics2D, toPathString, type AssetScope, type Color, type Loader, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
|
|
2
|
-
import { buildLatexPath, LatexNotLoaded, loadMathJax, MATHJAX_LOADER } from "../tex/geometry";
|
|
3
|
-
import type { Interval } from "./coords";
|
|
4
|
-
|
|
5
|
-
/** Shared drawing for the systems' own furniture: axis lines, tick marks and their numbers. */
|
|
6
|
-
|
|
7
|
-
/** What a system draws its axes and numbers in, and a plot its curve, until told otherwise. */
|
|
8
|
-
export const AXIS_COLOR = "#9ca3af";
|
|
9
|
-
export const PLOT_COLOR = "#58a6ff";
|
|
10
|
-
|
|
11
|
-
/** A stroke without end markers: an axis wears a tip, its ticks do not. */
|
|
12
|
-
export function withoutTips(stroke: StrokeResolved[]): StrokeResolved[] {
|
|
13
|
-
return stroke.map((layer) => ({ ...layer, startCap: undefined, endCap: undefined }));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface LabelStyle {
|
|
17
|
-
fontSize: number;
|
|
18
|
-
fontFamily: string;
|
|
19
|
-
color: Color;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface Label {
|
|
23
|
-
text: string;
|
|
24
|
-
/** Where the label's box is anchored, in pixels. */
|
|
25
|
-
at: Vector2;
|
|
26
|
-
align: "left" | "center" | "right";
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** How wide a label's box is, in ems: room for a long number, so alignment is by the box's edge. */
|
|
30
|
-
const LABEL_BOX_EM = 8;
|
|
31
|
-
|
|
32
|
-
/** A label written between dollar signs is a formula: `$\\frac{\\pi}{2}$`. */
|
|
33
|
-
const FORMULA = /^\$([\s\S]+)\$$/;
|
|
34
|
-
|
|
35
|
-
/** Ask the host for MathJax when any of `texts` is a formula, so it is there before the node is laid out. */
|
|
36
|
-
export function declareFormulas(assets: AssetScope, texts: readonly string[]): void {
|
|
37
|
-
if (texts.some((text) => FORMULA.test(text.trim()))) assets.loader(MATHJAX_LOADER, loadMathJax, { gate: "layout" });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** {@link declareFormulas}, for a component's `load`. */
|
|
41
|
-
export function loadFormulas(loader: Loader, texts: readonly string[]): void {
|
|
42
|
-
if (texts.some((text) => FORMULA.test(text.trim()))) loader.task(MATHJAX_LOADER, loadMathJax, { before: "layout" });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** What draws `labels`: text in the scene's face, and a typeset formula for each one written as such. */
|
|
46
|
-
export function drawLabels(labels: readonly Label[], style: LabelStyle): Graphics2D[] {
|
|
47
|
-
const out: Graphics2D[] = [];
|
|
48
|
-
const fill = Fills.color(style.color);
|
|
49
|
-
const text = new Graphics2D();
|
|
50
|
-
const width = style.fontSize * LABEL_BOX_EM;
|
|
51
|
-
let written = false;
|
|
52
|
-
for (const label of labels) {
|
|
53
|
-
const formula = FORMULA.exec(label.text.trim());
|
|
54
|
-
const glyphs = formula ? typeset(formula[1], label, style.fontSize) : null;
|
|
55
|
-
if (glyphs) {
|
|
56
|
-
for (const glyph of glyphs) out.push(glyph.fill(fill));
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const { at, align } = label;
|
|
60
|
-
written = true;
|
|
61
|
-
text.text({
|
|
62
|
-
text: label.text,
|
|
63
|
-
fontSize: style.fontSize,
|
|
64
|
-
// An empty family is left out, so the label takes the scene's own face.
|
|
65
|
-
...(style.fontFamily === "" ? {} : { fontFamily: style.fontFamily }),
|
|
66
|
-
wrap: false,
|
|
67
|
-
width,
|
|
68
|
-
textAlign: align,
|
|
69
|
-
x: align === "center" ? at.x : align === "right" ? at.x - width / 2 : at.x + width / 2,
|
|
70
|
-
y: at.y,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
if (written) out.push(text.fill(fill));
|
|
74
|
-
return out;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// A formula's glyphs with its box anchored at the label's point; `null` for one that will not typeset, which is then
|
|
78
|
-
// drawn as written, and nothing at all while MathJax is still on its way: its source is not a stand-in for it.
|
|
79
|
-
function typeset(source: string, { at, align }: Label, fontSize: number): Graphics2D[] | null {
|
|
80
|
-
let formula: ReturnType<typeof buildLatexPath>;
|
|
81
|
-
try {
|
|
82
|
-
formula = buildLatexPath(source, fontSize);
|
|
83
|
-
} catch (error) {
|
|
84
|
-
return error instanceof LatexNotLoaded ? [] : null;
|
|
85
|
-
}
|
|
86
|
-
const [minX, minY, maxX, maxY] = formula.bounds;
|
|
87
|
-
const dx = at.x + (align === "left" ? formula.width / 2 : align === "right" ? -formula.width / 2 : 0);
|
|
88
|
-
// Glyph paths are y-down; the frame whose centre lands on the node's is shifted the other way.
|
|
89
|
-
const frame: [number, number, number, number] = [minX - dx, minY + at.y, maxX - dx, maxY + at.y];
|
|
90
|
-
return formula.tokens.map((token) => new Graphics2D().path({ data: toPathString(token.path), centerBounds: frame }));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Whether a mark `u` of the way along an axis is inside the part of it drawn so far. */
|
|
94
|
-
export function drawn(u: number, start: number, end: number): boolean {
|
|
95
|
-
return u >= start - 1e-9 && u <= end + 1e-9;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** How far along `range` a value is, 0 to 1, by pixel distance. */
|
|
99
|
-
export function along(at: Vector2, from: Vector2, to: Vector2): number {
|
|
100
|
-
const dx = to.x - from.x;
|
|
101
|
-
const dy = to.y - from.y;
|
|
102
|
-
const length = dx * dx + dy * dy;
|
|
103
|
-
return length === 0 ? 0 : ((at.x - from.x) * dx + (at.y - from.y) * dy) / length;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function lerpInterval(from: Interval, to: Interval, t: number): Interval {
|
|
107
|
-
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function readInterval(value: Interval | undefined, previous?: Interval): Interval {
|
|
111
|
-
const [min, max] = Array.isArray(value) ? value : (previous ?? [0, 1]);
|
|
112
|
-
return [Number(min), Number(max)];
|
|
113
|
-
}
|
|
1
|
+
import { Fills, Graphics2D, toPathString, type AssetScope, type Color, type Loader, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
|
|
2
|
+
import { buildLatexPath, LatexNotLoaded, loadMathJax, MATHJAX_LOADER } from "../tex/geometry";
|
|
3
|
+
import type { Interval } from "./coords";
|
|
4
|
+
|
|
5
|
+
/** Shared drawing for the systems' own furniture: axis lines, tick marks and their numbers. */
|
|
6
|
+
|
|
7
|
+
/** What a system draws its axes and numbers in, and a plot its curve, until told otherwise. */
|
|
8
|
+
export const AXIS_COLOR = "#9ca3af";
|
|
9
|
+
export const PLOT_COLOR = "#58a6ff";
|
|
10
|
+
|
|
11
|
+
/** A stroke without end markers: an axis wears a tip, its ticks do not. */
|
|
12
|
+
export function withoutTips(stroke: StrokeResolved[]): StrokeResolved[] {
|
|
13
|
+
return stroke.map((layer) => ({ ...layer, startCap: undefined, endCap: undefined }));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface LabelStyle {
|
|
17
|
+
fontSize: number;
|
|
18
|
+
fontFamily: string;
|
|
19
|
+
color: Color;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface Label {
|
|
23
|
+
text: string;
|
|
24
|
+
/** Where the label's box is anchored, in pixels. */
|
|
25
|
+
at: Vector2;
|
|
26
|
+
align: "left" | "center" | "right";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** How wide a label's box is, in ems: room for a long number, so alignment is by the box's edge. */
|
|
30
|
+
const LABEL_BOX_EM = 8;
|
|
31
|
+
|
|
32
|
+
/** A label written between dollar signs is a formula: `$\\frac{\\pi}{2}$`. */
|
|
33
|
+
const FORMULA = /^\$([\s\S]+)\$$/;
|
|
34
|
+
|
|
35
|
+
/** Ask the host for MathJax when any of `texts` is a formula, so it is there before the node is laid out. */
|
|
36
|
+
export function declareFormulas(assets: AssetScope, texts: readonly string[]): void {
|
|
37
|
+
if (texts.some((text) => FORMULA.test(text.trim()))) assets.loader(MATHJAX_LOADER, loadMathJax, { gate: "layout" });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** {@link declareFormulas}, for a component's `load`. */
|
|
41
|
+
export function loadFormulas(loader: Loader, texts: readonly string[]): void {
|
|
42
|
+
if (texts.some((text) => FORMULA.test(text.trim()))) loader.task(MATHJAX_LOADER, loadMathJax, { before: "layout" });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What draws `labels`: text in the scene's face, and a typeset formula for each one written as such. */
|
|
46
|
+
export function drawLabels(labels: readonly Label[], style: LabelStyle): Graphics2D[] {
|
|
47
|
+
const out: Graphics2D[] = [];
|
|
48
|
+
const fill = Fills.color(style.color);
|
|
49
|
+
const text = new Graphics2D();
|
|
50
|
+
const width = style.fontSize * LABEL_BOX_EM;
|
|
51
|
+
let written = false;
|
|
52
|
+
for (const label of labels) {
|
|
53
|
+
const formula = FORMULA.exec(label.text.trim());
|
|
54
|
+
const glyphs = formula ? typeset(formula[1], label, style.fontSize) : null;
|
|
55
|
+
if (glyphs) {
|
|
56
|
+
for (const glyph of glyphs) out.push(glyph.fill(fill));
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const { at, align } = label;
|
|
60
|
+
written = true;
|
|
61
|
+
text.text({
|
|
62
|
+
text: label.text,
|
|
63
|
+
fontSize: style.fontSize,
|
|
64
|
+
// An empty family is left out, so the label takes the scene's own face.
|
|
65
|
+
...(style.fontFamily === "" ? {} : { fontFamily: style.fontFamily }),
|
|
66
|
+
wrap: false,
|
|
67
|
+
width,
|
|
68
|
+
textAlign: align,
|
|
69
|
+
x: align === "center" ? at.x : align === "right" ? at.x - width / 2 : at.x + width / 2,
|
|
70
|
+
y: at.y,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
if (written) out.push(text.fill(fill));
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// A formula's glyphs with its box anchored at the label's point; `null` for one that will not typeset, which is then
|
|
78
|
+
// drawn as written, and nothing at all while MathJax is still on its way: its source is not a stand-in for it.
|
|
79
|
+
function typeset(source: string, { at, align }: Label, fontSize: number): Graphics2D[] | null {
|
|
80
|
+
let formula: ReturnType<typeof buildLatexPath>;
|
|
81
|
+
try {
|
|
82
|
+
formula = buildLatexPath(source, fontSize);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
return error instanceof LatexNotLoaded ? [] : null;
|
|
85
|
+
}
|
|
86
|
+
const [minX, minY, maxX, maxY] = formula.bounds;
|
|
87
|
+
const dx = at.x + (align === "left" ? formula.width / 2 : align === "right" ? -formula.width / 2 : 0);
|
|
88
|
+
// Glyph paths are y-down; the frame whose centre lands on the node's is shifted the other way.
|
|
89
|
+
const frame: [number, number, number, number] = [minX - dx, minY + at.y, maxX - dx, maxY + at.y];
|
|
90
|
+
return formula.tokens.map((token) => new Graphics2D().path({ data: toPathString(token.path), centerBounds: frame }));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Whether a mark `u` of the way along an axis is inside the part of it drawn so far. */
|
|
94
|
+
export function drawn(u: number, start: number, end: number): boolean {
|
|
95
|
+
return u >= start - 1e-9 && u <= end + 1e-9;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** How far along `range` a value is, 0 to 1, by pixel distance. */
|
|
99
|
+
export function along(at: Vector2, from: Vector2, to: Vector2): number {
|
|
100
|
+
const dx = to.x - from.x;
|
|
101
|
+
const dy = to.y - from.y;
|
|
102
|
+
const length = dx * dx + dy * dy;
|
|
103
|
+
return length === 0 ? 0 : ((at.x - from.x) * dx + (at.y - from.y) * dy) / length;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function lerpInterval(from: Interval, to: Interval, t: number): Interval {
|
|
107
|
+
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function readInterval(value: Interval | undefined, previous?: Interval): Interval {
|
|
111
|
+
const [min, max] = Array.isArray(value) ? value : (previous ?? [0, 1]);
|
|
112
|
+
return [Number(min), Number(max)];
|
|
113
|
+
}
|
package/src/kit/geometry.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import type { Extents, PathCommand, Vector2 } from "@motionscript/sdk";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Runs of pixel points as path commands. Commands are y-up like every other
|
|
5
|
-
* `Graphics2D` op (only an SVG `d` string is y-down), so nothing is flipped here.
|
|
6
|
-
*/
|
|
7
|
-
export function runsToPath(runs: readonly (readonly Vector2[])[], closed = false): PathCommand[] {
|
|
8
|
-
const out: PathCommand[] = [];
|
|
9
|
-
for (const run of runs) {
|
|
10
|
-
if (run.length < 2) continue;
|
|
11
|
-
out.push({ type: "M", x: run[0].x, y: run[0].y });
|
|
12
|
-
for (let i = 1; i < run.length; i++) out.push({ type: "L", x: run[i].x, y: run[i].y });
|
|
13
|
-
if (closed) out.push({ type: "Z" });
|
|
14
|
-
}
|
|
15
|
-
return out;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function boundsOfRuns(runs: readonly (readonly Vector2[])[]): Extents | null {
|
|
19
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
20
|
-
for (const run of runs) {
|
|
21
|
-
if (run.length < 2) continue;
|
|
22
|
-
for (const p of run) {
|
|
23
|
-
if (p.x < minX) minX = p.x;
|
|
24
|
-
if (p.x > maxX) maxX = p.x;
|
|
25
|
-
if (p.y < minY) minY = p.y;
|
|
26
|
-
if (p.y > maxY) maxY = p.y;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return minX <= maxX ? { minX, minY, maxX, maxY } : null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** `area` grown by `margin` on every side. */
|
|
33
|
-
export function inflate(area: Extents, margin: number): Extents {
|
|
34
|
-
return { minX: area.minX - margin, minY: area.minY - margin, maxX: area.maxX + margin, maxY: area.maxY + margin };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function clamp(value: number, min: number, max: number): number {
|
|
38
|
-
return value < min ? min : value > max ? max : value;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** `data` without the arguments a command was not given, which arrive as `undefined` and are not destinations. */
|
|
42
|
-
export function stated<T extends object>(data: T | undefined): Partial<T> {
|
|
43
|
-
const out: Partial<T> = {};
|
|
44
|
-
if (!data) return out;
|
|
45
|
-
for (const key of Object.keys(data) as (keyof T)[]) {
|
|
46
|
-
if (data[key] !== undefined) out[key] = data[key];
|
|
47
|
-
}
|
|
48
|
-
return out;
|
|
49
|
-
}
|
|
1
|
+
import type { Extents, PathCommand, Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs of pixel points as path commands. Commands are y-up like every other
|
|
5
|
+
* `Graphics2D` op (only an SVG `d` string is y-down), so nothing is flipped here.
|
|
6
|
+
*/
|
|
7
|
+
export function runsToPath(runs: readonly (readonly Vector2[])[], closed = false): PathCommand[] {
|
|
8
|
+
const out: PathCommand[] = [];
|
|
9
|
+
for (const run of runs) {
|
|
10
|
+
if (run.length < 2) continue;
|
|
11
|
+
out.push({ type: "M", x: run[0].x, y: run[0].y });
|
|
12
|
+
for (let i = 1; i < run.length; i++) out.push({ type: "L", x: run[i].x, y: run[i].y });
|
|
13
|
+
if (closed) out.push({ type: "Z" });
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function boundsOfRuns(runs: readonly (readonly Vector2[])[]): Extents | null {
|
|
19
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
20
|
+
for (const run of runs) {
|
|
21
|
+
if (run.length < 2) continue;
|
|
22
|
+
for (const p of run) {
|
|
23
|
+
if (p.x < minX) minX = p.x;
|
|
24
|
+
if (p.x > maxX) maxX = p.x;
|
|
25
|
+
if (p.y < minY) minY = p.y;
|
|
26
|
+
if (p.y > maxY) maxY = p.y;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return minX <= maxX ? { minX, minY, maxX, maxY } : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** `area` grown by `margin` on every side. */
|
|
33
|
+
export function inflate(area: Extents, margin: number): Extents {
|
|
34
|
+
return { minX: area.minX - margin, minY: area.minY - margin, maxX: area.maxX + margin, maxY: area.maxY + margin };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
38
|
+
return value < min ? min : value > max ? max : value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `data` without the arguments a command was not given, which arrive as `undefined` and are not destinations. */
|
|
42
|
+
export function stated<T extends object>(data: T | undefined): Partial<T> {
|
|
43
|
+
const out: Partial<T> = {};
|
|
44
|
+
if (!data) return out;
|
|
45
|
+
for (const key of Object.keys(data) as (keyof T)[]) {
|
|
46
|
+
if (data[key] !== undefined) out[key] = data[key];
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
package/src/kit/graph.ts
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createContext,
|
|
3
|
-
Types,
|
|
4
|
-
type ChildView,
|
|
5
|
-
type EntryOptions,
|
|
6
|
-
type NodeView,
|
|
7
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
-
type Context as _Context,
|
|
9
|
-
} from "@motionscript/sdk";
|
|
10
|
-
import { PIXEL_MAP, type CoordinateMap } from "./coords";
|
|
11
|
-
import { CURVE_KEYS, isCurve, type CurveValues } from "./curve";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* What a coordinate system hands the plots and marks inside it: the map it was
|
|
15
|
-
* last laid out on, the curves it holds by id, and, under a plot, that plot.
|
|
16
|
-
* Inherited once, so it is a live object the system's layout keeps current.
|
|
17
|
-
*/
|
|
18
|
-
export interface GraphPlane {
|
|
19
|
-
readonly map: CoordinateMap;
|
|
20
|
-
readonly curves: ReadonlyMap<string, CurveValues>;
|
|
21
|
-
/** The curve a mark rides when it names none: the plot it sits in. */
|
|
22
|
-
readonly enclosing: CurveValues | null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** The plane of the nearest coordinate system: what a plot is laid out on. */
|
|
26
|
-
export const SYSTEM = createContext<GraphPlane | null>(null, "mathSystem");
|
|
27
|
-
/** The plane under a plot: the system's, riding that plot. */
|
|
28
|
-
export const PLOT = createContext<GraphPlane | null>(null, "mathPlot");
|
|
29
|
-
|
|
30
|
-
type Inherited = { readonly private: true };
|
|
31
|
-
|
|
32
|
-
/** A plot's plane: the system it sits in, `null` outside one. */
|
|
33
|
-
export const SYSTEM_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
|
|
34
|
-
private: true,
|
|
35
|
-
inherit: (use) => use(SYSTEM),
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
/** A mark's plane: the plot or the system that lays it out, `null` outside both. */
|
|
39
|
-
export const PLANE_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
|
|
40
|
-
private: true,
|
|
41
|
-
inherit: (use) => use(PLOT) ?? use(SYSTEM),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
/** A reference to the plot a mark reads, by id: a host offers the plots. */
|
|
45
|
-
export const PLOT_REF = Types.custom<string, NodeView | null, EntryOptions & { readonly nodeTypes: readonly string[] }>("", { kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false });
|
|
46
|
-
|
|
47
|
-
const NO_CURVES: ReadonlyMap<string, CurveValues> = new Map();
|
|
48
|
-
|
|
49
|
-
class SystemPlane implements GraphPlane {
|
|
50
|
-
map: CoordinateMap = PIXEL_MAP;
|
|
51
|
-
curves: ReadonlyMap<string, CurveValues> = NO_CURVES;
|
|
52
|
-
readonly enclosing = null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
class PlotPlane implements GraphPlane {
|
|
56
|
-
constructor(readonly parent: GraphPlane | null, readonly enclosing: CurveValues) { }
|
|
57
|
-
|
|
58
|
-
get map(): CoordinateMap {
|
|
59
|
-
return this.parent?.map ?? PIXEL_MAP;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get curves(): ReadonlyMap<string, CurveValues> {
|
|
63
|
-
return this.parent?.curves ?? NO_CURVES;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const systems = new WeakMap<object, SystemPlane>();
|
|
68
|
-
const plots = new WeakMap<object, PlotPlane>();
|
|
69
|
-
|
|
70
|
-
/** The plane a system provides, one per system: its `layout` writes the map and the curves into it. */
|
|
71
|
-
export function systemPlane(state: object): SystemPlane {
|
|
72
|
-
let plane = systems.get(state);
|
|
73
|
-
if (!plane) systems.set(state, (plane = new SystemPlane()));
|
|
74
|
-
return plane;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Lay `plane` out on `map`, holding the curves among `children`. */
|
|
78
|
-
export function settle(plane: SystemPlane, map: CoordinateMap, children: readonly ChildView[]): void {
|
|
79
|
-
plane.map = map;
|
|
80
|
-
const curves = new Map<string, CurveValues>();
|
|
81
|
-
collectCurves(children, curves);
|
|
82
|
-
plane.curves = curves;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** The plane a plot provides the marks inside it, one per plot and system. */
|
|
86
|
-
export function plotPlane(plot: CurveValues, parent: GraphPlane | null): GraphPlane {
|
|
87
|
-
let plane = plots.get(plot);
|
|
88
|
-
if (!plane || plane.parent !== parent) plots.set(plot, (plane = new PlotPlane(parent, plot)));
|
|
89
|
-
return plane;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* The curve a mark reads: the one `on` names in its system, or, naming none or
|
|
94
|
-
* one the system does not hold, the plot it sits in. `on` as the mark's own
|
|
95
|
-
* fields read a reference (a view) or its holder's layout does (an id). Outside
|
|
96
|
-
* any system a named curve is read wherever it is.
|
|
97
|
-
*/
|
|
98
|
-
export function curveFor(plane: GraphPlane | null, on: unknown, enclosing = true): CurveValues | null {
|
|
99
|
-
if (!plane) return isCurve(on) ? on.state : null;
|
|
100
|
-
const id = refId(on);
|
|
101
|
-
return (id === "" ? undefined : plane.curves.get(id)) ?? (enclosing ? plane.enclosing : null);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** The id a reference holds, read as an id or as a view of the node. */
|
|
105
|
-
export function refId(on: unknown): string {
|
|
106
|
-
if (typeof on === "string") return on;
|
|
107
|
-
if (typeof on === "object" && on !== null && typeof (on as { id?: unknown }).id === "string") return (on as { id: string }).id;
|
|
108
|
-
return "";
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function collectCurves(children: readonly ChildView[], into: Map<string, CurveValues>): void {
|
|
112
|
-
for (const child of children) {
|
|
113
|
-
if (child.id !== "" && CURVE_KEYS.includes(child.key)) into.set(child.id, child.state as unknown as CurveValues);
|
|
114
|
-
collectCurves(child.children, into);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
Types,
|
|
4
|
+
type ChildView,
|
|
5
|
+
type EntryOptions,
|
|
6
|
+
type NodeView,
|
|
7
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
+
type Context as _Context,
|
|
9
|
+
} from "@motionscript/sdk";
|
|
10
|
+
import { PIXEL_MAP, type CoordinateMap } from "./coords";
|
|
11
|
+
import { CURVE_KEYS, isCurve, type CurveValues } from "./curve";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What a coordinate system hands the plots and marks inside it: the map it was
|
|
15
|
+
* last laid out on, the curves it holds by id, and, under a plot, that plot.
|
|
16
|
+
* Inherited once, so it is a live object the system's layout keeps current.
|
|
17
|
+
*/
|
|
18
|
+
export interface GraphPlane {
|
|
19
|
+
readonly map: CoordinateMap;
|
|
20
|
+
readonly curves: ReadonlyMap<string, CurveValues>;
|
|
21
|
+
/** The curve a mark rides when it names none: the plot it sits in. */
|
|
22
|
+
readonly enclosing: CurveValues | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The plane of the nearest coordinate system: what a plot is laid out on. */
|
|
26
|
+
export const SYSTEM = createContext<GraphPlane | null>(null, "mathSystem");
|
|
27
|
+
/** The plane under a plot: the system's, riding that plot. */
|
|
28
|
+
export const PLOT = createContext<GraphPlane | null>(null, "mathPlot");
|
|
29
|
+
|
|
30
|
+
type Inherited = { readonly private: true };
|
|
31
|
+
|
|
32
|
+
/** A plot's plane: the system it sits in, `null` outside one. */
|
|
33
|
+
export const SYSTEM_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
|
|
34
|
+
private: true,
|
|
35
|
+
inherit: (use) => use(SYSTEM),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
/** A mark's plane: the plot or the system that lays it out, `null` outside both. */
|
|
39
|
+
export const PLANE_ENTRY = Types.custom<GraphPlane | null, GraphPlane | null, Inherited>(null, {
|
|
40
|
+
private: true,
|
|
41
|
+
inherit: (use) => use(PLOT) ?? use(SYSTEM),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** A reference to the plot a mark reads, by id: a host offers the plots. */
|
|
45
|
+
export const PLOT_REF = Types.custom<string, NodeView | null, EntryOptions & { readonly nodeTypes: readonly string[] }>("", { kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false });
|
|
46
|
+
|
|
47
|
+
const NO_CURVES: ReadonlyMap<string, CurveValues> = new Map();
|
|
48
|
+
|
|
49
|
+
class SystemPlane implements GraphPlane {
|
|
50
|
+
map: CoordinateMap = PIXEL_MAP;
|
|
51
|
+
curves: ReadonlyMap<string, CurveValues> = NO_CURVES;
|
|
52
|
+
readonly enclosing = null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class PlotPlane implements GraphPlane {
|
|
56
|
+
constructor(readonly parent: GraphPlane | null, readonly enclosing: CurveValues) { }
|
|
57
|
+
|
|
58
|
+
get map(): CoordinateMap {
|
|
59
|
+
return this.parent?.map ?? PIXEL_MAP;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get curves(): ReadonlyMap<string, CurveValues> {
|
|
63
|
+
return this.parent?.curves ?? NO_CURVES;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const systems = new WeakMap<object, SystemPlane>();
|
|
68
|
+
const plots = new WeakMap<object, PlotPlane>();
|
|
69
|
+
|
|
70
|
+
/** The plane a system provides, one per system: its `layout` writes the map and the curves into it. */
|
|
71
|
+
export function systemPlane(state: object): SystemPlane {
|
|
72
|
+
let plane = systems.get(state);
|
|
73
|
+
if (!plane) systems.set(state, (plane = new SystemPlane()));
|
|
74
|
+
return plane;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Lay `plane` out on `map`, holding the curves among `children`. */
|
|
78
|
+
export function settle(plane: SystemPlane, map: CoordinateMap, children: readonly ChildView[]): void {
|
|
79
|
+
plane.map = map;
|
|
80
|
+
const curves = new Map<string, CurveValues>();
|
|
81
|
+
collectCurves(children, curves);
|
|
82
|
+
plane.curves = curves;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The plane a plot provides the marks inside it, one per plot and system. */
|
|
86
|
+
export function plotPlane(plot: CurveValues, parent: GraphPlane | null): GraphPlane {
|
|
87
|
+
let plane = plots.get(plot);
|
|
88
|
+
if (!plane || plane.parent !== parent) plots.set(plot, (plane = new PlotPlane(parent, plot)));
|
|
89
|
+
return plane;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The curve a mark reads: the one `on` names in its system, or, naming none or
|
|
94
|
+
* one the system does not hold, the plot it sits in. `on` as the mark's own
|
|
95
|
+
* fields read a reference (a view) or its holder's layout does (an id). Outside
|
|
96
|
+
* any system a named curve is read wherever it is.
|
|
97
|
+
*/
|
|
98
|
+
export function curveFor(plane: GraphPlane | null, on: unknown, enclosing = true): CurveValues | null {
|
|
99
|
+
if (!plane) return isCurve(on) ? on.state : null;
|
|
100
|
+
const id = refId(on);
|
|
101
|
+
return (id === "" ? undefined : plane.curves.get(id)) ?? (enclosing ? plane.enclosing : null);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The id a reference holds, read as an id or as a view of the node. */
|
|
105
|
+
export function refId(on: unknown): string {
|
|
106
|
+
if (typeof on === "string") return on;
|
|
107
|
+
if (typeof on === "object" && on !== null && typeof (on as { id?: unknown }).id === "string") return (on as { id: string }).id;
|
|
108
|
+
return "";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function collectCurves(children: readonly ChildView[], into: Map<string, CurveValues>): void {
|
|
112
|
+
for (const child of children) {
|
|
113
|
+
if (child.id !== "" && CURVE_KEYS.includes(child.key)) into.set(child.id, child.state as unknown as CurveValues);
|
|
114
|
+
collectCurves(child.children, into);
|
|
115
|
+
}
|
|
116
|
+
}
|