@vitavision/stage2d 0.15.0 → 0.16.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/README.md +16 -0
- package/dist/index.d.ts +382 -1
- package/dist/index.js +944 -10
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/ContourLayer.tsx +211 -0
- package/src/components/PointMarquee.tsx +229 -0
- package/src/components/VectorField.tsx +302 -0
- package/src/components/VectorFieldKey.tsx +91 -0
- package/src/components/contour.ts +270 -0
- package/src/components/vectorGeometry.ts +159 -0
- package/src/index.ts +23 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of a vector field overlay: magnitudes, the scale that turns data units into
|
|
3
|
+
* image pixels, the path of an arrow, and the bins a colour map is quantised into.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is pure. Vectors are a flat `[dx0, dy0, dx1, dy1, …]` array in data units
|
|
6
|
+
* (a displacement in pixels, a velocity, a gradient); a vector with a non-finite component is
|
|
7
|
+
* absent.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* How data units become image pixels.
|
|
12
|
+
* - A number is image pixels per data unit: the arrows are geometry, so they grow with the zoom
|
|
13
|
+
* like the image does.
|
|
14
|
+
* - `{ length }` sizes the arrows so the `percentile` vector spans `length` screen pixels at
|
|
15
|
+
* every zoom: the field stays readable whatever the magnification.
|
|
16
|
+
*/
|
|
17
|
+
export type VectorScale =
|
|
18
|
+
| number
|
|
19
|
+
| {
|
|
20
|
+
/** The screen length, in CSS pixels, the reference vector spans. */
|
|
21
|
+
length: number;
|
|
22
|
+
/** Which magnitude is the reference, in `[0, 1]`. Defaults to `1`, the longest vector. */
|
|
23
|
+
percentile?: number | undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The length of vector `i`, or `NaN` when it is absent.
|
|
28
|
+
*
|
|
29
|
+
* @param vectors - Flat `[dx0, dy0, …]`.
|
|
30
|
+
* @param i - The vector's index.
|
|
31
|
+
* @returns `hypot(dx, dy)`, or `NaN` for a non-finite component.
|
|
32
|
+
*/
|
|
33
|
+
export function vectorMagnitude(vectors: ArrayLike<number>, i: number): number {
|
|
34
|
+
const dx = vectors[2 * i];
|
|
35
|
+
const dy = vectors[2 * i + 1];
|
|
36
|
+
if (dx === undefined || dy === undefined || !Number.isFinite(dx) || !Number.isFinite(dy)) return Number.NaN;
|
|
37
|
+
return Math.hypot(dx, dy);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The magnitudes of every vector, `NaN` for an absent one.
|
|
42
|
+
*
|
|
43
|
+
* @param vectors - Flat `[dx0, dy0, …]`.
|
|
44
|
+
* @returns One magnitude per vector.
|
|
45
|
+
*/
|
|
46
|
+
export function vectorMagnitudes(vectors: ArrayLike<number>): Float64Array {
|
|
47
|
+
const count = Math.floor(vectors.length / 2);
|
|
48
|
+
const out = new Float64Array(count);
|
|
49
|
+
for (let i = 0; i < count; i++) out[i] = vectorMagnitude(vectors, i);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A magnitude's rank statistic over the present vectors.
|
|
55
|
+
*
|
|
56
|
+
* @param magnitudes - As `vectorMagnitudes` returns them; `NaN` entries are ignored.
|
|
57
|
+
* @param p - The fraction in `[0, 1]` (clamped): `1` is the longest, `0.5` the median.
|
|
58
|
+
* @returns The nearest-rank value, or `0` when no vector is present.
|
|
59
|
+
*/
|
|
60
|
+
export function magnitudePercentile(magnitudes: ArrayLike<number>, p: number): number {
|
|
61
|
+
const live: number[] = [];
|
|
62
|
+
for (let i = 0; i < magnitudes.length; i++) {
|
|
63
|
+
const m = magnitudes[i]!;
|
|
64
|
+
if (Number.isFinite(m)) live.push(m);
|
|
65
|
+
}
|
|
66
|
+
if (live.length === 0) return 0;
|
|
67
|
+
live.sort((a, b) => a - b);
|
|
68
|
+
const q = Number.isFinite(p) ? Math.min(1, Math.max(0, p)) : 1;
|
|
69
|
+
return live[Math.max(0, Math.ceil(q * live.length) - 1)]!;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The scale in image pixels per data unit.
|
|
74
|
+
*
|
|
75
|
+
* @param scale - See `VectorScale`.
|
|
76
|
+
* @param referenceMagnitude - The reference vector's length in data units, for an automatic scale.
|
|
77
|
+
* @param imagePerScreenPx - Image pixels covered by one screen pixel at the current zoom.
|
|
78
|
+
* @returns Image pixels per data unit; `0` when an automatic scale has no vector to measure.
|
|
79
|
+
*/
|
|
80
|
+
export function imagePerUnit(scale: VectorScale, referenceMagnitude: number, imagePerScreenPx: number): number {
|
|
81
|
+
if (typeof scale === "number") return Number.isFinite(scale) && scale > 0 ? scale : 0;
|
|
82
|
+
if (!(referenceMagnitude > 0) || !(scale.length > 0)) return 0;
|
|
83
|
+
return (scale.length * imagePerScreenPx) / referenceMagnitude;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A round length for a key: the largest of 1, 2 and 5 times a power of ten that is at most `max`.
|
|
88
|
+
*
|
|
89
|
+
* @param max - The upper bound, in data units.
|
|
90
|
+
* @returns The round length, or `0` for a non-positive `max`.
|
|
91
|
+
*/
|
|
92
|
+
export function niceLength(max: number): number {
|
|
93
|
+
if (!(max > 0) || !Number.isFinite(max)) return 0;
|
|
94
|
+
const power = 10 ** Math.floor(Math.log10(max));
|
|
95
|
+
for (const m of [5, 2, 1]) if (m * power <= max * (1 + 1e-12)) return m * power;
|
|
96
|
+
return power;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The shape of an arrow, in screen pixels. */
|
|
100
|
+
export interface ArrowStyle {
|
|
101
|
+
/** The arrowhead's length. */
|
|
102
|
+
head: number;
|
|
103
|
+
/** The arrowhead's full width, as a fraction of its length. */
|
|
104
|
+
headWidth: number;
|
|
105
|
+
/** A vector shorter than this is not drawn. */
|
|
106
|
+
minLength: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const r = (v: number): string => String(Math.round(v * 1000) / 1000);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The path data of one arrow: a shaft and a filled head.
|
|
113
|
+
*
|
|
114
|
+
* The head is capped at 45 % of the arrow's length, so a short arrow is a small wedge rather
|
|
115
|
+
* than a head on no shaft. Lengths are in the units of `(x, y)` (image pixels); `unit` converts
|
|
116
|
+
* the style's screen pixels to them.
|
|
117
|
+
*
|
|
118
|
+
* @param x - The anchor's x.
|
|
119
|
+
* @param y - The anchor's y.
|
|
120
|
+
* @param dx - The displacement's x, already scaled to image pixels.
|
|
121
|
+
* @param dy - The displacement's y, already scaled to image pixels.
|
|
122
|
+
* @param unit - Image pixels per screen pixel.
|
|
123
|
+
* @param style - The arrow's shape.
|
|
124
|
+
* @returns Path data, or `""` when the arrow is absent or shorter than `style.minLength`.
|
|
125
|
+
*/
|
|
126
|
+
export function arrowPath(x: number, y: number, dx: number, dy: number, unit: number, style: ArrowStyle): string {
|
|
127
|
+
const len = Math.hypot(dx, dy);
|
|
128
|
+
if (!(len > 0) || len < style.minLength * unit) return "";
|
|
129
|
+
const ux = dx / len;
|
|
130
|
+
const uy = dy / len;
|
|
131
|
+
const head = Math.min(style.head * unit, 0.45 * len);
|
|
132
|
+
const half = (head * style.headWidth) / 2;
|
|
133
|
+
const tx = x + dx;
|
|
134
|
+
const ty = y + dy;
|
|
135
|
+
const bx = tx - ux * head;
|
|
136
|
+
const by = ty - uy * head;
|
|
137
|
+
return (
|
|
138
|
+
`M${r(x)} ${r(y)}L${r(bx)} ${r(by)}` +
|
|
139
|
+
`M${r(tx)} ${r(ty)}L${r(bx - uy * half)} ${r(by + ux * half)}L${r(bx + uy * half)} ${r(by - ux * half)}Z`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The number of colour steps a magnitude-coloured field is quantised into: one path each. */
|
|
144
|
+
export const VECTOR_COLOR_BINS = 24;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The colour bin of a magnitude.
|
|
148
|
+
*
|
|
149
|
+
* @param magnitude - The vector's length.
|
|
150
|
+
* @param low - The magnitude drawn in the map's first colour.
|
|
151
|
+
* @param high - The magnitude drawn in the map's last colour.
|
|
152
|
+
* @param bins - The number of bins. Defaults to `VECTOR_COLOR_BINS`.
|
|
153
|
+
* @returns An integer in `[0, bins - 1]`; a degenerate range puts everything in bin `0`.
|
|
154
|
+
*/
|
|
155
|
+
export function colorBin(magnitude: number, low: number, high: number, bins = VECTOR_COLOR_BINS): number {
|
|
156
|
+
if (!(high > low)) return 0;
|
|
157
|
+
const t = (magnitude - low) / (high - low);
|
|
158
|
+
return Math.min(bins - 1, Math.max(0, Math.floor(t * bins)));
|
|
159
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -130,6 +130,13 @@ export {
|
|
|
130
130
|
type PolylineIndex,
|
|
131
131
|
} from "./components/polylineIndex";
|
|
132
132
|
export { PointSet, type PointSetItem, type PointSetProps } from "./components/PointSet";
|
|
133
|
+
export {
|
|
134
|
+
PointMarquee,
|
|
135
|
+
type PointMarqueeHandle,
|
|
136
|
+
type PointMarqueeMode,
|
|
137
|
+
type PointMarqueeModifier,
|
|
138
|
+
type PointMarqueeProps,
|
|
139
|
+
} from "./components/PointMarquee";
|
|
133
140
|
export {
|
|
134
141
|
buildPointIndex,
|
|
135
142
|
buildPointIndexFrom,
|
|
@@ -178,6 +185,22 @@ export {
|
|
|
178
185
|
type HeatmapRgbaProps,
|
|
179
186
|
} from "./components/HeatmapLayer";
|
|
180
187
|
export { planeRange, rasterizePlane, type Colormap, type ValueRange } from "./components/heatmapRaster";
|
|
188
|
+
export { ContourLayer, type ContourLayerProps } from "./components/ContourLayer";
|
|
189
|
+
export { contourPaths, resolveLevels, type ContourLine } from "./components/contour";
|
|
190
|
+
export { VectorField, useVectorScale, type VectorFieldProps } from "./components/VectorField";
|
|
191
|
+
export { VectorFieldKey, type VectorFieldKeyProps } from "./components/VectorFieldKey";
|
|
192
|
+
export {
|
|
193
|
+
VECTOR_COLOR_BINS,
|
|
194
|
+
arrowPath,
|
|
195
|
+
colorBin,
|
|
196
|
+
imagePerUnit,
|
|
197
|
+
magnitudePercentile,
|
|
198
|
+
niceLength,
|
|
199
|
+
vectorMagnitude,
|
|
200
|
+
vectorMagnitudes,
|
|
201
|
+
type ArrowStyle,
|
|
202
|
+
type VectorScale,
|
|
203
|
+
} from "./components/vectorGeometry";
|
|
181
204
|
export { ShapeEditor, type ShapeEditorProps } from "./components/ShapeEditor";
|
|
182
205
|
export {
|
|
183
206
|
fromShapeFrame,
|