@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
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
strokeProperty,
|
|
6
|
-
type Command,
|
|
7
|
-
type CommandArgs,
|
|
8
|
-
type NodeConfig,
|
|
9
|
-
type Stroke,
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
10
5
|
type Vector2,
|
|
6
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
7
|
+
type Fill as _Fill,
|
|
8
|
+
type FillResolved as _FillResolved,
|
|
9
|
+
type Node2D as _Node2D,
|
|
10
|
+
type Node2DProps as _Node2DProps,
|
|
11
|
+
type Shadow as _Shadow,
|
|
12
|
+
type ShadowResolved as _ShadowResolved,
|
|
13
|
+
type Stroke as _Stroke,
|
|
14
|
+
type StrokeResolved as _StrokeResolved,
|
|
11
15
|
} from "@motionscript/sdk";
|
|
12
16
|
import type { CoordinateMap } from "../kit/coords";
|
|
13
|
-
import {
|
|
17
|
+
import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldSampler, type FieldStroke, type FieldValues } from "../kit/field-mark";
|
|
14
18
|
import { PLOT_COLOR } from "../kit/furniture";
|
|
15
|
-
import { clamp
|
|
16
|
-
import {
|
|
19
|
+
import { clamp } from "../kit/geometry";
|
|
20
|
+
import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
17
21
|
|
|
18
22
|
export interface StreamLinesProps extends FieldProps {
|
|
19
23
|
spacing?: number;
|
|
@@ -27,73 +31,78 @@ const MAX_LINES = 1600;
|
|
|
27
31
|
const STEPS_PER_SPACING = 8;
|
|
28
32
|
const MAX_STEPS = 600;
|
|
29
33
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* grid: `(-y, x)` draws circles. With a `window` below one, each line shows
|
|
33
|
-
* only a stretch of itself, and `flowTo` sends those stretches along.
|
|
34
|
-
*/
|
|
35
|
-
@node({
|
|
36
|
-
key: "streamLines",
|
|
37
|
-
within: SYSTEMS,
|
|
38
|
-
parentKey: "shape",
|
|
39
|
-
forkable: true,
|
|
40
|
-
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
|
|
41
|
-
seed: { expression: "(sin(y), cos(x))" },
|
|
42
|
-
})
|
|
43
|
-
export class StreamLines extends FieldMark<StreamLinesProps> {
|
|
34
|
+
const state = {
|
|
35
|
+
...FIELD_STATE,
|
|
44
36
|
/** How far apart the lines start, in graph units. */
|
|
45
|
-
|
|
37
|
+
spacing: Types.number(1, { min: 0.01, step: 0.1 }),
|
|
46
38
|
/** How far each line runs from where it starts, in graph units along itself. */
|
|
47
|
-
|
|
39
|
+
length: Types.number(3, { min: 0, step: 0.1 }),
|
|
48
40
|
/** The share of each line that is shown at once; one shows all of it. */
|
|
49
|
-
|
|
41
|
+
window: Types.number(1, { min: 0.01, max: 1, step: 0.01 }),
|
|
50
42
|
/** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
|
|
51
|
-
|
|
43
|
+
phase: Types.number(0, { step: 0.01 }),
|
|
44
|
+
};
|
|
52
45
|
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
interface LinesValues extends FieldValues {
|
|
47
|
+
readonly spacing: number;
|
|
48
|
+
readonly length: number;
|
|
49
|
+
readonly window: number;
|
|
50
|
+
readonly phase: number;
|
|
51
|
+
}
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
const LINES: FieldDrawing<LinesValues> = {
|
|
54
|
+
own: (s) => `${s.spacing}|${s.length}|${s.window}|${s.phase}`,
|
|
55
|
+
strokes: (s, map, field) => lines(s, map, field),
|
|
56
|
+
};
|
|
59
57
|
|
|
60
|
-
|
|
61
|
-
@command({ args: ["phase"] })
|
|
62
|
-
flowTo(args: CommandArgs<{ phase: number }> & { duration: number }): Command<StreamLinesProps> {
|
|
63
|
-
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
64
|
-
}
|
|
58
|
+
const traceLines = (s: LinesValues, read: MarkReading) => traceField("streamLines", LINES, s, read);
|
|
65
59
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
/**
|
|
61
|
+
* The lines a vector field carries a point along, one from each point of a
|
|
62
|
+
* grid: `(-y, x)` draws circles. With a `window` below one, each line shows
|
|
63
|
+
* only a stretch of itself, and `flowTo` sends those stretches along.
|
|
64
|
+
*/
|
|
65
|
+
export const StreamLines = traced(component2d({
|
|
66
|
+
key: "streamLines",
|
|
67
|
+
state,
|
|
68
|
+
defaults: { stroke: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
69
|
+
commands: {
|
|
70
|
+
...FIELD_COMMANDS,
|
|
71
|
+
/** Send the shown stretches along their lines: a whole number runs each line that many times over. */
|
|
72
|
+
flowTo: { args: ["phase"] },
|
|
73
|
+
},
|
|
74
|
+
measure: (_measurer, s) => inkSize(traceLines(s, reading(s)).runs),
|
|
75
|
+
render: (ctx, s) => drawField(ctx, s, traceLines(s, reading(s))),
|
|
76
|
+
}), (s: LinesValues, read: MarkReading) => traceLines(s, read).runs);
|
|
77
|
+
|
|
78
|
+
export type StreamLines = NodeOf<typeof StreamLines>;
|
|
69
79
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
function lines(s: LinesValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
|
|
81
|
+
const [x0, x1] = map.xRange;
|
|
82
|
+
const [y0, y1] = map.yRange;
|
|
83
|
+
let spacing = Math.max(s.spacing, 1e-6);
|
|
84
|
+
const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
|
|
85
|
+
while (count() > MAX_LINES) spacing *= 2;
|
|
86
|
+
const step = spacing / STEPS_PER_SPACING;
|
|
87
|
+
const steps = Math.min(MAX_STEPS, Math.round(s.length / step));
|
|
88
|
+
if (steps < 1) return [];
|
|
89
|
+
// A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
|
|
90
|
+
const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
|
|
91
|
+
const window = clamp(s.window, 0, 1);
|
|
82
92
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
93
|
+
const drawn: FieldStroke[] = [];
|
|
94
|
+
let index = 0;
|
|
95
|
+
for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
|
|
96
|
+
for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
|
|
97
|
+
const line = follow(field, i * spacing, j * spacing, step, steps, inside);
|
|
98
|
+
index++;
|
|
99
|
+
if (line.points.length < 2) continue;
|
|
100
|
+
const shown = window >= 1 ? line.points : stretch(line.points, window, s.phase + stagger(index));
|
|
101
|
+
if (shown.length < 2) continue;
|
|
102
|
+
drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
|
|
94
103
|
}
|
|
95
|
-
return drawn;
|
|
96
104
|
}
|
|
105
|
+
return drawn;
|
|
97
106
|
}
|
|
98
107
|
|
|
99
108
|
/** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
|
package/src/plots/traced-path.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
6
|
+
type Node2D as _Node2D,
|
|
7
|
+
type Node2DProps as _Node2DProps,
|
|
8
|
+
type Shadow as _Shadow,
|
|
9
|
+
type ShadowResolved as _ShadowResolved,
|
|
10
|
+
type Stroke as _Stroke,
|
|
11
|
+
type StrokeResolved as _StrokeResolved,
|
|
12
|
+
} from "@motionscript/sdk";
|
|
13
|
+
import { omit } from "../kit/component";
|
|
2
14
|
import type { Interval } from "../kit/coords";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { FunctionPlot, type FunctionPlotProps } from "./function-plot";
|
|
6
|
-
import { OUTLINE_TRIM } from "../kit/withdrawn";
|
|
15
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
16
|
+
import { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot, type FunctionPlotProps, type PlotValues } from "./function-plot";
|
|
7
17
|
|
|
8
18
|
export interface TracedPathProps extends FunctionPlotProps {
|
|
9
19
|
from?: number;
|
|
@@ -11,42 +21,50 @@ export interface TracedPathProps extends FunctionPlotProps {
|
|
|
11
21
|
tail?: number;
|
|
12
22
|
}
|
|
13
23
|
|
|
24
|
+
const state = {
|
|
25
|
+
// It reveals itself by its value, so it trims no outline of its own.
|
|
26
|
+
...omit(PLOT_STATE, "start", "end"),
|
|
27
|
+
/** The value the trail starts at. */
|
|
28
|
+
from: Types.number(0, { step: 0.1 }),
|
|
29
|
+
/** The value the point is at now: where the trail ends. */
|
|
30
|
+
value: Types.number(0, { step: 0.1 }),
|
|
31
|
+
/** How much of the trail is kept, in the value's own units; zero keeps all of it. */
|
|
32
|
+
tail: Types.number(0, { min: 0, step: 0.1 }),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface TrailValues extends PlotValues {
|
|
36
|
+
readonly from: number;
|
|
37
|
+
readonly value: number;
|
|
38
|
+
readonly tail: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
14
41
|
/**
|
|
15
42
|
* The trail a point leaves as a value changes: the curve of its equation from
|
|
16
43
|
* where the value began to where it is now. Bind `value` to a variable
|
|
17
44
|
* (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
|
|
18
45
|
* `traceTo`; a `tail` keeps only its most recent stretch.
|
|
19
46
|
*/
|
|
20
|
-
|
|
47
|
+
export const TracedPath = traced(component2d({
|
|
21
48
|
key: "tracedPath",
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/** The value the point is at now: where the trail ends. */
|
|
33
|
-
@property({ default: 0, step: 0.1 }) declare value: number;
|
|
34
|
-
/** How much of the trail is kept, in the value's own units; zero keeps all of it. */
|
|
35
|
-
@property({ default: 0, min: 0, step: 0.1 }) declare tail: number;
|
|
49
|
+
state,
|
|
50
|
+
defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
|
|
51
|
+
commands: {
|
|
52
|
+
...omit(PLOT_COMMANDS, "progressTo", "flash"),
|
|
53
|
+
/** Carry the point on to another value, the trail following it. */
|
|
54
|
+
traceTo: { args: ["value"] },
|
|
55
|
+
},
|
|
56
|
+
measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),
|
|
57
|
+
render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),
|
|
58
|
+
}), traceTrail);
|
|
36
59
|
|
|
37
|
-
|
|
38
|
-
super((props ?? {}) as NodeConfig<FunctionPlot<TracedPathProps>, TracedPathProps>);
|
|
39
|
-
}
|
|
60
|
+
export type TracedPath = NodeOf<typeof TracedPath>;
|
|
40
61
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
45
|
-
}
|
|
62
|
+
function traceTrail(s: TrailValues, read: MarkReading) {
|
|
63
|
+
return tracePlot(s, read, span(s));
|
|
64
|
+
}
|
|
46
65
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
66
|
+
/** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */
|
|
67
|
+
function span({ from, value, tail }: TrailValues): Interval {
|
|
68
|
+
const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
|
|
69
|
+
return value >= from ? [value - kept, value] : [value, value + kept];
|
|
52
70
|
}
|
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type Vector2,
|
|
6
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
7
|
+
type Fill as _Fill,
|
|
8
|
+
type FillResolved as _FillResolved,
|
|
9
|
+
type Node2D as _Node2D,
|
|
10
|
+
type Node2DProps as _Node2DProps,
|
|
11
|
+
type Shadow as _Shadow,
|
|
12
|
+
type ShadowResolved as _ShadowResolved,
|
|
13
|
+
type Stroke as _Stroke,
|
|
14
|
+
type StrokeResolved as _StrokeResolved,
|
|
15
|
+
} from "@motionscript/sdk";
|
|
2
16
|
import type { CoordinateMap } from "../kit/coords";
|
|
3
|
-
import {
|
|
17
|
+
import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldValues, type FieldSampler, type FieldStroke } from "../kit/field-mark";
|
|
4
18
|
import { PLOT_COLOR } from "../kit/furniture";
|
|
5
|
-
import {
|
|
19
|
+
import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
6
20
|
|
|
7
21
|
export interface VectorFieldProps extends FieldProps {
|
|
8
22
|
spacing?: number;
|
|
@@ -16,63 +30,68 @@ const HEAD_SHARE = 0.38;
|
|
|
16
30
|
const HEAD_MAX = 11;
|
|
17
31
|
const HEAD_ANGLE = (26 * Math.PI) / 180;
|
|
18
32
|
|
|
33
|
+
const state = {
|
|
34
|
+
...FIELD_STATE,
|
|
35
|
+
/** How far apart the arrows stand, in graph units. */
|
|
36
|
+
spacing: Types.number(1, { min: 0.01, step: 0.1 }),
|
|
37
|
+
/** How long an arrow is drawn for a vector of length one, in graph units. */
|
|
38
|
+
arrowScale: Types.number(0.4, { min: 0, step: 0.05 }),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
interface ArrowsValues extends FieldValues {
|
|
42
|
+
readonly spacing: number;
|
|
43
|
+
readonly arrowScale: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const ARROWS: FieldDrawing<ArrowsValues> = {
|
|
47
|
+
own: (s) => `${s.spacing}|${s.arrowScale}`,
|
|
48
|
+
strokes: (s, map, field) => arrows(s, map, field),
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const traceArrows = (s: ArrowsValues, read: MarkReading) => traceField("vectorField", ARROWS, s, read);
|
|
52
|
+
|
|
19
53
|
/**
|
|
20
54
|
* A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
|
|
21
55
|
* Each arrow is centred on its point and is as long as the field is strong
|
|
22
56
|
* there, up to the spacing.
|
|
23
57
|
*/
|
|
24
|
-
|
|
58
|
+
export const VectorField = traced(component2d({
|
|
25
59
|
key: "vectorField",
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
32
|
-
export class VectorField extends FieldMark<VectorFieldProps> {
|
|
33
|
-
/** How far apart the arrows stand, in graph units. */
|
|
34
|
-
@property({ default: 1, min: 0.01, step: 0.1 }) declare spacing: number;
|
|
35
|
-
/** How long an arrow is drawn for a vector of length one, in graph units. */
|
|
36
|
-
@property({ default: 0.4, min: 0, step: 0.05 }) declare arrowScale: number;
|
|
37
|
-
|
|
38
|
-
@strokeProperty({ default: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
|
|
39
|
-
declare stroke: Stroke;
|
|
60
|
+
state,
|
|
61
|
+
defaults: { stroke: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
62
|
+
commands: FIELD_COMMANDS,
|
|
63
|
+
measure: (_measurer, s) => inkSize(traceArrows(s, reading(s)).runs),
|
|
64
|
+
render: (ctx, s) => drawField(ctx, s, traceArrows(s, reading(s))),
|
|
65
|
+
}), (s: ArrowsValues, read: MarkReading) => traceArrows(s, read).runs);
|
|
40
66
|
|
|
41
|
-
|
|
42
|
-
super(props ?? {});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
protected own(): string {
|
|
46
|
-
return `${this.spacing}|${this.arrowScale}`;
|
|
47
|
-
}
|
|
67
|
+
export type VectorField = NodeOf<typeof VectorField>;
|
|
48
68
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
function arrows(s: ArrowsValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
|
|
70
|
+
const [x0, x1] = map.xRange;
|
|
71
|
+
const [y0, y1] = map.yRange;
|
|
72
|
+
let spacing = Math.max(s.spacing, 1e-6);
|
|
73
|
+
const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
|
|
74
|
+
while (count() > MAX_ARROWS) spacing *= 2;
|
|
55
75
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
76
|
+
const drawn: FieldStroke[] = [];
|
|
77
|
+
const v: Vector2 = { x: 0, y: 0 };
|
|
78
|
+
const longest = spacing * 0.9;
|
|
79
|
+
for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
|
|
80
|
+
for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
|
|
81
|
+
const gx = i * spacing;
|
|
82
|
+
const gy = j * spacing;
|
|
83
|
+
if (!field(gx, gy, v)) continue;
|
|
84
|
+
const magnitude = Math.hypot(v.x, v.y);
|
|
85
|
+
const length = Math.min(magnitude * s.arrowScale, longest);
|
|
86
|
+
if (!(length > 0)) continue;
|
|
87
|
+
const hx = ((v.x / magnitude) * length) / 2;
|
|
88
|
+
const hy = ((v.y / magnitude) * length) / 2;
|
|
89
|
+
const tail = map.toPoint(gx - hx, gy - hy);
|
|
90
|
+
const head = map.toPoint(gx + hx, gy + hy);
|
|
91
|
+
drawn.push({ runs: arrow(tail, head), magnitude });
|
|
73
92
|
}
|
|
74
|
-
return drawn;
|
|
75
93
|
}
|
|
94
|
+
return drawn;
|
|
76
95
|
}
|
|
77
96
|
|
|
78
97
|
/** A shaft and the two barbs of its head, in pixels. */
|