@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/follow.ts
CHANGED
|
@@ -1,229 +1,229 @@
|
|
|
1
|
-
import {
|
|
2
|
-
commandsFor,
|
|
3
|
-
pathOps,
|
|
4
|
-
Types,
|
|
5
|
-
type BoxBounds,
|
|
6
|
-
type NodeView,
|
|
7
|
-
type PaintProps,
|
|
8
|
-
type PathCommand,
|
|
9
|
-
type RenderContext,
|
|
10
|
-
type Vector2,
|
|
11
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
12
|
-
type Shadow as _Shadow,
|
|
13
|
-
type ShadowResolved as _ShadowResolved,
|
|
14
|
-
type Stroke as _Stroke,
|
|
15
|
-
type StrokeResolved as _StrokeResolved,
|
|
16
|
-
} from "@motionscript/sdk";
|
|
17
|
-
import { omit, PAINT, PAINT_COMMANDS } from "./component";
|
|
18
|
-
import { LatexMatrix } from "../tex/matrix";
|
|
19
|
-
import { Latex } from "../tex/node";
|
|
20
|
-
import { isFormula, latexPartBounds } from "../tex/typeset";
|
|
21
|
-
import { refId } from "./graph";
|
|
22
|
-
|
|
23
|
-
/** A node with parts that can be pointed at by name: a formula's terms. */
|
|
24
|
-
export interface Parted {
|
|
25
|
-
/** The box of `part`, y-up from the node's centre, or `null` when it has no such part. */
|
|
26
|
-
partBounds(part: string, occurrence?: number): BoxBounds | null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function isParted(node: object): boolean {
|
|
30
|
-
return typeof (node as Partial<Parted>).partBounds === "function" || isFormula(node);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** The box of `part` in `node`: its own `partBounds` answer, or a formula component's by its source. */
|
|
34
|
-
function partBoundsOf(node: object, part: string, occurrence: number): BoxBounds | null {
|
|
35
|
-
if (typeof (node as Partial<Parted>).partBounds === "function") return (node as Parted).partBounds(part, occurrence);
|
|
36
|
-
return latexPartBounds(node, part, occurrence) ?? null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** What a mark is drawn on: a node by id, and one part of it where it has parts. */
|
|
40
|
-
export interface Target {
|
|
41
|
-
on: string;
|
|
42
|
-
part?: string;
|
|
43
|
-
occurrence?: number;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** A target on its way to another: `t` of the way from `from`, or from the mark's resting target. */
|
|
47
|
-
export interface Retarget extends Target {
|
|
48
|
-
from?: Target | null;
|
|
49
|
-
t: number;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface FollowProps extends PaintProps {
|
|
53
|
-
on?: string;
|
|
54
|
-
part?: string;
|
|
55
|
-
occurrence?: number;
|
|
56
|
-
retarget?: Retarget | null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** A target's four corners in its mark's own frame, y-up: top left, top right, bottom right, bottom left. */
|
|
60
|
-
export type Quad = [Vector2, Vector2, Vector2, Vector2];
|
|
61
|
-
|
|
62
|
-
const restOf = ({ on, part, occurrence }: Target): Target => ({ on, part, occurrence });
|
|
63
|
-
|
|
64
|
-
function lerpRetarget(from: Retarget | null, to: Retarget | null, t: number): Retarget | null {
|
|
65
|
-
if (t <= 0 || !to) return t < 1 ? from : to;
|
|
66
|
-
return { ...restOf(to), from: from ? restOf(from) : null, t };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const REF = Types.custom<string, NodeView | null>("", { kind: "nodeRef", animatable: false });
|
|
70
|
-
const HELD_REF = Types.custom<string, NodeView | null, { readonly private: true }>("", { kind: "nodeRef", private: true });
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* A mark drawn on another node, or on one part of a formula: a brace, a box,
|
|
74
|
-
* an underline. It has no size of its own and takes no place in a layout; it
|
|
75
|
-
* is drawn where its target is, wherever the two are in the tree, and its
|
|
76
|
-
* selection box is its ink. Its entries, and what it draws on.
|
|
77
|
-
*/
|
|
78
|
-
export const FOLLOW_STATE = {
|
|
79
|
-
// It is only an outline.
|
|
80
|
-
...omit(PAINT, "fill"),
|
|
81
|
-
/** The node this mark is drawn on, by id. Left empty, it is the formula the mark sits inside. */
|
|
82
|
-
on: REF,
|
|
83
|
-
/** One part of that node, by its source, where it has parts: `x^2` of a formula. */
|
|
84
|
-
part: Types.text("", { animatable: false }),
|
|
85
|
-
/** Which of the places the part is written, counted from 1. */
|
|
86
|
-
occurrence: Types.number(1, { min: 1, step: 1, animatable: false }),
|
|
87
|
-
/** Where a `retargetTo` has taken the mark, and how far along it is. Not an author's to state. */
|
|
88
|
-
retarget: Types.custom<Retarget | null>(null, { lerp: lerpRetarget }),
|
|
89
|
-
/** The nodes the retarget names, there and where it set out from, read as nodes. */
|
|
90
|
-
retargetOn: HELD_REF,
|
|
91
|
-
retargetFrom: HELD_REF,
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
export const FOLLOW_COMMANDS = {
|
|
95
|
-
...omit(PAINT_COMMANDS, "fillTo"),
|
|
96
|
-
...commandsFor(FOLLOW_STATE, {
|
|
97
|
-
/** Move the mark onto another node, or another part. */
|
|
98
|
-
retargetTo: {
|
|
99
|
-
// No defaults for the node and the part: one left out keeps the mark's own.
|
|
100
|
-
args: {
|
|
101
|
-
on: Types.custom<string | undefined>(undefined, { kind: "nodeRef" }),
|
|
102
|
-
part: Types.custom<string | undefined>(undefined, { kind: "text" }),
|
|
103
|
-
occurrence: Types.number(1, { min: 1, step: 1 }),
|
|
104
|
-
},
|
|
105
|
-
run: (timeline, s) => {
|
|
106
|
-
const next = timeline.args;
|
|
107
|
-
const held = s.retarget ?? restingOf(s);
|
|
108
|
-
const to: Retarget = {
|
|
109
|
-
on: next.on ?? held.on,
|
|
110
|
-
part: next.part ?? (next.on ? "" : held.part),
|
|
111
|
-
occurrence: next.occurrence ?? 1,
|
|
112
|
-
t: 1,
|
|
113
|
-
};
|
|
114
|
-
const from = s.retarget;
|
|
115
|
-
return timeline.animate((t) => {
|
|
116
|
-
const retarget = lerpRetarget(from, to, t);
|
|
117
|
-
return { retarget, retargetOn: retarget?.on ?? "", retargetFrom: retarget?.from?.on ?? "" };
|
|
118
|
-
});
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
}),
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
/** What a follow mark reads of its state. */
|
|
125
|
-
export interface FollowValues {
|
|
126
|
-
readonly on: NodeView | null;
|
|
127
|
-
readonly part: string;
|
|
128
|
-
readonly occurrence: number;
|
|
129
|
-
readonly retarget: Retarget | null;
|
|
130
|
-
readonly retargetOn: NodeView | null;
|
|
131
|
-
readonly retargetFrom: NodeView | null;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function restingOf(s: FollowValues): Target {
|
|
135
|
-
return { on: refId(s.on), part: s.part, occurrence: s.occurrence };
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const containers = new WeakMap<object, NodeView | null>();
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* The formula a follow mark sits in, as its render last found it: what a mark
|
|
142
|
-
* naming no node is drawn on. Its bounds and handles read it from there, where
|
|
143
|
-
* there is no context to ask.
|
|
144
|
-
*/
|
|
145
|
-
export function container(ctx: RenderContext | null, s: object): NodeView | null {
|
|
146
|
-
if (!ctx) return containers.get(s) ?? null;
|
|
147
|
-
const found = ctx.ancestor(Latex) ?? ctx.ancestor(LatexMatrix);
|
|
148
|
-
const parted = found && isParted(found.state) ? found : null;
|
|
149
|
-
containers.set(s, parted);
|
|
150
|
-
return parted;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** The node the mark rests on: the one `on` names, or the formula it sits in. */
|
|
154
|
-
export function resting(s: FollowValues, parted: NodeView | null): NodeView | null {
|
|
155
|
-
return s.on ?? parted;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** The target's corners now: its resting one, or part-way to where a `retargetTo` is taking it. */
|
|
159
|
-
export function followedQuad(s: FollowValues, parted: NodeView | null): Quad | null {
|
|
160
|
-
const state = s.retarget;
|
|
161
|
-
const rest = (): Quad | null => quadOf(resting(s, parted), s);
|
|
162
|
-
if (!state) return rest();
|
|
163
|
-
const to = quadOf(state.on === "" ? parted : s.retargetOn, state);
|
|
164
|
-
const from = state.from ? quadOf(state.from.on === "" ? parted : s.retargetFrom, state.from) : rest();
|
|
165
|
-
if (!from || !to || state.t >= 1) return to ?? from;
|
|
166
|
-
return lerpQuad(from, to, state.t);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/** Where `target` is, as the mark sees it; `null` when it names no node or part. */
|
|
170
|
-
function quadOf(node: NodeView | null, target: Omit<Target, "on">): Quad | null {
|
|
171
|
-
if (!node) return null;
|
|
172
|
-
const box: BoxBounds | null =
|
|
173
|
-
target.part && isParted(node.state)
|
|
174
|
-
? partBoundsOf(node.state, target.part, target.occurrence || 1)
|
|
175
|
-
: { x: 0, y: 0, width: node.size.width, height: node.size.height };
|
|
176
|
-
if (!box) return null;
|
|
177
|
-
const frame = node.frame;
|
|
178
|
-
const turn = (frame.rotation * Math.PI) / 180;
|
|
179
|
-
const cos = Math.cos(turn);
|
|
180
|
-
const sin = Math.sin(turn);
|
|
181
|
-
// The engine's turn is clockwise, in a y-up frame.
|
|
182
|
-
const place = (x: number, y: number): Vector2 => ({
|
|
183
|
-
x: frame.x + (cos * x + sin * y) * frame.scale,
|
|
184
|
-
y: frame.y + (-sin * x + cos * y) * frame.scale,
|
|
185
|
-
});
|
|
186
|
-
const w = box.width / 2;
|
|
187
|
-
const h = box.height / 2;
|
|
188
|
-
return [place(box.x - w, box.y + h), place(box.x + w, box.y + h), place(box.x + w, box.y - h), place(box.x - w, box.y - h)];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function lerpQuad(a: Quad, b: Quad, t: number): Quad {
|
|
192
|
-
return a.map((from, i) => ({ x: from.x + (b[i].x - from.x) * t, y: from.y + (b[i].y - from.y) * t })) as Quad;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** The box of an outline, as a follow mark's bounds: it has no size of its own, so its ink is what is selected. */
|
|
196
|
-
export function outlineBounds(outline: readonly PathCommand[]): BoxBounds {
|
|
197
|
-
const ink = pathOps.bounds(outline as PathCommand[]);
|
|
198
|
-
if (!ink) return { x: 0, y: 0, width: 0, height: 0 };
|
|
199
|
-
return { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2, width: ink.maxX - ink.minX, height: ink.maxY - ink.minY };
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/** Every drawn point's centre frame: the mark's own centre, so its outline is where it says. */
|
|
203
|
-
export const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
|
|
204
|
-
|
|
205
|
-
// --- Geometry the marks share ---
|
|
206
|
-
|
|
207
|
-
export const add = (a: Vector2, b: Vector2, k = 1): Vector2 => ({ x: a.x + b.x * k, y: a.y + b.y * k });
|
|
208
|
-
export const between = (a: Vector2, b: Vector2, t = 0.5): Vector2 => ({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
|
|
209
|
-
|
|
210
|
-
/** The unit vector from `a` to `b`, and its length; a zero one points right. */
|
|
211
|
-
export function heading(a: Vector2, b: Vector2): { unit: Vector2; length: number } {
|
|
212
|
-
const dx = b.x - a.x;
|
|
213
|
-
const dy = b.y - a.y;
|
|
214
|
-
const length = Math.hypot(dx, dy);
|
|
215
|
-
return { unit: length === 0 ? { x: 1, y: 0 } : { x: dx / length, y: dy / length }, length };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** A quad grown by `by` pixels on every side, along its own edges. */
|
|
219
|
-
export function grown(quad: Quad, by: number): Quad {
|
|
220
|
-
const [tl, tr, br, bl] = quad;
|
|
221
|
-
const across = heading(tl, tr).unit;
|
|
222
|
-
const up = heading(bl, tl).unit;
|
|
223
|
-
return [
|
|
224
|
-
add(add(tl, across, -by), up, by),
|
|
225
|
-
add(add(tr, across, by), up, by),
|
|
226
|
-
add(add(br, across, by), up, -by),
|
|
227
|
-
add(add(bl, across, -by), up, -by),
|
|
228
|
-
];
|
|
229
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
commandsFor,
|
|
3
|
+
pathOps,
|
|
4
|
+
Types,
|
|
5
|
+
type BoxBounds,
|
|
6
|
+
type NodeView,
|
|
7
|
+
type PaintProps,
|
|
8
|
+
type PathCommand,
|
|
9
|
+
type RenderContext,
|
|
10
|
+
type Vector2,
|
|
11
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
12
|
+
type Shadow as _Shadow,
|
|
13
|
+
type ShadowResolved as _ShadowResolved,
|
|
14
|
+
type Stroke as _Stroke,
|
|
15
|
+
type StrokeResolved as _StrokeResolved,
|
|
16
|
+
} from "@motionscript/sdk";
|
|
17
|
+
import { omit, PAINT, PAINT_COMMANDS } from "./component";
|
|
18
|
+
import { LatexMatrix } from "../tex/matrix";
|
|
19
|
+
import { Latex } from "../tex/node";
|
|
20
|
+
import { isFormula, latexPartBounds } from "../tex/typeset";
|
|
21
|
+
import { refId } from "./graph";
|
|
22
|
+
|
|
23
|
+
/** A node with parts that can be pointed at by name: a formula's terms. */
|
|
24
|
+
export interface Parted {
|
|
25
|
+
/** The box of `part`, y-up from the node's centre, or `null` when it has no such part. */
|
|
26
|
+
partBounds(part: string, occurrence?: number): BoxBounds | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isParted(node: object): boolean {
|
|
30
|
+
return typeof (node as Partial<Parted>).partBounds === "function" || isFormula(node);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The box of `part` in `node`: its own `partBounds` answer, or a formula component's by its source. */
|
|
34
|
+
function partBoundsOf(node: object, part: string, occurrence: number): BoxBounds | null {
|
|
35
|
+
if (typeof (node as Partial<Parted>).partBounds === "function") return (node as Parted).partBounds(part, occurrence);
|
|
36
|
+
return latexPartBounds(node, part, occurrence) ?? null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What a mark is drawn on: a node by id, and one part of it where it has parts. */
|
|
40
|
+
export interface Target {
|
|
41
|
+
on: string;
|
|
42
|
+
part?: string;
|
|
43
|
+
occurrence?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A target on its way to another: `t` of the way from `from`, or from the mark's resting target. */
|
|
47
|
+
export interface Retarget extends Target {
|
|
48
|
+
from?: Target | null;
|
|
49
|
+
t: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface FollowProps extends PaintProps {
|
|
53
|
+
on?: string;
|
|
54
|
+
part?: string;
|
|
55
|
+
occurrence?: number;
|
|
56
|
+
retarget?: Retarget | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A target's four corners in its mark's own frame, y-up: top left, top right, bottom right, bottom left. */
|
|
60
|
+
export type Quad = [Vector2, Vector2, Vector2, Vector2];
|
|
61
|
+
|
|
62
|
+
const restOf = ({ on, part, occurrence }: Target): Target => ({ on, part, occurrence });
|
|
63
|
+
|
|
64
|
+
function lerpRetarget(from: Retarget | null, to: Retarget | null, t: number): Retarget | null {
|
|
65
|
+
if (t <= 0 || !to) return t < 1 ? from : to;
|
|
66
|
+
return { ...restOf(to), from: from ? restOf(from) : null, t };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const REF = Types.custom<string, NodeView | null>("", { kind: "nodeRef", animatable: false });
|
|
70
|
+
const HELD_REF = Types.custom<string, NodeView | null, { readonly private: true }>("", { kind: "nodeRef", private: true });
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A mark drawn on another node, or on one part of a formula: a brace, a box,
|
|
74
|
+
* an underline. It has no size of its own and takes no place in a layout; it
|
|
75
|
+
* is drawn where its target is, wherever the two are in the tree, and its
|
|
76
|
+
* selection box is its ink. Its entries, and what it draws on.
|
|
77
|
+
*/
|
|
78
|
+
export const FOLLOW_STATE = {
|
|
79
|
+
// It is only an outline.
|
|
80
|
+
...omit(PAINT, "fill"),
|
|
81
|
+
/** The node this mark is drawn on, by id. Left empty, it is the formula the mark sits inside. */
|
|
82
|
+
on: REF,
|
|
83
|
+
/** One part of that node, by its source, where it has parts: `x^2` of a formula. */
|
|
84
|
+
part: Types.text("", { animatable: false }),
|
|
85
|
+
/** Which of the places the part is written, counted from 1. */
|
|
86
|
+
occurrence: Types.number(1, { min: 1, step: 1, animatable: false }),
|
|
87
|
+
/** Where a `retargetTo` has taken the mark, and how far along it is. Not an author's to state. */
|
|
88
|
+
retarget: Types.custom<Retarget | null>(null, { lerp: lerpRetarget }),
|
|
89
|
+
/** The nodes the retarget names, there and where it set out from, read as nodes. */
|
|
90
|
+
retargetOn: HELD_REF,
|
|
91
|
+
retargetFrom: HELD_REF,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const FOLLOW_COMMANDS = {
|
|
95
|
+
...omit(PAINT_COMMANDS, "fillTo"),
|
|
96
|
+
...commandsFor(FOLLOW_STATE, {
|
|
97
|
+
/** Move the mark onto another node, or another part. */
|
|
98
|
+
retargetTo: {
|
|
99
|
+
// No defaults for the node and the part: one left out keeps the mark's own.
|
|
100
|
+
args: {
|
|
101
|
+
on: Types.custom<string | undefined>(undefined, { kind: "nodeRef" }),
|
|
102
|
+
part: Types.custom<string | undefined>(undefined, { kind: "text" }),
|
|
103
|
+
occurrence: Types.number(1, { min: 1, step: 1 }),
|
|
104
|
+
},
|
|
105
|
+
run: (timeline, s) => {
|
|
106
|
+
const next = timeline.args;
|
|
107
|
+
const held = s.retarget ?? restingOf(s);
|
|
108
|
+
const to: Retarget = {
|
|
109
|
+
on: next.on ?? held.on,
|
|
110
|
+
part: next.part ?? (next.on ? "" : held.part),
|
|
111
|
+
occurrence: next.occurrence ?? 1,
|
|
112
|
+
t: 1,
|
|
113
|
+
};
|
|
114
|
+
const from = s.retarget;
|
|
115
|
+
return timeline.animate((t) => {
|
|
116
|
+
const retarget = lerpRetarget(from, to, t);
|
|
117
|
+
return { retarget, retargetOn: retarget?.on ?? "", retargetFrom: retarget?.from?.on ?? "" };
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
}),
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** What a follow mark reads of its state. */
|
|
125
|
+
export interface FollowValues {
|
|
126
|
+
readonly on: NodeView | null;
|
|
127
|
+
readonly part: string;
|
|
128
|
+
readonly occurrence: number;
|
|
129
|
+
readonly retarget: Retarget | null;
|
|
130
|
+
readonly retargetOn: NodeView | null;
|
|
131
|
+
readonly retargetFrom: NodeView | null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function restingOf(s: FollowValues): Target {
|
|
135
|
+
return { on: refId(s.on), part: s.part, occurrence: s.occurrence };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const containers = new WeakMap<object, NodeView | null>();
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The formula a follow mark sits in, as its render last found it: what a mark
|
|
142
|
+
* naming no node is drawn on. Its bounds and handles read it from there, where
|
|
143
|
+
* there is no context to ask.
|
|
144
|
+
*/
|
|
145
|
+
export function container(ctx: RenderContext | null, s: object): NodeView | null {
|
|
146
|
+
if (!ctx) return containers.get(s) ?? null;
|
|
147
|
+
const found = ctx.ancestor(Latex) ?? ctx.ancestor(LatexMatrix);
|
|
148
|
+
const parted = found && isParted(found.state) ? found : null;
|
|
149
|
+
containers.set(s, parted);
|
|
150
|
+
return parted;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The node the mark rests on: the one `on` names, or the formula it sits in. */
|
|
154
|
+
export function resting(s: FollowValues, parted: NodeView | null): NodeView | null {
|
|
155
|
+
return s.on ?? parted;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The target's corners now: its resting one, or part-way to where a `retargetTo` is taking it. */
|
|
159
|
+
export function followedQuad(s: FollowValues, parted: NodeView | null): Quad | null {
|
|
160
|
+
const state = s.retarget;
|
|
161
|
+
const rest = (): Quad | null => quadOf(resting(s, parted), s);
|
|
162
|
+
if (!state) return rest();
|
|
163
|
+
const to = quadOf(state.on === "" ? parted : s.retargetOn, state);
|
|
164
|
+
const from = state.from ? quadOf(state.from.on === "" ? parted : s.retargetFrom, state.from) : rest();
|
|
165
|
+
if (!from || !to || state.t >= 1) return to ?? from;
|
|
166
|
+
return lerpQuad(from, to, state.t);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Where `target` is, as the mark sees it; `null` when it names no node or part. */
|
|
170
|
+
function quadOf(node: NodeView | null, target: Omit<Target, "on">): Quad | null {
|
|
171
|
+
if (!node) return null;
|
|
172
|
+
const box: BoxBounds | null =
|
|
173
|
+
target.part && isParted(node.state)
|
|
174
|
+
? partBoundsOf(node.state, target.part, target.occurrence || 1)
|
|
175
|
+
: { x: 0, y: 0, width: node.size.width, height: node.size.height };
|
|
176
|
+
if (!box) return null;
|
|
177
|
+
const frame = node.frame;
|
|
178
|
+
const turn = (frame.rotation * Math.PI) / 180;
|
|
179
|
+
const cos = Math.cos(turn);
|
|
180
|
+
const sin = Math.sin(turn);
|
|
181
|
+
// The engine's turn is clockwise, in a y-up frame.
|
|
182
|
+
const place = (x: number, y: number): Vector2 => ({
|
|
183
|
+
x: frame.x + (cos * x + sin * y) * frame.scale,
|
|
184
|
+
y: frame.y + (-sin * x + cos * y) * frame.scale,
|
|
185
|
+
});
|
|
186
|
+
const w = box.width / 2;
|
|
187
|
+
const h = box.height / 2;
|
|
188
|
+
return [place(box.x - w, box.y + h), place(box.x + w, box.y + h), place(box.x + w, box.y - h), place(box.x - w, box.y - h)];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function lerpQuad(a: Quad, b: Quad, t: number): Quad {
|
|
192
|
+
return a.map((from, i) => ({ x: from.x + (b[i].x - from.x) * t, y: from.y + (b[i].y - from.y) * t })) as Quad;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The box of an outline, as a follow mark's bounds: it has no size of its own, so its ink is what is selected. */
|
|
196
|
+
export function outlineBounds(outline: readonly PathCommand[]): BoxBounds {
|
|
197
|
+
const ink = pathOps.bounds(outline as PathCommand[]);
|
|
198
|
+
if (!ink) return { x: 0, y: 0, width: 0, height: 0 };
|
|
199
|
+
return { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2, width: ink.maxX - ink.minX, height: ink.maxY - ink.minY };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Every drawn point's centre frame: the mark's own centre, so its outline is where it says. */
|
|
203
|
+
export const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
|
|
204
|
+
|
|
205
|
+
// --- Geometry the marks share ---
|
|
206
|
+
|
|
207
|
+
export const add = (a: Vector2, b: Vector2, k = 1): Vector2 => ({ x: a.x + b.x * k, y: a.y + b.y * k });
|
|
208
|
+
export const between = (a: Vector2, b: Vector2, t = 0.5): Vector2 => ({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
|
|
209
|
+
|
|
210
|
+
/** The unit vector from `a` to `b`, and its length; a zero one points right. */
|
|
211
|
+
export function heading(a: Vector2, b: Vector2): { unit: Vector2; length: number } {
|
|
212
|
+
const dx = b.x - a.x;
|
|
213
|
+
const dy = b.y - a.y;
|
|
214
|
+
const length = Math.hypot(dx, dy);
|
|
215
|
+
return { unit: length === 0 ? { x: 1, y: 0 } : { x: dx / length, y: dy / length }, length };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** A quad grown by `by` pixels on every side, along its own edges. */
|
|
219
|
+
export function grown(quad: Quad, by: number): Quad {
|
|
220
|
+
const [tl, tr, br, bl] = quad;
|
|
221
|
+
const across = heading(tl, tr).unit;
|
|
222
|
+
const up = heading(bl, tl).unit;
|
|
223
|
+
return [
|
|
224
|
+
add(add(tl, across, -by), up, by),
|
|
225
|
+
add(add(tr, across, by), up, by),
|
|
226
|
+
add(add(br, across, by), up, -by),
|
|
227
|
+
add(add(bl, across, -by), up, -by),
|
|
228
|
+
];
|
|
229
|
+
}
|