@motionscript/math 0.1.0-alpha.5 → 0.1.0-alpha.7
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 +28 -0
- package/README.md +39 -33
- package/dist/annotate/angle-mark.d.ts +77 -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 +94 -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 +69 -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 +56 -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 +37 -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 +45 -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 +56 -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 +47 -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 +58 -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 +48 -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 +135 -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 +55 -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 +60 -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 +54 -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 +48 -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 +140 -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 +90 -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 +159 -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 +80 -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 +140 -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 +76 -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 +88 -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 +84 -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 +214 -54
- package/dist/three/axes3d.d.ts.map +1 -1
- package/dist/three/axes3d.js +163 -204
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +108 -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 +85 -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 +176 -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 +87 -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 +187 -160
- package/src/three/curve-plot3d.ts +107 -110
- package/src/three/entries.ts +42 -0
- package/src/three/plot-point3d.ts +42 -45
- package/src/three/space.ts +34 -9
- package/src/three/surface-plot3d.ts +312 -323
- package/src/three/vector-arrow3d.ts +65 -54
- package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
- package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
- package/src/kit/placement.ts +0 -8
package/src/marks/curve-area.ts
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
withdraw,
|
|
7
|
-
type Command,
|
|
8
|
-
type CommandArgs,
|
|
9
|
-
type Fill,
|
|
10
|
-
type NodeConfig,
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type NodeView,
|
|
11
6
|
type PaintProps,
|
|
12
7
|
type Vector2,
|
|
8
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
9
|
+
type Fill as _Fill,
|
|
10
|
+
type FillResolved as _FillResolved,
|
|
11
|
+
type Node2D as _Node2D,
|
|
12
|
+
type Node2DProps as _Node2DProps,
|
|
13
|
+
type Shadow as _Shadow,
|
|
14
|
+
type ShadowResolved as _ShadowResolved,
|
|
13
15
|
} from "@motionscript/sdk";
|
|
14
|
-
import
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { curveNamed, curveOf } from "../kit/curve";
|
|
16
|
+
import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
17
|
+
import type { Interval } from "../kit/coords";
|
|
18
|
+
import { curveValue } from "../kit/curve";
|
|
18
19
|
import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
20
|
+
import { clamp } from "../kit/geometry";
|
|
21
|
+
import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
|
|
22
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
21
23
|
|
|
22
24
|
export interface CurveAreaProps extends PaintProps {
|
|
23
25
|
on?: string;
|
|
@@ -25,65 +27,63 @@ export interface CurveAreaProps extends PaintProps {
|
|
|
25
27
|
domain?: Interval;
|
|
26
28
|
}
|
|
27
29
|
|
|
30
|
+
const state = {
|
|
31
|
+
// An area is only its fill, and has no outline to trim.
|
|
32
|
+
...omit(PAINT, "stroke", "start", "end"),
|
|
33
|
+
/** The plot that bounds the area, by id. */
|
|
34
|
+
on: PLOT_REF,
|
|
35
|
+
/** A second plot to fill down to, in place of the x axis. */
|
|
36
|
+
under: PLOT_REF,
|
|
37
|
+
domain: Types.custom<Interval>([0, 1], { resolve: readInterval, lerp: lerpInterval }),
|
|
38
|
+
graph: PLANE_ENTRY,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
interface AreaValues {
|
|
42
|
+
readonly on: NodeView | null;
|
|
43
|
+
readonly under: NodeView | null;
|
|
44
|
+
readonly domain: Interval;
|
|
45
|
+
}
|
|
46
|
+
|
|
28
47
|
/**
|
|
29
48
|
* The region between a plot and the x axis, or between two plots, over a span
|
|
30
49
|
* of x. It sits beside the plots it reads rather than inside one: it may read
|
|
31
50
|
* two, and it has a life of its own on the timeline, swept open or kept while
|
|
32
51
|
* a curve changes.
|
|
33
52
|
*/
|
|
34
|
-
|
|
53
|
+
export const CurveArea = traced(component2d({
|
|
35
54
|
key: "curveArea",
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
/** A second plot to fill down to, in place of the x axis. */
|
|
47
|
-
@property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare under: string;
|
|
48
|
-
@property({ default: [0, 1], mapper: readInterval, tween: lerpInterval })
|
|
49
|
-
declare domain: Interval;
|
|
55
|
+
state,
|
|
56
|
+
defaults: { fill: { type: "solid", color: PLOT_COLOR, opacity: 0.35 }, width: "hug", height: "hug" },
|
|
57
|
+
commands: {
|
|
58
|
+
...omit(PAINT_COMMANDS, "strokeTo", "progressTo", "flash"),
|
|
59
|
+
/** Animate the span of x that is filled: sweep an area open, or slide it along. */
|
|
60
|
+
domainTo: { args: ["domain"] },
|
|
61
|
+
},
|
|
62
|
+
measure: (_measurer, s) => inkSize(traceArea(s, reading(s))),
|
|
63
|
+
render: (ctx, s) => drawMark(ctx, s, traceArea(s, reading(s)), { closed: true }),
|
|
64
|
+
}), traceArea);
|
|
50
65
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
constructor(props?: NodeConfig<CurveArea, CurveAreaProps>) {
|
|
54
|
-
super(props ?? {});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Animate the span of x that is filled: sweep an area open, or slide it along. */
|
|
58
|
-
@command({ args: ["domain"] })
|
|
59
|
-
domainTo(args: CommandArgs<{ domain: Interval }> & { duration: number }): Command<CurveAreaProps> {
|
|
60
|
-
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
protected override isClosed(): boolean {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
+
export type CurveArea = NodeOf<typeof CurveArea>;
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
68
|
+
function traceArea(s: AreaValues, { map, plane }: MarkReading): Vector2[][] {
|
|
69
|
+
const top = curveFor(plane, s.on);
|
|
70
|
+
if (!top) return [];
|
|
71
|
+
const floor = curveFor(plane, s.under, false);
|
|
72
|
+
const [yMin, yMax] = map.yRange;
|
|
73
|
+
const a = Math.max(s.domain[0], map.xRange[0]);
|
|
74
|
+
const b = Math.min(s.domain[1], map.xRange[1]);
|
|
75
|
+
if (!(b > a)) return [];
|
|
75
76
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
return [[...upper, ...lower.reverse()]];
|
|
77
|
+
const width = Math.abs(map.toPoint(b, 0).x - map.toPoint(a, 0).x);
|
|
78
|
+
const steps = Math.max(8, Math.min(600, Math.round(width / 2)));
|
|
79
|
+
// Held inside the system's range, and at the axis where a curve has no value.
|
|
80
|
+
const height = (value: number) => (Number.isFinite(value) ? clamp(value, yMin, yMax) : clamp(0, yMin, yMax));
|
|
81
|
+
const upper: Vector2[] = [];
|
|
82
|
+
const lower: Vector2[] = [];
|
|
83
|
+
for (let i = 0; i <= steps; i++) {
|
|
84
|
+
const x = a + ((b - a) * i) / steps;
|
|
85
|
+
upper.push(map.toPoint(x, height(curveValue(top, x))));
|
|
86
|
+
lower.push(map.toPoint(x, height(floor ? curveValue(floor, x) : 0)));
|
|
88
87
|
}
|
|
88
|
+
return [[...upper, ...lower.reverse()]];
|
|
89
89
|
}
|
package/src/marks/drop-lines.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
withdraw,
|
|
6
|
-
type NodeConfig,
|
|
2
|
+
component2d,
|
|
3
|
+
type NodeOf,
|
|
4
|
+
type NodeView,
|
|
7
5
|
type PaintProps,
|
|
8
|
-
type Stroke,
|
|
9
6
|
type Vector2,
|
|
7
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
+
type Node2D as _Node2D,
|
|
9
|
+
type Node2DProps as _Node2DProps,
|
|
10
|
+
type Shadow as _Shadow,
|
|
11
|
+
type ShadowResolved as _ShadowResolved,
|
|
12
|
+
type Stroke as _Stroke,
|
|
13
|
+
type StrokeResolved as _StrokeResolved,
|
|
10
14
|
} from "@motionscript/sdk";
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import { Mark } from "../kit/mark";
|
|
14
|
-
import { curveOf } from "../kit/curve";
|
|
15
|
+
import { listed, omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
16
|
+
import { curveValue } from "../kit/curve";
|
|
15
17
|
import { AXIS_COLOR } from "../kit/furniture";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
+
import { clamp } from "../kit/geometry";
|
|
19
|
+
import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
|
|
20
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
18
21
|
|
|
19
22
|
export type DropAxis = "x" | "y" | "both";
|
|
20
23
|
|
|
@@ -23,41 +26,46 @@ export interface DropLinesProps extends PaintProps {
|
|
|
23
26
|
axis?: DropAxis;
|
|
24
27
|
}
|
|
25
28
|
|
|
29
|
+
const state = {
|
|
30
|
+
...omit(PAINT, "fill"),
|
|
31
|
+
/** A plot to take the point's height from, by id. */
|
|
32
|
+
on: PLOT_REF,
|
|
33
|
+
/** Which axis to drop to: `x` draws the vertical line, `y` the horizontal one. */
|
|
34
|
+
axis: listed(["x", "y", "both"], "both", { animatable: false }),
|
|
35
|
+
graph: PLANE_ENTRY,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
interface DropValues {
|
|
39
|
+
readonly x: number;
|
|
40
|
+
readonly y: number;
|
|
41
|
+
readonly on: NodeView | null;
|
|
42
|
+
readonly axis: DropAxis;
|
|
43
|
+
}
|
|
44
|
+
|
|
26
45
|
/**
|
|
27
46
|
* Guide lines from a graph point to the axes, reading its coordinates off them.
|
|
28
47
|
* The point is this node's `x`/`y`, or the point of a plot above its `x`.
|
|
29
48
|
*/
|
|
30
|
-
|
|
49
|
+
export const DropLines = traced(component2d({
|
|
31
50
|
key: "dropLines",
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
export class DropLines extends Mark<DropLinesProps> {
|
|
39
|
-
/** A plot to take the point's height from, by id. */
|
|
40
|
-
@property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
|
|
41
|
-
/** Which axis to drop to: `x` draws the vertical line, `y` the horizontal one. */
|
|
42
|
-
@property({ default: "both", options: ["x", "y", "both"], animatable: false }) declare axis: DropAxis;
|
|
43
|
-
|
|
44
|
-
@strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center", dash: [8, 6] } })
|
|
45
|
-
declare stroke: Stroke;
|
|
51
|
+
state,
|
|
52
|
+
defaults: { stroke: { weight: 2, fill: AXIS_COLOR, align: "center", dash: [8, 6] }, width: "hug", height: "hug" },
|
|
53
|
+
commands: omit(PAINT_COMMANDS, "fillTo"),
|
|
54
|
+
measure: (_measurer, s) => inkSize(traceDrops(s, reading(s))),
|
|
55
|
+
render: (ctx, s) => drawMark(ctx, s, traceDrops(s, reading(s))),
|
|
56
|
+
}), traceDrops);
|
|
46
57
|
|
|
47
|
-
|
|
48
|
-
super(props ?? {});
|
|
49
|
-
}
|
|
58
|
+
export type DropLines = NodeOf<typeof DropLines>;
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
60
|
+
function traceDrops(s: DropValues, { map, plane }: MarkReading): Vector2[][] {
|
|
61
|
+
const x = s.x;
|
|
62
|
+
const plot = curveFor(plane, s.on);
|
|
63
|
+
const y = plot ? curveValue(plot, x) : s.y;
|
|
64
|
+
if (!Number.isFinite(y)) return [];
|
|
65
|
+
const at = map.toPoint(x, y);
|
|
66
|
+
const runs: Vector2[][] = [];
|
|
67
|
+
// Each line starts on its axis, so drawing it on grows it towards the point.
|
|
68
|
+
if (s.axis !== "y") runs.push([map.toPoint(x, clamp(0, map.yRange[0], map.yRange[1])), at]);
|
|
69
|
+
if (s.axis !== "x") runs.push([map.toPoint(clamp(0, map.xRange[0], map.xRange[1]), y), at]);
|
|
70
|
+
return runs;
|
|
63
71
|
}
|
package/src/marks/plot-point.ts
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
fillProperty,
|
|
2
|
+
component2d,
|
|
4
3
|
Graphics2D,
|
|
5
|
-
|
|
6
|
-
PaintNode,
|
|
7
|
-
property,
|
|
8
|
-
withdraw,
|
|
9
|
-
type AssetScope,
|
|
4
|
+
Types,
|
|
10
5
|
type Color,
|
|
11
|
-
type
|
|
12
|
-
type NodeConfig,
|
|
6
|
+
type NodeOf,
|
|
13
7
|
type NormalizedColor,
|
|
14
8
|
type PaintProps,
|
|
15
|
-
|
|
16
|
-
type
|
|
17
|
-
type
|
|
9
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
10
|
+
type Fill as _Fill,
|
|
11
|
+
type FillResolved as _FillResolved,
|
|
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
18
|
} from "@motionscript/sdk";
|
|
19
|
-
import
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
19
|
+
import { listed, omit, PAINT, PAINT_COMMANDS, painted } from "../kit/component";
|
|
20
|
+
import { curveValue } from "../kit/curve";
|
|
21
|
+
import { drawLabels, loadFormulas, type Label } from "../kit/furniture";
|
|
22
|
+
import { curveFor, PLANE_ENTRY, PLOT_REF, type GraphPlane } from "../kit/graph";
|
|
23
|
+
import { strokeShape } from "../kit/mark";
|
|
24
|
+
import { anchored } from "../kit/place";
|
|
24
25
|
|
|
25
26
|
export interface PlotPointProps extends PaintProps {
|
|
26
27
|
on?: string;
|
|
@@ -32,76 +33,70 @@ export interface PlotPointProps extends PaintProps {
|
|
|
32
33
|
fontFamily?: string;
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
const state = {
|
|
37
|
+
// A dot has no outline worth trimming.
|
|
38
|
+
...omit(PAINT, "start", "end"),
|
|
39
|
+
/** The plot this point rides, by id; empty for a point that sits at its own `x`/`y`. */
|
|
40
|
+
on: PLOT_REF,
|
|
41
|
+
radius: Types.number(7, { min: 0, unit: "px" }),
|
|
42
|
+
label: Types.text("", { animatable: false }),
|
|
43
|
+
labelSide: listed(["above", "below", "left", "right"], "above", { animatable: false }),
|
|
44
|
+
labelSize: Types.number(18, { min: 1, unit: "px" }),
|
|
45
|
+
labelColor: Types.color("#e5e7eb"),
|
|
46
|
+
fontFamily: Types.text("", { animatable: false }),
|
|
47
|
+
graph: PLANE_ENTRY,
|
|
48
|
+
/** The graph height the point is at: the curve's when it rides one. A formula reads it back as `[key.value]`. */
|
|
49
|
+
value: Types.number(0, { private: true }),
|
|
50
|
+
};
|
|
51
|
+
|
|
35
52
|
/**
|
|
36
53
|
* A dot at graph coordinates, with an optional label. Named a plot in `on`, it
|
|
37
54
|
* rides that curve: its `x` is where along it, and its height is the curve's.
|
|
38
55
|
*/
|
|
39
|
-
|
|
56
|
+
export const PlotPoint = anchored(component2d({
|
|
40
57
|
key: "plotPoint",
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return { width: this.radius * 2, height: this.radius * 2 };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
override declareAssets(assets: AssetScope): void {
|
|
80
|
-
super.declareAssets(assets);
|
|
81
|
-
if (this.label !== "" && this.fontFamily !== "") assets.font(this.fontFamily);
|
|
82
|
-
declareFormulas(assets, [this.label]);
|
|
83
|
-
}
|
|
58
|
+
state,
|
|
59
|
+
defaults: { fill: "#e5e7eb", width: "hug", height: "hug", value: (s) => heightOf(s.graph, s.on, s.x, s.y) },
|
|
60
|
+
load: (loader, s) => {
|
|
61
|
+
if (s.label !== "" && s.fontFamily !== "") loader.font(s.fontFamily);
|
|
62
|
+
loadFormulas(loader, [s.label]);
|
|
63
|
+
},
|
|
64
|
+
commands: omit(PAINT_COMMANDS, "progressTo", "flash"),
|
|
65
|
+
measure: (_measurer, s) => ({ width: s.radius * 2, height: s.radius * 2 }),
|
|
66
|
+
render: (ctx, s) => {
|
|
67
|
+
const size = s.radius * 2;
|
|
68
|
+
const shape = size > 0 ? () => new Graphics2D().ellipse({ x: 0, y: 0, width: size, height: size }) : null;
|
|
69
|
+
painted(
|
|
70
|
+
ctx,
|
|
71
|
+
() => {
|
|
72
|
+
if (shape) ctx.draw(shape().shadow(s.shadow).fill(s.fill));
|
|
73
|
+
if (s.label === "") return;
|
|
74
|
+
const gap = s.radius + s.labelSize * 0.45;
|
|
75
|
+
const side = s.labelSide;
|
|
76
|
+
const horizontal = side === "left" || side === "right";
|
|
77
|
+
const label: Label = {
|
|
78
|
+
text: s.label,
|
|
79
|
+
align: side === "left" ? "right" : side === "right" ? "left" : "center",
|
|
80
|
+
at: {
|
|
81
|
+
x: horizontal ? (side === "left" ? -1 : 1) * gap : 0,
|
|
82
|
+
y: horizontal ? 0 : (side === "below" ? -1 : 1) * (gap + s.labelSize / 2),
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
ctx.draw(drawLabels([label], { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
|
|
86
|
+
},
|
|
87
|
+
() => {
|
|
88
|
+
if (shape) strokeShape(ctx, shape, s);
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
}), (s: { readonly on: unknown }, plane) => (x, y) => ({ x, y: heightOf(plane, s.on, x, y) }));
|
|
84
93
|
|
|
85
|
-
|
|
86
|
-
const size = this.radius * 2;
|
|
87
|
-
return size > 0 ? new Graphics2D().ellipse({ x: 0, y: 0, width: size, height: size }) : null;
|
|
88
|
-
}
|
|
94
|
+
export type PlotPoint = NodeOf<typeof PlotPoint>;
|
|
89
95
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const label: Label = {
|
|
97
|
-
text: this.label,
|
|
98
|
-
align: side === "left" ? "right" : side === "right" ? "left" : "center",
|
|
99
|
-
at: {
|
|
100
|
-
x: horizontal ? (side === "left" ? -1 : 1) * gap : 0,
|
|
101
|
-
y: horizontal ? 0 : (side === "below" ? -1 : 1) * (gap + this.labelSize / 2),
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
const style = { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor };
|
|
105
|
-
for (const part of drawLabels([label], style)) ctx.draw(part);
|
|
106
|
-
}
|
|
96
|
+
/** The height a point at `x`/`y` sits at: the curve's, when it rides one that has a value there. */
|
|
97
|
+
function heightOf(plane: GraphPlane | null, on: unknown, x: number, y: number): number {
|
|
98
|
+
const curve = curveFor(plane, on);
|
|
99
|
+
const height = curve ? curveValue(curve, x) : NaN;
|
|
100
|
+
// Off the end of its curve a point stays where its own `y` puts it rather than vanishing.
|
|
101
|
+
return Number.isFinite(height) ? height : y;
|
|
107
102
|
}
|
package/src/marks/riemann-sum.ts
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
strokeProperty,
|
|
7
|
-
withdraw,
|
|
8
|
-
type Command,
|
|
9
|
-
type CommandArgs,
|
|
10
|
-
type Fill,
|
|
11
|
-
type NodeConfig,
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type NodeView,
|
|
12
6
|
type PaintProps,
|
|
13
|
-
type Stroke,
|
|
14
7
|
type Vector2,
|
|
8
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
9
|
+
type Fill as _Fill,
|
|
10
|
+
type FillResolved as _FillResolved,
|
|
11
|
+
type Node2D as _Node2D,
|
|
12
|
+
type Node2DProps as _Node2DProps,
|
|
13
|
+
type Shadow as _Shadow,
|
|
14
|
+
type ShadowResolved as _ShadowResolved,
|
|
15
|
+
type Stroke as _Stroke,
|
|
16
|
+
type StrokeResolved as _StrokeResolved,
|
|
15
17
|
} from "@motionscript/sdk";
|
|
16
|
-
import
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import { curveOf } from "../kit/curve";
|
|
18
|
+
import { listed, omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
19
|
+
import type { Interval } from "../kit/coords";
|
|
20
|
+
import { curveValue } from "../kit/curve";
|
|
20
21
|
import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
22
|
+
import { clamp } from "../kit/geometry";
|
|
23
|
+
import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
|
|
24
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
23
25
|
|
|
24
26
|
export type RiemannSample = "left" | "right" | "center";
|
|
25
27
|
|
|
@@ -33,66 +35,65 @@ export interface RiemannSumProps extends PaintProps {
|
|
|
33
35
|
/** More rectangles than this is a filled area with extra steps. */
|
|
34
36
|
const MAX_RECTANGLES = 2000;
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
parentKey: "shape",
|
|
42
|
-
forkable: true,
|
|
43
|
-
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
|
|
44
|
-
})
|
|
45
|
-
@withdraw(...OUTLINE_TRIM)
|
|
46
|
-
export class RiemannSum extends Mark<RiemannSumProps> {
|
|
47
|
-
@property({ default: "", kind: "nodeRef", nodeTypes: ["functionPlot"], animatable: false }) declare on: string;
|
|
48
|
-
@property({ default: [0, 1], mapper: readInterval, tween: lerpInterval })
|
|
49
|
-
declare domain: Interval;
|
|
38
|
+
const state = {
|
|
39
|
+
// Its rectangles are revealed by `dx`, not by trimming their outlines.
|
|
40
|
+
...omit(PAINT, "start", "end"),
|
|
41
|
+
on: PLOT_REF,
|
|
42
|
+
domain: Types.custom<Interval>([0, 1], { resolve: readInterval, lerp: lerpInterval }),
|
|
50
43
|
/** Each rectangle's width, in graph units. */
|
|
51
|
-
|
|
44
|
+
dx: Types.number(0.25, { min: 0.001 }),
|
|
52
45
|
/** Where in its width a rectangle reads the curve's height. */
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
@strokeProperty({ default: { weight: 1.5, fill: PLOT_COLOR, align: "center", join: "round" } }) declare stroke: Stroke;
|
|
46
|
+
sample: listed(["left", "right", "center"], "left", { animatable: false }),
|
|
47
|
+
graph: PLANE_ENTRY,
|
|
48
|
+
};
|
|
57
49
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
refineTo(args: CommandArgs<{ dx: number }> & { duration: number }): Command<RiemannSumProps> {
|
|
65
|
-
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
66
|
-
}
|
|
50
|
+
interface SumValues {
|
|
51
|
+
readonly on: NodeView | null;
|
|
52
|
+
readonly domain: Interval;
|
|
53
|
+
readonly dx: number;
|
|
54
|
+
readonly sample: RiemannSample;
|
|
55
|
+
}
|
|
67
56
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
57
|
+
/** Rectangles under a plot, each `dx` wide and as tall as the curve where it is sampled. */
|
|
58
|
+
export const RiemannSum = traced(component2d({
|
|
59
|
+
key: "riemannSum",
|
|
60
|
+
state,
|
|
61
|
+
defaults: {
|
|
62
|
+
fill: { type: "solid", color: PLOT_COLOR, opacity: 0.35 },
|
|
63
|
+
stroke: { weight: 1.5, fill: PLOT_COLOR, align: "center", join: "round" },
|
|
64
|
+
width: "hug",
|
|
65
|
+
height: "hug",
|
|
66
|
+
},
|
|
67
|
+
commands: {
|
|
68
|
+
...omit(PAINT_COMMANDS, "progressTo", "flash"),
|
|
69
|
+
/** Animate the rectangles' width: narrower is the sum closing in on the area. */
|
|
70
|
+
refineTo: { args: ["dx"] },
|
|
71
|
+
/** Animate the span of x the rectangles cover. */
|
|
72
|
+
domainTo: { args: ["domain"] },
|
|
73
|
+
},
|
|
74
|
+
measure: (_measurer, s) => inkSize(traceSum(s, reading(s))),
|
|
75
|
+
render: (ctx, s) => drawMark(ctx, s, traceSum(s, reading(s)), { closed: true }),
|
|
76
|
+
}), traceSum);
|
|
73
77
|
|
|
74
|
-
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
78
|
+
export type RiemannSum = NodeOf<typeof RiemannSum>;
|
|
77
79
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
return runs;
|
|
80
|
+
function traceSum(s: SumValues, { map, plane }: MarkReading): Vector2[][] {
|
|
81
|
+
const plot = curveFor(plane, s.on);
|
|
82
|
+
const [a, b] = s.domain;
|
|
83
|
+
const dx = s.dx;
|
|
84
|
+
if (!plot || !(b > a) || !(dx > 0)) return [];
|
|
85
|
+
const [yMin, yMax] = map.yRange;
|
|
86
|
+
const base = clamp(0, yMin, yMax);
|
|
87
|
+
const at = s.sample === "left" ? 0 : s.sample === "right" ? 1 : 0.5;
|
|
88
|
+
const count = Math.min(MAX_RECTANGLES, Math.ceil((b - a) / dx - 1e-9));
|
|
89
|
+
const runs: Vector2[][] = [];
|
|
90
|
+
for (let i = 0; i < count; i++) {
|
|
91
|
+
const left = a + i * dx;
|
|
92
|
+
const right = Math.min(b, left + dx);
|
|
93
|
+
const value = curveValue(plot, left + (right - left) * at);
|
|
94
|
+
if (!Number.isFinite(value)) continue;
|
|
95
|
+
const height = clamp(value, yMin, yMax);
|
|
96
|
+
runs.push([map.toPoint(left, base), map.toPoint(left, height), map.toPoint(right, height), map.toPoint(right, base)]);
|
|
97
97
|
}
|
|
98
|
+
return runs;
|
|
98
99
|
}
|