@motionscript/math 0.1.0-alpha.5 → 0.1.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +39 -33
- package/dist/annotate/angle-mark.d.ts +52 -27
- package/dist/annotate/angle-mark.d.ts.map +1 -1
- package/dist/annotate/angle-mark.js +80 -115
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +59 -25
- package/dist/annotate/curly-brace.d.ts.map +1 -1
- package/dist/annotate/curly-brace.js +85 -100
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +43 -9
- package/dist/annotate/emphasis-mark.d.ts.map +1 -1
- package/dist/annotate/emphasis-mark.js +33 -34
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
- package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
- package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +2 -2
- package/dist/engine.d.ts +26 -19
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +17 -14
- package/dist/engine.js.map +1 -1
- package/dist/kit/component.d.ts +51 -0
- package/dist/kit/component.d.ts.map +1 -0
- package/dist/kit/component.js +30 -0
- package/dist/kit/component.js.map +1 -0
- package/dist/kit/curve.d.ts +22 -11
- package/dist/kit/curve.d.ts.map +1 -1
- package/dist/kit/curve.js +25 -18
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/field-mark.d.ts +58 -35
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js +100 -120
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/follow.d.ts +67 -29
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js +114 -110
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.d.ts +3 -1
- package/dist/kit/furniture.d.ts.map +1 -1
- package/dist/kit/furniture.js +5 -0
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/graph.d.ts +46 -0
- package/dist/kit/graph.d.ts.map +1 -0
- package/dist/kit/graph.js +90 -0
- package/dist/kit/graph.js.map +1 -0
- package/dist/kit/mark.d.ts +64 -39
- package/dist/kit/mark.d.ts.map +1 -1
- package/dist/kit/mark.js +80 -96
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/place.d.ts +10 -8
- package/dist/kit/place.d.ts.map +1 -1
- package/dist/kit/place.js +28 -30
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/system.d.ts +27 -29
- package/dist/kit/system.d.ts.map +1 -1
- package/dist/kit/system.js +62 -58
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/withdrawn.d.ts +3 -3
- package/dist/kit/withdrawn.js +3 -3
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.d.ts +24 -18
- package/dist/marks/curve-area.d.ts.map +1 -1
- package/dist/marks/curve-area.js +50 -73
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.d.ts +28 -10
- package/dist/marks/drop-lines.d.ts.map +1 -1
- package/dist/marks/drop-lines.js +37 -52
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +37 -20
- package/dist/marks/plot-point.d.ts.map +1 -1
- package/dist/marks/plot-point.js +65 -91
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +30 -26
- package/dist/marks/riemann-sum.d.ts.map +1 -1
- package/dist/marks/riemann-sum.js +57 -85
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +38 -14
- package/dist/marks/tangent-line.d.ts.map +1 -1
- package/dist/marks/tangent-line.js +73 -87
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +30 -20
- package/dist/marks/vector-arrow.d.ts.map +1 -1
- package/dist/marks/vector-arrow.js +58 -78
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.d.ts +13 -5
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +0 -6
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +107 -43
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js +146 -177
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.d.ts +34 -19
- package/dist/plots/points-plot.d.ts.map +1 -1
- package/dist/plots/points-plot.js +31 -58
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.d.ts +38 -19
- package/dist/plots/stream-lines.d.ts.map +1 -1
- package/dist/plots/stream-lines.js +60 -79
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.d.ts +34 -15
- package/dist/plots/traced-path.d.ts.map +1 -1
- package/dist/plots/traced-path.js +33 -48
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.d.ts +31 -11
- package/dist/plots/vector-field.d.ts.map +1 -1
- package/dist/plots/vector-field.js +50 -64
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/network-graph.d.ts +99 -49
- package/dist/structures/network-graph.d.ts.map +1 -1
- package/dist/structures/network-graph.js +168 -217
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.d.ts +69 -38
- package/dist/structures/sample-space.d.ts.map +1 -1
- package/dist/structures/sample-space.js +132 -170
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts +126 -37
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js +147 -183
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.d.ts +51 -28
- package/dist/systems/number-line.d.ts.map +1 -1
- package/dist/systems/number-line.js +82 -118
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.d.ts +92 -31
- package/dist/systems/number-plane.d.ts.map +1 -1
- package/dist/systems/number-plane.js +142 -149
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.d.ts +47 -26
- package/dist/systems/polar-plane.d.ts.map +1 -1
- package/dist/systems/polar-plane.js +107 -133
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/index.d.ts.map +1 -1
- package/dist/tex/index.js +1 -0
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/matrix.d.ts +56 -18
- package/dist/tex/matrix.d.ts.map +1 -1
- package/dist/tex/matrix.js +33 -50
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.d.ts +55 -201
- package/dist/tex/node.d.ts.map +1 -1
- package/dist/tex/node.js +23 -593
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/typeset.d.ts +133 -0
- package/dist/tex/typeset.d.ts.map +1 -0
- package/dist/tex/typeset.js +403 -0
- package/dist/tex/typeset.js.map +1 -0
- package/dist/three/axes3d.d.ts +140 -54
- package/dist/three/axes3d.d.ts.map +1 -1
- package/dist/three/axes3d.js +159 -204
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +70 -42
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js +97 -132
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.d.ts +17 -0
- package/dist/three/entries.d.ts.map +1 -0
- package/dist/three/entries.js +15 -0
- package/dist/three/entries.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +57 -12
- package/dist/three/plot-point3d.d.ts.map +1 -1
- package/dist/three/plot-point3d.js +27 -53
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.d.ts +17 -2
- package/dist/three/space.d.ts.map +1 -1
- package/dist/three/space.js +19 -8
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +131 -49
- package/dist/three/surface-plot3d.d.ts.map +1 -1
- package/dist/three/surface-plot3d.js +227 -261
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +58 -16
- package/dist/three/vector-arrow3d.d.ts.map +1 -1
- package/dist/three/vector-arrow3d.js +48 -67
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/src/annotate/angle-mark.ts +95 -105
- package/src/annotate/curly-brace.ts +107 -85
- package/src/annotate/emphasis-mark.ts +55 -26
- package/src/engine.ts +35 -21
- package/src/kit/component.ts +57 -0
- package/src/kit/curve.ts +38 -20
- package/src/kit/field-mark.ts +124 -108
- package/src/kit/follow.ts +146 -120
- package/src/kit/furniture.ts +6 -1
- package/src/kit/graph.ts +116 -0
- package/src/kit/mark.ts +135 -114
- package/src/kit/place.ts +32 -56
- package/src/kit/system.ts +77 -71
- package/src/kit/withdrawn.ts +3 -3
- package/src/marks/curve-area.ts +65 -65
- package/src/marks/drop-lines.ts +50 -42
- package/src/marks/plot-point.ts +77 -82
- package/src/marks/riemann-sum.ts +73 -72
- package/src/marks/tangent-line.ts +87 -71
- package/src/marks/vector-arrow.ts +73 -73
- package/src/nodes.ts +41 -5
- package/src/plots/function-plot.ts +155 -171
- package/src/plots/points-plot.ts +46 -50
- package/src/plots/stream-lines.ts +75 -66
- package/src/plots/traced-path.ts +51 -33
- package/src/plots/vector-field.ts +68 -49
- package/src/structures/network-graph.ts +168 -185
- package/src/structures/sample-space.ts +149 -148
- package/src/systems/axes2d.ts +165 -156
- package/src/systems/number-line.ts +91 -92
- package/src/systems/number-plane.ts +149 -120
- package/src/systems/polar-plane.ts +117 -116
- package/src/tex/index.ts +2 -1
- package/src/tex/matrix.ts +62 -40
- package/src/tex/node.ts +54 -621
- package/src/tex/typeset.ts +461 -0
- package/src/three/axes3d.ts +182 -161
- package/src/three/curve-plot3d.ts +107 -110
- package/src/three/entries.ts +42 -0
- package/src/three/plot-point3d.ts +42 -45
- package/src/three/space.ts +34 -9
- package/src/three/surface-plot3d.ts +312 -323
- package/src/three/vector-arrow3d.ts +65 -54
- package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
- package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
- package/src/kit/placement.ts +0 -8
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type Command,
|
|
8
|
-
type CommandArgs,
|
|
9
|
-
type NodeConfig,
|
|
2
|
+
component2d,
|
|
3
|
+
provides,
|
|
4
|
+
Types,
|
|
5
|
+
type EntryOptions,
|
|
6
|
+
type NodeOf,
|
|
10
7
|
type PaintProps,
|
|
11
|
-
type Stroke,
|
|
12
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,
|
|
13
16
|
} from "@motionscript/sdk";
|
|
17
|
+
import { createBoundedCache } from "../kit/cache";
|
|
18
|
+
import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
14
19
|
import { traceContour } from "../kit/contour";
|
|
15
20
|
import type { CoordinateMap, Interval } from "../kit/coords";
|
|
16
|
-
import type { Curve } from "../kit/curve";
|
|
17
21
|
import { equationParams, parseEquation, type Equation } from "../kit/equation";
|
|
18
22
|
import type { ExpressionScope } from "../kit/expression";
|
|
19
23
|
import { PLOT_COLOR } from "../kit/furniture";
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
24
|
+
import { PLOT, plotPlane, SYSTEM, SYSTEM_ENTRY } from "../kit/graph";
|
|
25
|
+
import { drawMark, inkCentre, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
26
|
+
import { openPlane, placeOnMap } from "../kit/place";
|
|
22
27
|
import { sampleCurve, type CurveProjection } from "../kit/sampler";
|
|
23
28
|
import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
|
|
24
|
-
import { SYSTEMS } from "../kit/placement";
|
|
25
|
-
import { FILL_PAINT } from "../kit/withdrawn";
|
|
26
29
|
|
|
27
30
|
export interface FunctionPlotProps extends PaintProps {
|
|
28
31
|
expression?: string | PlotSource;
|
|
@@ -32,189 +35,170 @@ export interface FunctionPlotProps extends PaintProps {
|
|
|
32
35
|
|
|
33
36
|
const TAU = Math.PI * 2;
|
|
34
37
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*/
|
|
40
|
-
@node({
|
|
41
|
-
key: "functionPlot",
|
|
42
|
-
within: SYSTEMS,
|
|
43
|
-
closed: true,
|
|
44
|
-
parentKey: "shape",
|
|
45
|
-
forkable: true,
|
|
46
|
-
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: true },
|
|
47
|
-
seed: { expression: "sin(x)" },
|
|
48
|
-
})
|
|
49
|
-
@withdraw(...FILL_PAINT)
|
|
50
|
-
export class FunctionPlot<P extends FunctionPlotProps = FunctionPlotProps> extends Mark<P> implements Curve {
|
|
51
|
-
@property({ default: "", mapper: readSource, tween: lerpSource, kind: "equation" })
|
|
52
|
-
declare expression: PlotSource;
|
|
53
|
-
|
|
38
|
+
/** A plot's entries: what a traced path is built on too. It is only a line, so it has no fill. */
|
|
39
|
+
export const PLOT_STATE = {
|
|
40
|
+
...omit(PAINT, "fill"),
|
|
41
|
+
expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "equation" }),
|
|
54
42
|
/**
|
|
55
43
|
* The span of the free variable that is drawn. Left out, a function runs the
|
|
56
44
|
* width of its system, a polar curve one turn and a parametric one from 0 to 1.
|
|
57
45
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
46
|
+
domain: Types.custom<Interval | null, Interval | null, EntryOptions & { readonly defaultMin: number; readonly defaultMax: number }>(null, { lerp: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 }),
|
|
60
47
|
/**
|
|
61
48
|
* The equation's named values: any name in it that is not a variable, a
|
|
62
49
|
* function or a constant. One left unset is 1, and a formula can bind each
|
|
63
50
|
* to a variable.
|
|
64
51
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
declare stroke: Stroke;
|
|
69
|
-
|
|
70
|
-
constructor(props?: NodeConfig<FunctionPlot<P>, P>) {
|
|
71
|
-
super((props ?? {}) as NodeConfig<FunctionPlot<P>, P>);
|
|
72
|
-
}
|
|
52
|
+
params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
|
|
53
|
+
graph: SYSTEM_ENTRY,
|
|
54
|
+
};
|
|
73
55
|
|
|
56
|
+
/** A plot's own commands, beside a line's paints. */
|
|
57
|
+
export const PLOT_COMMANDS = {
|
|
58
|
+
...omit(PAINT_COMMANDS, "fillTo"),
|
|
74
59
|
/** Change the equation, the curve bending from the one into the other. */
|
|
75
|
-
|
|
76
|
-
morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<P> {
|
|
77
|
-
return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
|
|
78
|
-
}
|
|
79
|
-
|
|
60
|
+
morphTo: { args: ["expression"] as const },
|
|
80
61
|
/** Animate the equation's named values. */
|
|
81
|
-
|
|
82
|
-
paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<P> {
|
|
83
|
-
return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
|
|
84
|
-
}
|
|
85
|
-
|
|
62
|
+
paramsTo: { args: ["params"] as const },
|
|
86
63
|
/** Animate the span that is drawn. */
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
|
|
90
|
-
}
|
|
64
|
+
domainTo: { args: ["domain"] as const },
|
|
65
|
+
};
|
|
91
66
|
|
|
92
|
-
|
|
93
|
-
* The curve's height at graph `x`, or `NaN` where it has none or is not a
|
|
94
|
-
* function of `x`. What a point, a tangent or an area riding this plot reads.
|
|
95
|
-
*/
|
|
96
|
-
valueAt(x: number): number {
|
|
97
|
-
const { source, blend } = this.expression;
|
|
98
|
-
const shift = this.shift();
|
|
99
|
-
const value = this._heightOf(source, x - shift.x) + shift.y;
|
|
100
|
-
if (!blend) return value;
|
|
101
|
-
const from = this._heightOf(blend.from, x - shift.x) + shift.y;
|
|
102
|
-
return from + (value - from) * blend.t;
|
|
103
|
-
}
|
|
67
|
+
export const PLOT_STROKE = { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } as const;
|
|
104
68
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
protected trace(map: CoordinateMap): Vector2[][] {
|
|
111
|
-
const { area } = map;
|
|
112
|
-
if (!Number.isFinite(area.maxX - area.minX)) return [];
|
|
113
|
-
const { source, blend } = this.expression;
|
|
114
|
-
const shift = this.shift();
|
|
115
|
-
const key = [source, blend?.from, blend?.t, map.revision, this.span(), JSON.stringify(this.params), shift.x, shift.y].join("|");
|
|
116
|
-
if (key === this._key) return this._runs;
|
|
117
|
-
this._key = key;
|
|
118
|
-
|
|
119
|
-
// A curve with no parameter to walk cannot bend into another: it changes over half way.
|
|
120
|
-
const shown = blend && blend.t < 0.5 ? blend.from : source;
|
|
121
|
-
if (this._equation(source)?.kind === "implicit" || (blend && this._equation(blend.from)?.kind === "implicit")) {
|
|
122
|
-
this._runs = this._contour(shown, map, shift);
|
|
123
|
-
return this._runs;
|
|
124
|
-
}
|
|
69
|
+
/** What a plot's curve is drawn from. */
|
|
70
|
+
export interface PlotValues {
|
|
71
|
+
readonly expression: PlotSource;
|
|
72
|
+
readonly params: ExpressionScope;
|
|
73
|
+
}
|
|
125
74
|
|
|
126
|
-
|
|
75
|
+
/**
|
|
76
|
+
* The curve of an equation, drawn in the coordinate system it is a child of.
|
|
77
|
+
* How the equation is written says what it is: see `Equation`. A mark put
|
|
78
|
+
* inside it reads it: the area under it, a tangent, a point that rides it.
|
|
79
|
+
*/
|
|
80
|
+
export const FunctionPlot = traced(component2d({
|
|
81
|
+
key: "functionPlot",
|
|
82
|
+
state: PLOT_STATE,
|
|
83
|
+
defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
|
|
84
|
+
commands: PLOT_COMMANDS,
|
|
85
|
+
// The marks inside it ride it, on its system's map.
|
|
86
|
+
provide: (s, inherited) => [provides(PLOT, plotPlane(s, inherited(SYSTEM)))],
|
|
87
|
+
measure: (_measurer, s) => inkSize(tracePlot(s, reading(s), s.domain)),
|
|
88
|
+
layout: (box, s, children) => {
|
|
89
|
+
const read = reading(s);
|
|
90
|
+
return placeOnMap(children, read.map, inkCentre(tracePlot(s, read, s.domain)), box, plotPlane(s, s.graph));
|
|
91
|
+
},
|
|
92
|
+
plane: (_box, s) => {
|
|
93
|
+
const read = reading(s);
|
|
94
|
+
return openPlane(read.map, inkCentre(tracePlot(s, read, s.domain)));
|
|
95
|
+
},
|
|
96
|
+
render: (ctx, s) => drawMark(ctx, s, tracePlot(s, reading(s), s.domain)),
|
|
97
|
+
}), (s: PlotValues & { readonly domain: Interval | null }, read: MarkReading) => tracePlot(s, read, s.domain));
|
|
98
|
+
|
|
99
|
+
export type FunctionPlot = NodeOf<typeof FunctionPlot>;
|
|
100
|
+
|
|
101
|
+
const traces = createBoundedCache<Vector2[][]>(256);
|
|
102
|
+
|
|
103
|
+
/** The runs a plot of `s` draws through `read`, over `span` of its free variable (all of it for `null`). */
|
|
104
|
+
export function tracePlot(s: PlotValues, read: MarkReading, span: Interval | null): Vector2[][] {
|
|
105
|
+
const { map, shift } = read;
|
|
106
|
+
const { area } = map;
|
|
107
|
+
if (!Number.isFinite(area.maxX - area.minX)) return [];
|
|
108
|
+
const { source, blend } = s.expression;
|
|
109
|
+
const key = [source, blend?.from, blend?.t, map.revision, span, JSON.stringify(s.params), shift.x, shift.y].join("|");
|
|
110
|
+
const known = traces.get(key);
|
|
111
|
+
if (known) return known;
|
|
112
|
+
|
|
113
|
+
let runs: Vector2[][];
|
|
114
|
+
// A curve with no parameter to walk cannot bend into another: it changes over half way.
|
|
115
|
+
const shown = blend && blend.t < 0.5 ? blend.from : source;
|
|
116
|
+
if (equationOf(source)?.kind === "implicit" || (blend && equationOf(blend.from)?.kind === "implicit")) {
|
|
117
|
+
runs = contour(shown, s.params, map, shift);
|
|
118
|
+
} else {
|
|
119
|
+
let curve = projectionOf(source, s.params, span, map, shift);
|
|
127
120
|
if (curve && blend) {
|
|
128
|
-
const from =
|
|
121
|
+
const from = projectionOf(blend.from, s.params, span, map, shift);
|
|
129
122
|
if (from) curve = between(from, curve, blend.t);
|
|
130
123
|
}
|
|
131
124
|
const width = area.maxX - area.minX;
|
|
132
|
-
|
|
125
|
+
runs = curve
|
|
133
126
|
? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })
|
|
134
127
|
: [];
|
|
135
|
-
return this._runs;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
private _key = "";
|
|
139
|
-
private _runs: Vector2[][] = [];
|
|
140
|
-
|
|
141
|
-
private _equation(source: string): Equation | null {
|
|
142
|
-
const equation = parseEquation(source, equationParams(source));
|
|
143
|
-
return equation instanceof Error ? null : equation;
|
|
144
128
|
}
|
|
129
|
+
traces.set(key, runs);
|
|
130
|
+
return runs;
|
|
131
|
+
}
|
|
145
132
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
133
|
+
function equationOf(source: string): Equation | null {
|
|
134
|
+
const equation = parseEquation(source, equationParams(source));
|
|
135
|
+
return equation instanceof Error ? null : equation;
|
|
136
|
+
}
|
|
151
137
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
138
|
+
/** Where an implicit `source` holds, over what the system shows, as pixel runs. */
|
|
139
|
+
function contour(source: string, params: ExpressionScope, map: CoordinateMap, shift: Vector2): Vector2[][] {
|
|
140
|
+
const equation = equationOf(source);
|
|
141
|
+
if (equation?.kind !== "implicit") return [];
|
|
142
|
+
const scope = scopeFor(equationParams(source), params);
|
|
143
|
+
const { area } = map;
|
|
144
|
+
const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
|
|
145
|
+
const { f } = equation;
|
|
146
|
+
const runs = traceContour(
|
|
147
|
+
(x, y) => f(x - shift.x, y - shift.y, scope),
|
|
148
|
+
map.xRange,
|
|
149
|
+
map.yRange,
|
|
150
|
+
cells(area.maxX - area.minX),
|
|
151
|
+
cells(area.maxY - area.minY),
|
|
152
|
+
);
|
|
153
|
+
return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
|
|
154
|
+
}
|
|
169
155
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
};
|
|
217
|
-
}
|
|
156
|
+
/** `source` as a pixel curve over a parameter running 0 to 1. */
|
|
157
|
+
function projectionOf(source: string, params: ExpressionScope, domain: Interval | null, map: CoordinateMap, shift: Vector2): CurveProjection | null {
|
|
158
|
+
const equation = equationOf(source);
|
|
159
|
+
if (!equation || equation.kind === "implicit") return null;
|
|
160
|
+
const scope = scopeFor(equationParams(source), params);
|
|
161
|
+
switch (equation.kind) {
|
|
162
|
+
case "function": {
|
|
163
|
+
const horizontal = equation.of === "x";
|
|
164
|
+
const [lo, hi] = horizontal ? map.xRange : map.yRange;
|
|
165
|
+
const offset = horizontal ? shift.x : shift.y;
|
|
166
|
+
// Only what the system shows is sampled, however wide the domain.
|
|
167
|
+
const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
|
|
168
|
+
const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
|
|
169
|
+
if (!(b > a)) return null;
|
|
170
|
+
const { f } = equation;
|
|
171
|
+
return (u, out) => {
|
|
172
|
+
const v = a + (b - a) * u;
|
|
173
|
+
const w = f(v, 0, scope);
|
|
174
|
+
if (!Number.isFinite(w)) return false;
|
|
175
|
+
if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);
|
|
176
|
+
else map.toPoint(w + shift.x, v + shift.y, out);
|
|
177
|
+
return true;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
case "polar": {
|
|
181
|
+
const [a, b] = domain ?? [0, TAU];
|
|
182
|
+
const { f } = equation;
|
|
183
|
+
return (u, out) => {
|
|
184
|
+
const theta = a + (b - a) * u;
|
|
185
|
+
const r = f(theta, 0, scope);
|
|
186
|
+
if (!Number.isFinite(r)) return false;
|
|
187
|
+
map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
|
|
188
|
+
return true;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
case "parametric": {
|
|
192
|
+
const [a, b] = domain ?? [0, 1];
|
|
193
|
+
const { x, y } = equation;
|
|
194
|
+
return (u, out) => {
|
|
195
|
+
const t = a + (b - a) * u;
|
|
196
|
+
const gx = x(t, 0, scope);
|
|
197
|
+
const gy = y(t, 0, scope);
|
|
198
|
+
if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;
|
|
199
|
+
map.toPoint(gx + shift.x, gy + shift.y, out);
|
|
200
|
+
return true;
|
|
201
|
+
};
|
|
218
202
|
}
|
|
219
203
|
}
|
|
220
204
|
}
|
package/src/plots/points-plot.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
strokeProperty,
|
|
6
|
-
type Command,
|
|
7
|
-
type CommandArgs,
|
|
8
|
-
type NodeConfig,
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
9
5
|
type PaintProps,
|
|
10
|
-
type Stroke,
|
|
11
6
|
type Vector2,
|
|
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 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,
|
|
12
16
|
} from "@motionscript/sdk";
|
|
13
|
-
import
|
|
14
|
-
import { stated } from "../kit/geometry";
|
|
15
|
-
import { Mark } from "../kit/mark";
|
|
17
|
+
import { PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
16
18
|
import { PLOT_COLOR } from "../kit/furniture";
|
|
17
|
-
import {
|
|
19
|
+
import { PLANE_ENTRY } from "../kit/graph";
|
|
20
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
18
21
|
|
|
19
22
|
export interface PointsPlotProps extends PaintProps {
|
|
20
23
|
points?: Vector2[];
|
|
@@ -38,50 +41,43 @@ function lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {
|
|
|
38
41
|
/** Points on the curve between each pair of a smooth line's points. */
|
|
39
42
|
const SMOOTH_STEPS = 16;
|
|
40
43
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
within: SYSTEMS,
|
|
45
|
-
parentKey: "shape",
|
|
46
|
-
forkable: true,
|
|
47
|
-
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
|
|
48
|
-
seed: { points: [{ x: -2, y: -1 }, { x: 0, y: 1 }, { x: 2, y: 0 }] },
|
|
49
|
-
})
|
|
50
|
-
export class PointsPlot extends Mark<PointsPlotProps> {
|
|
51
|
-
@property({ default: [], tween: lerpPoints }) declare points: Vector2[];
|
|
44
|
+
const state = {
|
|
45
|
+
...PAINT,
|
|
46
|
+
points: Types.custom<Vector2[]>([], { lerp: lerpPoints }),
|
|
52
47
|
/** Join the last point to the first. */
|
|
53
|
-
|
|
48
|
+
closed: Types.boolean(false, { animatable: false }),
|
|
54
49
|
/** Curve through the points instead of turning a corner at each. */
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
declare stroke: Stroke;
|
|
59
|
-
|
|
60
|
-
constructor(props?: NodeConfig<PointsPlot, PointsPlotProps>) {
|
|
61
|
-
super(props ?? {});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Move the points to new places; extra ones grow from the line's end. */
|
|
65
|
-
@command({ args: ["points"] })
|
|
66
|
-
pointsTo(args: CommandArgs<{ points: Vector2[] }> & { duration: number }): Command<PointsPlotProps> {
|
|
67
|
-
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
68
|
-
}
|
|
50
|
+
smooth: Types.boolean(false, { animatable: false }),
|
|
51
|
+
graph: PLANE_ENTRY,
|
|
52
|
+
};
|
|
69
53
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
54
|
+
interface PointsValues {
|
|
55
|
+
readonly points: Vector2[];
|
|
56
|
+
readonly closed: boolean;
|
|
57
|
+
readonly smooth: boolean;
|
|
58
|
+
}
|
|
73
59
|
|
|
60
|
+
/** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
|
|
61
|
+
export const PointsPlot = traced(component2d({
|
|
62
|
+
key: "pointsPlot",
|
|
63
|
+
state,
|
|
64
|
+
defaults: { stroke: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
65
|
+
commands: {
|
|
66
|
+
...PAINT_COMMANDS,
|
|
67
|
+
/** Move the points to new places; extra ones grow from the line's end. */
|
|
68
|
+
pointsTo: { args: ["points"] },
|
|
69
|
+
},
|
|
70
|
+
measure: (_measurer, s) => inkSize(tracePoints(s, reading(s))),
|
|
74
71
|
// An open line has no inside, so its fill waits until it is closed.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
72
|
+
render: (ctx, s) => drawMark(ctx, s, tracePoints(s, reading(s)), { closed: s.closed, fills: s.closed }),
|
|
73
|
+
}), tracePoints);
|
|
78
74
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
export type PointsPlot = NodeOf<typeof PointsPlot>;
|
|
76
|
+
|
|
77
|
+
function tracePoints(s: PointsValues, { map, shift }: MarkReading): Vector2[][] {
|
|
78
|
+
const pixels = s.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
|
|
79
|
+
if (pixels.length < 2) return [];
|
|
80
|
+
return [s.smooth && pixels.length > 2 ? spline(pixels, s.closed) : pixels];
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
// Catmull–Rom, so the curve passes through every point it was given.
|