@motionscript/math 0.0.0-stage → 0.1.0-alpha.4
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 +92 -0
- package/LICENSE +201 -0
- package/NOTICE +48 -0
- package/README.md +385 -3
- package/dist/annotate/angle-mark.d.ts +46 -0
- package/dist/annotate/angle-mark.d.ts.map +1 -0
- package/dist/annotate/angle-mark.js +132 -0
- package/dist/annotate/angle-mark.js.map +1 -0
- package/dist/annotate/curly-brace.d.ts +46 -0
- package/dist/annotate/curly-brace.d.ts.map +1 -0
- package/dist/annotate/curly-brace.js +114 -0
- package/dist/annotate/curly-brace.js.map +1 -0
- package/dist/annotate/emphasis-mark.d.ts +20 -0
- package/dist/annotate/emphasis-mark.d.ts.map +1 -0
- package/dist/annotate/emphasis-mark.js +81 -0
- package/dist/annotate/emphasis-mark.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +30 -0
- package/dist/browser/tex.js +2 -0
- package/dist/browser/tex.js.map +7 -0
- package/dist/engine.d.ts +64 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +42 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/brace.d.ts +8 -0
- package/dist/kit/brace.d.ts.map +1 -0
- package/dist/kit/brace.js +36 -0
- package/dist/kit/brace.js.map +1 -0
- package/dist/kit/cache.d.ts +30 -0
- package/dist/kit/cache.d.ts.map +1 -0
- package/dist/kit/cache.js +53 -0
- package/dist/kit/cache.js.map +1 -0
- package/dist/kit/contour.d.ts +8 -0
- package/dist/kit/contour.d.ts.map +1 -0
- package/dist/kit/contour.js +93 -0
- package/dist/kit/contour.js.map +1 -0
- package/dist/kit/coords.d.ts +69 -0
- package/dist/kit/coords.d.ts.map +1 -0
- package/dist/kit/coords.js +224 -0
- package/dist/kit/coords.js.map +1 -0
- package/dist/kit/curve.d.ts +17 -0
- package/dist/kit/curve.d.ts.map +1 -0
- package/dist/kit/curve.js +22 -0
- package/dist/kit/curve.js.map +1 -0
- package/dist/kit/equation.d.ts +61 -0
- package/dist/kit/equation.d.ts.map +1 -0
- package/dist/kit/equation.js +168 -0
- package/dist/kit/equation.js.map +1 -0
- package/dist/kit/equation3d.d.ts +50 -0
- package/dist/kit/equation3d.d.ts.map +1 -0
- package/dist/kit/equation3d.js +103 -0
- package/dist/kit/equation3d.js.map +1 -0
- package/dist/kit/expression.d.ts +35 -0
- package/dist/kit/expression.d.ts.map +1 -0
- package/dist/kit/expression.js +124 -0
- package/dist/kit/expression.js.map +1 -0
- package/dist/kit/field-mark.d.ts +58 -0
- package/dist/kit/field-mark.d.ts.map +1 -0
- package/dist/kit/field-mark.js +129 -0
- package/dist/kit/field-mark.js.map +1 -0
- package/dist/kit/field.d.ts +14 -0
- package/dist/kit/field.d.ts.map +1 -0
- package/dist/kit/field.js +46 -0
- package/dist/kit/field.js.map +1 -0
- package/dist/kit/follow.d.ts +71 -0
- package/dist/kit/follow.d.ts.map +1 -0
- package/dist/kit/follow.js +159 -0
- package/dist/kit/follow.js.map +1 -0
- package/dist/kit/furniture.d.ts +30 -0
- package/dist/kit/furniture.d.ts.map +1 -0
- package/dist/kit/furniture.js +87 -0
- package/dist/kit/furniture.js.map +1 -0
- package/dist/kit/geometry.d.ts +13 -0
- package/dist/kit/geometry.d.ts.map +1 -0
- package/dist/kit/geometry.js +54 -0
- package/dist/kit/geometry.js.map +1 -0
- package/dist/kit/mark.d.ts +47 -0
- package/dist/kit/mark.d.ts.map +1 -0
- package/dist/kit/mark.js +103 -0
- package/dist/kit/mark.js.map +1 -0
- package/dist/kit/part-fills.d.ts +23 -0
- package/dist/kit/part-fills.d.ts.map +1 -0
- package/dist/kit/part-fills.js +46 -0
- package/dist/kit/part-fills.js.map +1 -0
- package/dist/kit/place.d.ts +18 -0
- package/dist/kit/place.d.ts.map +1 -0
- package/dist/kit/place.js +68 -0
- package/dist/kit/place.js.map +1 -0
- package/dist/kit/placement.d.ts +8 -0
- package/dist/kit/placement.d.ts.map +1 -0
- package/dist/kit/placement.js +8 -0
- package/dist/kit/placement.js.map +1 -0
- package/dist/kit/sampler.d.ts +17 -0
- package/dist/kit/sampler.d.ts.map +1 -0
- package/dist/kit/sampler.js +250 -0
- package/dist/kit/sampler.js.map +1 -0
- package/dist/kit/scale.d.ts +12 -0
- package/dist/kit/scale.d.ts.map +1 -0
- package/dist/kit/scale.js +37 -0
- package/dist/kit/scale.js.map +1 -0
- package/dist/kit/source.d.ts +22 -0
- package/dist/kit/source.d.ts.map +1 -0
- package/dist/kit/source.js +45 -0
- package/dist/kit/source.js.map +1 -0
- package/dist/kit/system.d.ts +33 -0
- package/dist/kit/system.d.ts.map +1 -0
- package/dist/kit/system.js +67 -0
- package/dist/kit/system.js.map +1 -0
- package/dist/kit/ticks.d.ts +19 -0
- package/dist/kit/ticks.d.ts.map +1 -0
- package/dist/kit/ticks.js +78 -0
- package/dist/kit/ticks.js.map +1 -0
- package/dist/kit/withdrawn.d.ts +9 -0
- package/dist/kit/withdrawn.d.ts.map +1 -0
- package/dist/kit/withdrawn.js +9 -0
- package/dist/kit/withdrawn.js.map +1 -0
- package/dist/marks/curve-area.d.ts +32 -0
- package/dist/marks/curve-area.d.ts.map +1 -0
- package/dist/marks/curve-area.js +82 -0
- package/dist/marks/curve-area.js.map +1 -0
- package/dist/marks/drop-lines.d.ts +22 -0
- package/dist/marks/drop-lines.d.ts.map +1 -0
- package/dist/marks/drop-lines.js +58 -0
- package/dist/marks/drop-lines.js.map +1 -0
- package/dist/marks/plot-point.d.ts +35 -0
- package/dist/marks/plot-point.d.ts.map +1 -0
- package/dist/marks/plot-point.js +97 -0
- package/dist/marks/plot-point.js.map +1 -0
- package/dist/marks/riemann-sum.d.ts +37 -0
- package/dist/marks/riemann-sum.d.ts.map +1 -0
- package/dist/marks/riemann-sum.js +91 -0
- package/dist/marks/riemann-sum.js.map +1 -0
- package/dist/marks/tangent-line.d.ts +27 -0
- package/dist/marks/tangent-line.d.ts.map +1 -0
- package/dist/marks/tangent-line.js +103 -0
- package/dist/marks/tangent-line.js.map +1 -0
- package/dist/marks/vector-arrow.d.ts +32 -0
- package/dist/marks/vector-arrow.d.ts.map +1 -0
- package/dist/marks/vector-arrow.js +87 -0
- package/dist/marks/vector-arrow.js.map +1 -0
- package/dist/nodes.d.ts +35 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +63 -0
- package/dist/nodes.js.map +1 -0
- package/dist/plots/function-plot.d.ts +67 -0
- package/dist/plots/function-plot.d.ts.map +1 -0
- package/dist/plots/function-plot.js +215 -0
- package/dist/plots/function-plot.js.map +1 -0
- package/dist/plots/points-plot.d.ts +28 -0
- package/dist/plots/points-plot.d.ts.map +1 -0
- package/dist/plots/points-plot.js +99 -0
- package/dist/plots/points-plot.js.map +1 -0
- package/dist/plots/stream-lines.d.ts +35 -0
- package/dist/plots/stream-lines.d.ts.map +1 -0
- package/dist/plots/stream-lines.js +141 -0
- package/dist/plots/stream-lines.js.map +1 -0
- package/dist/plots/traced-path.d.ts +31 -0
- package/dist/plots/traced-path.d.ts.map +1 -0
- package/dist/plots/traced-path.js +56 -0
- package/dist/plots/traced-path.js.map +1 -0
- package/dist/plots/vector-field.d.ts +23 -0
- package/dist/plots/vector-field.d.ts.map +1 -0
- package/dist/plots/vector-field.js +91 -0
- package/dist/plots/vector-field.js.map +1 -0
- package/dist/structures/graph-layout.d.ts +11 -0
- package/dist/structures/graph-layout.d.ts.map +1 -0
- package/dist/structures/graph-layout.js +135 -0
- package/dist/structures/graph-layout.js.map +1 -0
- package/dist/structures/network-graph.d.ts +83 -0
- package/dist/structures/network-graph.d.ts.map +1 -0
- package/dist/structures/network-graph.js +289 -0
- package/dist/structures/network-graph.js.map +1 -0
- package/dist/structures/sample-space.d.ts +62 -0
- package/dist/structures/sample-space.d.ts.map +1 -0
- package/dist/structures/sample-space.js +199 -0
- package/dist/structures/sample-space.js.map +1 -0
- package/dist/systems/axes2d.d.ts +75 -0
- package/dist/systems/axes2d.d.ts.map +1 -0
- package/dist/systems/axes2d.js +193 -0
- package/dist/systems/axes2d.js.map +1 -0
- package/dist/systems/number-line.d.ts +49 -0
- package/dist/systems/number-line.d.ts.map +1 -0
- package/dist/systems/number-line.js +127 -0
- package/dist/systems/number-line.js.map +1 -0
- package/dist/systems/number-plane.d.ts +59 -0
- package/dist/systems/number-plane.d.ts.map +1 -0
- package/dist/systems/number-plane.js +193 -0
- package/dist/systems/number-plane.js.map +1 -0
- package/dist/systems/polar-plane.d.ts +48 -0
- package/dist/systems/polar-plane.d.ts.map +1 -0
- package/dist/systems/polar-plane.js +165 -0
- package/dist/systems/polar-plane.js.map +1 -0
- package/dist/tex/geometry.d.ts +85 -0
- package/dist/tex/geometry.d.ts.map +1 -0
- package/dist/tex/geometry.js +632 -0
- package/dist/tex/geometry.js.map +1 -0
- package/dist/tex/index.d.ts +11 -0
- package/dist/tex/index.d.ts.map +1 -0
- package/dist/tex/index.js +9 -0
- package/dist/tex/index.js.map +1 -0
- package/dist/tex/mathjax.d.ts +33 -0
- package/dist/tex/mathjax.d.ts.map +1 -0
- package/dist/tex/mathjax.js +74 -0
- package/dist/tex/mathjax.js.map +1 -0
- package/dist/tex/matrix.d.ts +31 -0
- package/dist/tex/matrix.d.ts.map +1 -0
- package/dist/tex/matrix.js +73 -0
- package/dist/tex/matrix.js.map +1 -0
- package/dist/tex/node.d.ts +215 -0
- package/dist/tex/node.d.ts.map +1 -0
- package/dist/tex/node.js +595 -0
- package/dist/tex/node.js.map +1 -0
- package/dist/tex/tween.d.ts +50 -0
- package/dist/tex/tween.d.ts.map +1 -0
- package/dist/tex/tween.js +253 -0
- package/dist/tex/tween.js.map +1 -0
- package/dist/three/axes3d.d.ts +87 -0
- package/dist/three/axes3d.d.ts.map +1 -0
- package/dist/three/axes3d.js +228 -0
- package/dist/three/axes3d.js.map +1 -0
- package/dist/three/curve-plot3d.d.ts +60 -0
- package/dist/three/curve-plot3d.d.ts.map +1 -0
- package/dist/three/curve-plot3d.js +143 -0
- package/dist/three/curve-plot3d.js.map +1 -0
- package/dist/three/label3d.d.ts +11 -0
- package/dist/three/label3d.d.ts.map +1 -0
- package/dist/three/label3d.js +40 -0
- package/dist/three/label3d.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +26 -0
- package/dist/three/plot-point3d.d.ts.map +1 -0
- package/dist/three/plot-point3d.js +60 -0
- package/dist/three/plot-point3d.js.map +1 -0
- package/dist/three/space.d.ts +28 -0
- package/dist/three/space.d.ts.map +1 -0
- package/dist/three/space.js +47 -0
- package/dist/three/space.js.map +1 -0
- package/dist/three/surface-plot3d.d.ts +80 -0
- package/dist/three/surface-plot3d.d.ts.map +1 -0
- package/dist/three/surface-plot3d.js +290 -0
- package/dist/three/surface-plot3d.js.map +1 -0
- package/dist/three/vector-arrow3d.d.ts +25 -0
- package/dist/three/vector-arrow3d.d.ts.map +1 -0
- package/dist/three/vector-arrow3d.js +72 -0
- package/dist/three/vector-arrow3d.js.map +1 -0
- package/package.json +79 -3
- package/registry.json +251 -0
- package/src/annotate/angle-mark.ts +147 -0
- package/src/annotate/curly-brace.ts +126 -0
- package/src/annotate/emphasis-mark.ts +89 -0
- package/src/engine.ts +72 -0
- package/src/index.ts +29 -0
- package/src/kit/brace.ts +37 -0
- package/src/kit/cache.ts +61 -0
- package/src/kit/contour.ts +94 -0
- package/src/kit/coords.ts +278 -0
- package/src/kit/curve.ts +29 -0
- package/src/kit/equation.ts +181 -0
- package/src/kit/equation3d.ts +137 -0
- package/src/kit/expression.ts +159 -0
- package/src/kit/field-mark.ts +155 -0
- package/src/kit/field.ts +50 -0
- package/src/kit/follow.ts +203 -0
- package/src/kit/furniture.ts +108 -0
- package/src/kit/geometry.ts +49 -0
- package/src/kit/mark.ts +131 -0
- package/src/kit/part-fills.ts +61 -0
- package/src/kit/place.ts +101 -0
- package/src/kit/placement.ts +8 -0
- package/src/kit/sampler.ts +281 -0
- package/src/kit/scale.ts +39 -0
- package/src/kit/source.ts +57 -0
- package/src/kit/system.ts +99 -0
- package/src/kit/ticks.ts +85 -0
- package/src/kit/withdrawn.ts +8 -0
- package/src/marks/curve-area.ts +89 -0
- package/src/marks/drop-lines.ts +63 -0
- package/src/marks/plot-point.ts +107 -0
- package/src/marks/riemann-sum.ts +98 -0
- package/src/marks/tangent-line.ts +103 -0
- package/src/marks/vector-arrow.ts +98 -0
- package/src/nodes.ts +63 -0
- package/src/plots/function-plot.ts +236 -0
- package/src/plots/points-plot.ts +107 -0
- package/src/plots/stream-lines.ts +151 -0
- package/src/plots/traced-path.ts +52 -0
- package/src/plots/vector-field.ts +88 -0
- package/src/structures/graph-layout.ts +145 -0
- package/src/structures/network-graph.ts +308 -0
- package/src/structures/sample-space.ts +209 -0
- package/src/systems/axes2d.ts +217 -0
- package/src/systems/number-line.ts +131 -0
- package/src/systems/number-plane.ts +203 -0
- package/src/systems/polar-plane.ts +176 -0
- package/src/tex/geometry.ts +703 -0
- package/src/tex/index.ts +14 -0
- package/src/tex/mathjax.ts +82 -0
- package/src/tex/matrix.ts +75 -0
- package/src/tex/node.ts +641 -0
- package/src/tex/tween.ts +308 -0
- package/src/three/axes3d.ts +240 -0
- package/src/three/curve-plot3d.ts +147 -0
- package/src/three/label3d.ts +50 -0
- package/src/three/plot-point3d.ts +66 -0
- package/src/three/space.ts +67 -0
- package/src/three/surface-plot3d.ts +323 -0
- package/src/three/vector-arrow3d.ts +77 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { command, node, property, strokeProperty, withdraw, } from "@motionscript/sdk";
|
|
8
|
+
import { traceContour } from "../kit/contour.js";
|
|
9
|
+
import { equationParams, parseEquation } from "../kit/equation.js";
|
|
10
|
+
import { PLOT_COLOR } from "../kit/furniture.js";
|
|
11
|
+
import { stated } from "../kit/geometry.js";
|
|
12
|
+
import { Mark } from "../kit/mark.js";
|
|
13
|
+
import { sampleCurve } from "../kit/sampler.js";
|
|
14
|
+
import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor } from "../kit/source.js";
|
|
15
|
+
import { SYSTEMS } from "../kit/placement.js";
|
|
16
|
+
import { FILL_PAINT } from "../kit/withdrawn.js";
|
|
17
|
+
const TAU = Math.PI * 2;
|
|
18
|
+
/**
|
|
19
|
+
* The curve of an equation, drawn in the coordinate system it is a child of.
|
|
20
|
+
* How the equation is written says what it is: see `Equation`. A mark put
|
|
21
|
+
* inside it reads it: the area under it, a tangent, a point that rides it.
|
|
22
|
+
*/
|
|
23
|
+
let FunctionPlot = class FunctionPlot extends Mark {
|
|
24
|
+
constructor(props) {
|
|
25
|
+
super((props ?? {}));
|
|
26
|
+
}
|
|
27
|
+
/** Change the equation, the curve bending from the one into the other. */
|
|
28
|
+
morphTo(args) {
|
|
29
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
30
|
+
}
|
|
31
|
+
/** Animate the equation's named values. */
|
|
32
|
+
paramsTo(args) {
|
|
33
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
34
|
+
}
|
|
35
|
+
/** Animate the span that is drawn. */
|
|
36
|
+
domainTo(args) {
|
|
37
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The curve's height at graph `x`, or `NaN` where it has none or is not a
|
|
41
|
+
* function of `x`. What a point, a tangent or an area riding this plot reads.
|
|
42
|
+
*/
|
|
43
|
+
valueAt(x) {
|
|
44
|
+
const { source, blend } = this.expression;
|
|
45
|
+
const shift = this.shift();
|
|
46
|
+
const value = this._heightOf(source, x - shift.x) + shift.y;
|
|
47
|
+
if (!blend)
|
|
48
|
+
return value;
|
|
49
|
+
const from = this._heightOf(blend.from, x - shift.x) + shift.y;
|
|
50
|
+
return from + (value - from) * blend.t;
|
|
51
|
+
}
|
|
52
|
+
/** The span of the free variable that is drawn: `domain`, unless a node built on this one says otherwise. */
|
|
53
|
+
span() {
|
|
54
|
+
return this.domain;
|
|
55
|
+
}
|
|
56
|
+
trace(map) {
|
|
57
|
+
const { area } = map;
|
|
58
|
+
if (!Number.isFinite(area.maxX - area.minX))
|
|
59
|
+
return [];
|
|
60
|
+
const { source, blend } = this.expression;
|
|
61
|
+
const shift = this.shift();
|
|
62
|
+
const key = [source, blend?.from, blend?.t, map.revision, this.span(), JSON.stringify(this.params), shift.x, shift.y].join("|");
|
|
63
|
+
if (key === this._key)
|
|
64
|
+
return this._runs;
|
|
65
|
+
this._key = key;
|
|
66
|
+
// A curve with no parameter to walk cannot bend into another: it changes over half way.
|
|
67
|
+
const shown = blend && blend.t < 0.5 ? blend.from : source;
|
|
68
|
+
if (this._equation(source)?.kind === "implicit" || (blend && this._equation(blend.from)?.kind === "implicit")) {
|
|
69
|
+
this._runs = this._contour(shown, map, shift);
|
|
70
|
+
return this._runs;
|
|
71
|
+
}
|
|
72
|
+
let curve = this._curve(source, map, shift);
|
|
73
|
+
if (curve && blend) {
|
|
74
|
+
const from = this._curve(blend.from, map, shift);
|
|
75
|
+
if (from)
|
|
76
|
+
curve = between(from, curve, blend.t);
|
|
77
|
+
}
|
|
78
|
+
const width = area.maxX - area.minX;
|
|
79
|
+
this._runs = curve
|
|
80
|
+
? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })
|
|
81
|
+
: [];
|
|
82
|
+
return this._runs;
|
|
83
|
+
}
|
|
84
|
+
_key = "";
|
|
85
|
+
_runs = [];
|
|
86
|
+
_equation(source) {
|
|
87
|
+
const equation = parseEquation(source, equationParams(source));
|
|
88
|
+
return equation instanceof Error ? null : equation;
|
|
89
|
+
}
|
|
90
|
+
_heightOf(source, x) {
|
|
91
|
+
const equation = this._equation(source);
|
|
92
|
+
if (equation?.kind !== "function" || equation.of !== "x")
|
|
93
|
+
return NaN;
|
|
94
|
+
return equation.f(x, 0, scopeFor(equationParams(source), this.params));
|
|
95
|
+
}
|
|
96
|
+
/** Where an implicit `source` holds, over what the system shows, as pixel runs. */
|
|
97
|
+
_contour(source, map, shift) {
|
|
98
|
+
const equation = this._equation(source);
|
|
99
|
+
if (equation?.kind !== "implicit")
|
|
100
|
+
return [];
|
|
101
|
+
const params = scopeFor(equationParams(source), this.params);
|
|
102
|
+
const { area } = map;
|
|
103
|
+
const cells = (pixels) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));
|
|
104
|
+
const { f } = equation;
|
|
105
|
+
const runs = traceContour((x, y) => f(x - shift.x, y - shift.y, params), map.xRange, map.yRange, cells(area.maxX - area.minX), cells(area.maxY - area.minY));
|
|
106
|
+
return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));
|
|
107
|
+
}
|
|
108
|
+
/** `source` as a pixel curve over a parameter running 0 to 1. */
|
|
109
|
+
_curve(source, map, shift) {
|
|
110
|
+
const equation = this._equation(source);
|
|
111
|
+
if (!equation || equation.kind === "implicit")
|
|
112
|
+
return null;
|
|
113
|
+
const params = scopeFor(equationParams(source), this.params);
|
|
114
|
+
const domain = this.span();
|
|
115
|
+
switch (equation.kind) {
|
|
116
|
+
case "function": {
|
|
117
|
+
const horizontal = equation.of === "x";
|
|
118
|
+
const [lo, hi] = horizontal ? map.xRange : map.yRange;
|
|
119
|
+
const offset = horizontal ? shift.x : shift.y;
|
|
120
|
+
// Only what the system shows is sampled, however wide the domain.
|
|
121
|
+
const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);
|
|
122
|
+
const b = Math.min(hi - offset, domain?.[1] ?? Infinity);
|
|
123
|
+
if (!(b > a))
|
|
124
|
+
return null;
|
|
125
|
+
const { f } = equation;
|
|
126
|
+
return (u, out) => {
|
|
127
|
+
const v = a + (b - a) * u;
|
|
128
|
+
const w = f(v, 0, params);
|
|
129
|
+
if (!Number.isFinite(w))
|
|
130
|
+
return false;
|
|
131
|
+
if (horizontal)
|
|
132
|
+
map.toPoint(v + shift.x, w + shift.y, out);
|
|
133
|
+
else
|
|
134
|
+
map.toPoint(w + shift.x, v + shift.y, out);
|
|
135
|
+
return true;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
case "polar": {
|
|
139
|
+
const [a, b] = domain ?? [0, TAU];
|
|
140
|
+
const { f } = equation;
|
|
141
|
+
return (u, out) => {
|
|
142
|
+
const theta = a + (b - a) * u;
|
|
143
|
+
const r = f(theta, 0, params);
|
|
144
|
+
if (!Number.isFinite(r))
|
|
145
|
+
return false;
|
|
146
|
+
map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);
|
|
147
|
+
return true;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
case "parametric": {
|
|
151
|
+
const [a, b] = domain ?? [0, 1];
|
|
152
|
+
const { x, y } = equation;
|
|
153
|
+
return (u, out) => {
|
|
154
|
+
const t = a + (b - a) * u;
|
|
155
|
+
const gx = x(t, 0, params);
|
|
156
|
+
const gy = y(t, 0, params);
|
|
157
|
+
if (!Number.isFinite(gx) || !Number.isFinite(gy))
|
|
158
|
+
return false;
|
|
159
|
+
map.toPoint(gx + shift.x, gy + shift.y, out);
|
|
160
|
+
return true;
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
__decorate([
|
|
167
|
+
property({ default: "", mapper: readSource, tween: lerpSource, kind: "equation" })
|
|
168
|
+
], FunctionPlot.prototype, "expression", void 0);
|
|
169
|
+
__decorate([
|
|
170
|
+
property({ default: null, tween: lerpDomain, kind: "range", defaultMin: -5, defaultMax: 5 })
|
|
171
|
+
], FunctionPlot.prototype, "domain", void 0);
|
|
172
|
+
__decorate([
|
|
173
|
+
property({ default: {}, tween: lerpParams })
|
|
174
|
+
], FunctionPlot.prototype, "params", void 0);
|
|
175
|
+
__decorate([
|
|
176
|
+
strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
|
|
177
|
+
], FunctionPlot.prototype, "stroke", void 0);
|
|
178
|
+
__decorate([
|
|
179
|
+
command({ args: ["expression"] })
|
|
180
|
+
], FunctionPlot.prototype, "morphTo", null);
|
|
181
|
+
__decorate([
|
|
182
|
+
command({ args: ["params"] })
|
|
183
|
+
], FunctionPlot.prototype, "paramsTo", null);
|
|
184
|
+
__decorate([
|
|
185
|
+
command({ args: ["domain"] })
|
|
186
|
+
], FunctionPlot.prototype, "domainTo", null);
|
|
187
|
+
FunctionPlot = __decorate([
|
|
188
|
+
node({
|
|
189
|
+
key: "functionPlot",
|
|
190
|
+
within: SYSTEMS,
|
|
191
|
+
closed: true,
|
|
192
|
+
parentKey: "shape",
|
|
193
|
+
forkable: true,
|
|
194
|
+
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: true },
|
|
195
|
+
seed: { expression: "sin(x)" },
|
|
196
|
+
}),
|
|
197
|
+
withdraw(...FILL_PAINT)
|
|
198
|
+
], FunctionPlot);
|
|
199
|
+
export { FunctionPlot };
|
|
200
|
+
// Roughly one cell of the contour's grid to this many pixels, each way.
|
|
201
|
+
const CONTOUR_CELL = 5;
|
|
202
|
+
const CONTOUR_MAX = 220;
|
|
203
|
+
// Two curves blended point for point along their parameters, in pixels, so any
|
|
204
|
+
// kind bends into any other.
|
|
205
|
+
function between(from, to, t) {
|
|
206
|
+
const a = { x: 0, y: 0 };
|
|
207
|
+
return (u, out) => {
|
|
208
|
+
if (!from(u, a) || !to(u, out))
|
|
209
|
+
return false;
|
|
210
|
+
out.x = a.x + (out.x - a.x) * t;
|
|
211
|
+
out.y = a.y + (out.y - a.y) * t;
|
|
212
|
+
return true;
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=function-plot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-plot.js","sourceRoot":"","sources":["../../src/plots/function-plot.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACH,OAAO,EACP,IAAI,EACJ,QAAQ,EACR,cAAc,EACd,QAAQ,GAOX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,OAAO,EAAE,cAAc,EAAE,aAAa,EAAiB,MAAM,iBAAiB,CAAC;AAE/E,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,WAAW,EAAwB,MAAM,gBAAgB,CAAC;AACnE,OAAO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AAY;IAVxB,IAAI,CAAC;QACF,GAAG,EAAE,cAAc;QACnB,MAAM,EAAE,OAAO;QACf,MAAM,EAAE,IAAI;QACZ,SAAS,EAAE,OAAO;QAClB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,gBAAgB,EAAE,KAAK,EAAE,iBAAiB,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE;QAC1G,IAAI,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE;KACjC,CAAC;IACD,QAAQ,CAAC,GAAG,UAAU,CAAC;GACX,YAAY,CA0KxB;;AAED,wEAAwE;AACxE,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,WAAW,GAAG,GAAG,CAAC;AAExB,+EAA+E;AAC/E,6BAA6B;AAC7B,SAAS,OAAO,CAAC,IAAqB,EAAE,EAAmB,EAAE,CAAS;IAClE,MAAM,CAAC,GAAY,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;IAClC,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE;QACd,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC;YAAE,OAAO,KAAK,CAAC;QAC7C,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAChC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC;IAChB,CAAC,CAAC;AACN,CAAC","sourcesContent":["import {\r\n command,\r\n node,\r\n property,\r\n strokeProperty,\r\n withdraw,\r\n type Command,\r\n type CommandArgs,\r\n type NodeConfig,\r\n type PaintProps,\r\n type Stroke,\r\n type Vector2,\r\n} from \"@motionscript/sdk\";\r\nimport { traceContour } from \"../kit/contour\";\r\nimport type { CoordinateMap, Interval } from \"../kit/coords\";\r\nimport type { Curve } from \"../kit/curve\";\r\nimport { equationParams, parseEquation, type Equation } from \"../kit/equation\";\r\nimport type { ExpressionScope } from \"../kit/expression\";\r\nimport { PLOT_COLOR } from \"../kit/furniture\";\r\nimport { stated } from \"../kit/geometry\";\r\nimport { Mark } from \"../kit/mark\";\r\nimport { sampleCurve, type CurveProjection } from \"../kit/sampler\";\r\nimport { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from \"../kit/source\";\r\nimport { SYSTEMS } from \"../kit/placement\";\r\nimport { FILL_PAINT } from \"../kit/withdrawn\";\r\n\r\nexport interface FunctionPlotProps extends PaintProps {\r\n expression?: string | PlotSource;\r\n domain?: Interval | null;\r\n params?: ExpressionScope;\r\n}\r\n\r\nconst TAU = Math.PI * 2;\r\n\r\n/**\r\n * The curve of an equation, drawn in the coordinate system it is a child of.\r\n * How the equation is written says what it is: see `Equation`. A mark put\r\n * inside it reads it: the area under it, a tangent, a point that rides it.\r\n */\r\n@node({\r\n key: \"functionPlot\",\r\n within: SYSTEMS,\r\n closed: true,\r\n parentKey: \"shape\",\r\n forkable: true,\r\n layout: { children: \"freeform\", defaultWidthMode: \"hug\", defaultHeightMode: \"hug\", acceptsChildren: true },\r\n seed: { expression: \"sin(x)\" },\r\n})\r\n@withdraw(...FILL_PAINT)\r\nexport class FunctionPlot<P extends FunctionPlotProps = FunctionPlotProps> extends Mark<P> implements Curve {\r\n @property({ default: \"\", mapper: readSource, tween: lerpSource, kind: \"equation\" })\r\n declare expression: PlotSource;\r\n\r\n /**\r\n * The span of the free variable that is drawn. Left out, a function runs the\r\n * width of its system, a polar curve one turn and a parametric one from 0 to 1.\r\n */\r\n @property({ default: null, tween: lerpDomain, kind: \"range\", defaultMin: -5, defaultMax: 5 }) declare domain: Interval | null;\r\n\r\n /**\r\n * The equation's named values: any name in it that is not a variable, a\r\n * function or a constant. One left unset is 1, and a formula can bind each\r\n * to a variable.\r\n */\r\n @property({ default: {}, tween: lerpParams }) declare params: ExpressionScope;\r\n\r\n @strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" } })\r\n declare stroke: Stroke;\r\n\r\n constructor(props?: NodeConfig<FunctionPlot<P>, P>) {\r\n super((props ?? {}) as NodeConfig<FunctionPlot<P>, P>);\r\n }\r\n\r\n /** Change the equation, the curve bending from the one into the other. */\r\n @command({ args: [\"expression\"] })\r\n morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<P> {\r\n return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });\r\n }\r\n\r\n /** Animate the equation's named values. */\r\n @command({ args: [\"params\"] })\r\n paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<P> {\r\n return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });\r\n }\r\n\r\n /** Animate the span that is drawn. */\r\n @command({ args: [\"domain\"] })\r\n domainTo(args: CommandArgs<{ domain: Interval | null }> & { duration: number }): Command<P> {\r\n return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });\r\n }\r\n\r\n /**\r\n * The curve's height at graph `x`, or `NaN` where it has none or is not a\r\n * function of `x`. What a point, a tangent or an area riding this plot reads.\r\n */\r\n valueAt(x: number): number {\r\n const { source, blend } = this.expression;\r\n const shift = this.shift();\r\n const value = this._heightOf(source, x - shift.x) + shift.y;\r\n if (!blend) return value;\r\n const from = this._heightOf(blend.from, x - shift.x) + shift.y;\r\n return from + (value - from) * blend.t;\r\n }\r\n\r\n /** The span of the free variable that is drawn: `domain`, unless a node built on this one says otherwise. */\r\n protected span(): Interval | null {\r\n return this.domain;\r\n }\r\n\r\n protected trace(map: CoordinateMap): Vector2[][] {\r\n const { area } = map;\r\n if (!Number.isFinite(area.maxX - area.minX)) return [];\r\n const { source, blend } = this.expression;\r\n const shift = this.shift();\r\n const key = [source, blend?.from, blend?.t, map.revision, this.span(), JSON.stringify(this.params), shift.x, shift.y].join(\"|\");\r\n if (key === this._key) return this._runs;\r\n this._key = key;\r\n\r\n // A curve with no parameter to walk cannot bend into another: it changes over half way.\r\n const shown = blend && blend.t < 0.5 ? blend.from : source;\r\n if (this._equation(source)?.kind === \"implicit\" || (blend && this._equation(blend.from)?.kind === \"implicit\")) {\r\n this._runs = this._contour(shown, map, shift);\r\n return this._runs;\r\n }\r\n\r\n let curve = this._curve(source, map, shift);\r\n if (curve && blend) {\r\n const from = this._curve(blend.from, map, shift);\r\n if (from) curve = between(from, curve, blend.t);\r\n }\r\n const width = area.maxX - area.minX;\r\n this._runs = curve\r\n ? sampleCurve(curve, 0, 1, area, { seeds: Math.min(512, Math.max(64, Math.round(width / 8))), minStep: 0.5 / Math.max(1, width) })\r\n : [];\r\n return this._runs;\r\n }\r\n\r\n private _key = \"\";\r\n private _runs: Vector2[][] = [];\r\n\r\n private _equation(source: string): Equation | null {\r\n const equation = parseEquation(source, equationParams(source));\r\n return equation instanceof Error ? null : equation;\r\n }\r\n\r\n private _heightOf(source: string, x: number): number {\r\n const equation = this._equation(source);\r\n if (equation?.kind !== \"function\" || equation.of !== \"x\") return NaN;\r\n return equation.f(x, 0, scopeFor(equationParams(source), this.params));\r\n }\r\n\r\n /** Where an implicit `source` holds, over what the system shows, as pixel runs. */\r\n private _contour(source: string, map: CoordinateMap, shift: Vector2): Vector2[][] {\r\n const equation = this._equation(source);\r\n if (equation?.kind !== \"implicit\") return [];\r\n const params = scopeFor(equationParams(source), this.params);\r\n const { area } = map;\r\n const cells = (pixels: number) => Math.min(CONTOUR_MAX, Math.max(8, Math.round(pixels / CONTOUR_CELL)));\r\n const { f } = equation;\r\n const runs = traceContour(\r\n (x, y) => f(x - shift.x, y - shift.y, params),\r\n map.xRange,\r\n map.yRange,\r\n cells(area.maxX - area.minX),\r\n cells(area.maxY - area.minY),\r\n );\r\n return runs.map((run) => run.map((point) => map.toPoint(point.x, point.y)));\r\n }\r\n\r\n /** `source` as a pixel curve over a parameter running 0 to 1. */\r\n private _curve(source: string, map: CoordinateMap, shift: Vector2): CurveProjection | null {\r\n const equation = this._equation(source);\r\n if (!equation || equation.kind === \"implicit\") return null;\r\n const params = scopeFor(equationParams(source), this.params);\r\n const domain = this.span();\r\n switch (equation.kind) {\r\n case \"function\": {\r\n const horizontal = equation.of === \"x\";\r\n const [lo, hi] = horizontal ? map.xRange : map.yRange;\r\n const offset = horizontal ? shift.x : shift.y;\r\n // Only what the system shows is sampled, however wide the domain.\r\n const a = Math.max(lo - offset, domain?.[0] ?? -Infinity);\r\n const b = Math.min(hi - offset, domain?.[1] ?? Infinity);\r\n if (!(b > a)) return null;\r\n const { f } = equation;\r\n return (u, out) => {\r\n const v = a + (b - a) * u;\r\n const w = f(v, 0, params);\r\n if (!Number.isFinite(w)) return false;\r\n if (horizontal) map.toPoint(v + shift.x, w + shift.y, out);\r\n else map.toPoint(w + shift.x, v + shift.y, out);\r\n return true;\r\n };\r\n }\r\n case \"polar\": {\r\n const [a, b] = domain ?? [0, TAU];\r\n const { f } = equation;\r\n return (u, out) => {\r\n const theta = a + (b - a) * u;\r\n const r = f(theta, 0, params);\r\n if (!Number.isFinite(r)) return false;\r\n map.toPoint(r * Math.cos(theta) + shift.x, r * Math.sin(theta) + shift.y, out);\r\n return true;\r\n };\r\n }\r\n case \"parametric\": {\r\n const [a, b] = domain ?? [0, 1];\r\n const { x, y } = equation;\r\n return (u, out) => {\r\n const t = a + (b - a) * u;\r\n const gx = x(t, 0, params);\r\n const gy = y(t, 0, params);\r\n if (!Number.isFinite(gx) || !Number.isFinite(gy)) return false;\r\n map.toPoint(gx + shift.x, gy + shift.y, out);\r\n return true;\r\n };\r\n }\r\n }\r\n }\r\n}\r\n\r\n// Roughly one cell of the contour's grid to this many pixels, each way.\r\nconst CONTOUR_CELL = 5;\r\nconst CONTOUR_MAX = 220;\r\n\r\n// Two curves blended point for point along their parameters, in pixels, so any\r\n// kind bends into any other.\r\nfunction between(from: CurveProjection, to: CurveProjection, t: number): CurveProjection {\r\n const a: Vector2 = { x: 0, y: 0 };\r\n return (u, out) => {\r\n if (!from(u, a) || !to(u, out)) return false;\r\n out.x = a.x + (out.x - a.x) * t;\r\n out.y = a.y + (out.y - a.y) * t;\r\n return true;\r\n };\r\n}\r\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Command, type CommandArgs, type NodeConfig, type PaintProps, type Stroke, type Vector2 } from "@motionscript/sdk";
|
|
2
|
+
import type { CoordinateMap } from "../kit/coords.js";
|
|
3
|
+
import { Mark } from "../kit/mark.js";
|
|
4
|
+
export interface PointsPlotProps extends PaintProps {
|
|
5
|
+
points?: Vector2[];
|
|
6
|
+
closed?: boolean;
|
|
7
|
+
smooth?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
|
|
10
|
+
export declare class PointsPlot extends Mark<PointsPlotProps> {
|
|
11
|
+
points: Vector2[];
|
|
12
|
+
/** Join the last point to the first. */
|
|
13
|
+
closed: boolean;
|
|
14
|
+
/** Curve through the points instead of turning a corner at each. */
|
|
15
|
+
smooth: boolean;
|
|
16
|
+
stroke: Stroke;
|
|
17
|
+
constructor(props?: NodeConfig<PointsPlot, PointsPlotProps>);
|
|
18
|
+
/** Move the points to new places; extra ones grow from the line's end. */
|
|
19
|
+
pointsTo(args: CommandArgs<{
|
|
20
|
+
points: Vector2[];
|
|
21
|
+
}> & {
|
|
22
|
+
duration: number;
|
|
23
|
+
}): Command<PointsPlotProps>;
|
|
24
|
+
protected isClosed(): boolean;
|
|
25
|
+
protected paintsFill(): boolean;
|
|
26
|
+
protected trace(map: CoordinateMap): Vector2[][];
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=points-plot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"points-plot.d.ts","sourceRoot":"","sources":["../../src/plots/points-plot.ts"],"names":[],"mappings":"AAAA,OAAO,EAKH,KAAK,OAAO,EACZ,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,MAAM,EACX,KAAK,OAAO,EACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAInC,MAAM,WAAW,eAAgB,SAAQ,UAAU;IAC/C,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;CACpB;AAkBD,+HAA+H;AAC/H,qBAQa,UAAW,SAAQ,IAAI,CAAC,eAAe,CAAC;IACK,MAAM,EAAE,OAAO,EAAE,CAAC;IACxE,wCAAwC;IACiB,MAAM,EAAE,OAAO,CAAC;IACzE,oEAAoE;IACX,MAAM,EAAE,OAAO,CAAC;IAGjE,MAAM,EAAE,MAAM,CAAC;gBAEX,KAAK,CAAC,EAAE,UAAU,CAAC,UAAU,EAAE,eAAe,CAAC;IAI3D,0EAA0E;IAE1E,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;QAAE,MAAM,EAAE,OAAO,EAAE,CAAA;KAAE,CAAC,GAAG;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,eAAe,CAAC;cAIhF,QAAQ,IAAI,OAAO;cAKnB,UAAU,IAAI,OAAO;IAIxC,SAAS,CAAC,KAAK,CAAC,GAAG,EAAE,aAAa,GAAG,OAAO,EAAE,EAAE;CAMnD"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { command, node, property, strokeProperty, } from "@motionscript/sdk";
|
|
8
|
+
import { stated } from "../kit/geometry.js";
|
|
9
|
+
import { Mark } from "../kit/mark.js";
|
|
10
|
+
import { PLOT_COLOR } from "../kit/furniture.js";
|
|
11
|
+
import { SYSTEMS } from "../kit/placement.js";
|
|
12
|
+
// A shorter list is padded with its own last point, so a line grows new points out of its end.
|
|
13
|
+
function lerpPoints(from, to, t) {
|
|
14
|
+
const count = Math.max(from.length, to.length);
|
|
15
|
+
if (from.length === 0 || to.length === 0)
|
|
16
|
+
return t < 1 ? from : to;
|
|
17
|
+
const out = new Array(count);
|
|
18
|
+
for (let i = 0; i < count; i++) {
|
|
19
|
+
const a = from[Math.min(i, from.length - 1)];
|
|
20
|
+
const b = to[Math.min(i, to.length - 1)];
|
|
21
|
+
out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
/** Points on the curve between each pair of a smooth line's points. */
|
|
26
|
+
const SMOOTH_STEPS = 16;
|
|
27
|
+
/** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */
|
|
28
|
+
let PointsPlot = class PointsPlot extends Mark {
|
|
29
|
+
constructor(props) {
|
|
30
|
+
super(props ?? {});
|
|
31
|
+
}
|
|
32
|
+
/** Move the points to new places; extra ones grow from the line's end. */
|
|
33
|
+
pointsTo(args) {
|
|
34
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
35
|
+
}
|
|
36
|
+
isClosed() {
|
|
37
|
+
return this.closed;
|
|
38
|
+
}
|
|
39
|
+
// An open line has no inside, so its fill waits until it is closed.
|
|
40
|
+
paintsFill() {
|
|
41
|
+
return this.closed;
|
|
42
|
+
}
|
|
43
|
+
trace(map) {
|
|
44
|
+
const shift = this.shift();
|
|
45
|
+
const pixels = this.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));
|
|
46
|
+
if (pixels.length < 2)
|
|
47
|
+
return [];
|
|
48
|
+
return [this.smooth && pixels.length > 2 ? spline(pixels, this.closed) : pixels];
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
__decorate([
|
|
52
|
+
property({ default: [], tween: lerpPoints })
|
|
53
|
+
], PointsPlot.prototype, "points", void 0);
|
|
54
|
+
__decorate([
|
|
55
|
+
property({ default: false, animatable: false })
|
|
56
|
+
], PointsPlot.prototype, "closed", void 0);
|
|
57
|
+
__decorate([
|
|
58
|
+
property({ default: false, animatable: false })
|
|
59
|
+
], PointsPlot.prototype, "smooth", void 0);
|
|
60
|
+
__decorate([
|
|
61
|
+
strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
|
|
62
|
+
], PointsPlot.prototype, "stroke", void 0);
|
|
63
|
+
__decorate([
|
|
64
|
+
command({ args: ["points"] })
|
|
65
|
+
], PointsPlot.prototype, "pointsTo", null);
|
|
66
|
+
PointsPlot = __decorate([
|
|
67
|
+
node({
|
|
68
|
+
key: "pointsPlot",
|
|
69
|
+
within: SYSTEMS,
|
|
70
|
+
parentKey: "shape",
|
|
71
|
+
forkable: true,
|
|
72
|
+
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
|
|
73
|
+
seed: { points: [{ x: -2, y: -1 }, { x: 0, y: 1 }, { x: 2, y: 0 }] },
|
|
74
|
+
})
|
|
75
|
+
], PointsPlot);
|
|
76
|
+
export { PointsPlot };
|
|
77
|
+
// Catmull–Rom, so the curve passes through every point it was given.
|
|
78
|
+
function spline(points, closed) {
|
|
79
|
+
const n = points.length;
|
|
80
|
+
const at = (i) => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);
|
|
81
|
+
const out = [];
|
|
82
|
+
const spans = closed ? n : n - 1;
|
|
83
|
+
for (let i = 0; i < spans; i++) {
|
|
84
|
+
const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
|
|
85
|
+
for (let s = 0; s < SMOOTH_STEPS; s++) {
|
|
86
|
+
const t = s / SMOOTH_STEPS;
|
|
87
|
+
const t2 = t * t;
|
|
88
|
+
const t3 = t2 * t;
|
|
89
|
+
out.push({
|
|
90
|
+
x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),
|
|
91
|
+
y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (!closed)
|
|
96
|
+
out.push(points[n - 1]);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=points-plot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"points-plot.js","sourceRoot":"","sources":["../../src/plots/points-plot.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACH,OAAO,EACP,IAAI,EACJ,QAAQ,EACR,cAAc,GAOjB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAQ3C,+FAA+F;AAC/F,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AC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;aACpI,CAAC,CAAC;QACP,CAAC;IACL,CAAC;IACD,IAAI,CAAC,MAAM;QAAE,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACrC,OAAO,GAAG,CAAC;AACf,CAAC","sourcesContent":["import {\r\n command,\r\n node,\r\n property,\r\n strokeProperty,\r\n type Command,\r\n type CommandArgs,\r\n type NodeConfig,\r\n type PaintProps,\r\n type Stroke,\r\n type Vector2,\r\n} from \"@motionscript/sdk\";\r\nimport type { CoordinateMap } from \"../kit/coords\";\r\nimport { stated } from \"../kit/geometry\";\r\nimport { Mark } from \"../kit/mark\";\r\nimport { PLOT_COLOR } from \"../kit/furniture\";\r\nimport { SYSTEMS } from \"../kit/placement\";\r\n\r\nexport interface PointsPlotProps extends PaintProps {\r\n points?: Vector2[];\r\n closed?: boolean;\r\n smooth?: boolean;\r\n}\r\n\r\n// A shorter list is padded with its own last point, so a line grows new points out of its end.\r\nfunction lerpPoints(from: Vector2[], to: Vector2[], t: number): Vector2[] {\r\n const count = Math.max(from.length, to.length);\r\n if (from.length === 0 || to.length === 0) return t < 1 ? from : to;\r\n const out = new Array<Vector2>(count);\r\n for (let i = 0; i < count; i++) {\r\n const a = from[Math.min(i, from.length - 1)];\r\n const b = to[Math.min(i, to.length - 1)];\r\n out[i] = { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };\r\n }\r\n return out;\r\n}\r\n\r\n/** Points on the curve between each pair of a smooth line's points. */\r\nconst SMOOTH_STEPS = 16;\r\n\r\n/** A line through listed graph points: data, or a shape drawn in graph coordinates when closed, which is when it is filled. */\r\n@node({\r\n key: \"pointsPlot\",\r\n within: SYSTEMS,\r\n parentKey: \"shape\",\r\n forkable: true,\r\n layout: { children: \"freeform\", defaultWidthMode: \"hug\", defaultHeightMode: \"hug\", acceptsChildren: false },\r\n seed: { points: [{ x: -2, y: -1 }, { x: 0, y: 1 }, { x: 2, y: 0 }] },\r\n})\r\nexport class PointsPlot extends Mark<PointsPlotProps> {\r\n @property({ default: [], tween: lerpPoints }) declare points: Vector2[];\r\n /** Join the last point to the first. */\r\n @property({ default: false, animatable: false }) declare closed: boolean;\r\n /** Curve through the points instead of turning a corner at each. */\r\n @property({ default: false, animatable: false }) declare smooth: boolean;\r\n\r\n @strokeProperty({ default: { weight: 3, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" } })\r\n declare stroke: Stroke;\r\n\r\n constructor(props?: NodeConfig<PointsPlot, PointsPlotProps>) {\r\n super(props ?? {});\r\n }\r\n\r\n /** Move the points to new places; extra ones grow from the line's end. */\r\n @command({ args: [\"points\"] })\r\n pointsTo(args: CommandArgs<{ points: Vector2[] }> & { duration: number }): Command<PointsPlotProps> {\r\n return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });\r\n }\r\n\r\n protected override isClosed(): boolean {\r\n return this.closed;\r\n }\r\n\r\n // An open line has no inside, so its fill waits until it is closed.\r\n protected override paintsFill(): boolean {\r\n return this.closed;\r\n }\r\n\r\n protected trace(map: CoordinateMap): Vector2[][] {\r\n const shift = this.shift();\r\n const pixels = this.points.map((p) => map.toPoint(p.x + shift.x, p.y + shift.y));\r\n if (pixels.length < 2) return [];\r\n return [this.smooth && pixels.length > 2 ? spline(pixels, this.closed) : pixels];\r\n }\r\n}\r\n\r\n// Catmull–Rom, so the curve passes through every point it was given.\r\nfunction spline(points: readonly Vector2[], closed: boolean): Vector2[] {\r\n const n = points.length;\r\n const at = (i: number): Vector2 => (closed ? points[((i % n) + n) % n] : points[Math.max(0, Math.min(n - 1, i))]);\r\n const out: Vector2[] = [];\r\n const spans = closed ? n : n - 1;\r\n for (let i = 0; i < spans; i++) {\r\n const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);\r\n for (let s = 0; s < SMOOTH_STEPS; s++) {\r\n const t = s / SMOOTH_STEPS;\r\n const t2 = t * t;\r\n const t3 = t2 * t;\r\n out.push({\r\n x: 0.5 * (2 * p1.x + (p2.x - p0.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (3 * p1.x - p0.x - 3 * p2.x + p3.x) * t3),\r\n y: 0.5 * (2 * p1.y + (p2.y - p0.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (3 * p1.y - p0.y - 3 * p2.y + p3.y) * t3),\r\n });\r\n }\r\n }\r\n if (!closed) out.push(points[n - 1]);\r\n return out;\r\n}\r\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Command, type CommandArgs, type NodeConfig, type Stroke } from "@motionscript/sdk";
|
|
2
|
+
import type { CoordinateMap } from "../kit/coords.js";
|
|
3
|
+
import { FieldMark, type FieldProps, type FieldSampler, type FieldStroke } from "../kit/field-mark.js";
|
|
4
|
+
export interface StreamLinesProps extends FieldProps {
|
|
5
|
+
spacing?: number;
|
|
6
|
+
length?: number;
|
|
7
|
+
window?: number;
|
|
8
|
+
phase?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The lines a vector field carries a point along, one from each point of a
|
|
12
|
+
* grid: `(-y, x)` draws circles. With a `window` below one, each line shows
|
|
13
|
+
* only a stretch of itself, and `flowTo` sends those stretches along.
|
|
14
|
+
*/
|
|
15
|
+
export declare class StreamLines extends FieldMark<StreamLinesProps> {
|
|
16
|
+
/** How far apart the lines start, in graph units. */
|
|
17
|
+
spacing: number;
|
|
18
|
+
/** How far each line runs from where it starts, in graph units along itself. */
|
|
19
|
+
length: number;
|
|
20
|
+
/** The share of each line that is shown at once; one shows all of it. */
|
|
21
|
+
window: number;
|
|
22
|
+
/** Where along its line each shown stretch is: one turn of it runs every line from end to end. */
|
|
23
|
+
phase: number;
|
|
24
|
+
stroke: Stroke;
|
|
25
|
+
constructor(props?: NodeConfig<StreamLines, StreamLinesProps>);
|
|
26
|
+
/** Send the shown stretches along their lines: a whole number runs each line that many times over. */
|
|
27
|
+
flowTo(args: CommandArgs<{
|
|
28
|
+
phase: number;
|
|
29
|
+
}> & {
|
|
30
|
+
duration: number;
|
|
31
|
+
}): Command<StreamLinesProps>;
|
|
32
|
+
protected own(): string;
|
|
33
|
+
protected strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[];
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=stream-lines.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-lines.d.ts","sourceRoot":"","sources":["../../src/plots/stream-lines.ts"],"names":[],"mappings":"AAAA,OAAO,EAKH,KAAK,OAAO,EACZ,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,MAAM,EAEd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAKpG,MAAM,WAAW,gBAAiB,SAAQ,UAAU;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB;AAOD;;;;GAIG;AACH,qBAQa,WAAY,SAAQ,SAAS,CAAC,gBAAgB,CAAC;IACxD,qDAAqD;IACG,OAAO,EAAE,MAAM,CAAC;IACxE,gFAAgF;IAC3B,MAAM,EAAE,MAAM,CAAC;IACpE,yEAAyE;IACR,MAAM,EAAE,MAAM,CAAC;IAChF,kGAAkG;IACpD,KAAK,EAAE,MAAM,CAAC;IAGpD,MAAM,EAAE,MAAM,CAAC;gBAEX,KAAK,CAAC,EAAE,UAAU,CAAC,WAAW,EAAE,gBAAgB,CAAC;IAI7D,sGAAsG;IAEtG,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,GAAG;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAI9F,SAAS,CAAC,GAAG,IAAI,MAAM;IAIvB,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,YAAY,GAAG,WAAW,EAAE;CA2B5E"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { command, node, property, strokeProperty, } from "@motionscript/sdk";
|
|
8
|
+
import { FieldMark } from "../kit/field-mark.js";
|
|
9
|
+
import { PLOT_COLOR } from "../kit/furniture.js";
|
|
10
|
+
import { clamp, stated } from "../kit/geometry.js";
|
|
11
|
+
import { SYSTEMS } from "../kit/placement.js";
|
|
12
|
+
const MAX_LINES = 1600;
|
|
13
|
+
/** Steps a line takes across one spacing: what keeps a tight swirl round. */
|
|
14
|
+
const STEPS_PER_SPACING = 8;
|
|
15
|
+
const MAX_STEPS = 600;
|
|
16
|
+
/**
|
|
17
|
+
* The lines a vector field carries a point along, one from each point of a
|
|
18
|
+
* grid: `(-y, x)` draws circles. With a `window` below one, each line shows
|
|
19
|
+
* only a stretch of itself, and `flowTo` sends those stretches along.
|
|
20
|
+
*/
|
|
21
|
+
let StreamLines = class StreamLines extends FieldMark {
|
|
22
|
+
constructor(props) {
|
|
23
|
+
super(props ?? {});
|
|
24
|
+
}
|
|
25
|
+
/** Send the shown stretches along their lines: a whole number runs each line that many times over. */
|
|
26
|
+
flowTo(args) {
|
|
27
|
+
return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
|
|
28
|
+
}
|
|
29
|
+
own() {
|
|
30
|
+
return `${this.spacing}|${this.length}|${this.window}|${this.phase}`;
|
|
31
|
+
}
|
|
32
|
+
strokes(map, field) {
|
|
33
|
+
const [x0, x1] = map.xRange;
|
|
34
|
+
const [y0, y1] = map.yRange;
|
|
35
|
+
let spacing = Math.max(this.spacing, 1e-6);
|
|
36
|
+
const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
|
|
37
|
+
while (count() > MAX_LINES)
|
|
38
|
+
spacing *= 2;
|
|
39
|
+
const step = spacing / STEPS_PER_SPACING;
|
|
40
|
+
const steps = Math.min(MAX_STEPS, Math.round(this.length / step));
|
|
41
|
+
if (steps < 1)
|
|
42
|
+
return [];
|
|
43
|
+
// A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.
|
|
44
|
+
const inside = (x, y) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;
|
|
45
|
+
const window = clamp(this.window, 0, 1);
|
|
46
|
+
const drawn = [];
|
|
47
|
+
let index = 0;
|
|
48
|
+
for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
|
|
49
|
+
for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
|
|
50
|
+
const line = follow(field, i * spacing, j * spacing, step, steps, inside);
|
|
51
|
+
index++;
|
|
52
|
+
if (line.points.length < 2)
|
|
53
|
+
continue;
|
|
54
|
+
const shown = window >= 1 ? line.points : stretch(line.points, window, this.phase + stagger(index));
|
|
55
|
+
if (shown.length < 2)
|
|
56
|
+
continue;
|
|
57
|
+
drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return drawn;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
__decorate([
|
|
64
|
+
property({ default: 1, min: 0.01, step: 0.1 })
|
|
65
|
+
], StreamLines.prototype, "spacing", void 0);
|
|
66
|
+
__decorate([
|
|
67
|
+
property({ default: 3, min: 0, step: 0.1 })
|
|
68
|
+
], StreamLines.prototype, "length", void 0);
|
|
69
|
+
__decorate([
|
|
70
|
+
property({ default: 1, min: 0.01, max: 1, step: 0.01 })
|
|
71
|
+
], StreamLines.prototype, "window", void 0);
|
|
72
|
+
__decorate([
|
|
73
|
+
property({ default: 0, step: 0.01 })
|
|
74
|
+
], StreamLines.prototype, "phase", void 0);
|
|
75
|
+
__decorate([
|
|
76
|
+
strokeProperty({ default: { weight: 2, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" } })
|
|
77
|
+
], StreamLines.prototype, "stroke", void 0);
|
|
78
|
+
__decorate([
|
|
79
|
+
command({ args: ["phase"] })
|
|
80
|
+
], StreamLines.prototype, "flowTo", null);
|
|
81
|
+
StreamLines = __decorate([
|
|
82
|
+
node({
|
|
83
|
+
key: "streamLines",
|
|
84
|
+
within: SYSTEMS,
|
|
85
|
+
parentKey: "shape",
|
|
86
|
+
forkable: true,
|
|
87
|
+
layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
|
|
88
|
+
seed: { expression: "(sin(y), cos(x))" },
|
|
89
|
+
})
|
|
90
|
+
], StreamLines);
|
|
91
|
+
export { StreamLines };
|
|
92
|
+
/** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */
|
|
93
|
+
function follow(field, x, y, step, steps, inside) {
|
|
94
|
+
const points = [{ x, y }];
|
|
95
|
+
const v = { x: 0, y: 0 };
|
|
96
|
+
let strength = 0;
|
|
97
|
+
let read = 0;
|
|
98
|
+
// The field's direction at a point, as a unit step; `false` where it has none or is at rest.
|
|
99
|
+
const heading = (px, py, out) => {
|
|
100
|
+
if (!field(px, py, v))
|
|
101
|
+
return false;
|
|
102
|
+
const magnitude = Math.hypot(v.x, v.y);
|
|
103
|
+
if (!(magnitude > 1e-9))
|
|
104
|
+
return false;
|
|
105
|
+
out.x = v.x / magnitude;
|
|
106
|
+
out.y = v.y / magnitude;
|
|
107
|
+
strength += magnitude;
|
|
108
|
+
read++;
|
|
109
|
+
return true;
|
|
110
|
+
};
|
|
111
|
+
const a = { x: 0, y: 0 };
|
|
112
|
+
const b = { x: 0, y: 0 };
|
|
113
|
+
for (let i = 0; i < steps; i++) {
|
|
114
|
+
// Midpoint rule: the direction half a step on is the one the whole step takes.
|
|
115
|
+
if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b))
|
|
116
|
+
break;
|
|
117
|
+
x += b.x * step;
|
|
118
|
+
y += b.y * step;
|
|
119
|
+
if (!inside(x, y))
|
|
120
|
+
break;
|
|
121
|
+
points.push({ x, y });
|
|
122
|
+
}
|
|
123
|
+
return { points, magnitude: read > 0 ? strength / read : 0 };
|
|
124
|
+
}
|
|
125
|
+
/** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */
|
|
126
|
+
function stretch(points, share, phase) {
|
|
127
|
+
const turn = phase - Math.floor(phase);
|
|
128
|
+
const end = turn * (1 + share);
|
|
129
|
+
const from = Math.max(0, end - share);
|
|
130
|
+
const to = Math.min(1, end);
|
|
131
|
+
if (!(to > from))
|
|
132
|
+
return [];
|
|
133
|
+
const last = points.length - 1;
|
|
134
|
+
return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);
|
|
135
|
+
}
|
|
136
|
+
/** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */
|
|
137
|
+
function stagger(index) {
|
|
138
|
+
const mixed = Math.sin(index * 12.9898) * 43758.5453;
|
|
139
|
+
return mixed - Math.floor(mixed);
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=stream-lines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-lines.js","sourceRoot":"","sources":["../../src/plots/stream-lines.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACH,OAAO,EACP,IAAI,EACJ,QAAQ,EACR,cAAc,GAMjB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,SAAS,EAAwD,MAAM,mBAAmB,CAAC;AACpG,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAS3C,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,6EAA6E;AAC7E,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,SAAS,GAAG,GAAG,CAAC;AAEtB;;;;GAIG;AASI,IAAM,WAAW,GAAjB,MAAM,WAAY,SAAQ,SAA2B;IAaxD,YAAY,KAAiD;QACzD,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IACvB,CAAC;IAED,sGAAsG;IAEtG,MAAM,CAAC,IAA2D;QAC9D,OAAO,IAAI,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAC9F,CAAC;IAES,GAAG;QACT,OAAO,GAAG,I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IAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,UAAU,CAAC;IACrD,OAAO,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import {\r\n command,\r\n node,\r\n property,\r\n strokeProperty,\r\n type Command,\r\n type CommandArgs,\r\n type NodeConfig,\r\n type Stroke,\r\n type Vector2,\r\n} from \"@motionscript/sdk\";\r\nimport type { CoordinateMap } from \"../kit/coords\";\r\nimport { FieldMark, type FieldProps, type FieldSampler, type FieldStroke } from \"../kit/field-mark\";\r\nimport { PLOT_COLOR } from \"../kit/furniture\";\r\nimport { clamp, stated } from \"../kit/geometry\";\r\nimport { SYSTEMS } from \"../kit/placement\";\r\n\r\nexport interface StreamLinesProps extends FieldProps {\r\n spacing?: number;\r\n length?: number;\r\n window?: number;\r\n phase?: number;\r\n}\r\n\r\nconst MAX_LINES = 1600;\r\n/** Steps a line takes across one spacing: what keeps a tight swirl round. */\r\nconst STEPS_PER_SPACING = 8;\r\nconst MAX_STEPS = 600;\r\n\r\n/**\r\n * The lines a vector field carries a point along, one from each point of a\r\n * grid: `(-y, x)` draws circles. With a `window` below one, each line shows\r\n * only a stretch of itself, and `flowTo` sends those stretches along.\r\n */\r\n@node({\r\n key: \"streamLines\",\r\n within: SYSTEMS,\r\n parentKey: \"shape\",\r\n forkable: true,\r\n layout: { children: \"freeform\", defaultWidthMode: \"hug\", defaultHeightMode: \"hug\", acceptsChildren: false },\r\n seed: { expression: \"(sin(y), cos(x))\" },\r\n})\r\nexport class StreamLines extends FieldMark<StreamLinesProps> {\r\n /** How far apart the lines start, in graph units. */\r\n @property({ default: 1, min: 0.01, step: 0.1 }) declare spacing: number;\r\n /** How far each line runs from where it starts, in graph units along itself. */\r\n @property({ default: 3, min: 0, step: 0.1 }) declare length: number;\r\n /** The share of each line that is shown at once; one shows all of it. */\r\n @property({ default: 1, min: 0.01, max: 1, step: 0.01 }) declare window: number;\r\n /** Where along its line each shown stretch is: one turn of it runs every line from end to end. */\r\n @property({ default: 0, step: 0.01 }) declare phase: number;\r\n\r\n @strokeProperty({ default: { weight: 2, fill: PLOT_COLOR, align: \"center\", cap: \"round\", join: \"round\" } })\r\n declare stroke: Stroke;\r\n\r\n constructor(props?: NodeConfig<StreamLines, StreamLinesProps>) {\r\n super(props ?? {});\r\n }\r\n\r\n /** Send the shown stretches along their lines: a whole number runs each line that many times over. */\r\n @command({ args: [\"phase\"] })\r\n flowTo(args: CommandArgs<{ phase: number }> & { duration: number }): Command<StreamLinesProps> {\r\n return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });\r\n }\r\n\r\n protected own(): string {\r\n return `${this.spacing}|${this.length}|${this.window}|${this.phase}`;\r\n }\r\n\r\n protected strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[] {\r\n const [x0, x1] = map.xRange;\r\n const [y0, y1] = map.yRange;\r\n let spacing = Math.max(this.spacing, 1e-6);\r\n const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);\r\n while (count() > MAX_LINES) spacing *= 2;\r\n const step = spacing / STEPS_PER_SPACING;\r\n const steps = Math.min(MAX_STEPS, Math.round(this.length / step));\r\n if (steps < 1) return [];\r\n // A line is followed a little past what is shown, so it leaves the box rather than stopping at its edge.\r\n const inside = (x: number, y: number) => x >= x0 - spacing && x <= x1 + spacing && y >= y0 - spacing && y <= y1 + spacing;\r\n const window = clamp(this.window, 0, 1);\r\n\r\n const drawn: FieldStroke[] = [];\r\n let index = 0;\r\n for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {\r\n for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {\r\n const line = follow(field, i * spacing, j * spacing, step, steps, inside);\r\n index++;\r\n if (line.points.length < 2) continue;\r\n const shown = window >= 1 ? line.points : stretch(line.points, window, this.phase + stagger(index));\r\n if (shown.length < 2) continue;\r\n drawn.push({ runs: [shown.map((p) => map.toPoint(p.x, p.y))], magnitude: line.magnitude });\r\n }\r\n }\r\n return drawn;\r\n }\r\n}\r\n\r\n/** The graph points a field carries a point through from `(x, y)`, at an even pace, and how strong it was on the way. */\r\nfunction follow(\r\n field: FieldSampler,\r\n x: number,\r\n y: number,\r\n step: number,\r\n steps: number,\r\n inside: (x: number, y: number) => boolean,\r\n): { points: Vector2[]; magnitude: number } {\r\n const points: Vector2[] = [{ x, y }];\r\n const v: Vector2 = { x: 0, y: 0 };\r\n let strength = 0;\r\n let read = 0;\r\n // The field's direction at a point, as a unit step; `false` where it has none or is at rest.\r\n const heading = (px: number, py: number, out: Vector2): boolean => {\r\n if (!field(px, py, v)) return false;\r\n const magnitude = Math.hypot(v.x, v.y);\r\n if (!(magnitude > 1e-9)) return false;\r\n out.x = v.x / magnitude;\r\n out.y = v.y / magnitude;\r\n strength += magnitude;\r\n read++;\r\n return true;\r\n };\r\n const a: Vector2 = { x: 0, y: 0 };\r\n const b: Vector2 = { x: 0, y: 0 };\r\n for (let i = 0; i < steps; i++) {\r\n // Midpoint rule: the direction half a step on is the one the whole step takes.\r\n if (!heading(x, y, a) || !heading(x + (a.x * step) / 2, y + (a.y * step) / 2, b)) break;\r\n x += b.x * step;\r\n y += b.y * step;\r\n if (!inside(x, y)) break;\r\n points.push({ x, y });\r\n }\r\n return { points, magnitude: read > 0 ? strength / read : 0 };\r\n}\r\n\r\n/** The part of `points` a window `share` long shows at `phase`: it enters at one end and leaves by the other. */\r\nfunction stretch(points: Vector2[], share: number, phase: number): Vector2[] {\r\n const turn = phase - Math.floor(phase);\r\n const end = turn * (1 + share);\r\n const from = Math.max(0, end - share);\r\n const to = Math.min(1, end);\r\n if (!(to > from)) return [];\r\n const last = points.length - 1;\r\n return points.slice(Math.floor(from * last), Math.ceil(to * last) + 1);\r\n}\r\n\r\n/** A share of a turn that differs from line to line and never from frame to frame, so the stretches do not march in step. */\r\nfunction stagger(index: number): number {\r\n const mixed = Math.sin(index * 12.9898) * 43758.5453;\r\n return mixed - Math.floor(mixed);\r\n}\r\n"]}
|