@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/kit/source.ts
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import type { Interval } from "./coords";
|
|
2
|
-
import type { ExpressionScope } from "./expression";
|
|
3
|
-
|
|
4
|
-
/** What a plot's equation, domain and named values are held as, and how each tweens: shared by every plot, 2D and 3D. */
|
|
5
|
-
|
|
6
|
-
/** An equation on its way to another: both are drawn, `t` of the way across. */
|
|
7
|
-
export interface PlotSource {
|
|
8
|
-
source: string;
|
|
9
|
-
blend?: { from: string; t: number };
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function readSource(value: string | PlotSource | undefined): PlotSource {
|
|
13
|
-
if (typeof value === "string") return { source: value };
|
|
14
|
-
return value ?? { source: "" };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Mid-tween the prop holds both equations, so a seek restores the blend with it.
|
|
18
|
-
export function lerpSource(from: PlotSource, to: PlotSource, t: number): PlotSource {
|
|
19
|
-
if (t <= 0 || from.source === to.source) return t <= 0 ? from : to;
|
|
20
|
-
return t >= 1 ? to : { source: to.source, blend: { from: from.source, t } };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function lerpDomain(from: Interval | null, to: Interval | null, t: number): Interval | null {
|
|
24
|
-
if (!from || !to) return t < 1 ? from : to;
|
|
25
|
-
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** What a named value is until it is given one. */
|
|
29
|
-
export const UNSET_PARAM = 1;
|
|
30
|
-
|
|
31
|
-
export function lerpParams(from: ExpressionScope, to: ExpressionScope, t: number): ExpressionScope {
|
|
32
|
-
const out: Record<string, number> = { ...from };
|
|
33
|
-
for (const [name, value] of Object.entries(to)) {
|
|
34
|
-
const start = from[name] ?? UNSET_PARAM;
|
|
35
|
-
out[name] = start + (value - start) * t;
|
|
36
|
-
}
|
|
37
|
-
return out;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** `params` with every name of `names` given a value, so one nobody has set yet still draws. */
|
|
41
|
-
export function scopeFor(names: readonly string[], params: ExpressionScope): ExpressionScope {
|
|
42
|
-
let filled: Record<string, number> | null = null;
|
|
43
|
-
for (const name of names) {
|
|
44
|
-
if (typeof params[name] !== "number") (filled ??= { ...params })[name] = UNSET_PARAM;
|
|
45
|
-
}
|
|
46
|
-
return filled ?? params;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** A string as a number that changes when it does: what a geometry's `revision` wants. */
|
|
50
|
-
export function revisionOf(key: string): number {
|
|
51
|
-
let hash = 0x811c9dc5;
|
|
52
|
-
for (let i = 0; i < key.length; i++) {
|
|
53
|
-
hash ^= key.charCodeAt(i);
|
|
54
|
-
hash = Math.imul(hash, 0x01000193);
|
|
55
|
-
}
|
|
56
|
-
return hash >>> 0;
|
|
57
|
-
}
|
|
1
|
+
import type { Interval } from "./coords";
|
|
2
|
+
import type { ExpressionScope } from "./expression";
|
|
3
|
+
|
|
4
|
+
/** What a plot's equation, domain and named values are held as, and how each tweens: shared by every plot, 2D and 3D. */
|
|
5
|
+
|
|
6
|
+
/** An equation on its way to another: both are drawn, `t` of the way across. */
|
|
7
|
+
export interface PlotSource {
|
|
8
|
+
source: string;
|
|
9
|
+
blend?: { from: string; t: number };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function readSource(value: string | PlotSource | undefined): PlotSource {
|
|
13
|
+
if (typeof value === "string") return { source: value };
|
|
14
|
+
return value ?? { source: "" };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Mid-tween the prop holds both equations, so a seek restores the blend with it.
|
|
18
|
+
export function lerpSource(from: PlotSource, to: PlotSource, t: number): PlotSource {
|
|
19
|
+
if (t <= 0 || from.source === to.source) return t <= 0 ? from : to;
|
|
20
|
+
return t >= 1 ? to : { source: to.source, blend: { from: from.source, t } };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function lerpDomain(from: Interval | null, to: Interval | null, t: number): Interval | null {
|
|
24
|
+
if (!from || !to) return t < 1 ? from : to;
|
|
25
|
+
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What a named value is until it is given one. */
|
|
29
|
+
export const UNSET_PARAM = 1;
|
|
30
|
+
|
|
31
|
+
export function lerpParams(from: ExpressionScope, to: ExpressionScope, t: number): ExpressionScope {
|
|
32
|
+
const out: Record<string, number> = { ...from };
|
|
33
|
+
for (const [name, value] of Object.entries(to)) {
|
|
34
|
+
const start = from[name] ?? UNSET_PARAM;
|
|
35
|
+
out[name] = start + (value - start) * t;
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** `params` with every name of `names` given a value, so one nobody has set yet still draws. */
|
|
41
|
+
export function scopeFor(names: readonly string[], params: ExpressionScope): ExpressionScope {
|
|
42
|
+
let filled: Record<string, number> | null = null;
|
|
43
|
+
for (const name of names) {
|
|
44
|
+
if (typeof params[name] !== "number") (filled ??= { ...params })[name] = UNSET_PARAM;
|
|
45
|
+
}
|
|
46
|
+
return filled ?? params;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A string as a number that changes when it does: what a geometry's `revision` wants. */
|
|
50
|
+
export function revisionOf(key: string): number {
|
|
51
|
+
let hash = 0x811c9dc5;
|
|
52
|
+
for (let i = 0; i < key.length; i++) {
|
|
53
|
+
hash ^= key.charCodeAt(i);
|
|
54
|
+
hash = Math.imul(hash, 0x01000193);
|
|
55
|
+
}
|
|
56
|
+
return hash >>> 0;
|
|
57
|
+
}
|
package/src/kit/system.ts
CHANGED
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Graphics2D,
|
|
3
|
-
insetsOps,
|
|
4
|
-
Paint,
|
|
5
|
-
provides,
|
|
6
|
-
type ChildPlane,
|
|
7
|
-
type ChildView,
|
|
8
|
-
type Extents,
|
|
9
|
-
type InsetsResolved,
|
|
10
|
-
type Placement2D,
|
|
11
|
-
type Provided,
|
|
12
|
-
type RenderContext,
|
|
13
|
-
type Shadow,
|
|
14
|
-
type Fill,
|
|
15
|
-
type Size2D,
|
|
16
|
-
type Vector2,
|
|
17
|
-
} from "@motionscript/sdk";
|
|
18
|
-
import type { CoordinateMap } from "./coords";
|
|
19
|
-
import { settle, SYSTEM, systemPlane } from "./graph";
|
|
20
|
-
import { openPlane, placeOnMap } from "./place";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* What a coordinate system shares: a node whose children sit in its graph
|
|
24
|
-
* coordinates. A child's `x`/`y` are in the system's units and the child keeps
|
|
25
|
-
* its own size, so anything can be put on a graph: a dot, a label, an image. A
|
|
26
|
-
* mark is drawn through the system's map instead and laid out on its ink.
|
|
27
|
-
*
|
|
28
|
-
* The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
|
|
29
|
-
* the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
|
|
30
|
-
* document's children would otherwise share their indices with them.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
const CENTRE: Vector2 = { x: 0, y: 0 };
|
|
34
|
-
|
|
35
|
-
/** What a system's helpers read of its state. */
|
|
36
|
-
export interface SystemState {
|
|
37
|
-
readonly padding: InsetsResolved;
|
|
38
|
-
readonly fill: Fill;
|
|
39
|
-
readonly shadow: Shadow;
|
|
40
|
-
readonly clip: boolean;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** How a system's graph values land on its plot area. */
|
|
44
|
-
export type MapFor<S> = (area: Extents, state: S) => CoordinateMap;
|
|
45
|
-
|
|
46
|
-
/** The plot area: the box less its padding, y-up from the box's centre. */
|
|
47
|
-
export function plotArea(size: Size2D, padding: InsetsResolved): Extents {
|
|
48
|
-
const inner = insetsOps.insetBounds(size.width, size.height, padding);
|
|
49
|
-
return {
|
|
50
|
-
minX: inner.x - inner.width / 2,
|
|
51
|
-
maxX: inner.x + inner.width / 2,
|
|
52
|
-
minY: -inner.y - inner.height / 2,
|
|
53
|
-
maxY: -inner.y + inner.height / 2,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** A system's map at a laid-out `size`. */
|
|
58
|
-
export function systemMap<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): CoordinateMap {
|
|
59
|
-
return mapFor(plotArea(size, state.padding), state);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** A system is the size it is given: its children are placed in it, not measured into it. */
|
|
63
|
-
export function systemSize(state: SystemState): Size2D {
|
|
64
|
-
const { left, right, top, bottom } = state.padding;
|
|
65
|
-
return { width: left + right, height: top + bottom };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** What a system hands its subtree: its plane, which its layout keeps current. */
|
|
69
|
-
export function systemProvides(state: object): readonly Provided[] {
|
|
70
|
-
return [provides(SYSTEM, systemPlane(state))];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Lay a system's children out on its map. */
|
|
74
|
-
export function systemLayout<S extends SystemState>(size: Size2D, state: S, children: readonly ChildView[], mapFor: MapFor<S>): Placement2D[] {
|
|
75
|
-
const map = systemMap(size, state, mapFor);
|
|
76
|
-
const plane = systemPlane(state);
|
|
77
|
-
settle(plane, map, children);
|
|
78
|
-
const inner = insetsOps.insetBounds(size.width, size.height, state.padding);
|
|
79
|
-
return placeOnMap(children, map, CENTRE, inner, plane);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Where a child dropped on a system would sit. */
|
|
83
|
-
export function systemOpenPlane<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): ChildPlane {
|
|
84
|
-
return openPlane(systemMap(size, state, mapFor), CENTRE);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* A system's paint: the backdrop, then the furniture under its children, kept
|
|
89
|
-
* in the plot area where the map spills from it, then the children, confined
|
|
90
|
-
* to the plot area when `clip` is set. The stroke is the axis line, drawn with
|
|
91
|
-
* the furniture: painted after the children it would land over them.
|
|
92
|
-
*/
|
|
93
|
-
export function drawSystem(ctx: RenderContext, state: SystemState, map: CoordinateMap, furniture: (map: CoordinateMap) => Graphics2D[]): void {
|
|
94
|
-
const { width, height } = ctx.size;
|
|
95
|
-
const { minX, minY, maxX, maxY } = map.area;
|
|
96
|
-
const area = new Graphics2D().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
|
|
97
|
-
ctx.inFront(() => {
|
|
98
|
-
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).shadow(state.shadow).fill(state.fill));
|
|
99
|
-
const parts = furniture(map);
|
|
100
|
-
if (map.spills) ctx.clip(area, () => ctx.draw(parts));
|
|
101
|
-
else ctx.draw(parts);
|
|
102
|
-
});
|
|
103
|
-
// No shape of its own to paint here; the plot area is what clips the children and what picking tests.
|
|
104
|
-
Paint.draw(ctx, state as unknown as Paint.PaintState, null, { clipShape: area });
|
|
105
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
insetsOps,
|
|
4
|
+
Paint,
|
|
5
|
+
provides,
|
|
6
|
+
type ChildPlane,
|
|
7
|
+
type ChildView,
|
|
8
|
+
type Extents,
|
|
9
|
+
type InsetsResolved,
|
|
10
|
+
type Placement2D,
|
|
11
|
+
type Provided,
|
|
12
|
+
type RenderContext,
|
|
13
|
+
type Shadow,
|
|
14
|
+
type Fill,
|
|
15
|
+
type Size2D,
|
|
16
|
+
type Vector2,
|
|
17
|
+
} from "@motionscript/sdk";
|
|
18
|
+
import type { CoordinateMap } from "./coords";
|
|
19
|
+
import { settle, SYSTEM, systemPlane } from "./graph";
|
|
20
|
+
import { openPlane, placeOnMap } from "./place";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* What a coordinate system shares: a node whose children sit in its graph
|
|
24
|
+
* coordinates. A child's `x`/`y` are in the system's units and the child keeps
|
|
25
|
+
* its own size, so anything can be put on a graph: a dot, a label, an image. A
|
|
26
|
+
* mark is drawn through the system's map instead and laid out on its ink.
|
|
27
|
+
*
|
|
28
|
+
* The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
|
|
29
|
+
* the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
|
|
30
|
+
* document's children would otherwise share their indices with them.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const CENTRE: Vector2 = { x: 0, y: 0 };
|
|
34
|
+
|
|
35
|
+
/** What a system's helpers read of its state. */
|
|
36
|
+
export interface SystemState {
|
|
37
|
+
readonly padding: InsetsResolved;
|
|
38
|
+
readonly fill: Fill;
|
|
39
|
+
readonly shadow: Shadow;
|
|
40
|
+
readonly clip: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** How a system's graph values land on its plot area. */
|
|
44
|
+
export type MapFor<S> = (area: Extents, state: S) => CoordinateMap;
|
|
45
|
+
|
|
46
|
+
/** The plot area: the box less its padding, y-up from the box's centre. */
|
|
47
|
+
export function plotArea(size: Size2D, padding: InsetsResolved): Extents {
|
|
48
|
+
const inner = insetsOps.insetBounds(size.width, size.height, padding);
|
|
49
|
+
return {
|
|
50
|
+
minX: inner.x - inner.width / 2,
|
|
51
|
+
maxX: inner.x + inner.width / 2,
|
|
52
|
+
minY: -inner.y - inner.height / 2,
|
|
53
|
+
maxY: -inner.y + inner.height / 2,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A system's map at a laid-out `size`. */
|
|
58
|
+
export function systemMap<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): CoordinateMap {
|
|
59
|
+
return mapFor(plotArea(size, state.padding), state);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A system is the size it is given: its children are placed in it, not measured into it. */
|
|
63
|
+
export function systemSize(state: SystemState): Size2D {
|
|
64
|
+
const { left, right, top, bottom } = state.padding;
|
|
65
|
+
return { width: left + right, height: top + bottom };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** What a system hands its subtree: its plane, which its layout keeps current. */
|
|
69
|
+
export function systemProvides(state: object): readonly Provided[] {
|
|
70
|
+
return [provides(SYSTEM, systemPlane(state))];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Lay a system's children out on its map. */
|
|
74
|
+
export function systemLayout<S extends SystemState>(size: Size2D, state: S, children: readonly ChildView[], mapFor: MapFor<S>): Placement2D[] {
|
|
75
|
+
const map = systemMap(size, state, mapFor);
|
|
76
|
+
const plane = systemPlane(state);
|
|
77
|
+
settle(plane, map, children);
|
|
78
|
+
const inner = insetsOps.insetBounds(size.width, size.height, state.padding);
|
|
79
|
+
return placeOnMap(children, map, CENTRE, inner, plane);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Where a child dropped on a system would sit. */
|
|
83
|
+
export function systemOpenPlane<S extends SystemState>(size: Size2D, state: S, mapFor: MapFor<S>): ChildPlane {
|
|
84
|
+
return openPlane(systemMap(size, state, mapFor), CENTRE);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A system's paint: the backdrop, then the furniture under its children, kept
|
|
89
|
+
* in the plot area where the map spills from it, then the children, confined
|
|
90
|
+
* to the plot area when `clip` is set. The stroke is the axis line, drawn with
|
|
91
|
+
* the furniture: painted after the children it would land over them.
|
|
92
|
+
*/
|
|
93
|
+
export function drawSystem(ctx: RenderContext, state: SystemState, map: CoordinateMap, furniture: (map: CoordinateMap) => Graphics2D[]): void {
|
|
94
|
+
const { width, height } = ctx.size;
|
|
95
|
+
const { minX, minY, maxX, maxY } = map.area;
|
|
96
|
+
const area = new Graphics2D().rect({ x: (minX + maxX) / 2, y: (minY + maxY) / 2, width: maxX - minX, height: maxY - minY });
|
|
97
|
+
ctx.inFront(() => {
|
|
98
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).shadow(state.shadow).fill(state.fill));
|
|
99
|
+
const parts = furniture(map);
|
|
100
|
+
if (map.spills) ctx.clip(area, () => ctx.draw(parts));
|
|
101
|
+
else ctx.draw(parts);
|
|
102
|
+
});
|
|
103
|
+
// No shape of its own to paint here; the plot area is what clips the children and what picking tests.
|
|
104
|
+
Paint.draw(ctx, state as unknown as Paint.PaintState, null, { clipShape: area });
|
|
105
|
+
}
|
package/src/kit/ticks.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
import { niceStep } from "@motionscript/sdk/component";
|
|
2
|
-
import type { AxisScaling, Interval } from "./coords";
|
|
3
|
-
|
|
4
|
-
/** Where an axis is marked, and the spacing its labels are rounded to. */
|
|
5
|
-
export interface Ticks {
|
|
6
|
-
values: number[];
|
|
7
|
-
step: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** Past this an axis is not marked at all, rather than drawn as a solid bar of ticks. */
|
|
11
|
-
const MAX_TICKS = 200;
|
|
12
|
-
|
|
13
|
-
/** How far apart, in pixels, marks sit when the axis picks its own step. */
|
|
14
|
-
const AUTO_SPACING = 64;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The marks along `range`, drawn over `length` pixels. A `step` of zero lets the
|
|
18
|
-
* axis choose a tidy one (1, 2 or 5 times a power of ten) for its size; a log
|
|
19
|
-
* axis is marked at the powers of ten whatever the step.
|
|
20
|
-
*/
|
|
21
|
-
export function ticksFor(range: Interval, step: number, length: number, scaling: AxisScaling = "linear"): Ticks {
|
|
22
|
-
const [min, max] = range;
|
|
23
|
-
if (scaling === "log") return decades(min, max);
|
|
24
|
-
const span = max - min;
|
|
25
|
-
if (!(span > 0)) return { values: [], step: 1 };
|
|
26
|
-
const chosen = step > 0 ? step : niceStep((AUTO_SPACING / Math.max(1, length)) * span);
|
|
27
|
-
return { values: multiplesWithin(min, max, chosen), step: chosen };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function decades(min: number, max: number): Ticks {
|
|
31
|
-
const values: number[] = [];
|
|
32
|
-
for (let k = Math.ceil(Math.log10(min) - 1e-9); k <= Math.floor(Math.log10(max) + 1e-9) && values.length < MAX_TICKS; k++) {
|
|
33
|
-
values.push(10 ** k);
|
|
34
|
-
}
|
|
35
|
-
return { values, step: 0 };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function multiplesWithin(min: number, max: number, step: number): number[] {
|
|
39
|
-
const first = Math.ceil(min / step - 1e-9);
|
|
40
|
-
const last = Math.floor(max / step + 1e-9);
|
|
41
|
-
const count = last - first + 1;
|
|
42
|
-
if (!Number.isFinite(count) || count <= 0 || count > MAX_TICKS) return [];
|
|
43
|
-
const out = new Array<number>(count);
|
|
44
|
-
// Multiplied rather than accumulated, so the tenth tick of 0.1 is 1 and not 0.9999999999999999.
|
|
45
|
-
for (let i = 0; i < count; i++) out[i] = (first + i) * step;
|
|
46
|
-
return out;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function decadeOf(value: number): number {
|
|
50
|
-
return Math.floor(Math.log10(value) + 1e-12);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* `value` as a tick label, with as many decimals as `step` needs and no more:
|
|
55
|
-
* 0.30000000000000004 on a 0.1 ladder is `0.3`. A `step` of zero (a log axis)
|
|
56
|
-
* prints the value as it is.
|
|
57
|
-
*/
|
|
58
|
-
export function formatTick(value: number, step: number): string {
|
|
59
|
-
if (value === 0) return "0";
|
|
60
|
-
if (!Number.isFinite(value)) return "";
|
|
61
|
-
if (!(step > 0)) {
|
|
62
|
-
const decade = decadeOf(Math.abs(value));
|
|
63
|
-
return decade >= 6 || decade <= -5 ? trimExponent(value.toExponential(0)) : String(Number(value.toPrecision(12)));
|
|
64
|
-
}
|
|
65
|
-
const decade = decadeOf(Math.abs(value));
|
|
66
|
-
const digits = Math.max(1, decade - decadeOf(step) + 1);
|
|
67
|
-
if ((decade >= 6 || decade <= -5) && digits <= 6) return trimExponent(value.toExponential(digits - 1));
|
|
68
|
-
const decimals = decimalsOf(step);
|
|
69
|
-
const text = value.toFixed(decimals);
|
|
70
|
-
return decimals > 0 ? text.replace(/\.?0+$/, "") : text;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** The decimal places it takes to write `step` out: 1 for 0.5, 2 for 0.25. */
|
|
74
|
-
function decimalsOf(step: number): number {
|
|
75
|
-
for (let d = 0; d < 12; d++) {
|
|
76
|
-
const scaled = step * 10 ** d;
|
|
77
|
-
if (Math.abs(scaled - Math.round(scaled)) < 1e-9 * Math.max(1, scaled)) return d;
|
|
78
|
-
}
|
|
79
|
-
return 12;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function trimExponent(text: string): string {
|
|
83
|
-
const [mantissa, exponent] = text.split("e");
|
|
84
|
-
return `${mantissa.replace(/\.?0+$/, "")}e${Number(exponent)}`;
|
|
85
|
-
}
|
|
1
|
+
import { niceStep } from "@motionscript/sdk/component";
|
|
2
|
+
import type { AxisScaling, Interval } from "./coords";
|
|
3
|
+
|
|
4
|
+
/** Where an axis is marked, and the spacing its labels are rounded to. */
|
|
5
|
+
export interface Ticks {
|
|
6
|
+
values: number[];
|
|
7
|
+
step: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Past this an axis is not marked at all, rather than drawn as a solid bar of ticks. */
|
|
11
|
+
const MAX_TICKS = 200;
|
|
12
|
+
|
|
13
|
+
/** How far apart, in pixels, marks sit when the axis picks its own step. */
|
|
14
|
+
const AUTO_SPACING = 64;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The marks along `range`, drawn over `length` pixels. A `step` of zero lets the
|
|
18
|
+
* axis choose a tidy one (1, 2 or 5 times a power of ten) for its size; a log
|
|
19
|
+
* axis is marked at the powers of ten whatever the step.
|
|
20
|
+
*/
|
|
21
|
+
export function ticksFor(range: Interval, step: number, length: number, scaling: AxisScaling = "linear"): Ticks {
|
|
22
|
+
const [min, max] = range;
|
|
23
|
+
if (scaling === "log") return decades(min, max);
|
|
24
|
+
const span = max - min;
|
|
25
|
+
if (!(span > 0)) return { values: [], step: 1 };
|
|
26
|
+
const chosen = step > 0 ? step : niceStep((AUTO_SPACING / Math.max(1, length)) * span);
|
|
27
|
+
return { values: multiplesWithin(min, max, chosen), step: chosen };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function decades(min: number, max: number): Ticks {
|
|
31
|
+
const values: number[] = [];
|
|
32
|
+
for (let k = Math.ceil(Math.log10(min) - 1e-9); k <= Math.floor(Math.log10(max) + 1e-9) && values.length < MAX_TICKS; k++) {
|
|
33
|
+
values.push(10 ** k);
|
|
34
|
+
}
|
|
35
|
+
return { values, step: 0 };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function multiplesWithin(min: number, max: number, step: number): number[] {
|
|
39
|
+
const first = Math.ceil(min / step - 1e-9);
|
|
40
|
+
const last = Math.floor(max / step + 1e-9);
|
|
41
|
+
const count = last - first + 1;
|
|
42
|
+
if (!Number.isFinite(count) || count <= 0 || count > MAX_TICKS) return [];
|
|
43
|
+
const out = new Array<number>(count);
|
|
44
|
+
// Multiplied rather than accumulated, so the tenth tick of 0.1 is 1 and not 0.9999999999999999.
|
|
45
|
+
for (let i = 0; i < count; i++) out[i] = (first + i) * step;
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function decadeOf(value: number): number {
|
|
50
|
+
return Math.floor(Math.log10(value) + 1e-12);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `value` as a tick label, with as many decimals as `step` needs and no more:
|
|
55
|
+
* 0.30000000000000004 on a 0.1 ladder is `0.3`. A `step` of zero (a log axis)
|
|
56
|
+
* prints the value as it is.
|
|
57
|
+
*/
|
|
58
|
+
export function formatTick(value: number, step: number): string {
|
|
59
|
+
if (value === 0) return "0";
|
|
60
|
+
if (!Number.isFinite(value)) return "";
|
|
61
|
+
if (!(step > 0)) {
|
|
62
|
+
const decade = decadeOf(Math.abs(value));
|
|
63
|
+
return decade >= 6 || decade <= -5 ? trimExponent(value.toExponential(0)) : String(Number(value.toPrecision(12)));
|
|
64
|
+
}
|
|
65
|
+
const decade = decadeOf(Math.abs(value));
|
|
66
|
+
const digits = Math.max(1, decade - decadeOf(step) + 1);
|
|
67
|
+
if ((decade >= 6 || decade <= -5) && digits <= 6) return trimExponent(value.toExponential(digits - 1));
|
|
68
|
+
const decimals = decimalsOf(step);
|
|
69
|
+
const text = value.toFixed(decimals);
|
|
70
|
+
return decimals > 0 ? text.replace(/\.?0+$/, "") : text;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The decimal places it takes to write `step` out: 1 for 0.5, 2 for 0.25. */
|
|
74
|
+
function decimalsOf(step: number): number {
|
|
75
|
+
for (let d = 0; d < 12; d++) {
|
|
76
|
+
const scaled = step * 10 ** d;
|
|
77
|
+
if (Math.abs(scaled - Math.round(scaled)) < 1e-9 * Math.max(1, scaled)) return d;
|
|
78
|
+
}
|
|
79
|
+
return 12;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function trimExponent(text: string): string {
|
|
83
|
+
const [mantissa, exponent] = text.split("e");
|
|
84
|
+
return `${mantissa.replace(/\.?0+$/, "")}e${Number(exponent)}`;
|
|
85
|
+
}
|
package/src/kit/withdrawn.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The paint entries and commands a math node may have no use for: a node that
|
|
3
|
-
* is only a line has no fill, an area no outline, and one that reveals itself
|
|
4
|
-
* in its own way no outline trim.
|
|
5
|
-
*/
|
|
6
|
-
export const FILL_PAINT = ["fill", "fillTo"] as const;
|
|
7
|
-
export const STROKE_PAINT = ["stroke", "strokeTo"] as const;
|
|
8
|
-
export const OUTLINE_TRIM = ["start", "end", "progressTo", "flash"] as const;
|
|
1
|
+
/**
|
|
2
|
+
* The paint entries and commands a math node may have no use for: a node that
|
|
3
|
+
* is only a line has no fill, an area no outline, and one that reveals itself
|
|
4
|
+
* in its own way no outline trim.
|
|
5
|
+
*/
|
|
6
|
+
export const FILL_PAINT = ["fill", "fillTo"] as const;
|
|
7
|
+
export const STROKE_PAINT = ["stroke", "strokeTo"] as const;
|
|
8
|
+
export const OUTLINE_TRIM = ["start", "end", "progressTo", "flash"] as const;
|