@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/field-mark.ts
CHANGED
|
@@ -1,171 +1,171 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Graphics2D,
|
|
3
|
-
Types,
|
|
4
|
-
type Fill,
|
|
5
|
-
type FillResolved,
|
|
6
|
-
type PaintProps,
|
|
7
|
-
type RenderContext,
|
|
8
|
-
type StrokeResolved,
|
|
9
|
-
type Vector2,
|
|
10
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
11
|
-
type Shadow as _Shadow,
|
|
12
|
-
type ShadowResolved as _ShadowResolved,
|
|
13
|
-
type Stroke as _Stroke,
|
|
14
|
-
} from "@motionscript/sdk";
|
|
15
|
-
import { createBoundedCache } from "./cache";
|
|
16
|
-
import { omit, PAINT, PAINT_COMMANDS } from "./component";
|
|
17
|
-
import type { CoordinateMap } from "./coords";
|
|
18
|
-
import type { ExpressionScope } from "./expression";
|
|
19
|
-
import { fieldParams, parseField } from "./field";
|
|
20
|
-
import { runsToPath } from "./geometry";
|
|
21
|
-
import { PLANE_ENTRY } from "./graph";
|
|
22
|
-
import { drawMark, type MarkPaint, type MarkReading } from "./mark";
|
|
23
|
-
import { fillAt, lerpFillScale, readFillScale } from "./scale";
|
|
24
|
-
import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "./source";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* What a mark drawn from a vector field, `(P(x, y), Q(x, y))`, shares: arrows,
|
|
28
|
-
* or the lines that follow it, all in one stroke. Without strength fills it is
|
|
29
|
-
* one path in the stroke's paint; with them, what it draws is sorted by the
|
|
30
|
-
* field's strength into a few shades.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
export interface FieldProps extends PaintProps {
|
|
34
|
-
expression?: string | PlotSource;
|
|
35
|
-
params?: ExpressionScope;
|
|
36
|
-
strengthFills?: Fill[];
|
|
37
|
-
maxMagnitude?: number;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** A field's vector at a graph point, written into `out`; `false` where it has none. */
|
|
41
|
-
export type FieldSampler = (x: number, y: number, out: Vector2) => boolean;
|
|
42
|
-
|
|
43
|
-
/** One thing a field draws, and how strong the field is there: what it is coloured by. */
|
|
44
|
-
export interface FieldStroke {
|
|
45
|
-
runs: Vector2[][];
|
|
46
|
-
magnitude: number;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** How many shades the strength fills are drawn in: each is one path. */
|
|
50
|
-
const SHADES = 12;
|
|
51
|
-
|
|
52
|
-
/** A field mark's entries. It is only strokes, revealed its own way: it has no fill and trims no outline. */
|
|
53
|
-
export const FIELD_STATE = {
|
|
54
|
-
...omit(PAINT, "fill", "start", "end"),
|
|
55
|
-
expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
|
|
56
|
-
/** The equation's named values: any name in it that is not `x`, `y`, a function or a constant. */
|
|
57
|
-
params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
|
|
58
|
-
/** Paints by the field's strength, weakest first. Empty draws everything in the stroke's own paint. */
|
|
59
|
-
strengthFills: Types.custom<Fill[], FillResolved[][]>([], { resolve: readFillScale, lerp: lerpFillScale, kind: "vector" }),
|
|
60
|
-
/** The strength the last paint stands for; zero takes the strongest the field is where it is drawn. */
|
|
61
|
-
maxMagnitude: Types.number(0, { min: 0, step: 0.1 }),
|
|
62
|
-
graph: PLANE_ENTRY,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const FIELD_COMMANDS = {
|
|
66
|
-
...omit(PAINT_COMMANDS, "fillTo", "progressTo", "flash"),
|
|
67
|
-
/** Change the field, each vector turning from the one into the other. */
|
|
68
|
-
morphTo: { args: ["expression"] as const },
|
|
69
|
-
/** Animate the equation's named values. */
|
|
70
|
-
paramsTo: { args: ["params"] as const },
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/** What a field mark is drawn from. */
|
|
74
|
-
export interface FieldValues {
|
|
75
|
-
readonly expression: PlotSource;
|
|
76
|
-
readonly params: ExpressionScope;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** What one kind of field mark draws of `field` through `map`, and the entries of its own that change it. */
|
|
80
|
-
export interface FieldDrawing<S> {
|
|
81
|
-
strokes(s: S, map: CoordinateMap, field: FieldSampler): FieldStroke[];
|
|
82
|
-
/** The entries of its own that change what `strokes` returns, as a key. */
|
|
83
|
-
own(s: S): string;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
interface Traced {
|
|
87
|
-
readonly drawn: FieldStroke[];
|
|
88
|
-
readonly runs: Vector2[][];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const traces = createBoundedCache<Traced>(64);
|
|
92
|
-
const EMPTY: Traced = { drawn: [], runs: [] };
|
|
93
|
-
|
|
94
|
-
/** What a field mark of `kind` draws through `read`: each stroke with its strength, and all their runs. */
|
|
95
|
-
export function traceField<S extends FieldValues>(kind: string, drawing: FieldDrawing<S>, s: S, { map, shift }: MarkReading): Traced {
|
|
96
|
-
const { area } = map;
|
|
97
|
-
if (!Number.isFinite(area.maxX - area.minX)) return EMPTY;
|
|
98
|
-
const { source, blend } = s.expression;
|
|
99
|
-
const key = [kind, source, blend?.from, blend?.t, map.revision, JSON.stringify(s.params), shift.x, shift.y, drawing.own(s)].join("|");
|
|
100
|
-
const known = traces.get(key);
|
|
101
|
-
if (known) return known;
|
|
102
|
-
const field = sampler(s, shift);
|
|
103
|
-
const drawn = field ? drawing.strokes(s, map, field) : [];
|
|
104
|
-
const traced = { drawn, runs: drawn.flatMap((stroke) => stroke.runs) };
|
|
105
|
-
traces.set(key, traced);
|
|
106
|
-
return traced;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** A field mark's paint: one stroke, or a shade per strength when it has strength fills. */
|
|
110
|
-
export function drawField(
|
|
111
|
-
ctx: RenderContext,
|
|
112
|
-
s: MarkPaint & { readonly strengthFills: FillResolved[][]; readonly maxMagnitude: number },
|
|
113
|
-
{ drawn, runs }: Traced,
|
|
114
|
-
): void {
|
|
115
|
-
const scale = s.strengthFills;
|
|
116
|
-
const stroke = s.stroke as StrokeResolved[];
|
|
117
|
-
const shaded = scale.length > 0 && stroke.length > 0;
|
|
118
|
-
drawMark(ctx, s, runs, {
|
|
119
|
-
ink: shaded
|
|
120
|
-
? (ctx, ink) => {
|
|
121
|
-
const top = s.maxMagnitude > 0 ? s.maxMagnitude : drawn.reduce((most, d) => Math.max(most, d.magnitude), 0);
|
|
122
|
-
const shades: Vector2[][][] = Array.from({ length: SHADES }, () => []);
|
|
123
|
-
for (const d of drawn) {
|
|
124
|
-
const share = top > 0 ? Math.min(1, d.magnitude / top) : 0;
|
|
125
|
-
shades[Math.min(SHADES - 1, Math.floor(share * SHADES))].push(...d.runs);
|
|
126
|
-
}
|
|
127
|
-
shades.forEach((shade, index) => {
|
|
128
|
-
if (shade.length === 0) return;
|
|
129
|
-
const fill = fillAt(scale, (index + 0.5) / SHADES);
|
|
130
|
-
ctx.draw(
|
|
131
|
-
new Graphics2D()
|
|
132
|
-
.path({ data: runsToPath(shade), centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY], start: s.start ?? 0, end: s.end ?? 1 })
|
|
133
|
-
.shadow(s.shadow)
|
|
134
|
-
.stroke(stroke.map((layer) => ({ ...layer, fill }))),
|
|
135
|
-
);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
: undefined,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** The field at a graph point, measured from the mark's own shift, mid-morph a blend of two. */
|
|
143
|
-
function sampler(s: FieldValues, shift: Vector2): FieldSampler | null {
|
|
144
|
-
const { source, blend } = s.expression;
|
|
145
|
-
const to = fieldOf(source, s.params, shift);
|
|
146
|
-
if (!to || !blend) return to;
|
|
147
|
-
const from = fieldOf(blend.from, s.params, shift);
|
|
148
|
-
if (!from) return to;
|
|
149
|
-
const start: Vector2 = { x: 0, y: 0 };
|
|
150
|
-
return (x, y, out) => {
|
|
151
|
-
if (!from(x, y, start) || !to(x, y, out)) return false;
|
|
152
|
-
out.x = start.x + (out.x - start.x) * blend.t;
|
|
153
|
-
out.y = start.y + (out.y - start.y) * blend.t;
|
|
154
|
-
return true;
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function fieldOf(source: string, params: ExpressionScope, shift: Vector2): FieldSampler | null {
|
|
159
|
-
const names = fieldParams(source);
|
|
160
|
-
const field = parseField(source, names);
|
|
161
|
-
if (field instanceof Error) return null;
|
|
162
|
-
const scope = scopeFor(names, params);
|
|
163
|
-
const { x, y } = field;
|
|
164
|
-
return (gx, gy, out) => {
|
|
165
|
-
const vx = x(gx - shift.x, gy - shift.y, scope);
|
|
166
|
-
const vy = y(gx - shift.x, gy - shift.y, scope);
|
|
167
|
-
out.x = vx;
|
|
168
|
-
out.y = vy;
|
|
169
|
-
return Number.isFinite(vx) && Number.isFinite(vy);
|
|
170
|
-
};
|
|
171
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
Types,
|
|
4
|
+
type Fill,
|
|
5
|
+
type FillResolved,
|
|
6
|
+
type PaintProps,
|
|
7
|
+
type RenderContext,
|
|
8
|
+
type StrokeResolved,
|
|
9
|
+
type Vector2,
|
|
10
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
11
|
+
type Shadow as _Shadow,
|
|
12
|
+
type ShadowResolved as _ShadowResolved,
|
|
13
|
+
type Stroke as _Stroke,
|
|
14
|
+
} from "@motionscript/sdk";
|
|
15
|
+
import { createBoundedCache } from "./cache";
|
|
16
|
+
import { omit, PAINT, PAINT_COMMANDS } from "./component";
|
|
17
|
+
import type { CoordinateMap } from "./coords";
|
|
18
|
+
import type { ExpressionScope } from "./expression";
|
|
19
|
+
import { fieldParams, parseField } from "./field";
|
|
20
|
+
import { runsToPath } from "./geometry";
|
|
21
|
+
import { PLANE_ENTRY } from "./graph";
|
|
22
|
+
import { drawMark, type MarkPaint, type MarkReading } from "./mark";
|
|
23
|
+
import { fillAt, lerpFillScale, readFillScale } from "./scale";
|
|
24
|
+
import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "./source";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* What a mark drawn from a vector field, `(P(x, y), Q(x, y))`, shares: arrows,
|
|
28
|
+
* or the lines that follow it, all in one stroke. Without strength fills it is
|
|
29
|
+
* one path in the stroke's paint; with them, what it draws is sorted by the
|
|
30
|
+
* field's strength into a few shades.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
export interface FieldProps extends PaintProps {
|
|
34
|
+
expression?: string | PlotSource;
|
|
35
|
+
params?: ExpressionScope;
|
|
36
|
+
strengthFills?: Fill[];
|
|
37
|
+
maxMagnitude?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A field's vector at a graph point, written into `out`; `false` where it has none. */
|
|
41
|
+
export type FieldSampler = (x: number, y: number, out: Vector2) => boolean;
|
|
42
|
+
|
|
43
|
+
/** One thing a field draws, and how strong the field is there: what it is coloured by. */
|
|
44
|
+
export interface FieldStroke {
|
|
45
|
+
runs: Vector2[][];
|
|
46
|
+
magnitude: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How many shades the strength fills are drawn in: each is one path. */
|
|
50
|
+
const SHADES = 12;
|
|
51
|
+
|
|
52
|
+
/** A field mark's entries. It is only strokes, revealed its own way: it has no fill and trims no outline. */
|
|
53
|
+
export const FIELD_STATE = {
|
|
54
|
+
...omit(PAINT, "fill", "start", "end"),
|
|
55
|
+
expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
|
|
56
|
+
/** The equation's named values: any name in it that is not `x`, `y`, a function or a constant. */
|
|
57
|
+
params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
|
|
58
|
+
/** Paints by the field's strength, weakest first. Empty draws everything in the stroke's own paint. */
|
|
59
|
+
strengthFills: Types.custom<Fill[], FillResolved[][]>([], { resolve: readFillScale, lerp: lerpFillScale, kind: "vector" }),
|
|
60
|
+
/** The strength the last paint stands for; zero takes the strongest the field is where it is drawn. */
|
|
61
|
+
maxMagnitude: Types.number(0, { min: 0, step: 0.1 }),
|
|
62
|
+
graph: PLANE_ENTRY,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const FIELD_COMMANDS = {
|
|
66
|
+
...omit(PAINT_COMMANDS, "fillTo", "progressTo", "flash"),
|
|
67
|
+
/** Change the field, each vector turning from the one into the other. */
|
|
68
|
+
morphTo: { args: ["expression"] as const },
|
|
69
|
+
/** Animate the equation's named values. */
|
|
70
|
+
paramsTo: { args: ["params"] as const },
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** What a field mark is drawn from. */
|
|
74
|
+
export interface FieldValues {
|
|
75
|
+
readonly expression: PlotSource;
|
|
76
|
+
readonly params: ExpressionScope;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What one kind of field mark draws of `field` through `map`, and the entries of its own that change it. */
|
|
80
|
+
export interface FieldDrawing<S> {
|
|
81
|
+
strokes(s: S, map: CoordinateMap, field: FieldSampler): FieldStroke[];
|
|
82
|
+
/** The entries of its own that change what `strokes` returns, as a key. */
|
|
83
|
+
own(s: S): string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface Traced {
|
|
87
|
+
readonly drawn: FieldStroke[];
|
|
88
|
+
readonly runs: Vector2[][];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const traces = createBoundedCache<Traced>(64);
|
|
92
|
+
const EMPTY: Traced = { drawn: [], runs: [] };
|
|
93
|
+
|
|
94
|
+
/** What a field mark of `kind` draws through `read`: each stroke with its strength, and all their runs. */
|
|
95
|
+
export function traceField<S extends FieldValues>(kind: string, drawing: FieldDrawing<S>, s: S, { map, shift }: MarkReading): Traced {
|
|
96
|
+
const { area } = map;
|
|
97
|
+
if (!Number.isFinite(area.maxX - area.minX)) return EMPTY;
|
|
98
|
+
const { source, blend } = s.expression;
|
|
99
|
+
const key = [kind, source, blend?.from, blend?.t, map.revision, JSON.stringify(s.params), shift.x, shift.y, drawing.own(s)].join("|");
|
|
100
|
+
const known = traces.get(key);
|
|
101
|
+
if (known) return known;
|
|
102
|
+
const field = sampler(s, shift);
|
|
103
|
+
const drawn = field ? drawing.strokes(s, map, field) : [];
|
|
104
|
+
const traced = { drawn, runs: drawn.flatMap((stroke) => stroke.runs) };
|
|
105
|
+
traces.set(key, traced);
|
|
106
|
+
return traced;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A field mark's paint: one stroke, or a shade per strength when it has strength fills. */
|
|
110
|
+
export function drawField(
|
|
111
|
+
ctx: RenderContext,
|
|
112
|
+
s: MarkPaint & { readonly strengthFills: FillResolved[][]; readonly maxMagnitude: number },
|
|
113
|
+
{ drawn, runs }: Traced,
|
|
114
|
+
): void {
|
|
115
|
+
const scale = s.strengthFills;
|
|
116
|
+
const stroke = s.stroke as StrokeResolved[];
|
|
117
|
+
const shaded = scale.length > 0 && stroke.length > 0;
|
|
118
|
+
drawMark(ctx, s, runs, {
|
|
119
|
+
ink: shaded
|
|
120
|
+
? (ctx, ink) => {
|
|
121
|
+
const top = s.maxMagnitude > 0 ? s.maxMagnitude : drawn.reduce((most, d) => Math.max(most, d.magnitude), 0);
|
|
122
|
+
const shades: Vector2[][][] = Array.from({ length: SHADES }, () => []);
|
|
123
|
+
for (const d of drawn) {
|
|
124
|
+
const share = top > 0 ? Math.min(1, d.magnitude / top) : 0;
|
|
125
|
+
shades[Math.min(SHADES - 1, Math.floor(share * SHADES))].push(...d.runs);
|
|
126
|
+
}
|
|
127
|
+
shades.forEach((shade, index) => {
|
|
128
|
+
if (shade.length === 0) return;
|
|
129
|
+
const fill = fillAt(scale, (index + 0.5) / SHADES);
|
|
130
|
+
ctx.draw(
|
|
131
|
+
new Graphics2D()
|
|
132
|
+
.path({ data: runsToPath(shade), centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY], start: s.start ?? 0, end: s.end ?? 1 })
|
|
133
|
+
.shadow(s.shadow)
|
|
134
|
+
.stroke(stroke.map((layer) => ({ ...layer, fill }))),
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
: undefined,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The field at a graph point, measured from the mark's own shift, mid-morph a blend of two. */
|
|
143
|
+
function sampler(s: FieldValues, shift: Vector2): FieldSampler | null {
|
|
144
|
+
const { source, blend } = s.expression;
|
|
145
|
+
const to = fieldOf(source, s.params, shift);
|
|
146
|
+
if (!to || !blend) return to;
|
|
147
|
+
const from = fieldOf(blend.from, s.params, shift);
|
|
148
|
+
if (!from) return to;
|
|
149
|
+
const start: Vector2 = { x: 0, y: 0 };
|
|
150
|
+
return (x, y, out) => {
|
|
151
|
+
if (!from(x, y, start) || !to(x, y, out)) return false;
|
|
152
|
+
out.x = start.x + (out.x - start.x) * blend.t;
|
|
153
|
+
out.y = start.y + (out.y - start.y) * blend.t;
|
|
154
|
+
return true;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function fieldOf(source: string, params: ExpressionScope, shift: Vector2): FieldSampler | null {
|
|
159
|
+
const names = fieldParams(source);
|
|
160
|
+
const field = parseField(source, names);
|
|
161
|
+
if (field instanceof Error) return null;
|
|
162
|
+
const scope = scopeFor(names, params);
|
|
163
|
+
const { x, y } = field;
|
|
164
|
+
return (gx, gy, out) => {
|
|
165
|
+
const vx = x(gx - shift.x, gy - shift.y, scope);
|
|
166
|
+
const vy = y(gx - shift.x, gy - shift.y, scope);
|
|
167
|
+
out.x = vx;
|
|
168
|
+
out.y = vy;
|
|
169
|
+
return Number.isFinite(vx) && Number.isFinite(vy);
|
|
170
|
+
};
|
|
171
|
+
}
|
package/src/kit/field.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import { createBoundedCache } from "./cache";
|
|
2
|
-
import { equationNames, splitTuple } from "./equation";
|
|
3
|
-
import { compileExpressionCached, type CompiledExpression } from "./expression";
|
|
4
|
-
|
|
5
|
-
/** A vector at every point of the plane, two parts in `x` and `y`: `(-y, x)`. A warp is written the same way. */
|
|
6
|
-
export interface FieldEquation {
|
|
7
|
-
x: CompiledExpression;
|
|
8
|
-
y: CompiledExpression;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const FIELD_VARIABLES = ["x", "y"] as const;
|
|
12
|
-
|
|
13
|
-
const NAMES: ReadonlySet<string> = new Set(FIELD_VARIABLES);
|
|
14
|
-
const fields = createBoundedCache<FieldEquation | Error>(128);
|
|
15
|
-
|
|
16
|
-
/** The named values a field's equation reads: every name that is not `x` or `y`. */
|
|
17
|
-
export function fieldParams(source: string): readonly string[] {
|
|
18
|
-
return equationNames(source).filter((name) => !NAMES.has(name));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** `source` as a field, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
|
|
22
|
-
export function parseField(source: string, params: readonly string[] = []): FieldEquation | Error {
|
|
23
|
-
const key = `${params.join(",")}|${source}`;
|
|
24
|
-
const hit = fields.get(key);
|
|
25
|
-
if (hit !== undefined) return hit;
|
|
26
|
-
let result: FieldEquation | Error;
|
|
27
|
-
try {
|
|
28
|
-
const text = source.trim();
|
|
29
|
-
if (text === "") throw new Error("Empty equation");
|
|
30
|
-
const parts = splitTuple(text);
|
|
31
|
-
if (parts?.length !== 2) throw new Error("A field has two parts: (x, y)");
|
|
32
|
-
const [x, y] = parts.map((part) => {
|
|
33
|
-
const compiled = compileExpressionCached(part.trim(), FIELD_VARIABLES, params);
|
|
34
|
-
if (compiled instanceof Error) throw compiled;
|
|
35
|
-
return compiled;
|
|
36
|
-
});
|
|
37
|
-
result = { x, y };
|
|
38
|
-
} catch (error) {
|
|
39
|
-
result = error instanceof Error ? error : new Error(String(error));
|
|
40
|
-
}
|
|
41
|
-
fields.set(key, result);
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Why `source` is not a field, or `null`; empty text is not an error. */
|
|
46
|
-
export function fieldError(source: string): string | null {
|
|
47
|
-
if (source.trim() === "") return null;
|
|
48
|
-
const field = parseField(source, fieldParams(source));
|
|
49
|
-
return field instanceof Error ? field.message : null;
|
|
50
|
-
}
|
|
1
|
+
import { createBoundedCache } from "./cache";
|
|
2
|
+
import { equationNames, splitTuple } from "./equation";
|
|
3
|
+
import { compileExpressionCached, type CompiledExpression } from "./expression";
|
|
4
|
+
|
|
5
|
+
/** A vector at every point of the plane, two parts in `x` and `y`: `(-y, x)`. A warp is written the same way. */
|
|
6
|
+
export interface FieldEquation {
|
|
7
|
+
x: CompiledExpression;
|
|
8
|
+
y: CompiledExpression;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const FIELD_VARIABLES = ["x", "y"] as const;
|
|
12
|
+
|
|
13
|
+
const NAMES: ReadonlySet<string> = new Set(FIELD_VARIABLES);
|
|
14
|
+
const fields = createBoundedCache<FieldEquation | Error>(128);
|
|
15
|
+
|
|
16
|
+
/** The named values a field's equation reads: every name that is not `x` or `y`. */
|
|
17
|
+
export function fieldParams(source: string): readonly string[] {
|
|
18
|
+
return equationNames(source).filter((name) => !NAMES.has(name));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** `source` as a field, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
|
|
22
|
+
export function parseField(source: string, params: readonly string[] = []): FieldEquation | Error {
|
|
23
|
+
const key = `${params.join(",")}|${source}`;
|
|
24
|
+
const hit = fields.get(key);
|
|
25
|
+
if (hit !== undefined) return hit;
|
|
26
|
+
let result: FieldEquation | Error;
|
|
27
|
+
try {
|
|
28
|
+
const text = source.trim();
|
|
29
|
+
if (text === "") throw new Error("Empty equation");
|
|
30
|
+
const parts = splitTuple(text);
|
|
31
|
+
if (parts?.length !== 2) throw new Error("A field has two parts: (x, y)");
|
|
32
|
+
const [x, y] = parts.map((part) => {
|
|
33
|
+
const compiled = compileExpressionCached(part.trim(), FIELD_VARIABLES, params);
|
|
34
|
+
if (compiled instanceof Error) throw compiled;
|
|
35
|
+
return compiled;
|
|
36
|
+
});
|
|
37
|
+
result = { x, y };
|
|
38
|
+
} catch (error) {
|
|
39
|
+
result = error instanceof Error ? error : new Error(String(error));
|
|
40
|
+
}
|
|
41
|
+
fields.set(key, result);
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Why `source` is not a field, or `null`; empty text is not an error. */
|
|
46
|
+
export function fieldError(source: string): string | null {
|
|
47
|
+
if (source.trim() === "") return null;
|
|
48
|
+
const field = parseField(source, fieldParams(source));
|
|
49
|
+
return field instanceof Error ? field.message : null;
|
|
50
|
+
}
|