@motionscript/math 0.1.0-alpha.8 → 0.1.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +390 -391
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
- package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/graph.js.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/part-fills.js.map +1 -1
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/scale.js.map +1 -1
- package/dist/kit/source.js.map +1 -1
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/ticks.js.map +1 -1
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/graph-layout.js.map +1 -1
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +251 -251
- package/src/annotate/angle-mark.ts +137 -137
- package/src/annotate/curly-brace.ts +148 -148
- package/src/annotate/emphasis-mark.ts +118 -118
- package/src/engine.ts +86 -86
- package/src/index.ts +29 -29
- package/src/kit/brace.ts +37 -37
- package/src/kit/cache.ts +61 -61
- package/src/kit/component.ts +57 -57
- package/src/kit/contour.ts +94 -94
- package/src/kit/coords.ts +278 -278
- package/src/kit/curve.ts +47 -47
- package/src/kit/equation.ts +181 -181
- package/src/kit/equation3d.ts +137 -137
- package/src/kit/expression.ts +159 -159
- package/src/kit/field-mark.ts +171 -171
- package/src/kit/field.ts +50 -50
- package/src/kit/follow.ts +229 -229
- package/src/kit/furniture.ts +113 -113
- package/src/kit/geometry.ts +49 -49
- package/src/kit/graph.ts +116 -116
- package/src/kit/mark.ts +152 -152
- package/src/kit/part-fills.ts +61 -61
- package/src/kit/place.ts +77 -77
- package/src/kit/sampler.ts +281 -281
- package/src/kit/scale.ts +39 -39
- package/src/kit/source.ts +57 -57
- package/src/kit/system.ts +105 -105
- package/src/kit/ticks.ts +85 -85
- package/src/kit/withdrawn.ts +8 -8
- package/src/marks/curve-area.ts +89 -89
- package/src/marks/drop-lines.ts +71 -71
- package/src/marks/plot-point.ts +102 -102
- package/src/marks/riemann-sum.ts +99 -99
- package/src/marks/tangent-line.ts +119 -119
- package/src/marks/vector-arrow.ts +98 -98
- package/src/nodes.ts +99 -99
- package/src/plots/function-plot.ts +220 -220
- package/src/plots/points-plot.ts +103 -103
- package/src/plots/stream-lines.ts +160 -160
- package/src/plots/traced-path.ts +70 -70
- package/src/plots/vector-field.ts +107 -107
- package/src/structures/graph-layout.ts +145 -145
- package/src/structures/network-graph.ts +291 -291
- package/src/structures/sample-space.ts +210 -210
- package/src/systems/axes2d.ts +226 -226
- package/src/systems/number-line.ts +130 -130
- package/src/systems/number-plane.ts +232 -232
- package/src/systems/polar-plane.ts +177 -177
- package/src/tex/geometry.ts +703 -703
- package/src/tex/index.ts +15 -15
- package/src/tex/mathjax.ts +82 -82
- package/src/tex/matrix.ts +97 -97
- package/src/tex/node.ts +74 -74
- package/src/tex/tween.ts +308 -308
- package/src/tex/typeset.ts +461 -461
- package/src/three/axes3d.ts +267 -267
- package/src/three/curve-plot3d.ts +146 -146
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -52
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -318
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
- package/dist/kit/placement.d.ts +0 -8
- package/dist/kit/placement.d.ts.map +0 -1
- package/dist/kit/placement.js +0 -8
- package/dist/kit/placement.js.map +0 -1
package/src/marks/curve-area.ts
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Types,
|
|
4
|
-
type NodeOf,
|
|
5
|
-
type NodeView,
|
|
6
|
-
type PaintProps,
|
|
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
|
-
} from "@motionscript/sdk";
|
|
16
|
-
import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
17
|
-
import type { Interval } from "../kit/coords";
|
|
18
|
-
import { curveValue } from "../kit/curve";
|
|
19
|
-
import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
|
|
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";
|
|
23
|
-
|
|
24
|
-
export interface CurveAreaProps extends PaintProps {
|
|
25
|
-
on?: string;
|
|
26
|
-
under?: string;
|
|
27
|
-
domain?: Interval;
|
|
28
|
-
}
|
|
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
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The region between a plot and the x axis, or between two plots, over a span
|
|
49
|
-
* of x. It sits beside the plots it reads rather than inside one: it may read
|
|
50
|
-
* two, and it has a life of its own on the timeline, swept open or kept while
|
|
51
|
-
* a curve changes.
|
|
52
|
-
*/
|
|
53
|
-
export const CurveArea = traced(component2d({
|
|
54
|
-
key: "curveArea",
|
|
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);
|
|
65
|
-
|
|
66
|
-
export type CurveArea = NodeOf<typeof CurveArea>;
|
|
67
|
-
|
|
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 [];
|
|
76
|
-
|
|
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)));
|
|
87
|
-
}
|
|
88
|
-
return [[...upper, ...lower.reverse()]];
|
|
89
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type NodeView,
|
|
6
|
+
type PaintProps,
|
|
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
|
+
} from "@motionscript/sdk";
|
|
16
|
+
import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
17
|
+
import type { Interval } from "../kit/coords";
|
|
18
|
+
import { curveValue } from "../kit/curve";
|
|
19
|
+
import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
|
|
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";
|
|
23
|
+
|
|
24
|
+
export interface CurveAreaProps extends PaintProps {
|
|
25
|
+
on?: string;
|
|
26
|
+
under?: string;
|
|
27
|
+
domain?: Interval;
|
|
28
|
+
}
|
|
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
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The region between a plot and the x axis, or between two plots, over a span
|
|
49
|
+
* of x. It sits beside the plots it reads rather than inside one: it may read
|
|
50
|
+
* two, and it has a life of its own on the timeline, swept open or kept while
|
|
51
|
+
* a curve changes.
|
|
52
|
+
*/
|
|
53
|
+
export const CurveArea = traced(component2d({
|
|
54
|
+
key: "curveArea",
|
|
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);
|
|
65
|
+
|
|
66
|
+
export type CurveArea = NodeOf<typeof CurveArea>;
|
|
67
|
+
|
|
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 [];
|
|
76
|
+
|
|
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)));
|
|
87
|
+
}
|
|
88
|
+
return [[...upper, ...lower.reverse()]];
|
|
89
|
+
}
|
package/src/marks/drop-lines.ts
CHANGED
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
type NodeOf,
|
|
4
|
-
type NodeView,
|
|
5
|
-
type PaintProps,
|
|
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,
|
|
14
|
-
} from "@motionscript/sdk";
|
|
15
|
-
import { listed, omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
16
|
-
import { curveValue } from "../kit/curve";
|
|
17
|
-
import { AXIS_COLOR } from "../kit/furniture";
|
|
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";
|
|
21
|
-
|
|
22
|
-
export type DropAxis = "x" | "y" | "both";
|
|
23
|
-
|
|
24
|
-
export interface DropLinesProps extends PaintProps {
|
|
25
|
-
on?: string;
|
|
26
|
-
axis?: DropAxis;
|
|
27
|
-
}
|
|
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
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Guide lines from a graph point to the axes, reading its coordinates off them.
|
|
47
|
-
* The point is this node's `x`/`y`, or the point of a plot above its `x`.
|
|
48
|
-
*/
|
|
49
|
-
export const DropLines = traced(component2d({
|
|
50
|
-
key: "dropLines",
|
|
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);
|
|
57
|
-
|
|
58
|
-
export type DropLines = NodeOf<typeof DropLines>;
|
|
59
|
-
|
|
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;
|
|
71
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
type NodeOf,
|
|
4
|
+
type NodeView,
|
|
5
|
+
type PaintProps,
|
|
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,
|
|
14
|
+
} from "@motionscript/sdk";
|
|
15
|
+
import { listed, omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
16
|
+
import { curveValue } from "../kit/curve";
|
|
17
|
+
import { AXIS_COLOR } from "../kit/furniture";
|
|
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";
|
|
21
|
+
|
|
22
|
+
export type DropAxis = "x" | "y" | "both";
|
|
23
|
+
|
|
24
|
+
export interface DropLinesProps extends PaintProps {
|
|
25
|
+
on?: string;
|
|
26
|
+
axis?: DropAxis;
|
|
27
|
+
}
|
|
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
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Guide lines from a graph point to the axes, reading its coordinates off them.
|
|
47
|
+
* The point is this node's `x`/`y`, or the point of a plot above its `x`.
|
|
48
|
+
*/
|
|
49
|
+
export const DropLines = traced(component2d({
|
|
50
|
+
key: "dropLines",
|
|
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);
|
|
57
|
+
|
|
58
|
+
export type DropLines = NodeOf<typeof DropLines>;
|
|
59
|
+
|
|
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;
|
|
71
|
+
}
|
package/src/marks/plot-point.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Graphics2D,
|
|
4
|
-
Types,
|
|
5
|
-
type Color,
|
|
6
|
-
type NodeOf,
|
|
7
|
-
type NormalizedColor,
|
|
8
|
-
type PaintProps,
|
|
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
|
-
} from "@motionscript/sdk";
|
|
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";
|
|
25
|
-
|
|
26
|
-
export interface PlotPointProps extends PaintProps {
|
|
27
|
-
on?: string;
|
|
28
|
-
radius?: number;
|
|
29
|
-
label?: string;
|
|
30
|
-
labelSide?: "above" | "below" | "left" | "right";
|
|
31
|
-
labelSize?: number;
|
|
32
|
-
labelColor?: Color;
|
|
33
|
-
fontFamily?: string;
|
|
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
|
-
|
|
52
|
-
/**
|
|
53
|
-
* A dot at graph coordinates, with an optional label. Named a plot in `on`, it
|
|
54
|
-
* rides that curve: its `x` is where along it, and its height is the curve's.
|
|
55
|
-
*/
|
|
56
|
-
export const PlotPoint = anchored(component2d({
|
|
57
|
-
key: "plotPoint",
|
|
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) }));
|
|
93
|
-
|
|
94
|
-
export type PlotPoint = NodeOf<typeof PlotPoint>;
|
|
95
|
-
|
|
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;
|
|
102
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Types,
|
|
5
|
+
type Color,
|
|
6
|
+
type NodeOf,
|
|
7
|
+
type NormalizedColor,
|
|
8
|
+
type PaintProps,
|
|
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
|
+
} from "@motionscript/sdk";
|
|
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";
|
|
25
|
+
|
|
26
|
+
export interface PlotPointProps extends PaintProps {
|
|
27
|
+
on?: string;
|
|
28
|
+
radius?: number;
|
|
29
|
+
label?: string;
|
|
30
|
+
labelSide?: "above" | "below" | "left" | "right";
|
|
31
|
+
labelSize?: number;
|
|
32
|
+
labelColor?: Color;
|
|
33
|
+
fontFamily?: string;
|
|
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
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A dot at graph coordinates, with an optional label. Named a plot in `on`, it
|
|
54
|
+
* rides that curve: its `x` is where along it, and its height is the curve's.
|
|
55
|
+
*/
|
|
56
|
+
export const PlotPoint = anchored(component2d({
|
|
57
|
+
key: "plotPoint",
|
|
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) }));
|
|
93
|
+
|
|
94
|
+
export type PlotPoint = NodeOf<typeof PlotPoint>;
|
|
95
|
+
|
|
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;
|
|
102
|
+
}
|