@motionscript/math 0.0.0-stage → 0.1.0-alpha.10
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 +144 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +389 -2
- package/dist/annotate/angle-mark.d.ts +96 -0
- package/dist/annotate/angle-mark.d.ts.map +1 -0
- package/dist/annotate/angle-mark.js +97 -0
- package/dist/annotate/angle-mark.js.map +1 -0
- package/dist/annotate/curly-brace.d.ts +115 -0
- package/dist/annotate/curly-brace.d.ts.map +1 -0
- package/dist/annotate/curly-brace.js +99 -0
- package/dist/annotate/curly-brace.js.map +1 -0
- package/dist/annotate/emphasis-mark.d.ts +80 -0
- package/dist/annotate/emphasis-mark.d.ts.map +1 -0
- package/dist/annotate/emphasis-mark.js +80 -0
- package/dist/annotate/emphasis-mark.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
- package/dist/browser/chunks/chunk-IRZCG6PM.js +2 -0
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/chunk-WMVW6ZIV.js +2 -0
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/mathjax-WV55AC2P.js +61 -0
- package/dist/browser/chunks/mathjax-WV55AC2P.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +30 -0
- package/dist/browser/tex.js +2 -0
- package/dist/browser/tex.js.map +7 -0
- package/dist/engine.d.ts +71 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +45 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/brace.d.ts +8 -0
- package/dist/kit/brace.d.ts.map +1 -0
- package/dist/kit/brace.js +36 -0
- package/dist/kit/brace.js.map +1 -0
- package/dist/kit/cache.d.ts +30 -0
- package/dist/kit/cache.d.ts.map +1 -0
- package/dist/kit/cache.js +53 -0
- package/dist/kit/cache.js.map +1 -0
- package/dist/kit/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/contour.d.ts +8 -0
- package/dist/kit/contour.d.ts.map +1 -0
- package/dist/kit/contour.js +93 -0
- package/dist/kit/contour.js.map +1 -0
- package/dist/kit/coords.d.ts +69 -0
- package/dist/kit/coords.d.ts.map +1 -0
- package/dist/kit/coords.js +224 -0
- package/dist/kit/coords.js.map +1 -0
- package/dist/kit/curve.d.ts +28 -0
- package/dist/kit/curve.d.ts.map +1 -0
- package/dist/kit/curve.js +29 -0
- package/dist/kit/curve.js.map +1 -0
- package/dist/kit/equation.d.ts +61 -0
- package/dist/kit/equation.d.ts.map +1 -0
- package/dist/kit/equation.js +168 -0
- package/dist/kit/equation.js.map +1 -0
- package/dist/kit/equation3d.d.ts +50 -0
- package/dist/kit/equation3d.d.ts.map +1 -0
- package/dist/kit/equation3d.js +103 -0
- package/dist/kit/equation3d.js.map +1 -0
- package/dist/kit/expression.d.ts +35 -0
- package/dist/kit/expression.d.ts.map +1 -0
- package/dist/kit/expression.js +124 -0
- package/dist/kit/expression.js.map +1 -0
- package/dist/kit/field-mark.d.ts +79 -0
- package/dist/kit/field-mark.d.ts.map +1 -0
- package/dist/kit/field-mark.js +109 -0
- package/dist/kit/field-mark.js.map +1 -0
- package/dist/kit/field.d.ts +14 -0
- package/dist/kit/field.d.ts.map +1 -0
- package/dist/kit/field.js +46 -0
- package/dist/kit/field.js.map +1 -0
- package/dist/kit/follow.d.ts +109 -0
- package/dist/kit/follow.d.ts.map +1 -0
- package/dist/kit/follow.js +163 -0
- package/dist/kit/follow.js.map +1 -0
- package/dist/kit/furniture.d.ts +32 -0
- package/dist/kit/furniture.d.ts.map +1 -0
- package/dist/kit/furniture.js +92 -0
- package/dist/kit/furniture.js.map +1 -0
- package/dist/kit/geometry.d.ts +13 -0
- package/dist/kit/geometry.d.ts.map +1 -0
- package/dist/kit/geometry.js +54 -0
- package/dist/kit/geometry.js.map +1 -0
- package/dist/kit/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 +72 -0
- package/dist/kit/mark.d.ts.map +1 -0
- package/dist/kit/mark.js +87 -0
- package/dist/kit/mark.js.map +1 -0
- package/dist/kit/part-fills.d.ts +23 -0
- package/dist/kit/part-fills.d.ts.map +1 -0
- package/dist/kit/part-fills.js +46 -0
- package/dist/kit/part-fills.js.map +1 -0
- package/dist/kit/place.d.ts +20 -0
- package/dist/kit/place.d.ts.map +1 -0
- package/dist/kit/place.js +66 -0
- package/dist/kit/place.js.map +1 -0
- package/dist/kit/sampler.d.ts +17 -0
- package/dist/kit/sampler.d.ts.map +1 -0
- package/dist/kit/sampler.js +250 -0
- package/dist/kit/sampler.js.map +1 -0
- package/dist/kit/scale.d.ts +12 -0
- package/dist/kit/scale.d.ts.map +1 -0
- package/dist/kit/scale.js +37 -0
- package/dist/kit/scale.js.map +1 -0
- package/dist/kit/source.d.ts +22 -0
- package/dist/kit/source.d.ts.map +1 -0
- package/dist/kit/source.js +45 -0
- package/dist/kit/source.js.map +1 -0
- package/dist/kit/system.d.ts +31 -0
- package/dist/kit/system.d.ts.map +1 -0
- package/dist/kit/system.js +71 -0
- package/dist/kit/system.js.map +1 -0
- package/dist/kit/ticks.d.ts +19 -0
- package/dist/kit/ticks.d.ts.map +1 -0
- package/dist/kit/ticks.js +78 -0
- package/dist/kit/ticks.js.map +1 -0
- package/dist/kit/withdrawn.d.ts +9 -0
- package/dist/kit/withdrawn.d.ts.map +1 -0
- package/dist/kit/withdrawn.js +9 -0
- package/dist/kit/withdrawn.js.map +1 -0
- package/dist/marks/curve-area.d.ts +51 -0
- package/dist/marks/curve-area.d.ts.map +1 -0
- package/dist/marks/curve-area.js +59 -0
- package/dist/marks/curve-area.js.map +1 -0
- package/dist/marks/drop-lines.d.ts +57 -0
- package/dist/marks/drop-lines.d.ts.map +1 -0
- package/dist/marks/drop-lines.js +43 -0
- package/dist/marks/drop-lines.js.map +1 -0
- package/dist/marks/plot-point.d.ts +71 -0
- package/dist/marks/plot-point.d.ts.map +1 -0
- package/dist/marks/plot-point.js +71 -0
- package/dist/marks/plot-point.js.map +1 -0
- package/dist/marks/riemann-sum.d.ts +58 -0
- package/dist/marks/riemann-sum.d.ts.map +1 -0
- package/dist/marks/riemann-sum.js +63 -0
- package/dist/marks/riemann-sum.js.map +1 -0
- package/dist/marks/tangent-line.d.ts +71 -0
- package/dist/marks/tangent-line.d.ts.map +1 -0
- package/dist/marks/tangent-line.js +89 -0
- package/dist/marks/tangent-line.js.map +1 -0
- package/dist/marks/vector-arrow.d.ts +60 -0
- package/dist/marks/vector-arrow.d.ts.map +1 -0
- package/dist/marks/vector-arrow.js +67 -0
- package/dist/marks/vector-arrow.js.map +1 -0
- package/dist/nodes.d.ts +43 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +57 -0
- package/dist/nodes.js.map +1 -0
- package/dist/plots/function-plot.d.ts +159 -0
- package/dist/plots/function-plot.d.ts.map +1 -0
- package/dist/plots/function-plot.js +184 -0
- package/dist/plots/function-plot.js.map +1 -0
- package/dist/plots/points-plot.d.ts +64 -0
- package/dist/plots/points-plot.d.ts.map +1 -0
- package/dist/plots/points-plot.js +72 -0
- package/dist/plots/points-plot.js.map +1 -0
- package/dist/plots/stream-lines.d.ts +76 -0
- package/dist/plots/stream-lines.d.ts.map +1 -0
- package/dist/plots/stream-lines.js +122 -0
- package/dist/plots/stream-lines.js.map +1 -0
- package/dist/plots/traced-path.d.ts +70 -0
- package/dist/plots/traced-path.d.ts.map +1 -0
- package/dist/plots/traced-path.js +41 -0
- package/dist/plots/traced-path.js.map +1 -0
- package/dist/plots/vector-field.d.ts +60 -0
- package/dist/plots/vector-field.d.ts.map +1 -0
- package/dist/plots/vector-field.js +77 -0
- package/dist/plots/vector-field.js.map +1 -0
- package/dist/structures/graph-layout.d.ts +11 -0
- package/dist/structures/graph-layout.d.ts.map +1 -0
- package/dist/structures/graph-layout.js +135 -0
- package/dist/structures/graph-layout.js.map +1 -0
- package/dist/structures/network-graph.d.ts +174 -0
- package/dist/structures/network-graph.d.ts.map +1 -0
- package/dist/structures/network-graph.js +240 -0
- package/dist/structures/network-graph.js.map +1 -0
- package/dist/structures/sample-space.d.ts +114 -0
- package/dist/structures/sample-space.d.ts.map +1 -0
- package/dist/structures/sample-space.js +161 -0
- package/dist/structures/sample-space.js.map +1 -0
- package/dist/systems/axes2d.d.ts +197 -0
- package/dist/systems/axes2d.d.ts.map +1 -0
- package/dist/systems/axes2d.js +157 -0
- package/dist/systems/axes2d.js.map +1 -0
- package/dist/systems/number-line.d.ts +101 -0
- package/dist/systems/number-line.d.ts.map +1 -0
- package/dist/systems/number-line.js +91 -0
- package/dist/systems/number-line.js.map +1 -0
- package/dist/systems/number-plane.d.ts +168 -0
- package/dist/systems/number-plane.d.ts.map +1 -0
- package/dist/systems/number-plane.js +186 -0
- package/dist/systems/number-plane.js.map +1 -0
- package/dist/systems/polar-plane.d.ts +98 -0
- package/dist/systems/polar-plane.d.ts.map +1 -0
- package/dist/systems/polar-plane.js +139 -0
- package/dist/systems/polar-plane.js.map +1 -0
- package/dist/tex/geometry.d.ts +85 -0
- package/dist/tex/geometry.d.ts.map +1 -0
- package/dist/tex/geometry.js +632 -0
- package/dist/tex/geometry.js.map +1 -0
- package/dist/tex/index.d.ts +11 -0
- package/dist/tex/index.d.ts.map +1 -0
- package/dist/tex/index.js +10 -0
- package/dist/tex/index.js.map +1 -0
- package/dist/tex/mathjax.d.ts +33 -0
- package/dist/tex/mathjax.d.ts.map +1 -0
- package/dist/tex/mathjax.js +74 -0
- package/dist/tex/mathjax.js.map +1 -0
- package/dist/tex/matrix.d.ts +101 -0
- package/dist/tex/matrix.d.ts.map +1 -0
- package/dist/tex/matrix.js +56 -0
- package/dist/tex/matrix.js.map +1 -0
- package/dist/tex/node.d.ts +98 -0
- package/dist/tex/node.d.ts.map +1 -0
- package/dist/tex/node.js +25 -0
- package/dist/tex/node.js.map +1 -0
- package/dist/tex/tween.d.ts +50 -0
- package/dist/tex/tween.d.ts.map +1 -0
- package/dist/tex/tween.js +253 -0
- package/dist/tex/tween.js.map +1 -0
- package/dist/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 +247 -0
- package/dist/three/axes3d.d.ts.map +1 -0
- package/dist/three/axes3d.js +187 -0
- package/dist/three/axes3d.js.map +1 -0
- package/dist/three/curve-plot3d.d.ts +126 -0
- package/dist/three/curve-plot3d.d.ts.map +1 -0
- package/dist/three/curve-plot3d.js +109 -0
- package/dist/three/curve-plot3d.js.map +1 -0
- 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/label3d.d.ts +11 -0
- package/dist/three/label3d.d.ts.map +1 -0
- package/dist/three/label3d.js +42 -0
- package/dist/three/label3d.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +99 -0
- package/dist/three/plot-point3d.d.ts.map +1 -0
- package/dist/three/plot-point3d.js +34 -0
- package/dist/three/plot-point3d.js.map +1 -0
- package/dist/three/space.d.ts +43 -0
- package/dist/three/space.d.ts.map +1 -0
- package/dist/three/space.js +58 -0
- package/dist/three/space.js.map +1 -0
- package/dist/three/surface-plot3d.d.ts +207 -0
- package/dist/three/surface-plot3d.d.ts.map +1 -0
- package/dist/three/surface-plot3d.js +261 -0
- package/dist/three/surface-plot3d.js.map +1 -0
- package/dist/three/vector-arrow3d.d.ts +96 -0
- package/dist/three/vector-arrow3d.d.ts.map +1 -0
- package/dist/three/vector-arrow3d.js +53 -0
- package/dist/three/vector-arrow3d.js.map +1 -0
- package/package.json +80 -3
- package/registry.json +251 -0
- package/src/annotate/angle-mark.ts +137 -0
- package/src/annotate/curly-brace.ts +148 -0
- package/src/annotate/emphasis-mark.ts +118 -0
- package/src/engine.ts +86 -0
- package/src/index.ts +29 -0
- package/src/kit/brace.ts +37 -0
- package/src/kit/cache.ts +61 -0
- package/src/kit/component.ts +57 -0
- package/src/kit/contour.ts +94 -0
- package/src/kit/coords.ts +278 -0
- package/src/kit/curve.ts +47 -0
- package/src/kit/equation.ts +181 -0
- package/src/kit/equation3d.ts +137 -0
- package/src/kit/expression.ts +159 -0
- package/src/kit/field-mark.ts +171 -0
- package/src/kit/field.ts +50 -0
- package/src/kit/follow.ts +229 -0
- package/src/kit/furniture.ts +113 -0
- package/src/kit/geometry.ts +49 -0
- package/src/kit/graph.ts +116 -0
- package/src/kit/mark.ts +152 -0
- package/src/kit/part-fills.ts +61 -0
- package/src/kit/place.ts +77 -0
- package/src/kit/sampler.ts +281 -0
- package/src/kit/scale.ts +39 -0
- package/src/kit/source.ts +57 -0
- package/src/kit/system.ts +105 -0
- package/src/kit/ticks.ts +85 -0
- package/src/kit/withdrawn.ts +8 -0
- package/src/marks/curve-area.ts +89 -0
- package/src/marks/drop-lines.ts +71 -0
- package/src/marks/plot-point.ts +102 -0
- package/src/marks/riemann-sum.ts +99 -0
- package/src/marks/tangent-line.ts +119 -0
- package/src/marks/vector-arrow.ts +98 -0
- package/src/nodes.ts +99 -0
- package/src/plots/function-plot.ts +220 -0
- package/src/plots/points-plot.ts +103 -0
- package/src/plots/stream-lines.ts +160 -0
- package/src/plots/traced-path.ts +70 -0
- package/src/plots/vector-field.ts +107 -0
- package/src/structures/graph-layout.ts +145 -0
- package/src/structures/network-graph.ts +291 -0
- package/src/structures/sample-space.ts +210 -0
- package/src/systems/axes2d.ts +226 -0
- package/src/systems/number-line.ts +130 -0
- package/src/systems/number-plane.ts +232 -0
- package/src/systems/polar-plane.ts +177 -0
- package/src/tex/geometry.ts +703 -0
- package/src/tex/index.ts +15 -0
- package/src/tex/mathjax.ts +82 -0
- package/src/tex/matrix.ts +97 -0
- package/src/tex/node.ts +74 -0
- package/src/tex/tween.ts +308 -0
- package/src/tex/typeset.ts +461 -0
- package/src/three/axes3d.ts +267 -0
- package/src/three/curve-plot3d.ts +146 -0
- package/src/three/entries.ts +42 -0
- package/src/three/label3d.ts +52 -0
- package/src/three/plot-point3d.ts +63 -0
- package/src/three/space.ts +92 -0
- package/src/three/surface-plot3d.ts +318 -0
- package/src/three/vector-arrow3d.ts +88 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Types,
|
|
5
|
+
type Color,
|
|
6
|
+
type NodeOf,
|
|
7
|
+
type NodeView,
|
|
8
|
+
type NormalizedColor,
|
|
9
|
+
type PathCommand,
|
|
10
|
+
type Vector2,
|
|
11
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
12
|
+
type Node2D as _Node2D,
|
|
13
|
+
type Node2DProps as _Node2DProps,
|
|
14
|
+
type Shadow as _Shadow,
|
|
15
|
+
type ShadowResolved as _ShadowResolved,
|
|
16
|
+
type Stroke as _Stroke,
|
|
17
|
+
type StrokeResolved as _StrokeResolved,
|
|
18
|
+
} from "@motionscript/sdk";
|
|
19
|
+
import { braceOutline } from "../kit/brace";
|
|
20
|
+
import { listed, painted } from "../kit/component";
|
|
21
|
+
import {
|
|
22
|
+
add, between, container, FOLLOW_COMMANDS, FOLLOW_STATE, followedQuad, heading, outlineBounds, OWN_FRAME, resting,
|
|
23
|
+
type FollowProps, type FollowValues, type Quad,
|
|
24
|
+
} from "../kit/follow";
|
|
25
|
+
import { drawLabels, loadFormulas } from "../kit/furniture";
|
|
26
|
+
import { strokeShape } from "../kit/mark";
|
|
27
|
+
|
|
28
|
+
export type BraceSide = "below" | "above" | "left" | "right";
|
|
29
|
+
|
|
30
|
+
export interface CurlyBraceProps extends FollowProps {
|
|
31
|
+
side?: BraceSide;
|
|
32
|
+
tail?: Vector2;
|
|
33
|
+
head?: Vector2;
|
|
34
|
+
clearance?: number;
|
|
35
|
+
reach?: number;
|
|
36
|
+
label?: string;
|
|
37
|
+
labelSize?: number;
|
|
38
|
+
labelColor?: Color;
|
|
39
|
+
fontFamily?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The edge of a target a brace runs along, as two corner indexes of its quad, read with the brace's bulge to the left. */
|
|
43
|
+
const EDGE: Record<BraceSide, [number, number]> = {
|
|
44
|
+
below: [2, 3],
|
|
45
|
+
above: [0, 1],
|
|
46
|
+
left: [3, 0],
|
|
47
|
+
right: [1, 2],
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const state = {
|
|
51
|
+
...FOLLOW_STATE,
|
|
52
|
+
side: listed(["below", "above", "left", "right"], "below", { animatable: false }),
|
|
53
|
+
/** The two ends of a brace that follows nothing, from the brace's own position. It bulges to the left of tail to head. */
|
|
54
|
+
tail: Types.vector2({ x: -80, y: 0 }),
|
|
55
|
+
head: Types.vector2({ x: 80, y: 0 }),
|
|
56
|
+
/** The room between the target and the brace. (`gap` is a layout's.) */
|
|
57
|
+
clearance: Types.number(8, { min: 0, unit: "px" }),
|
|
58
|
+
/** How far the brace reaches from its ends to its tip. (`depth` is a node's place in 3D.) */
|
|
59
|
+
reach: Types.number(18, { min: 2, unit: "px" }),
|
|
60
|
+
label: Types.text("", { animatable: false }),
|
|
61
|
+
labelSize: Types.number(28, { min: 1, unit: "px" }),
|
|
62
|
+
labelColor: Types.color("#e5e7eb"),
|
|
63
|
+
fontFamily: Types.text("", { animatable: false }),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
interface BraceValues extends FollowValues {
|
|
67
|
+
readonly side: BraceSide;
|
|
68
|
+
readonly tail: Vector2;
|
|
69
|
+
readonly head: Vector2;
|
|
70
|
+
readonly clearance: number;
|
|
71
|
+
readonly reach: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Whether the brace follows nothing, and so spans its own two points. */
|
|
75
|
+
function spans(s: BraceValues, parted: NodeView | null): boolean {
|
|
76
|
+
return !s.retarget && resting(s, parted) === null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** No target: the span itself, as an edge with nothing above it. */
|
|
80
|
+
function quadOf(s: BraceValues, parted: NodeView | null): Quad | null {
|
|
81
|
+
return spans(s, parted) ? [s.head, s.tail, s.tail, s.head] : followedQuad(s, parted);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The edge the brace runs along, pushed out by the gap, and the way it bulges. */
|
|
85
|
+
function edgeOf(s: BraceValues, parted: NodeView | null, quad: Quad): { from: Vector2; to: Vector2; out: Vector2 } {
|
|
86
|
+
const [i, j] = spans(s, parted) ? [1, 0] : EDGE[s.side];
|
|
87
|
+
const { unit } = heading(quad[i], quad[j]);
|
|
88
|
+
// Left of the edge's own direction, in a y-up frame.
|
|
89
|
+
const out = { x: -unit.y, y: unit.x };
|
|
90
|
+
return { from: add(quad[i], out, s.clearance), to: add(quad[j], out, s.clearance), out };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function outline(s: BraceValues, parted: NodeView | null): PathCommand[] {
|
|
94
|
+
const quad = quadOf(s, parted);
|
|
95
|
+
if (!quad) return [];
|
|
96
|
+
const { from, to, out } = edgeOf(s, parted, quad);
|
|
97
|
+
return braceOutline(from, to, out, s.reach);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** One of the two ends to drag, while the brace spans them. */
|
|
101
|
+
const end = (which: "tail" | "head") => ({
|
|
102
|
+
at: (s: BraceValues) => (spans(s, container(null, s)) ? s[which] : null),
|
|
103
|
+
value: (at: Vector2) => ({ x: at.x, y: at.y }),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A curly brace along one side of a node or of a part of a formula, or between
|
|
108
|
+
* two points when it names neither, with an optional label at its tip. A label
|
|
109
|
+
* written between dollar signs is typeset.
|
|
110
|
+
*/
|
|
111
|
+
export const CurlyBrace = component2d({
|
|
112
|
+
key: "curlyBrace",
|
|
113
|
+
state,
|
|
114
|
+
defaults: { stroke: { weight: 3, fill: "#e5e7eb", align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
115
|
+
load: (loader, s) => {
|
|
116
|
+
if (s.label !== "" && s.fontFamily !== "") loader.font(s.fontFamily);
|
|
117
|
+
loadFormulas(loader, [s.label]);
|
|
118
|
+
},
|
|
119
|
+
commands: FOLLOW_COMMANDS,
|
|
120
|
+
handles: { tail: end("tail"), head: end("head") },
|
|
121
|
+
measure: () => ({ width: 0, height: 0 }),
|
|
122
|
+
bounds: (_size, s) => outlineBounds(outline(s, container(null, s))),
|
|
123
|
+
render: (ctx, s) => {
|
|
124
|
+
const parted = container(ctx, s);
|
|
125
|
+
const data = outline(s, parted);
|
|
126
|
+
const shape = data.length > 0 ? () => new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: s.start, end: s.end }) : null;
|
|
127
|
+
painted(
|
|
128
|
+
ctx,
|
|
129
|
+
() => {
|
|
130
|
+
if (shape) ctx.draw(shape().shadow(s.shadow).fill([]));
|
|
131
|
+
if (s.label === "") return;
|
|
132
|
+
const quad = quadOf(s, parted);
|
|
133
|
+
if (!quad) return;
|
|
134
|
+
const { from, to, out } = edgeOf(s, parted, quad);
|
|
135
|
+
const reach = Math.min(s.reach, heading(from, to).length / 2) + s.labelSize * 0.75;
|
|
136
|
+
const at = add(between(from, to), out, reach);
|
|
137
|
+
const align = out.x > 0.5 ? "left" : out.x < -0.5 ? "right" : "center";
|
|
138
|
+
const style = { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor };
|
|
139
|
+
ctx.draw(drawLabels([{ text: s.label, at, align }], style));
|
|
140
|
+
},
|
|
141
|
+
() => {
|
|
142
|
+
if (shape) strokeShape(ctx, shape, s);
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
export type CurlyBrace = NodeOf<typeof CurlyBrace>;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Types,
|
|
5
|
+
type NodeOf,
|
|
6
|
+
type NodeView,
|
|
7
|
+
type PathCommand,
|
|
8
|
+
type Vector2,
|
|
9
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
10
|
+
type Node2D as _Node2D,
|
|
11
|
+
type Node2DProps as _Node2DProps,
|
|
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 { listed, painted } from "../kit/component";
|
|
18
|
+
import {
|
|
19
|
+
add, between, container, FOLLOW_COMMANDS, FOLLOW_STATE, followedQuad, grown, heading, outlineBounds, OWN_FRAME, type FollowProps, type FollowValues, type Quad,
|
|
20
|
+
} from "../kit/follow";
|
|
21
|
+
import { strokeShape } from "../kit/mark";
|
|
22
|
+
|
|
23
|
+
export type EmphasisShape = "box" | "circle" | "underline" | "cross";
|
|
24
|
+
|
|
25
|
+
export interface EmphasisMarkProps extends FollowProps {
|
|
26
|
+
shape?: EmphasisShape;
|
|
27
|
+
clearance?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** How far a cubic's handle reaches to draw a quarter of an ellipse. */
|
|
31
|
+
const QUARTER = 0.5523;
|
|
32
|
+
|
|
33
|
+
const line = (a: Vector2, b: Vector2): PathCommand[] => [
|
|
34
|
+
{ type: "M", x: a.x, y: a.y },
|
|
35
|
+
{ type: "L", x: b.x, y: b.y },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const SHAPES: Record<EmphasisShape, (quad: Quad, gap: number) => PathCommand[]> = {
|
|
39
|
+
box: (quad, gap) => {
|
|
40
|
+
const [tl, tr, br, bl] = grown(quad, gap);
|
|
41
|
+
return [{ type: "M", x: tl.x, y: tl.y }, ...[tr, br, bl].map((p): PathCommand => ({ type: "L", x: p.x, y: p.y })), { type: "Z" }];
|
|
42
|
+
},
|
|
43
|
+
// The ellipse through the grown box's corners.
|
|
44
|
+
circle: (quad, gap) => {
|
|
45
|
+
const [tl, tr, br, bl] = grown(quad, gap);
|
|
46
|
+
const centre = between(tl, br);
|
|
47
|
+
const across = { x: ((tr.x - tl.x) / 2) * Math.SQRT2, y: ((tr.y - tl.y) / 2) * Math.SQRT2 };
|
|
48
|
+
const up = { x: ((tl.x - bl.x) / 2) * Math.SQRT2, y: ((tl.y - bl.y) / 2) * Math.SQRT2 };
|
|
49
|
+
const at = (a: number, u: number) => add(add(centre, across, a), up, u);
|
|
50
|
+
const arc = (c1: Vector2, c2: Vector2, end: Vector2): PathCommand => ({
|
|
51
|
+
type: "C", x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: end.x, y: end.y,
|
|
52
|
+
});
|
|
53
|
+
const k = QUARTER;
|
|
54
|
+
const start = at(0, 1);
|
|
55
|
+
return [
|
|
56
|
+
{ type: "M", x: start.x, y: start.y },
|
|
57
|
+
arc(at(k, 1), at(1, k), at(1, 0)),
|
|
58
|
+
arc(at(1, -k), at(k, -1), at(0, -1)),
|
|
59
|
+
arc(at(-k, -1), at(-1, -k), at(-1, 0)),
|
|
60
|
+
arc(at(-1, k), at(-k, 1), start),
|
|
61
|
+
{ type: "Z" },
|
|
62
|
+
];
|
|
63
|
+
},
|
|
64
|
+
underline: (quad, gap) => {
|
|
65
|
+
const [, , br, bl] = quad;
|
|
66
|
+
const down = heading(quad[0], bl).unit;
|
|
67
|
+
return line(add(bl, down, gap), add(br, down, gap));
|
|
68
|
+
},
|
|
69
|
+
cross: (quad, gap) => {
|
|
70
|
+
const [tl, tr, br, bl] = grown(quad, gap);
|
|
71
|
+
return [...line(tl, br), ...line(bl, tr)];
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const state = {
|
|
76
|
+
...FOLLOW_STATE,
|
|
77
|
+
shape: listed(["box", "circle", "underline", "cross"], "box", { animatable: false }),
|
|
78
|
+
/** The room between the target and the mark. (`gap` is a layout's.) */
|
|
79
|
+
clearance: Types.number(10, { unit: "px" }),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
interface EmphasisValues extends FollowValues {
|
|
83
|
+
readonly shape: EmphasisShape;
|
|
84
|
+
readonly clearance: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function outline(s: EmphasisValues, parted: NodeView | null): PathCommand[] {
|
|
88
|
+
const quad = followedQuad(s, parted);
|
|
89
|
+
return quad ? SHAPES[s.shape](quad, s.clearance) : [];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A box, a circle, an underline or a cross drawn on a node, or on one part of
|
|
94
|
+
* a formula. `progressTo` draws it on, and `retargetTo` slides it to another.
|
|
95
|
+
*/
|
|
96
|
+
export const EmphasisMark = component2d({
|
|
97
|
+
key: "emphasisMark",
|
|
98
|
+
state,
|
|
99
|
+
defaults: { stroke: { weight: 3, fill: "#facc15", align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
100
|
+
commands: FOLLOW_COMMANDS,
|
|
101
|
+
measure: () => ({ width: 0, height: 0 }),
|
|
102
|
+
bounds: (_size, s) => outlineBounds(outline(s, container(null, s))),
|
|
103
|
+
render: (ctx, s) => {
|
|
104
|
+
const data = outline(s, container(ctx, s));
|
|
105
|
+
const shape = data.length > 0 ? () => new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: s.start, end: s.end }) : null;
|
|
106
|
+
painted(
|
|
107
|
+
ctx,
|
|
108
|
+
() => {
|
|
109
|
+
if (shape) ctx.draw(shape().shadow(s.shadow).fill([]));
|
|
110
|
+
},
|
|
111
|
+
() => {
|
|
112
|
+
if (shape) strokeShape(ctx, shape, s);
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
export type EmphasisMark = NodeOf<typeof EmphasisMark>;
|
package/src/engine.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The math **kit**: what the nodes share, and what a copied one does not copy.
|
|
3
|
+
*
|
|
4
|
+
* A copy takes a node's own files into a project and rewrites its reach for
|
|
5
|
+
* these to `@motionscript/math`, so MathJax, the morph and every shared module
|
|
6
|
+
* stay a real dependency with one identity.
|
|
7
|
+
*/
|
|
8
|
+
export {
|
|
9
|
+
blankLatex, buildLatexPath, findLatexParts, latexPartNames, LatexError, LatexNotLoaded, loadMathJax, mathJaxLoaded, MATHJAX_LOADER,
|
|
10
|
+
} from './tex/geometry';
|
|
11
|
+
export type { LatexPart, LatexToken, LatexPathResult } from './tex/geometry';
|
|
12
|
+
export { defaultLatexMorph } from './tex/tween';
|
|
13
|
+
export type { AnimatedToken, LatexMorphStrategy } from './tex/tween';
|
|
14
|
+
export { Latex } from './tex/node';
|
|
15
|
+
export type { LatexProps, LatexStrategies } from './tex/node';
|
|
16
|
+
export {
|
|
17
|
+
drawFormula, formula, formulaFrame, formulaOf, isFormula, latexPartBounds, layoutFormula, lerpMorph, loadFormula, measureFormula, Morph, morphOf,
|
|
18
|
+
partBoundsIn, settled, TYPESET, TYPESET_COMMANDS,
|
|
19
|
+
} from './tex/typeset';
|
|
20
|
+
export type { FormulaFrame, Morphing, TypesetValues } from './tex/typeset';
|
|
21
|
+
export { lerpPartFills, partPaint, readPartFills, withPartFill } from './kit/part-fills';
|
|
22
|
+
export type { PartFill, PartFillResolved } from './kit/part-fills';
|
|
23
|
+
export { FILL_PAINT, OUTLINE_TRIM, STROKE_PAINT } from './kit/withdrawn';
|
|
24
|
+
export { createBoundedCache } from './kit/cache';
|
|
25
|
+
export { listed, omit, PAINT, PAINT_COMMANDS, painted } from './kit/component';
|
|
26
|
+
export type { BoundedCache } from './kit/cache';
|
|
27
|
+
|
|
28
|
+
export { drawSystem, plotArea, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from './kit/system';
|
|
29
|
+
export type { MapFor, SystemState } from './kit/system';
|
|
30
|
+
// A number plane is axes with a grid, so a copied one builds on these rather than a copy of them.
|
|
31
|
+
export { Axes2d, AXES, AXES_COMMANDS, axesFurniture, axesMap, axesOf, loadAxes } from './systems/axes2d';
|
|
32
|
+
export type { Axes2dProps, AxesValues, AxisLayout } from './systems/axes2d';
|
|
33
|
+
export type { Anchor } from './kit/place';
|
|
34
|
+
export { anchored, openPlane, placeOnMap } from './kit/place';
|
|
35
|
+
export {
|
|
36
|
+
castsShadow, drawMark, graphPoint, inkCentre, inkSize, markRuns, markShape, ownPoint, reading, strokeShape, traced,
|
|
37
|
+
} from './kit/mark';
|
|
38
|
+
export type { MarkOptions, MarkPaint, MarkReading, MarkState, Tracer } from './kit/mark';
|
|
39
|
+
export { curveFor, PLANE_ENTRY, PLOT, PLOT_REF, plotPlane, refId, settle, SYSTEM, SYSTEM_ENTRY, systemPlane } from './kit/graph';
|
|
40
|
+
export type { GraphPlane } from './kit/graph';
|
|
41
|
+
export { axisScale, cartesianMap, resolveInterval, PIXEL_MAP } from './kit/coords';
|
|
42
|
+
export type { AxisScale, AxisScaling, CartesianOptions, CoordinateMap, Interval } from './kit/coords';
|
|
43
|
+
export { compileExpression, compileExpressionCached, expressionVocabulary, MATH_DIALECT } from './kit/expression';
|
|
44
|
+
export type { CompiledExpression, ExpressionScope, ExpressionVocabulary } from './kit/expression';
|
|
45
|
+
export { equationError, equationParams, parseEquation, EQUATION_VARIABLES } from './kit/equation';
|
|
46
|
+
export type { Equation } from './kit/equation';
|
|
47
|
+
export { lerpDomain, lerpParams, lerpSource, readSource, revisionOf, scopeFor, UNSET_PARAM } from './kit/source';
|
|
48
|
+
export type { PlotSource } from './kit/source';
|
|
49
|
+
export { sampleCurve } from './kit/sampler';
|
|
50
|
+
export type { CurveProjection, SampleOptions } from './kit/sampler';
|
|
51
|
+
export { formatTick, ticksFor } from './kit/ticks';
|
|
52
|
+
export type { Ticks } from './kit/ticks';
|
|
53
|
+
export { boundsOfRuns, clamp, inflate, runsToPath, stated } from './kit/geometry';
|
|
54
|
+
export { CURVE_KEYS, curveValue, isCurve } from './kit/curve';
|
|
55
|
+
export type { CurveValues } from './kit/curve';
|
|
56
|
+
export { along, AXIS_COLOR, declareFormulas, drawLabels, drawn, lerpInterval, loadFormulas, PLOT_COLOR, readInterval, withoutTips } from './kit/furniture';
|
|
57
|
+
export type { Label, LabelStyle } from './kit/furniture';
|
|
58
|
+
export {
|
|
59
|
+
add, between, container, FOLLOW_COMMANDS, FOLLOW_STATE, followedQuad, grown, heading, outlineBounds, OWN_FRAME, resting,
|
|
60
|
+
} from './kit/follow';
|
|
61
|
+
export type { FollowProps, FollowValues, Parted, Quad, Retarget, Target } from './kit/follow';
|
|
62
|
+
export {
|
|
63
|
+
parseSpaceCurve, parseSurface, spaceCurveError, spaceCurveParams, surfaceError, surfaceParams, EQUATION_KINDS, EQUATION_VARIABLES_3D,
|
|
64
|
+
} from './kit/equation3d';
|
|
65
|
+
export type { EquationLanguage, SpaceCurveEquation, SurfaceEquation } from './kit/equation3d';
|
|
66
|
+
export { aim, graphEntry, graphOf, graphSpace, spaceOf, GRAPH_3D } from './three/space';
|
|
67
|
+
export type { Graph3D, GraphSpace } from './three/space';
|
|
68
|
+
export { domainEntry, meshEntries } from './three/entries';
|
|
69
|
+
export { drawLabel3D } from './three/label3d';
|
|
70
|
+
export type { LabelStyle3D } from './three/label3d';
|
|
71
|
+
// A traced path is a plot whose span follows a value, so a copied one builds on these rather than a copy of them.
|
|
72
|
+
export { FunctionPlot, PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot } from './plots/function-plot';
|
|
73
|
+
export type { FunctionPlotProps, PlotValues } from './plots/function-plot';
|
|
74
|
+
export { drawField, FIELD_COMMANDS, FIELD_STATE, traceField } from './kit/field-mark';
|
|
75
|
+
export type { FieldDrawing, FieldProps, FieldSampler, FieldStroke, FieldValues } from './kit/field-mark';
|
|
76
|
+
export { fieldError, fieldParams, parseField, FIELD_VARIABLES } from './kit/field';
|
|
77
|
+
export type { FieldEquation } from './kit/field';
|
|
78
|
+
export { traceContour } from './kit/contour';
|
|
79
|
+
export { colorAt, fillAt, lerpFillScale, lerpScale, readFillScale, readScale } from './kit/scale';
|
|
80
|
+
export {
|
|
81
|
+
IDENTITY_MATRIX, isIdentityMatrix, lerpMatrix, lineThrough, readMatrix, transformedMap, warpedMap,
|
|
82
|
+
} from './kit/coords';
|
|
83
|
+
export type { Matrix2, Warp } from './kit/coords';
|
|
84
|
+
export { braceOutline } from './kit/brace';
|
|
85
|
+
export { GRAPH_LAYOUTS, layOut } from './structures/graph-layout';
|
|
86
|
+
export type { GraphEdge, GraphLayout } from './structures/graph-layout';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export * from './tex/node';
|
|
2
|
+
export * from './tex/matrix';
|
|
3
|
+
export * from './systems/number-line';
|
|
4
|
+
export * from './systems/axes2d';
|
|
5
|
+
export * from './systems/number-plane';
|
|
6
|
+
export * from './plots/function-plot';
|
|
7
|
+
export * from './plots/points-plot';
|
|
8
|
+
export * from './marks/plot-point';
|
|
9
|
+
export * from './marks/vector-arrow';
|
|
10
|
+
export * from './marks/curve-area';
|
|
11
|
+
export * from './marks/riemann-sum';
|
|
12
|
+
export * from './marks/tangent-line';
|
|
13
|
+
export * from './marks/drop-lines';
|
|
14
|
+
export * from './annotate/curly-brace';
|
|
15
|
+
export * from './annotate/angle-mark';
|
|
16
|
+
export * from './annotate/emphasis-mark';
|
|
17
|
+
export * from './three/axes3d';
|
|
18
|
+
export * from './three/surface-plot3d';
|
|
19
|
+
export * from './three/curve-plot3d';
|
|
20
|
+
export * from './three/plot-point3d';
|
|
21
|
+
export * from './three/vector-arrow3d';
|
|
22
|
+
export * from './systems/polar-plane';
|
|
23
|
+
export * from './plots/vector-field';
|
|
24
|
+
export * from './plots/stream-lines';
|
|
25
|
+
export * from './plots/traced-path';
|
|
26
|
+
export * from './structures/network-graph';
|
|
27
|
+
export * from './structures/sample-space';
|
|
28
|
+
export * from './engine';
|
|
29
|
+
export { NODES } from "./nodes";
|
package/src/kit/brace.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PathCommand, Vector2 } from "@motionscript/sdk";
|
|
2
|
+
import { add, between, heading } from "./follow";
|
|
3
|
+
|
|
4
|
+
/** How far a quarter circle's control points sit along its tangents, as a share of its radius. */
|
|
5
|
+
const QUARTER = 0.5523;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A curly brace from `from` to `to`, bulging toward `out` (a unit vector) and
|
|
9
|
+
* `reach` deep at its tip: what a brace node draws, and a sample space beside
|
|
10
|
+
* its parts. Path commands, y-up.
|
|
11
|
+
*/
|
|
12
|
+
export function braceOutline(from: Vector2, to: Vector2, out: Vector2, reach: number): PathCommand[] {
|
|
13
|
+
const { unit, length } = heading(from, to);
|
|
14
|
+
if (length === 0) return [];
|
|
15
|
+
const r = Math.min(reach / 2, length / 4);
|
|
16
|
+
const mid = between(from, to);
|
|
17
|
+
const lift = (p: Vector2, k: number) => add(p, out, k * r);
|
|
18
|
+
const along = (p: Vector2, k: number) => add(p, unit, k * r);
|
|
19
|
+
const h = QUARTER;
|
|
20
|
+
const curve = (c1: Vector2, c2: Vector2, end: Vector2): PathCommand => ({
|
|
21
|
+
type: "C", x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: end.x, y: end.y,
|
|
22
|
+
});
|
|
23
|
+
const a = lift(along(from, 1), 1);
|
|
24
|
+
const b = lift(along(mid, -1), 1);
|
|
25
|
+
const tip = lift(mid, 2);
|
|
26
|
+
const c = lift(along(mid, 1), 1);
|
|
27
|
+
const d = lift(along(to, -1), 1);
|
|
28
|
+
return [
|
|
29
|
+
{ type: "M", x: from.x, y: from.y },
|
|
30
|
+
curve(lift(from, h), lift(along(from, 1 - h), 1), a),
|
|
31
|
+
{ type: "L", x: b.x, y: b.y },
|
|
32
|
+
curve(lift(along(mid, -1 + h), 1), lift(mid, 2 - h), tip),
|
|
33
|
+
curve(lift(mid, 2 - h), lift(along(mid, 1 - h), 1), c),
|
|
34
|
+
{ type: "L", x: d.x, y: d.y },
|
|
35
|
+
curve(lift(along(to, -1 + h), 1), lift(to, h), to),
|
|
36
|
+
];
|
|
37
|
+
}
|
package/src/kit/cache.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A bounded, least-recently-used memo.
|
|
3
|
+
*
|
|
4
|
+
* Sits under `geometry.ts`, and imports nothing, so it is testable in bare Node:
|
|
5
|
+
* everything else in this package reaches MathJax's browser adaptor at module
|
|
6
|
+
* scope, which means a test that touches it needs a DOM. The one piece of logic
|
|
7
|
+
* here worth being sure about — what gets thrown away when the memo is full —
|
|
8
|
+
* is exactly the piece that does not need one.
|
|
9
|
+
*
|
|
10
|
+
* Least-recently-used rather than oldest-first, and that matters for the shape
|
|
11
|
+
* of work this sees. A `Latex` node re-resolves its formula whenever `fontSize`
|
|
12
|
+
* changes, so animating the size of one formula inserts a fresh key every frame
|
|
13
|
+
* and would, under a first-in-first-out rule, walk the whole memo out of it in a
|
|
14
|
+
* couple of seconds — including the entries for every *other* formula in the
|
|
15
|
+
* scene, none of which had done anything wrong. Touching an entry on read keeps
|
|
16
|
+
* the formulas that are actually being asked for ahead of the ones streaming
|
|
17
|
+
* past.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface BoundedCache<T> {
|
|
21
|
+
get(key: string): T | undefined;
|
|
22
|
+
set(key: string, value: T): void;
|
|
23
|
+
/** Entries held. For tests and for anyone measuring. */
|
|
24
|
+
readonly size: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param limit Most entries to hold. One more arriving evicts the least
|
|
29
|
+
* recently read or written.
|
|
30
|
+
*/
|
|
31
|
+
export function createBoundedCache<T>(limit: number): BoundedCache<T> {
|
|
32
|
+
// Insertion order is the eviction order, which is what makes a plain `Map`
|
|
33
|
+
// an LRU as long as every read re-inserts.
|
|
34
|
+
const entries = new Map<string, T>();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
get(key) {
|
|
38
|
+
if (!entries.has(key)) return undefined;
|
|
39
|
+
const value = entries.get(key) as T;
|
|
40
|
+
// Delete-then-set moves the key to the end, so the next eviction
|
|
41
|
+
// takes the genuinely coldest entry rather than the oldest one.
|
|
42
|
+
entries.delete(key);
|
|
43
|
+
entries.set(key, value);
|
|
44
|
+
return value;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
set(key, value) {
|
|
48
|
+
entries.delete(key);
|
|
49
|
+
entries.set(key, value);
|
|
50
|
+
while (entries.size > limit) {
|
|
51
|
+
const coldest = entries.keys().next().value;
|
|
52
|
+
if (coldest === undefined) break;
|
|
53
|
+
entries.delete(coldest);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
get size() {
|
|
58
|
+
return entries.size;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Paint,
|
|
3
|
+
Types,
|
|
4
|
+
type EntryOptions,
|
|
5
|
+
type RenderContext,
|
|
6
|
+
type Type,
|
|
7
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
+
type Fill as _Fill,
|
|
9
|
+
type FillResolved as _FillResolved,
|
|
10
|
+
type Shadow as _Shadow,
|
|
11
|
+
type ShadowResolved as _ShadowResolved,
|
|
12
|
+
type Stroke as _Stroke,
|
|
13
|
+
type StrokeResolved as _StrokeResolved,
|
|
14
|
+
} from "@motionscript/sdk";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* What a component in this kit shares beyond the factory: the paints a math
|
|
18
|
+
* node draws with, listed choices, and the order a node paints in.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const { overlay: _overlay, ...paint } = Paint.state;
|
|
22
|
+
const { overlayTo: _overlayTo, ...paintCommands } = Paint.commands;
|
|
23
|
+
|
|
24
|
+
/** A math node's paints: fill, stroke, shadow and the outline trim, with no overlay. */
|
|
25
|
+
export const PAINT = paint;
|
|
26
|
+
export const PAINT_COMMANDS = paintCommands;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* One of `values`, held as text with its choices listed (no `kind: "enum"`).
|
|
30
|
+
*/
|
|
31
|
+
export function listed<const V extends readonly string[]>(values: V, initial: V[number], options?: ListedOptions): Type<V[number]>;
|
|
32
|
+
/** With no default: an arg left out is one the command was not given. */
|
|
33
|
+
export function listed<const V extends readonly string[]>(values: V, initial: undefined, options?: ListedOptions): Type<V[number] | undefined>;
|
|
34
|
+
export function listed(values: readonly string[], initial: string | undefined, options: ListedOptions = {}): Type<string | undefined> {
|
|
35
|
+
return Types.custom<string | undefined, string | undefined, EntryOptions & { readonly options: readonly string[] }>(initial, { ...options, options: values });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ListedOptions {
|
|
39
|
+
readonly animatable?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `record` without `keys`: the paints a node has no use for. */
|
|
43
|
+
export function omit<T extends object, const K extends keyof T>(record: T, ...keys: K[]): Omit<T, K> {
|
|
44
|
+
const out = { ...record };
|
|
45
|
+
for (const key of keys) delete out[key];
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A node's paint order: its own paint under its children, then what it lays
|
|
51
|
+
* over them, each cut at the near plane of a space it crosses.
|
|
52
|
+
*/
|
|
53
|
+
export function painted(ctx: RenderContext, own: () => void, over: () => void = () => { }): void {
|
|
54
|
+
ctx.inFront(own);
|
|
55
|
+
ctx.children();
|
|
56
|
+
ctx.inFront(over);
|
|
57
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The lines along which a function of two variables is zero, over a box of
|
|
5
|
+
* graph values: marching squares, its crossings placed by linear interpolation
|
|
6
|
+
* and joined end to end into runs. Points are graph coordinates.
|
|
7
|
+
*/
|
|
8
|
+
export function traceContour(
|
|
9
|
+
f: (x: number, y: number) => number,
|
|
10
|
+
[x0, x1]: readonly [number, number],
|
|
11
|
+
[y0, y1]: readonly [number, number],
|
|
12
|
+
columns: number,
|
|
13
|
+
rows: number,
|
|
14
|
+
): Vector2[][] {
|
|
15
|
+
if (!(x1 > x0) || !(y1 > y0) || columns < 1 || rows < 1) return [];
|
|
16
|
+
const dx = (x1 - x0) / columns;
|
|
17
|
+
const dy = (y1 - y0) / rows;
|
|
18
|
+
const stride = columns + 1;
|
|
19
|
+
const values = new Float64Array(stride * (rows + 1));
|
|
20
|
+
for (let j = 0; j <= rows; j++) {
|
|
21
|
+
for (let i = 0; i <= columns; i++) values[j * stride + i] = f(x0 + i * dx, y0 + j * dy);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// A crossing lives on a grid edge, which is its name: two cells sharing the edge share the point.
|
|
25
|
+
const points = new Map<number, Vector2>();
|
|
26
|
+
const edgeCount = stride * (rows + 1);
|
|
27
|
+
const crossing = (i: number, j: number, vertical: boolean): number | null => {
|
|
28
|
+
const a = values[j * stride + i];
|
|
29
|
+
const b = vertical ? values[(j + 1) * stride + i] : values[j * stride + i + 1];
|
|
30
|
+
if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 === b < 0) return null;
|
|
31
|
+
const key = (vertical ? edgeCount : 0) + j * stride + i;
|
|
32
|
+
if (!points.has(key)) {
|
|
33
|
+
const t = a / (a - b);
|
|
34
|
+
points.set(key, vertical ? { x: x0 + i * dx, y: y0 + (j + t) * dy } : { x: x0 + (i + t) * dx, y: y0 + j * dy });
|
|
35
|
+
}
|
|
36
|
+
return key;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const next = new Map<number, number[]>();
|
|
40
|
+
const join = (a: number | null, b: number | null): void => {
|
|
41
|
+
if (a === null || b === null) return;
|
|
42
|
+
(next.get(a) ?? next.set(a, []).get(a)!).push(b);
|
|
43
|
+
(next.get(b) ?? next.set(b, []).get(b)!).push(a);
|
|
44
|
+
};
|
|
45
|
+
for (let j = 0; j < rows; j++) {
|
|
46
|
+
for (let i = 0; i < columns; i++) {
|
|
47
|
+
const bottom = crossing(i, j, false);
|
|
48
|
+
const top = crossing(i, j + 1, false);
|
|
49
|
+
const left = crossing(i, j, true);
|
|
50
|
+
const right = crossing(i + 1, j, true);
|
|
51
|
+
const found = [bottom, right, top, left].filter((edge) => edge !== null);
|
|
52
|
+
if (found.length === 2) join(found[0], found[1]);
|
|
53
|
+
else if (found.length === 4) {
|
|
54
|
+
// A saddle: the centre's sign says which pairs of edges the curve joins.
|
|
55
|
+
const centre = f(x0 + (i + 0.5) * dx, y0 + (j + 0.5) * dy);
|
|
56
|
+
const corner = values[j * stride + i];
|
|
57
|
+
if (centre < 0 === corner < 0) {
|
|
58
|
+
join(bottom, right);
|
|
59
|
+
join(top, left);
|
|
60
|
+
} else {
|
|
61
|
+
join(bottom, left);
|
|
62
|
+
join(top, right);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const runs: Vector2[][] = [];
|
|
69
|
+
const used = new Set<number>();
|
|
70
|
+
const walk = (from: number): Vector2[] => {
|
|
71
|
+
const run: Vector2[] = [points.get(from)!];
|
|
72
|
+
used.add(from);
|
|
73
|
+
let at = from;
|
|
74
|
+
for (;;) {
|
|
75
|
+
const onward = (next.get(at) ?? []).find((key) => !used.has(key));
|
|
76
|
+
if (onward === undefined) break;
|
|
77
|
+
used.add(onward);
|
|
78
|
+
run.push(points.get(onward)!);
|
|
79
|
+
at = onward;
|
|
80
|
+
}
|
|
81
|
+
return run;
|
|
82
|
+
};
|
|
83
|
+
// Open runs first, from their ends, so each is one run and not two halves.
|
|
84
|
+
for (const [key, links] of next) {
|
|
85
|
+
if (links.length === 1 && !used.has(key)) runs.push(walk(key));
|
|
86
|
+
}
|
|
87
|
+
for (const key of next.keys()) {
|
|
88
|
+
if (used.has(key)) continue;
|
|
89
|
+
const run = walk(key);
|
|
90
|
+
run.push(run[0]);
|
|
91
|
+
runs.push(run);
|
|
92
|
+
}
|
|
93
|
+
return runs.filter((run) => run.length > 1);
|
|
94
|
+
}
|