@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
package/src/systems/axes2d.ts
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
command,
|
|
2
|
+
component2d,
|
|
4
3
|
Graphics2D,
|
|
5
|
-
|
|
6
|
-
property,
|
|
7
|
-
strokeProperty,
|
|
8
|
-
type AssetScope,
|
|
4
|
+
Types,
|
|
9
5
|
type Color,
|
|
10
|
-
type Command,
|
|
11
|
-
type CommandArgs,
|
|
12
6
|
type Extents,
|
|
13
|
-
type
|
|
7
|
+
type InsetsResolved,
|
|
8
|
+
type Loader,
|
|
9
|
+
type NodeOf,
|
|
14
10
|
type NormalizedColor,
|
|
15
11
|
type PaintProps,
|
|
16
|
-
type Stroke,
|
|
17
12
|
type StrokeResolved,
|
|
13
|
+
type Values,
|
|
18
14
|
type Vector2,
|
|
15
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
16
|
+
type Fill as _Fill,
|
|
17
|
+
type FillResolved as _FillResolved,
|
|
18
|
+
type Node2D as _Node2D,
|
|
19
|
+
type Node2DProps as _Node2DProps,
|
|
20
|
+
type Shadow as _Shadow,
|
|
21
|
+
type ShadowResolved as _ShadowResolved,
|
|
22
|
+
type Stroke as _Stroke,
|
|
19
23
|
} from "@motionscript/sdk";
|
|
24
|
+
import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
20
25
|
import { cartesianMap, lineThrough, resolveInterval, type AxisScaling, type CoordinateMap, type Interval } from "../kit/coords";
|
|
21
|
-
import { clamp
|
|
22
|
-
import {
|
|
26
|
+
import { clamp } from "../kit/geometry";
|
|
27
|
+
import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
|
|
23
28
|
import { formatTick, ticksFor, type Ticks } from "../kit/ticks";
|
|
24
|
-
import { along, AXIS_COLOR,
|
|
29
|
+
import { along, AXIS_COLOR, drawLabels, drawn, loadFormulas, type Label, lerpInterval, readInterval, withoutTips } from "../kit/furniture";
|
|
25
30
|
|
|
26
31
|
export interface Axes2dProps extends PaintProps {
|
|
27
32
|
xRange?: Interval;
|
|
@@ -42,7 +47,7 @@ export interface Axes2dProps extends PaintProps {
|
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
/** One axis, as its furniture is drawn: its line's ends and what is marked along it. */
|
|
45
|
-
interface AxisLayout {
|
|
50
|
+
export interface AxisLayout {
|
|
46
51
|
from: Vector2;
|
|
47
52
|
to: Vector2;
|
|
48
53
|
/** The pixel points that draw its line: its two ends, or more where the system's map bends it. */
|
|
@@ -54,164 +59,168 @@ interface AxisLayout {
|
|
|
54
59
|
at(value: number): Vector2;
|
|
55
60
|
}
|
|
56
61
|
|
|
57
|
-
|
|
58
|
-
* An x and a y axis, crossing at the origin while it is in range and at the
|
|
59
|
-
* nearest edge otherwise. Children sit in its graph coordinates.
|
|
60
|
-
*/
|
|
61
|
-
@node({
|
|
62
|
-
key: "axes2d",
|
|
63
|
-
parentKey: "node",
|
|
64
|
-
forkable: true,
|
|
65
|
-
layout: { children: "freeform", defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
|
|
66
|
-
seed: { width: 800, height: 500, padding: 32 },
|
|
67
|
-
})
|
|
68
|
-
export class Axes2d<P extends Axes2dProps = Axes2dProps> extends CoordinateSystem<P> {
|
|
69
|
-
@property({ default: [-5, 5], mapper: readInterval, tween: lerpInterval })
|
|
70
|
-
declare xRange: Interval;
|
|
71
|
-
@property({ default: [-3, 3], mapper: readInterval, tween: lerpInterval })
|
|
72
|
-
declare yRange: Interval;
|
|
62
|
+
const interval = (initial: Interval) => Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
|
|
73
63
|
|
|
64
|
+
/** An x and a y axis's entries: what a number plane is built on too. */
|
|
65
|
+
export const AXES = {
|
|
66
|
+
...PAINT,
|
|
67
|
+
xRange: interval([-5, 5]),
|
|
68
|
+
yRange: interval([-3, 3]),
|
|
74
69
|
/** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
xStep: Types.number(0, { min: 0 }),
|
|
71
|
+
yStep: Types.number(0, { min: 0 }),
|
|
72
|
+
xScaling: listed(["linear", "log"], "linear", { animatable: false }),
|
|
73
|
+
yScaling: listed(["linear", "log"], "linear", { animatable: false }),
|
|
79
74
|
/** Give a y unit the length of an x unit: `yRange` keeps its centre and takes the span that fits. */
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
@property({ default: "", animatable: false }) declare fontFamily: string;
|
|
75
|
+
squareUnits: Types.boolean(false, { animatable: false }),
|
|
76
|
+
ticks: Types.boolean(true, { animatable: false }),
|
|
77
|
+
tickSize: Types.number(10, { min: 0, unit: "px" }),
|
|
78
|
+
labels: Types.boolean(true, { animatable: false }),
|
|
79
|
+
labelSize: Types.number(18, { min: 1, unit: "px" }),
|
|
80
|
+
labelColor: Types.color(AXIS_COLOR),
|
|
81
|
+
fontFamily: Types.text("", { animatable: false }),
|
|
88
82
|
/** A title at the positive end of each axis. */
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
@strokeProperty({ default: { weight: 2, fill: AXIS_COLOR, align: "center", endCap: "triangle", capScale: 5 } })
|
|
93
|
-
declare stroke: Stroke;
|
|
94
|
-
|
|
95
|
-
constructor(props?: NodeConfig<Axes2d<P>, P>) {
|
|
96
|
-
super((props ?? {}) as NodeConfig<Axes2d<P>, P>);
|
|
97
|
-
}
|
|
83
|
+
xLabel: Types.text("", { animatable: false }),
|
|
84
|
+
yLabel: Types.text("", { animatable: false }),
|
|
85
|
+
};
|
|
98
86
|
|
|
87
|
+
/** The axes' own commands, beside a shape's paints. */
|
|
88
|
+
export const AXES_COMMANDS = {
|
|
89
|
+
...PAINT_COMMANDS,
|
|
99
90
|
/** Animate the window of values the axes show: a pan when a range's ends move alike, a zoom when they do not. */
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
|
|
103
|
-
}
|
|
91
|
+
rangeTo: { args: ["xRange", "yRange"] as const },
|
|
92
|
+
};
|
|
104
93
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (this.fontFamily !== "") assets.font(this.fontFamily);
|
|
108
|
-
declareFormulas(assets, [this.xLabel, this.yLabel]);
|
|
109
|
-
}
|
|
94
|
+
/** What axes are drawn from: their own entries, as a number plane's helpers read them too. */
|
|
95
|
+
export type AxesValues = Values<typeof AXES> & { readonly padding: InsetsResolved; readonly clip: boolean };
|
|
110
96
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
97
|
+
/** What axes declare before they are laid out: their face, and MathJax for a title written as a formula. */
|
|
98
|
+
export function loadAxes(loader: Loader, s: AxesValues): void {
|
|
99
|
+
if (s.fontFamily !== "") loader.font(s.fontFamily);
|
|
100
|
+
loadFormulas(loader, [s.xLabel, s.yLabel]);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** How the axes' values land on a plot area. */
|
|
104
|
+
export function axesMap(area: Extents, s: AxesValues): CoordinateMap {
|
|
105
|
+
const xRange = resolveInterval(s.xRange, s.xScaling);
|
|
106
|
+
let yRange = resolveInterval(s.yRange, s.yScaling);
|
|
107
|
+
const width = area.maxX - area.minX;
|
|
108
|
+
if (s.squareUnits && s.xScaling === "linear" && s.yScaling === "linear" && width > 0) {
|
|
109
|
+
const span = ((xRange[1] - xRange[0]) * (area.maxY - area.minY)) / width;
|
|
110
|
+
const centre = (yRange[0] + yRange[1]) / 2;
|
|
111
|
+
if (span > 0) yRange = [centre - span / 2, centre + span / 2];
|
|
121
112
|
}
|
|
113
|
+
return cartesianMap({ xRange, yRange, area, xScaling: s.xScaling, yScaling: s.yScaling });
|
|
114
|
+
}
|
|
122
115
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
116
|
+
/** The two axes as laid out on `map`: where each runs, what it marks, and where the other crosses it. */
|
|
117
|
+
export function axesOf(map: CoordinateMap, s: AxesValues): [AxisLayout, AxisLayout] {
|
|
118
|
+
const [xMin, xMax] = map.xRange;
|
|
119
|
+
const [yMin, yMax] = map.yRange;
|
|
120
|
+
// An axis sits on the other's zero, or on its near edge when zero is out of range or it is logarithmic.
|
|
121
|
+
const crossX = s.xScaling === "log" ? xMin : clamp(0, xMin, xMax);
|
|
122
|
+
const crossY = s.yScaling === "log" ? yMin : clamp(0, yMin, yMax);
|
|
123
|
+
const xFrom = map.toPoint(xMin, crossY);
|
|
124
|
+
const xTo = map.toPoint(xMax, crossY);
|
|
125
|
+
const yFrom = map.toPoint(crossX, yMin);
|
|
126
|
+
const yTo = map.toPoint(crossX, yMax);
|
|
127
|
+
return [
|
|
128
|
+
{
|
|
129
|
+
from: xFrom,
|
|
130
|
+
to: xTo,
|
|
131
|
+
line: lineThrough(map, xMin, crossY, xMax, crossY),
|
|
132
|
+
marks: ticksFor(map.xRange, s.xStep, Math.hypot(xTo.x - xFrom.x, xTo.y - xFrom.y), s.xScaling),
|
|
133
|
+
crossing: crossX,
|
|
134
|
+
at: (value) => map.toPoint(value, crossY),
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
from: yFrom,
|
|
138
|
+
to: yTo,
|
|
139
|
+
line: lineThrough(map, crossX, yMin, crossX, yMax),
|
|
140
|
+
marks: ticksFor(map.yRange, s.yStep, Math.hypot(yTo.x - yFrom.x, yTo.y - yFrom.y), s.yScaling),
|
|
141
|
+
crossing: crossY,
|
|
142
|
+
at: (value) => map.toPoint(crossX, value),
|
|
143
|
+
},
|
|
144
|
+
];
|
|
145
|
+
}
|
|
129
146
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
147
|
+
/** What the axes draw of themselves on `map`, over what `backdrop` draws behind them (a number plane's grid). */
|
|
148
|
+
export function axesFurniture(
|
|
149
|
+
map: CoordinateMap,
|
|
150
|
+
s: AxesValues,
|
|
151
|
+
backdrop: (x: AxisLayout, y: AxisLayout) => Graphics2D[] = () => [],
|
|
152
|
+
): Graphics2D[] {
|
|
153
|
+
const stroke = s.stroke as StrokeResolved[];
|
|
154
|
+
const { start, end } = s;
|
|
155
|
+
const [x, y] = axesOf(map, s);
|
|
156
|
+
const parts: Graphics2D[] = [...backdrop(x, y)];
|
|
157
|
+
if (stroke.length === 0 && !s.labels) return parts;
|
|
158
|
+
|
|
159
|
+
if (stroke.length > 0) {
|
|
160
|
+
parts.push(
|
|
161
|
+
new Graphics2D()
|
|
162
|
+
.line({ points: x.line, radius: 0, start, end })
|
|
163
|
+
.line({ points: y.line, radius: 0, start, end })
|
|
164
|
+
.stroke(stroke),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
138
167
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
if (!this.labels) continue;
|
|
159
|
-
labels.push(
|
|
160
|
-
horizontal
|
|
161
|
-
? { text: formatTick(value, axis.marks.step), at: { x: at.x, y: at.y - gap - this.labelSize / 2 }, align: "center" }
|
|
162
|
-
: { text: formatTick(value, axis.marks.step), at: { x: at.x - gap, y: at.y }, align: "right" },
|
|
163
|
-
);
|
|
168
|
+
const half = s.tickSize / 2;
|
|
169
|
+
const gap = half + s.labelSize * 0.35;
|
|
170
|
+
const tickMarks = new Graphics2D();
|
|
171
|
+
const labels: Label[] = [];
|
|
172
|
+
let ticked = false;
|
|
173
|
+
const mark = (axis: AxisLayout, horizontal: boolean): void => {
|
|
174
|
+
for (const value of axis.marks.values) {
|
|
175
|
+
if (Math.abs(value - axis.crossing) < 1e-9 * Math.max(1, Math.abs(value))) continue;
|
|
176
|
+
const at = axis.at(value);
|
|
177
|
+
if (!drawn(along(at, axis.from, axis.to), start, end)) continue;
|
|
178
|
+
if (s.ticks && half > 0) {
|
|
179
|
+
tickMarks.line({
|
|
180
|
+
points: horizontal
|
|
181
|
+
? [{ x: at.x, y: at.y - half }, { x: at.x, y: at.y + half }]
|
|
182
|
+
: [{ x: at.x - half, y: at.y }, { x: at.x + half, y: at.y }],
|
|
183
|
+
radius: 0,
|
|
184
|
+
});
|
|
185
|
+
ticked = true;
|
|
164
186
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
labels.push({ text: this.xLabel, at: { x: x.to.x, y: x.to.y + gap + this.labelSize / 2 }, align: "right" });
|
|
172
|
-
}
|
|
173
|
-
if (this.yLabel !== "" && end >= 1) {
|
|
174
|
-
labels.push({ text: this.yLabel, at: { x: y.to.x + gap, y: y.to.y - this.labelSize / 2 }, align: "left" });
|
|
187
|
+
if (!s.labels) continue;
|
|
188
|
+
labels.push(
|
|
189
|
+
horizontal
|
|
190
|
+
? { text: formatTick(value, axis.marks.step), at: { x: at.x, y: at.y - gap - s.labelSize / 2 }, align: "center" }
|
|
191
|
+
: { text: formatTick(value, axis.marks.step), at: { x: at.x - gap, y: at.y }, align: "right" },
|
|
192
|
+
);
|
|
175
193
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
194
|
+
};
|
|
195
|
+
mark(x, true);
|
|
196
|
+
mark(y, false);
|
|
197
|
+
if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
|
|
179
198
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return [];
|
|
199
|
+
if (s.xLabel !== "" && end >= 1) {
|
|
200
|
+
labels.push({ text: s.xLabel, at: { x: x.to.x, y: x.to.y + gap + s.labelSize / 2 }, align: "right" });
|
|
183
201
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
protected axes(map: CoordinateMap): [AxisLayout, AxisLayout] {
|
|
187
|
-
const [xMin, xMax] = map.xRange;
|
|
188
|
-
const [yMin, yMax] = map.yRange;
|
|
189
|
-
// An axis sits on the other's zero, or on its near edge when zero is out of range or it is logarithmic.
|
|
190
|
-
const crossX = this.xScaling === "log" ? xMin : clamp(0, xMin, xMax);
|
|
191
|
-
const crossY = this.yScaling === "log" ? yMin : clamp(0, yMin, yMax);
|
|
192
|
-
const xFrom = map.toPoint(xMin, crossY);
|
|
193
|
-
const xTo = map.toPoint(xMax, crossY);
|
|
194
|
-
const yFrom = map.toPoint(crossX, yMin);
|
|
195
|
-
const yTo = map.toPoint(crossX, yMax);
|
|
196
|
-
return [
|
|
197
|
-
{
|
|
198
|
-
from: xFrom,
|
|
199
|
-
to: xTo,
|
|
200
|
-
line: lineThrough(map, xMin, crossY, xMax, crossY),
|
|
201
|
-
marks: ticksFor(map.xRange, this.xStep, Math.hypot(xTo.x - xFrom.x, xTo.y - xFrom.y), this.xScaling),
|
|
202
|
-
crossing: crossX,
|
|
203
|
-
at: (value) => map.toPoint(value, crossY),
|
|
204
|
-
},
|
|
205
|
-
{
|
|
206
|
-
from: yFrom,
|
|
207
|
-
to: yTo,
|
|
208
|
-
line: lineThrough(map, crossX, yMin, crossX, yMax),
|
|
209
|
-
marks: ticksFor(map.yRange, this.yStep, Math.hypot(yTo.x - yFrom.x, yTo.y - yFrom.y), this.yScaling),
|
|
210
|
-
crossing: crossY,
|
|
211
|
-
at: (value) => map.toPoint(crossX, value),
|
|
212
|
-
},
|
|
213
|
-
];
|
|
202
|
+
if (s.yLabel !== "" && end >= 1) {
|
|
203
|
+
labels.push({ text: s.yLabel, at: { x: y.to.x + gap, y: y.to.y - s.labelSize / 2 }, align: "left" });
|
|
214
204
|
}
|
|
205
|
+
parts.push(...drawLabels(labels, { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
|
|
206
|
+
return parts;
|
|
215
207
|
}
|
|
216
208
|
|
|
217
|
-
|
|
209
|
+
/**
|
|
210
|
+
* An x and a y axis, crossing at the origin while it is in range and at the
|
|
211
|
+
* nearest edge otherwise. Children sit in its graph coordinates.
|
|
212
|
+
*/
|
|
213
|
+
export const Axes2d = component2d({
|
|
214
|
+
key: "axes2d",
|
|
215
|
+
state: AXES,
|
|
216
|
+
defaults: { stroke: { weight: 2, fill: AXIS_COLOR, align: "center", endCap: "triangle", capScale: 5 } },
|
|
217
|
+
load: (loader, s) => loadAxes(loader, s),
|
|
218
|
+
commands: AXES_COMMANDS,
|
|
219
|
+
provide: (s) => systemProvides(s),
|
|
220
|
+
measure: (_measurer, s) => systemSize(s),
|
|
221
|
+
layout: (box, s, children) => systemLayout(box, s, children, axesMap),
|
|
222
|
+
plane: (box, s) => systemOpenPlane(box, s, axesMap),
|
|
223
|
+
render: (ctx, s) => drawSystem(ctx, s, systemMap(ctx.size, s, axesMap), (map) => axesFurniture(map, s)),
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
export type Axes2d = NodeOf<typeof Axes2d>;
|
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
command,
|
|
2
|
+
component2d,
|
|
4
3
|
Graphics2D,
|
|
5
|
-
|
|
6
|
-
property,
|
|
7
|
-
strokeProperty,
|
|
8
|
-
type AssetScope,
|
|
4
|
+
Types,
|
|
9
5
|
type Color,
|
|
10
|
-
type Command,
|
|
11
|
-
type CommandArgs,
|
|
12
6
|
type Extents,
|
|
13
|
-
type
|
|
7
|
+
type NodeOf,
|
|
14
8
|
type NormalizedColor,
|
|
15
9
|
type PaintProps,
|
|
16
|
-
type Stroke,
|
|
17
10
|
type StrokeResolved,
|
|
11
|
+
type Values,
|
|
18
12
|
type Vector2,
|
|
13
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
14
|
+
type Fill as _Fill,
|
|
15
|
+
type FillResolved as _FillResolved,
|
|
16
|
+
type Node2D as _Node2D,
|
|
17
|
+
type Node2DProps as _Node2DProps,
|
|
18
|
+
type Shadow as _Shadow,
|
|
19
|
+
type ShadowResolved as _ShadowResolved,
|
|
20
|
+
type Stroke as _Stroke,
|
|
19
21
|
} from "@motionscript/sdk";
|
|
22
|
+
import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
20
23
|
import { cartesianMap, resolveInterval, type AxisScaling, type CoordinateMap, type Interval } from "../kit/coords";
|
|
21
|
-
import { stated } from "../kit/geometry";
|
|
22
|
-
import { CoordinateSystem } from "../kit/system";
|
|
23
|
-
import { formatTick, ticksFor } from "../kit/ticks";
|
|
24
24
|
import { along, AXIS_COLOR, drawLabels, drawn, lerpInterval, readInterval, withoutTips, type Label } from "../kit/furniture";
|
|
25
|
+
import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
|
|
26
|
+
import { formatTick, ticksFor } from "../kit/ticks";
|
|
25
27
|
|
|
26
28
|
export interface NumberLineProps extends PaintProps {
|
|
27
29
|
range?: Interval;
|
|
@@ -38,94 +40,91 @@ export interface NumberLineProps extends PaintProps {
|
|
|
38
40
|
fontFamily?: string;
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
const state = {
|
|
44
|
+
...PAINT,
|
|
45
|
+
range: Types.custom<Interval>([-5, 5], { resolve: readInterval, lerp: lerpInterval }),
|
|
46
|
+
/** The spacing of the ticks; zero picks a tidy one for the line's length. */
|
|
47
|
+
step: Types.number(0, { min: 0 }),
|
|
48
|
+
scaling: listed(["linear", "log"], "linear", { animatable: false }),
|
|
49
|
+
ticks: Types.boolean(true, { animatable: false }),
|
|
50
|
+
tickSize: Types.number(12, { min: 0, unit: "px" }),
|
|
51
|
+
/** Values whose ticks are drawn twice as long. */
|
|
52
|
+
longTicks: Types.custom<number[]>([], { animatable: false }),
|
|
53
|
+
labels: Types.boolean(true, { animatable: false }),
|
|
54
|
+
labelExclude: Types.custom<number[]>([], { animatable: false }),
|
|
55
|
+
labelSide: listed(["below", "above"], "below", { animatable: false }),
|
|
56
|
+
labelSize: Types.number(18, { min: 1, unit: "px" }),
|
|
57
|
+
labelColor: Types.color(AXIS_COLOR),
|
|
58
|
+
fontFamily: Types.text("", { animatable: false }),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type LineValues = Values<typeof state>;
|
|
62
|
+
|
|
41
63
|
/**
|
|
42
64
|
* One axis with ticks and numbers. A child's `x` is a value on the line and its
|
|
43
65
|
* `y` is pixels above it, so a dot or a label sits on a number.
|
|
44
66
|
*/
|
|
45
|
-
|
|
67
|
+
export const NumberLine = component2d({
|
|
46
68
|
key: "numberLine",
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
@property({ default: true, animatable: false }) declare labels: boolean;
|
|
64
|
-
@property({ default: [], animatable: false }) declare labelExclude: number[];
|
|
65
|
-
@property({ default: "below", options: ["below", "above"], animatable: false }) declare labelSide: "below" | "above";
|
|
66
|
-
@property({ default: 18, min: 1, unit: "px" }) declare labelSize: number;
|
|
67
|
-
@colorProperty({ default: AXIS_COLOR }) declare labelColor: Color;
|
|
68
|
-
@property({ default: "", animatable: false }) declare fontFamily: string;
|
|
69
|
+
state,
|
|
70
|
+
defaults: { stroke: { weight: 2, fill: AXIS_COLOR, align: "center" } },
|
|
71
|
+
load: (loader, s) => {
|
|
72
|
+
if (s.fontFamily !== "") loader.font(s.fontFamily);
|
|
73
|
+
},
|
|
74
|
+
commands: {
|
|
75
|
+
...PAINT_COMMANDS,
|
|
76
|
+
/** Animate the span of values the line shows: a pan when both ends move alike, a zoom when they do not. */
|
|
77
|
+
rangeTo: { args: ["range"] },
|
|
78
|
+
},
|
|
79
|
+
provide: (s) => systemProvides(s),
|
|
80
|
+
measure: (_measurer, s) => systemSize(s),
|
|
81
|
+
layout: (box, s, children) => systemLayout(box, s, children, lineMap),
|
|
82
|
+
plane: (box, s) => systemOpenPlane(box, s, lineMap),
|
|
83
|
+
render: (ctx, s) => drawSystem(ctx, s, systemMap(ctx.size, s, lineMap), (map) => lineFurniture(map, s)),
|
|
84
|
+
});
|
|
69
85
|
|
|
70
|
-
|
|
71
|
-
declare stroke: Stroke;
|
|
86
|
+
export type NumberLine = NodeOf<typeof NumberLine>;
|
|
72
87
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
override declareAssets(assets: AssetScope): void {
|
|
84
|
-
super.declareAssets(assets);
|
|
85
|
-
if (this.fontFamily !== "") assets.font(this.fontFamily);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
protected mapFor(area: Extents): CoordinateMap {
|
|
89
|
-
return cartesianMap({
|
|
90
|
-
xRange: resolveInterval(this.range, this.scaling),
|
|
91
|
-
yRange: [area.minY, area.maxY],
|
|
92
|
-
area,
|
|
93
|
-
xScaling: this.scaling,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
88
|
+
function lineMap(area: Extents, s: LineValues): CoordinateMap {
|
|
89
|
+
return cartesianMap({
|
|
90
|
+
xRange: resolveInterval(s.range, s.scaling),
|
|
91
|
+
yRange: [area.minY, area.maxY],
|
|
92
|
+
area,
|
|
93
|
+
xScaling: s.scaling,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
97
|
+
function lineFurniture(map: CoordinateMap, s: LineValues): Graphics2D[] {
|
|
98
|
+
const stroke = s.stroke as StrokeResolved[];
|
|
99
|
+
const { start, end } = s;
|
|
100
|
+
const [min, max] = map.xRange;
|
|
101
|
+
const from = map.toPoint(min, 0);
|
|
102
|
+
const to = map.toPoint(max, 0);
|
|
103
|
+
const parts: Graphics2D[] = [];
|
|
104
|
+
if (stroke.length > 0) parts.push(new Graphics2D().line({ points: [from, to], radius: 0, start, end }).stroke(stroke));
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
106
|
+
const marks = ticksFor(map.xRange, s.step, to.x - from.x, s.scaling);
|
|
107
|
+
const sign = s.labelSide === "below" ? -1 : 1;
|
|
108
|
+
const tickMarks = new Graphics2D();
|
|
109
|
+
const labels: Label[] = [];
|
|
110
|
+
let ticked = false;
|
|
111
|
+
for (const value of marks.values) {
|
|
112
|
+
const at: Vector2 = map.toPoint(value, 0);
|
|
113
|
+
if (!drawn(along(at, from, to), start, end)) continue;
|
|
114
|
+
const half = s.longTicks.includes(value) ? s.tickSize : s.tickSize / 2;
|
|
115
|
+
if (s.ticks && half > 0) {
|
|
116
|
+
tickMarks.line({ points: [{ x: at.x, y: at.y - half }, { x: at.x, y: at.y + half }], radius: 0 });
|
|
117
|
+
ticked = true;
|
|
118
|
+
}
|
|
119
|
+
if (s.labels && !s.labelExclude.includes(value)) {
|
|
120
|
+
labels.push({
|
|
121
|
+
text: formatTick(value, marks.step),
|
|
122
|
+
at: { x: at.x, y: at.y + sign * (s.tickSize / 2 + s.labelSize * 0.9) },
|
|
123
|
+
align: "center",
|
|
124
|
+
});
|
|
126
125
|
}
|
|
127
|
-
if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
|
|
128
|
-
parts.push(...drawLabels(labels, { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor }));
|
|
129
|
-
return parts;
|
|
130
126
|
}
|
|
127
|
+
if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
|
|
128
|
+
parts.push(...drawLabels(labels, { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
|
|
129
|
+
return parts;
|
|
131
130
|
}
|