@motionscript/math 0.1.0-alpha.8 → 0.1.0
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 +6 -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/manifest.json +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts.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/marks/riemann-sum.ts
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Types,
|
|
4
|
-
type NodeOf,
|
|
5
|
-
type NodeView,
|
|
6
|
-
type PaintProps,
|
|
7
|
-
type Vector2,
|
|
8
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
9
|
-
type Fill as _Fill,
|
|
10
|
-
type FillResolved as _FillResolved,
|
|
11
|
-
type Node2D as _Node2D,
|
|
12
|
-
type Node2DProps as _Node2DProps,
|
|
13
|
-
type Shadow as _Shadow,
|
|
14
|
-
type ShadowResolved as _ShadowResolved,
|
|
15
|
-
type Stroke as _Stroke,
|
|
16
|
-
type StrokeResolved as _StrokeResolved,
|
|
17
|
-
} from "@motionscript/sdk";
|
|
18
|
-
import { listed, omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
19
|
-
import type { Interval } from "../kit/coords";
|
|
20
|
-
import { curveValue } from "../kit/curve";
|
|
21
|
-
import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
|
|
22
|
-
import { clamp } from "../kit/geometry";
|
|
23
|
-
import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
|
|
24
|
-
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
25
|
-
|
|
26
|
-
export type RiemannSample = "left" | "right" | "center";
|
|
27
|
-
|
|
28
|
-
export interface RiemannSumProps extends PaintProps {
|
|
29
|
-
on?: string;
|
|
30
|
-
domain?: Interval;
|
|
31
|
-
dx?: number;
|
|
32
|
-
sample?: RiemannSample;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** More rectangles than this is a filled area with extra steps. */
|
|
36
|
-
const MAX_RECTANGLES = 2000;
|
|
37
|
-
|
|
38
|
-
const state = {
|
|
39
|
-
// Its rectangles are revealed by `dx`, not by trimming their outlines.
|
|
40
|
-
...omit(PAINT, "start", "end"),
|
|
41
|
-
on: PLOT_REF,
|
|
42
|
-
domain: Types.custom<Interval>([0, 1], { resolve: readInterval, lerp: lerpInterval }),
|
|
43
|
-
/** Each rectangle's width, in graph units. */
|
|
44
|
-
dx: Types.number(0.25, { min: 0.001 }),
|
|
45
|
-
/** Where in its width a rectangle reads the curve's height. */
|
|
46
|
-
sample: listed(["left", "right", "center"], "left", { animatable: false }),
|
|
47
|
-
graph: PLANE_ENTRY,
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
interface SumValues {
|
|
51
|
-
readonly on: NodeView | null;
|
|
52
|
-
readonly domain: Interval;
|
|
53
|
-
readonly dx: number;
|
|
54
|
-
readonly sample: RiemannSample;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Rectangles under a plot, each `dx` wide and as tall as the curve where it is sampled. */
|
|
58
|
-
export const RiemannSum = traced(component2d({
|
|
59
|
-
key: "riemannSum",
|
|
60
|
-
state,
|
|
61
|
-
defaults: {
|
|
62
|
-
fill: { type: "solid", color: PLOT_COLOR, opacity: 0.35 },
|
|
63
|
-
stroke: { weight: 1.5, fill: PLOT_COLOR, align: "center", join: "round" },
|
|
64
|
-
width: "hug",
|
|
65
|
-
height: "hug",
|
|
66
|
-
},
|
|
67
|
-
commands: {
|
|
68
|
-
...omit(PAINT_COMMANDS, "progressTo", "flash"),
|
|
69
|
-
/** Animate the rectangles' width: narrower is the sum closing in on the area. */
|
|
70
|
-
refineTo: { args: ["dx"] },
|
|
71
|
-
/** Animate the span of x the rectangles cover. */
|
|
72
|
-
domainTo: { args: ["domain"] },
|
|
73
|
-
},
|
|
74
|
-
measure: (_measurer, s) => inkSize(traceSum(s, reading(s))),
|
|
75
|
-
render: (ctx, s) => drawMark(ctx, s, traceSum(s, reading(s)), { closed: true }),
|
|
76
|
-
}), traceSum);
|
|
77
|
-
|
|
78
|
-
export type RiemannSum = NodeOf<typeof RiemannSum>;
|
|
79
|
-
|
|
80
|
-
function traceSum(s: SumValues, { map, plane }: MarkReading): Vector2[][] {
|
|
81
|
-
const plot = curveFor(plane, s.on);
|
|
82
|
-
const [a, b] = s.domain;
|
|
83
|
-
const dx = s.dx;
|
|
84
|
-
if (!plot || !(b > a) || !(dx > 0)) return [];
|
|
85
|
-
const [yMin, yMax] = map.yRange;
|
|
86
|
-
const base = clamp(0, yMin, yMax);
|
|
87
|
-
const at = s.sample === "left" ? 0 : s.sample === "right" ? 1 : 0.5;
|
|
88
|
-
const count = Math.min(MAX_RECTANGLES, Math.ceil((b - a) / dx - 1e-9));
|
|
89
|
-
const runs: Vector2[][] = [];
|
|
90
|
-
for (let i = 0; i < count; i++) {
|
|
91
|
-
const left = a + i * dx;
|
|
92
|
-
const right = Math.min(b, left + dx);
|
|
93
|
-
const value = curveValue(plot, left + (right - left) * at);
|
|
94
|
-
if (!Number.isFinite(value)) continue;
|
|
95
|
-
const height = clamp(value, yMin, yMax);
|
|
96
|
-
runs.push([map.toPoint(left, base), map.toPoint(left, height), map.toPoint(right, height), map.toPoint(right, base)]);
|
|
97
|
-
}
|
|
98
|
-
return runs;
|
|
99
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type NodeView,
|
|
6
|
+
type PaintProps,
|
|
7
|
+
type Vector2,
|
|
8
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
9
|
+
type Fill as _Fill,
|
|
10
|
+
type FillResolved as _FillResolved,
|
|
11
|
+
type Node2D as _Node2D,
|
|
12
|
+
type Node2DProps as _Node2DProps,
|
|
13
|
+
type Shadow as _Shadow,
|
|
14
|
+
type ShadowResolved as _ShadowResolved,
|
|
15
|
+
type Stroke as _Stroke,
|
|
16
|
+
type StrokeResolved as _StrokeResolved,
|
|
17
|
+
} from "@motionscript/sdk";
|
|
18
|
+
import { listed, omit, PAINT, PAINT_COMMANDS } from "../kit/component";
|
|
19
|
+
import type { Interval } from "../kit/coords";
|
|
20
|
+
import { curveValue } from "../kit/curve";
|
|
21
|
+
import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
|
|
22
|
+
import { clamp } from "../kit/geometry";
|
|
23
|
+
import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
|
|
24
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
25
|
+
|
|
26
|
+
export type RiemannSample = "left" | "right" | "center";
|
|
27
|
+
|
|
28
|
+
export interface RiemannSumProps extends PaintProps {
|
|
29
|
+
on?: string;
|
|
30
|
+
domain?: Interval;
|
|
31
|
+
dx?: number;
|
|
32
|
+
sample?: RiemannSample;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** More rectangles than this is a filled area with extra steps. */
|
|
36
|
+
const MAX_RECTANGLES = 2000;
|
|
37
|
+
|
|
38
|
+
const state = {
|
|
39
|
+
// Its rectangles are revealed by `dx`, not by trimming their outlines.
|
|
40
|
+
...omit(PAINT, "start", "end"),
|
|
41
|
+
on: PLOT_REF,
|
|
42
|
+
domain: Types.custom<Interval>([0, 1], { resolve: readInterval, lerp: lerpInterval }),
|
|
43
|
+
/** Each rectangle's width, in graph units. */
|
|
44
|
+
dx: Types.number(0.25, { min: 0.001 }),
|
|
45
|
+
/** Where in its width a rectangle reads the curve's height. */
|
|
46
|
+
sample: listed(["left", "right", "center"], "left", { animatable: false }),
|
|
47
|
+
graph: PLANE_ENTRY,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
interface SumValues {
|
|
51
|
+
readonly on: NodeView | null;
|
|
52
|
+
readonly domain: Interval;
|
|
53
|
+
readonly dx: number;
|
|
54
|
+
readonly sample: RiemannSample;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Rectangles under a plot, each `dx` wide and as tall as the curve where it is sampled. */
|
|
58
|
+
export const RiemannSum = traced(component2d({
|
|
59
|
+
key: "riemannSum",
|
|
60
|
+
state,
|
|
61
|
+
defaults: {
|
|
62
|
+
fill: { type: "solid", color: PLOT_COLOR, opacity: 0.35 },
|
|
63
|
+
stroke: { weight: 1.5, fill: PLOT_COLOR, align: "center", join: "round" },
|
|
64
|
+
width: "hug",
|
|
65
|
+
height: "hug",
|
|
66
|
+
},
|
|
67
|
+
commands: {
|
|
68
|
+
...omit(PAINT_COMMANDS, "progressTo", "flash"),
|
|
69
|
+
/** Animate the rectangles' width: narrower is the sum closing in on the area. */
|
|
70
|
+
refineTo: { args: ["dx"] },
|
|
71
|
+
/** Animate the span of x the rectangles cover. */
|
|
72
|
+
domainTo: { args: ["domain"] },
|
|
73
|
+
},
|
|
74
|
+
measure: (_measurer, s) => inkSize(traceSum(s, reading(s))),
|
|
75
|
+
render: (ctx, s) => drawMark(ctx, s, traceSum(s, reading(s)), { closed: true }),
|
|
76
|
+
}), traceSum);
|
|
77
|
+
|
|
78
|
+
export type RiemannSum = NodeOf<typeof RiemannSum>;
|
|
79
|
+
|
|
80
|
+
function traceSum(s: SumValues, { map, plane }: MarkReading): Vector2[][] {
|
|
81
|
+
const plot = curveFor(plane, s.on);
|
|
82
|
+
const [a, b] = s.domain;
|
|
83
|
+
const dx = s.dx;
|
|
84
|
+
if (!plot || !(b > a) || !(dx > 0)) return [];
|
|
85
|
+
const [yMin, yMax] = map.yRange;
|
|
86
|
+
const base = clamp(0, yMin, yMax);
|
|
87
|
+
const at = s.sample === "left" ? 0 : s.sample === "right" ? 1 : 0.5;
|
|
88
|
+
const count = Math.min(MAX_RECTANGLES, Math.ceil((b - a) / dx - 1e-9));
|
|
89
|
+
const runs: Vector2[][] = [];
|
|
90
|
+
for (let i = 0; i < count; i++) {
|
|
91
|
+
const left = a + i * dx;
|
|
92
|
+
const right = Math.min(b, left + dx);
|
|
93
|
+
const value = curveValue(plot, left + (right - left) * at);
|
|
94
|
+
if (!Number.isFinite(value)) continue;
|
|
95
|
+
const height = clamp(value, yMin, yMax);
|
|
96
|
+
runs.push([map.toPoint(left, base), map.toPoint(left, height), map.toPoint(right, height), map.toPoint(right, base)]);
|
|
97
|
+
}
|
|
98
|
+
return runs;
|
|
99
|
+
}
|
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Types,
|
|
4
|
-
type NodeOf,
|
|
5
|
-
type NodeView,
|
|
6
|
-
type PaintProps,
|
|
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,
|
|
15
|
-
} from "@motionscript/sdk";
|
|
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";
|
|
20
|
-
|
|
21
|
-
export interface TangentLineProps extends PaintProps {
|
|
22
|
-
on?: string;
|
|
23
|
-
length?: number;
|
|
24
|
-
dx?: number;
|
|
25
|
-
legs?: boolean;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** The step a slope is measured over, as a share of the system's width. */
|
|
29
|
-
const SLOPE_STEP = 1e-4;
|
|
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
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The tangent to a plot at this node's `x`, a fixed length on screen. With a
|
|
53
|
-
* `dx` it is the secant through `x` and `x + dx` instead, and `legs` draws that
|
|
54
|
-
* secant's run and rise.
|
|
55
|
-
*/
|
|
56
|
-
export const TangentLine = traced(component2d({
|
|
57
|
-
key: "tangentLine",
|
|
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);
|
|
72
|
-
|
|
73
|
-
export type TangentLine = NodeOf<typeof TangentLine>;
|
|
74
|
-
|
|
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
|
-
}
|
|
81
|
-
|
|
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;
|
|
89
|
-
|
|
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);
|
|
95
|
-
const span = Math.hypot(b.x - a.x, b.y - a.y);
|
|
96
|
-
if (span === 0) return [];
|
|
97
|
-
const reach = Math.max(half, span / 2 + 16);
|
|
98
|
-
const ux = (b.x - a.x) / span;
|
|
99
|
-
const uy = (b.y - a.y) / span;
|
|
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];
|
|
104
|
-
}
|
|
105
|
-
|
|
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 }]];
|
|
119
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type NodeView,
|
|
6
|
+
type PaintProps,
|
|
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,
|
|
15
|
+
} from "@motionscript/sdk";
|
|
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";
|
|
20
|
+
|
|
21
|
+
export interface TangentLineProps extends PaintProps {
|
|
22
|
+
on?: string;
|
|
23
|
+
length?: number;
|
|
24
|
+
dx?: number;
|
|
25
|
+
legs?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The step a slope is measured over, as a share of the system's width. */
|
|
29
|
+
const SLOPE_STEP = 1e-4;
|
|
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
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The tangent to a plot at this node's `x`, a fixed length on screen. With a
|
|
53
|
+
* `dx` it is the secant through `x` and `x + dx` instead, and `legs` draws that
|
|
54
|
+
* secant's run and rise.
|
|
55
|
+
*/
|
|
56
|
+
export const TangentLine = traced(component2d({
|
|
57
|
+
key: "tangentLine",
|
|
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);
|
|
72
|
+
|
|
73
|
+
export type TangentLine = NodeOf<typeof TangentLine>;
|
|
74
|
+
|
|
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
|
+
}
|
|
81
|
+
|
|
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;
|
|
89
|
+
|
|
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);
|
|
95
|
+
const span = Math.hypot(b.x - a.x, b.y - a.y);
|
|
96
|
+
if (span === 0) return [];
|
|
97
|
+
const reach = Math.max(half, span / 2 + 16);
|
|
98
|
+
const ux = (b.x - a.x) / span;
|
|
99
|
+
const uy = (b.y - a.y) / span;
|
|
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];
|
|
104
|
+
}
|
|
105
|
+
|
|
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 }]];
|
|
119
|
+
}
|
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Types,
|
|
4
|
-
type NodeOf,
|
|
5
|
-
type PaintProps,
|
|
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,
|
|
14
|
-
} from "@motionscript/sdk";
|
|
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";
|
|
18
|
-
|
|
19
|
-
export interface VectorArrowProps extends PaintProps {
|
|
20
|
-
tail?: Vector2;
|
|
21
|
-
head?: Vector2;
|
|
22
|
-
bend?: number;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Degrees of arc per drawn segment of a bent arrow. */
|
|
26
|
-
const BEND_STEP = 4;
|
|
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
|
-
|
|
55
|
-
/**
|
|
56
|
-
* An arrow from `tail` to `head`, two graph points. Its arrowhead is the stroke's
|
|
57
|
-
* `endCap`, so a plain segment, a double arrow or a dashed one is a change of
|
|
58
|
-
* stroke. (`from` and `to` are not its names because every node has a `to`.)
|
|
59
|
-
*/
|
|
60
|
-
export const VectorArrow = traced(component2d({
|
|
61
|
-
key: "vectorArrow",
|
|
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);
|
|
73
|
-
|
|
74
|
-
export type VectorArrow = NodeOf<typeof VectorArrow>;
|
|
75
|
-
|
|
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]];
|
|
82
|
-
|
|
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) });
|
|
96
|
-
}
|
|
97
|
-
return [run];
|
|
98
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type PaintProps,
|
|
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,
|
|
14
|
+
} from "@motionscript/sdk";
|
|
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";
|
|
18
|
+
|
|
19
|
+
export interface VectorArrowProps extends PaintProps {
|
|
20
|
+
tail?: Vector2;
|
|
21
|
+
head?: Vector2;
|
|
22
|
+
bend?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Degrees of arc per drawn segment of a bent arrow. */
|
|
26
|
+
const BEND_STEP = 4;
|
|
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
|
+
|
|
55
|
+
/**
|
|
56
|
+
* An arrow from `tail` to `head`, two graph points. Its arrowhead is the stroke's
|
|
57
|
+
* `endCap`, so a plain segment, a double arrow or a dashed one is a change of
|
|
58
|
+
* stroke. (`from` and `to` are not its names because every node has a `to`.)
|
|
59
|
+
*/
|
|
60
|
+
export const VectorArrow = traced(component2d({
|
|
61
|
+
key: "vectorArrow",
|
|
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);
|
|
73
|
+
|
|
74
|
+
export type VectorArrow = NodeOf<typeof VectorArrow>;
|
|
75
|
+
|
|
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]];
|
|
82
|
+
|
|
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) });
|
|
96
|
+
}
|
|
97
|
+
return [run];
|
|
98
|
+
}
|