@motionscript/math 0.1.0-alpha.5 → 0.1.0-alpha.7
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 +28 -0
- package/README.md +39 -33
- package/dist/annotate/angle-mark.d.ts +77 -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 +94 -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 +69 -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 +56 -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 +37 -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 +45 -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 +56 -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 +47 -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 +58 -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 +48 -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 +135 -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 +55 -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 +60 -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 +54 -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 +48 -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 +140 -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 +90 -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 +159 -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 +80 -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 +140 -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 +76 -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 +88 -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 +84 -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 +214 -54
- package/dist/three/axes3d.d.ts.map +1 -1
- package/dist/three/axes3d.js +163 -204
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +108 -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 +85 -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 +176 -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 +87 -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 +187 -160
- 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,17 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type NodeConfig,
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type NodeView,
|
|
7
6
|
type PaintProps,
|
|
8
|
-
type Stroke,
|
|
9
7
|
type Vector2,
|
|
8
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
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,
|
|
10
15
|
} from "@motionscript/sdk";
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
16
|
+
import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
17
|
+
import { curveValue, type CurveValues } from "../kit/curve";
|
|
18
|
+
import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
|
|
19
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
15
20
|
|
|
16
21
|
export interface TangentLineProps extends PaintProps {
|
|
17
22
|
on?: string;
|
|
@@ -23,81 +28,92 @@ export interface TangentLineProps extends PaintProps {
|
|
|
23
28
|
/** The step a slope is measured over, as a share of the system's width. */
|
|
24
29
|
const SLOPE_STEP = 1e-4;
|
|
25
30
|
|
|
31
|
+
const state = {
|
|
32
|
+
...omit(PAINT, "fill"),
|
|
33
|
+
on: PLOT_REF,
|
|
34
|
+
length: Types.number(260, { min: 0, unit: "px" }),
|
|
35
|
+
/** The second point's distance along x; zero is the tangent itself. */
|
|
36
|
+
dx: Types.number(0, { min: 0 }),
|
|
37
|
+
legs: Types.boolean(false, { animatable: false }),
|
|
38
|
+
graph: PLANE_ENTRY,
|
|
39
|
+
/** The slope drawn, in graph units: the curve's at `x`, or the secant's when there is a `dx`. A formula reads it back as `[key.slope]`. */
|
|
40
|
+
slope: Types.number(NaN, { private: true }),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
interface TangentValues {
|
|
44
|
+
readonly x: number;
|
|
45
|
+
readonly on: NodeView | null;
|
|
46
|
+
readonly length: number;
|
|
47
|
+
readonly dx: number;
|
|
48
|
+
readonly legs: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
26
51
|
/**
|
|
27
52
|
* The tangent to a plot at this node's `x`, a fixed length on screen. With a
|
|
28
53
|
* `dx` it is the secant through `x` and `x + dx` instead, and `legs` draws that
|
|
29
54
|
* secant's run and rise.
|
|
30
55
|
*/
|
|
31
|
-
|
|
56
|
+
export const TangentLine = traced(component2d({
|
|
32
57
|
key: "tangentLine",
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
58
|
+
state,
|
|
59
|
+
defaults: {
|
|
60
|
+
stroke: { weight: 3, fill: "#f59e0b", align: "center", cap: "round", join: "round" },
|
|
61
|
+
width: "hug",
|
|
62
|
+
height: "hug",
|
|
63
|
+
slope: (s) => {
|
|
64
|
+
const plot = curveFor(s.graph, s.on);
|
|
65
|
+
return plot ? tangentSlope(plot, s.x, s.dx) : NaN;
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
commands: omit(PAINT_COMMANDS, "fillTo"),
|
|
69
|
+
measure: (_measurer, s) => inkSize(traceTangent(s, reading(s))),
|
|
70
|
+
render: (ctx, s) => drawMark(ctx, s, traceTangent(s, reading(s))),
|
|
71
|
+
}), traceTangent);
|
|
45
72
|
|
|
46
|
-
|
|
47
|
-
declare stroke: Stroke;
|
|
73
|
+
export type TangentLine = NodeOf<typeof TangentLine>;
|
|
48
74
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const plot = curveOf(this, this.on);
|
|
56
|
-
if (!plot) return NaN;
|
|
57
|
-
if (this.dx > 0) return (plot.valueAt(this.x + this.dx) - plot.valueAt(this.x)) / this.dx;
|
|
58
|
-
return slopeAt(plot, this.x, 1e-6 * Math.max(1, Math.abs(this.x)));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
protected trace(map: CoordinateMap): Vector2[][] {
|
|
62
|
-
const plot = curveOf(this, this.on);
|
|
63
|
-
if (!plot) return [];
|
|
64
|
-
const x = this.x;
|
|
65
|
-
const y = plot.valueAt(x);
|
|
66
|
-
if (!Number.isFinite(y)) return [];
|
|
67
|
-
const half = this.length / 2;
|
|
75
|
+
/** The slope a tangent at `x` draws on `curve`, in graph units: the curve's at `x`, or the secant's to `x + dx`. */
|
|
76
|
+
export function tangentSlope(curve: CurveValues, x: number, dx = 0): number {
|
|
77
|
+
if (dx > 0) return (curveValue(curve, x + dx) - curveValue(curve, x)) / dx;
|
|
78
|
+
const h = 1e-6 * Math.max(1, Math.abs(x));
|
|
79
|
+
return (curveValue(curve, x + h) - curveValue(curve, x - h)) / (2 * h);
|
|
80
|
+
}
|
|
68
81
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const reach = Math.max(half, span / 2 + 16);
|
|
77
|
-
const ux = (b.x - a.x) / span;
|
|
78
|
-
const uy = (b.y - a.y) / span;
|
|
79
|
-
const mx = (a.x + b.x) / 2;
|
|
80
|
-
const my = (a.y + b.y) / 2;
|
|
81
|
-
const secant = [{ x: mx - ux * reach, y: my - uy * reach }, { x: mx + ux * reach, y: my + uy * reach }];
|
|
82
|
-
return this.legs ? [secant, [a, map.toPoint(x + this.dx, y), b]] : [secant];
|
|
83
|
-
}
|
|
82
|
+
function traceTangent(s: TangentValues, { map, plane }: MarkReading): Vector2[][] {
|
|
83
|
+
const plot = curveFor(plane, s.on);
|
|
84
|
+
if (!plot) return [];
|
|
85
|
+
const x = s.x;
|
|
86
|
+
const y = curveValue(plot, x);
|
|
87
|
+
if (!Number.isFinite(y)) return [];
|
|
88
|
+
const half = s.length / 2;
|
|
84
89
|
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
const p = map.toPoint(x, y);
|
|
91
|
-
const a = map.toPoint(x - h, before);
|
|
92
|
-
const b = map.toPoint(x + h, after);
|
|
90
|
+
if (s.dx > 0) {
|
|
91
|
+
const y2 = curveValue(plot, x + s.dx);
|
|
92
|
+
if (!Number.isFinite(y2)) return [];
|
|
93
|
+
const a = map.toPoint(x, y);
|
|
94
|
+
const b = map.toPoint(x + s.dx, y2);
|
|
93
95
|
const span = Math.hypot(b.x - a.x, b.y - a.y);
|
|
94
96
|
if (span === 0) return [];
|
|
97
|
+
const reach = Math.max(half, span / 2 + 16);
|
|
95
98
|
const ux = (b.x - a.x) / span;
|
|
96
99
|
const uy = (b.y - a.y) / span;
|
|
97
|
-
|
|
100
|
+
const mx = (a.x + b.x) / 2;
|
|
101
|
+
const my = (a.y + b.y) / 2;
|
|
102
|
+
const secant = [{ x: mx - ux * reach, y: my - uy * reach }, { x: mx + ux * reach, y: my + uy * reach }];
|
|
103
|
+
return s.legs ? [secant, [a, map.toPoint(x + s.dx, y), b]] : [secant];
|
|
98
104
|
}
|
|
99
|
-
}
|
|
100
105
|
|
|
101
|
-
|
|
102
|
-
|
|
106
|
+
const [xMin, xMax] = map.xRange;
|
|
107
|
+
const h = Number.isFinite(xMax - xMin) ? (xMax - xMin) * SLOPE_STEP : 1e-4;
|
|
108
|
+
const before = curveValue(plot, x - h);
|
|
109
|
+
const after = curveValue(plot, x + h);
|
|
110
|
+
if (!Number.isFinite(before) || !Number.isFinite(after)) return [];
|
|
111
|
+
const p = map.toPoint(x, y);
|
|
112
|
+
const a = map.toPoint(x - h, before);
|
|
113
|
+
const b = map.toPoint(x + h, after);
|
|
114
|
+
const span = Math.hypot(b.x - a.x, b.y - a.y);
|
|
115
|
+
if (span === 0) return [];
|
|
116
|
+
const ux = (b.x - a.x) / span;
|
|
117
|
+
const uy = (b.y - a.y) / span;
|
|
118
|
+
return [[{ x: p.x - ux * half, y: p.y - uy * half }, { x: p.x + ux * half, y: p.y + uy * half }]];
|
|
103
119
|
}
|
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
strokeProperty,
|
|
6
|
-
vector2Property,
|
|
7
|
-
withdraw,
|
|
8
|
-
type Command,
|
|
9
|
-
type CommandArgs,
|
|
10
|
-
type NodeConfig,
|
|
11
|
-
type NodeHandle,
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
12
5
|
type PaintProps,
|
|
13
|
-
type Stroke,
|
|
14
6
|
type Vector2,
|
|
7
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
+
type Node2D as _Node2D,
|
|
9
|
+
type Node2DProps as _Node2DProps,
|
|
10
|
+
type Shadow as _Shadow,
|
|
11
|
+
type ShadowResolved as _ShadowResolved,
|
|
12
|
+
type Stroke as _Stroke,
|
|
13
|
+
type StrokeResolved as _StrokeResolved,
|
|
15
14
|
} from "@motionscript/sdk";
|
|
16
|
-
import
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import { SYSTEMS } from "../kit/placement";
|
|
20
|
-
import { FILL_PAINT } from "../kit/withdrawn";
|
|
15
|
+
import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
16
|
+
import { PLANE_ENTRY } from "../kit/graph";
|
|
17
|
+
import { drawMark, graphPoint, inkSize, ownPoint, reading, traced, type MarkReading, type MarkState } from "../kit/mark";
|
|
21
18
|
|
|
22
19
|
export interface VectorArrowProps extends PaintProps {
|
|
23
20
|
tail?: Vector2;
|
|
@@ -28,71 +25,74 @@ export interface VectorArrowProps extends PaintProps {
|
|
|
28
25
|
/** Degrees of arc per drawn segment of a bent arrow. */
|
|
29
26
|
const BEND_STEP = 4;
|
|
30
27
|
|
|
28
|
+
const state = {
|
|
29
|
+
...omit(PAINT, "fill"),
|
|
30
|
+
tail: Types.vector2(),
|
|
31
|
+
head: Types.vector2({ x: 1, y: 1 }),
|
|
32
|
+
/** The arc the arrow sweeps, in degrees: zero is straight, positive bows to its left. */
|
|
33
|
+
bend: Types.number(0, { min: -180, max: 180, unit: "°" }),
|
|
34
|
+
graph: PLANE_ENTRY,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
interface ArrowValues {
|
|
38
|
+
readonly tail: Vector2;
|
|
39
|
+
readonly head: Vector2;
|
|
40
|
+
readonly bend: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A handle on one end of the arrow: where the end is in its own frame, and the graph point a drop there gives. */
|
|
44
|
+
const end = (which: "tail" | "head") => ({
|
|
45
|
+
at: (s: ArrowValues & MarkState) => {
|
|
46
|
+
const read = reading(s);
|
|
47
|
+
return ownPoint(read, traceArrow(s, read), s[which]);
|
|
48
|
+
},
|
|
49
|
+
value: (at: Vector2, s: ArrowValues & MarkState) => {
|
|
50
|
+
const read = reading(s);
|
|
51
|
+
return graphPoint(read, traceArrow(s, read), at);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
31
55
|
/**
|
|
32
56
|
* An arrow from `tail` to `head`, two graph points. Its arrowhead is the stroke's
|
|
33
57
|
* `endCap`, so a plain segment, a double arrow or a dashed one is a change of
|
|
34
58
|
* stroke. (`from` and `to` are not its names because every node has a `to`.)
|
|
35
59
|
*/
|
|
36
|
-
|
|
60
|
+
export const VectorArrow = traced(component2d({
|
|
37
61
|
key: "vectorArrow",
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
@strokeProperty({ default: { weight: 4, fill: "#e5e7eb", align: "center", endCap: "triangle" } })
|
|
51
|
-
declare stroke: Stroke;
|
|
62
|
+
state,
|
|
63
|
+
defaults: { stroke: { weight: 4, fill: "#e5e7eb", align: "center", endCap: "triangle" }, width: "hug", height: "hug" },
|
|
64
|
+
commands: {
|
|
65
|
+
...omit(PAINT_COMMANDS, "fillTo"),
|
|
66
|
+
/** Move either end, in graph coordinates. */
|
|
67
|
+
endpointsTo: { args: ["tail", "head"] },
|
|
68
|
+
},
|
|
69
|
+
handles: { tail: end("tail"), head: end("head") },
|
|
70
|
+
measure: (_measurer, s) => inkSize(traceArrow(s, reading(s))),
|
|
71
|
+
render: (ctx, s) => drawMark(ctx, s, traceArrow(s, reading(s))),
|
|
72
|
+
}), traceArrow);
|
|
52
73
|
|
|
53
|
-
|
|
54
|
-
super(props ?? {});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Move either end, in graph coordinates. */
|
|
58
|
-
@command({ args: ["tail", "head"] })
|
|
59
|
-
endpointsTo(args: CommandArgs<{ tail?: Vector2; head?: Vector2 }> & { duration: number }): Command<VectorArrowProps> {
|
|
60
|
-
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
protected trace(map: CoordinateMap): Vector2[][] {
|
|
64
|
-
const shift = this.shift();
|
|
65
|
-
const a = map.toPoint(this.tail.x + shift.x, this.tail.y + shift.y);
|
|
66
|
-
const b = map.toPoint(this.head.x + shift.x, this.head.y + shift.y);
|
|
67
|
-
const sweep = (this.bend * Math.PI) / 180;
|
|
68
|
-
const chord = Math.hypot(b.x - a.x, b.y - a.y);
|
|
69
|
-
if (Math.abs(sweep) < 1e-3 || chord === 0) return [[a, b]];
|
|
74
|
+
export type VectorArrow = NodeOf<typeof VectorArrow>;
|
|
70
75
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const cy = (a.y + b.y) / 2 - (side * (b.x - a.x) * reach) / chord;
|
|
78
|
-
const from = Math.atan2(a.y - cy, a.x - cx);
|
|
79
|
-
const steps = Math.max(8, Math.ceil(Math.abs(this.bend) / BEND_STEP));
|
|
80
|
-
const run: Vector2[] = [];
|
|
81
|
-
for (let i = 0; i <= steps; i++) {
|
|
82
|
-
const angle = from - (sweep * i) / steps;
|
|
83
|
-
run.push({ x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
|
|
84
|
-
}
|
|
85
|
-
return [run];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
override _handles(): readonly NodeHandle[] {
|
|
89
|
-
return [
|
|
90
|
-
{ prop: "tail", at: this.ownPoint(this.tail) },
|
|
91
|
-
{ prop: "head", at: this.ownPoint(this.head) },
|
|
92
|
-
];
|
|
93
|
-
}
|
|
76
|
+
function traceArrow(s: ArrowValues, { map, shift }: MarkReading): Vector2[][] {
|
|
77
|
+
const a = map.toPoint(s.tail.x + shift.x, s.tail.y + shift.y);
|
|
78
|
+
const b = map.toPoint(s.head.x + shift.x, s.head.y + shift.y);
|
|
79
|
+
const sweep = (s.bend * Math.PI) / 180;
|
|
80
|
+
const chord = Math.hypot(b.x - a.x, b.y - a.y);
|
|
81
|
+
if (Math.abs(sweep) < 1e-3 || chord === 0) return [[a, b]];
|
|
94
82
|
|
|
95
|
-
|
|
96
|
-
|
|
83
|
+
// The circle through both ends that subtends `sweep` at its centre.
|
|
84
|
+
const radius = chord / (2 * Math.sin(Math.abs(sweep) / 2));
|
|
85
|
+
const side = Math.sign(sweep);
|
|
86
|
+
const reach = radius * Math.cos(sweep / 2);
|
|
87
|
+
// The centre sits to the arrow's right for a positive sweep, so the arc bows to its left.
|
|
88
|
+
const cx = (a.x + b.x) / 2 + (side * (b.y - a.y) * reach) / chord;
|
|
89
|
+
const cy = (a.y + b.y) / 2 - (side * (b.x - a.x) * reach) / chord;
|
|
90
|
+
const from = Math.atan2(a.y - cy, a.x - cx);
|
|
91
|
+
const steps = Math.max(8, Math.ceil(Math.abs(s.bend) / BEND_STEP));
|
|
92
|
+
const run: Vector2[] = [];
|
|
93
|
+
for (let i = 0; i <= steps; i++) {
|
|
94
|
+
const angle = from - (sweep * i) / steps;
|
|
95
|
+
run.push({ x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
|
|
97
96
|
}
|
|
97
|
+
return [run];
|
|
98
98
|
}
|
package/src/nodes.ts
CHANGED
|
@@ -27,12 +27,48 @@ import { SurfacePlot3d } from "./three/surface-plot3d";
|
|
|
27
27
|
import { VectorArrow3d } from "./three/vector-arrow3d";
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
* Every node type this package publishes.
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* `
|
|
30
|
+
* Every node type this package publishes.
|
|
31
|
+
*
|
|
32
|
+
* A host registers these by handing the array to an engine —
|
|
33
|
+
* `new Engine(platform, { nodes: NODES })` — which reads each component's key.
|
|
34
|
+
* Defining a component only *declares* that key: nothing is registered by
|
|
35
|
+
* importing a module, so a registry holds exactly what its owner asked for.
|
|
36
|
+
*
|
|
37
|
+
* Listing the component **values** is also what survives bundling: this package
|
|
38
|
+
* declares `sideEffects: false`, and a document names a node type by string, so
|
|
39
|
+
* an import kept only for its side effect is exactly what gets shaken out.
|
|
34
40
|
*/
|
|
35
|
-
|
|
41
|
+
// Typed by name, so the declaration names each component rather than spelling out its type through core.
|
|
42
|
+
type KitNode =
|
|
43
|
+
| typeof Latex
|
|
44
|
+
| typeof LatexMatrix
|
|
45
|
+
| typeof NumberLine
|
|
46
|
+
| typeof Axes2d
|
|
47
|
+
| typeof NumberPlane
|
|
48
|
+
| typeof FunctionPlot
|
|
49
|
+
| typeof PointsPlot
|
|
50
|
+
| typeof PlotPoint
|
|
51
|
+
| typeof VectorArrow
|
|
52
|
+
| typeof CurveArea
|
|
53
|
+
| typeof RiemannSum
|
|
54
|
+
| typeof TangentLine
|
|
55
|
+
| typeof DropLines
|
|
56
|
+
| typeof CurlyBrace
|
|
57
|
+
| typeof AngleMark
|
|
58
|
+
| typeof EmphasisMark
|
|
59
|
+
| typeof Axes3d
|
|
60
|
+
| typeof SurfacePlot3d
|
|
61
|
+
| typeof CurvePlot3d
|
|
62
|
+
| typeof PlotPoint3d
|
|
63
|
+
| typeof VectorArrow3d
|
|
64
|
+
| typeof PolarPlane
|
|
65
|
+
| typeof VectorField
|
|
66
|
+
| typeof StreamLines
|
|
67
|
+
| typeof TracedPath
|
|
68
|
+
| typeof NetworkGraph
|
|
69
|
+
| typeof SampleSpace;
|
|
70
|
+
|
|
71
|
+
export const NODES: KitNode[] = [
|
|
36
72
|
Latex,
|
|
37
73
|
LatexMatrix,
|
|
38
74
|
NumberLine,
|