@motionscript/math 0.1.0-alpha.7 → 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 +4 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -48
- package/README.md +390 -391
- package/dist/annotate/angle-mark.d.ts +6 -6
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +9 -9
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +9 -9
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-ABYBW64J.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 +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 +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 +2 -2
- 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 +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.d.ts +6 -6
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +3 -3
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +3 -3
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +6 -6
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +6 -6
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +8 -8
- 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.d.ts +2 -2
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.d.ts +2 -2
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts +4 -4
- 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.d.ts +27 -27
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js +3 -2
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.d.ts.map +1 -1
- package/dist/three/label3d.js +3 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.d.ts +27 -27
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +36 -36
- package/dist/three/surface-plot3d.js +6 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +27 -27
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +5 -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 -144
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -50
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -312
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-ABYBW64J.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/systems/axes2d.ts
CHANGED
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Graphics2D,
|
|
4
|
-
Types,
|
|
5
|
-
type Color,
|
|
6
|
-
type Extents,
|
|
7
|
-
type InsetsResolved,
|
|
8
|
-
type Loader,
|
|
9
|
-
type NodeOf,
|
|
10
|
-
type NormalizedColor,
|
|
11
|
-
type PaintProps,
|
|
12
|
-
type StrokeResolved,
|
|
13
|
-
type Values,
|
|
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,
|
|
23
|
-
} from "@motionscript/sdk";
|
|
24
|
-
import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
25
|
-
import { cartesianMap, lineThrough, resolveInterval, type AxisScaling, type CoordinateMap, type Interval } from "../kit/coords";
|
|
26
|
-
import { clamp } from "../kit/geometry";
|
|
27
|
-
import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
|
|
28
|
-
import { formatTick, ticksFor, type Ticks } from "../kit/ticks";
|
|
29
|
-
import { along, AXIS_COLOR, drawLabels, drawn, loadFormulas, type Label, lerpInterval, readInterval, withoutTips } from "../kit/furniture";
|
|
30
|
-
|
|
31
|
-
export interface Axes2dProps extends PaintProps {
|
|
32
|
-
xRange?: Interval;
|
|
33
|
-
yRange?: Interval;
|
|
34
|
-
xStep?: number;
|
|
35
|
-
yStep?: number;
|
|
36
|
-
xScaling?: AxisScaling;
|
|
37
|
-
yScaling?: AxisScaling;
|
|
38
|
-
squareUnits?: boolean;
|
|
39
|
-
ticks?: boolean;
|
|
40
|
-
tickSize?: number;
|
|
41
|
-
labels?: boolean;
|
|
42
|
-
labelSize?: number;
|
|
43
|
-
labelColor?: Color;
|
|
44
|
-
fontFamily?: string;
|
|
45
|
-
xLabel?: string;
|
|
46
|
-
yLabel?: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** One axis, as its furniture is drawn: its line's ends and what is marked along it. */
|
|
50
|
-
export interface AxisLayout {
|
|
51
|
-
from: Vector2;
|
|
52
|
-
to: Vector2;
|
|
53
|
-
/** The pixel points that draw its line: its two ends, or more where the system's map bends it. */
|
|
54
|
-
line: Vector2[];
|
|
55
|
-
marks: Ticks;
|
|
56
|
-
/** The value on this axis the other one crosses at, which is left unmarked. */
|
|
57
|
-
crossing: number;
|
|
58
|
-
/** A mark's pixel point. */
|
|
59
|
-
at(value: number): Vector2;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const interval = (initial: Interval) => Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
|
|
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]),
|
|
69
|
-
/** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
|
|
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 }),
|
|
74
|
-
/** Give a y unit the length of an x unit: `yRange` keeps its centre and takes the span that fits. */
|
|
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 }),
|
|
82
|
-
/** A title at the positive end of each axis. */
|
|
83
|
-
xLabel: Types.text("", { animatable: false }),
|
|
84
|
-
yLabel: Types.text("", { animatable: false }),
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/** The axes' own commands, beside a shape's paints. */
|
|
88
|
-
export const AXES_COMMANDS = {
|
|
89
|
-
...PAINT_COMMANDS,
|
|
90
|
-
/** Animate the window of values the axes show: a pan when a range's ends move alike, a zoom when they do not. */
|
|
91
|
-
rangeTo: { args: ["xRange", "yRange"] as const },
|
|
92
|
-
};
|
|
93
|
-
|
|
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 };
|
|
96
|
-
|
|
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];
|
|
112
|
-
}
|
|
113
|
-
return cartesianMap({ xRange, yRange, area, xScaling: s.xScaling, yScaling: s.yScaling });
|
|
114
|
-
}
|
|
115
|
-
|
|
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
|
-
}
|
|
146
|
-
|
|
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
|
-
}
|
|
167
|
-
|
|
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;
|
|
186
|
-
}
|
|
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
|
-
);
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
mark(x, true);
|
|
196
|
-
mark(y, false);
|
|
197
|
-
if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
|
|
198
|
-
|
|
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" });
|
|
201
|
-
}
|
|
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" });
|
|
204
|
-
}
|
|
205
|
-
parts.push(...drawLabels(labels, { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
|
|
206
|
-
return parts;
|
|
207
|
-
}
|
|
208
|
-
|
|
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
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Types,
|
|
5
|
+
type Color,
|
|
6
|
+
type Extents,
|
|
7
|
+
type InsetsResolved,
|
|
8
|
+
type Loader,
|
|
9
|
+
type NodeOf,
|
|
10
|
+
type NormalizedColor,
|
|
11
|
+
type PaintProps,
|
|
12
|
+
type StrokeResolved,
|
|
13
|
+
type Values,
|
|
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,
|
|
23
|
+
} from "@motionscript/sdk";
|
|
24
|
+
import { listed, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
25
|
+
import { cartesianMap, lineThrough, resolveInterval, type AxisScaling, type CoordinateMap, type Interval } from "../kit/coords";
|
|
26
|
+
import { clamp } from "../kit/geometry";
|
|
27
|
+
import { drawSystem, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
|
|
28
|
+
import { formatTick, ticksFor, type Ticks } from "../kit/ticks";
|
|
29
|
+
import { along, AXIS_COLOR, drawLabels, drawn, loadFormulas, type Label, lerpInterval, readInterval, withoutTips } from "../kit/furniture";
|
|
30
|
+
|
|
31
|
+
export interface Axes2dProps extends PaintProps {
|
|
32
|
+
xRange?: Interval;
|
|
33
|
+
yRange?: Interval;
|
|
34
|
+
xStep?: number;
|
|
35
|
+
yStep?: number;
|
|
36
|
+
xScaling?: AxisScaling;
|
|
37
|
+
yScaling?: AxisScaling;
|
|
38
|
+
squareUnits?: boolean;
|
|
39
|
+
ticks?: boolean;
|
|
40
|
+
tickSize?: number;
|
|
41
|
+
labels?: boolean;
|
|
42
|
+
labelSize?: number;
|
|
43
|
+
labelColor?: Color;
|
|
44
|
+
fontFamily?: string;
|
|
45
|
+
xLabel?: string;
|
|
46
|
+
yLabel?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** One axis, as its furniture is drawn: its line's ends and what is marked along it. */
|
|
50
|
+
export interface AxisLayout {
|
|
51
|
+
from: Vector2;
|
|
52
|
+
to: Vector2;
|
|
53
|
+
/** The pixel points that draw its line: its two ends, or more where the system's map bends it. */
|
|
54
|
+
line: Vector2[];
|
|
55
|
+
marks: Ticks;
|
|
56
|
+
/** The value on this axis the other one crosses at, which is left unmarked. */
|
|
57
|
+
crossing: number;
|
|
58
|
+
/** A mark's pixel point. */
|
|
59
|
+
at(value: number): Vector2;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const interval = (initial: Interval) => Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
|
|
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]),
|
|
69
|
+
/** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
|
|
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 }),
|
|
74
|
+
/** Give a y unit the length of an x unit: `yRange` keeps its centre and takes the span that fits. */
|
|
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 }),
|
|
82
|
+
/** A title at the positive end of each axis. */
|
|
83
|
+
xLabel: Types.text("", { animatable: false }),
|
|
84
|
+
yLabel: Types.text("", { animatable: false }),
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** The axes' own commands, beside a shape's paints. */
|
|
88
|
+
export const AXES_COMMANDS = {
|
|
89
|
+
...PAINT_COMMANDS,
|
|
90
|
+
/** Animate the window of values the axes show: a pan when a range's ends move alike, a zoom when they do not. */
|
|
91
|
+
rangeTo: { args: ["xRange", "yRange"] as const },
|
|
92
|
+
};
|
|
93
|
+
|
|
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 };
|
|
96
|
+
|
|
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];
|
|
112
|
+
}
|
|
113
|
+
return cartesianMap({ xRange, yRange, area, xScaling: s.xScaling, yScaling: s.yScaling });
|
|
114
|
+
}
|
|
115
|
+
|
|
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
|
+
}
|
|
146
|
+
|
|
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
|
+
}
|
|
167
|
+
|
|
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;
|
|
186
|
+
}
|
|
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
|
+
);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
mark(x, true);
|
|
196
|
+
mark(y, false);
|
|
197
|
+
if (ticked && stroke.length > 0) parts.push(tickMarks.stroke(withoutTips(stroke)));
|
|
198
|
+
|
|
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" });
|
|
201
|
+
}
|
|
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" });
|
|
204
|
+
}
|
|
205
|
+
parts.push(...drawLabels(labels, { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor }));
|
|
206
|
+
return parts;
|
|
207
|
+
}
|
|
208
|
+
|
|
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>;
|