@vitavision/stage2d 0.8.1 → 0.10.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 +41 -0
- package/dist/index.d.ts +1083 -3
- package/dist/index.js +2815 -68
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/AreaSet.tsx +312 -0
- package/src/components/DraftShape.tsx +175 -0
- package/src/components/GridLayer.tsx +209 -0
- package/src/components/HeatmapLayer.tsx +211 -0
- package/src/components/PointSet.tsx +366 -0
- package/src/components/PolylineSet.tsx +39 -0
- package/src/components/ShapeEditor.tsx +290 -0
- package/src/components/areaIndex.ts +361 -0
- package/src/components/gridEdges.ts +70 -0
- package/src/components/heatmapRaster.ts +84 -0
- package/src/components/labelLod.ts +90 -0
- package/src/components/markerShapes.ts +119 -0
- package/src/components/pointIndex.ts +320 -0
- package/src/components/polylineIndex.ts +4 -3
- package/src/components/shapeEdit.ts +232 -0
- package/src/components/stage/ImageStage.tsx +264 -59
- package/src/components/stage/gesture.ts +81 -0
- package/src/components/stage/hitContext.ts +16 -0
- package/src/components/stage/hitTest.ts +181 -0
- package/src/components/stage/useStageHitTest.ts +127 -0
- package/src/components/useShapeDrag.ts +104 -0
- package/src/index.ts +96 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A detected lattice over the image: the corners of a calibration board with the row and
|
|
3
|
+
* column edges between neighbours, and optional `(i, j)` labels.
|
|
4
|
+
*
|
|
5
|
+
* Nodes are a `PointSet` (batched markers, the point index, hover and selection), so a grid of
|
|
6
|
+
* 20,000 corners is a dozen DOM nodes; the edges are two more batched paths (ADR-0004 rules 1
|
|
7
|
+
* and 4). Edges are not picked: a person points at a corner.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useMemo } from "react";
|
|
11
|
+
|
|
12
|
+
import { latticeEdges, type LatticeAxis } from "./gridEdges";
|
|
13
|
+
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
14
|
+
import { PointSet, type PointSetItem } from "./PointSet";
|
|
15
|
+
import type { MarkerShape } from "./markerShapes";
|
|
16
|
+
import type { PointId } from "./pointIndex";
|
|
17
|
+
import { useStage } from "./stage/ImageStage";
|
|
18
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
19
|
+
import { STAGE_HIT_PRIORITY } from "./stage/hitTest";
|
|
20
|
+
import { useScreenPx } from "./stage/useScreenPx";
|
|
21
|
+
import { imageViewBox } from "./stage/view";
|
|
22
|
+
|
|
23
|
+
/** One node of a `GridLayer`: a point with its lattice index. */
|
|
24
|
+
export interface GridNode extends PointSetItem {
|
|
25
|
+
/** The lattice index along the first axis. */
|
|
26
|
+
i: number;
|
|
27
|
+
/** The lattice index along the second axis. */
|
|
28
|
+
j: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** How the edges along one lattice axis are drawn. */
|
|
32
|
+
export interface GridEdgeStyle {
|
|
33
|
+
/** The overlay role of the edges. Defaults to `"structure"`: they are context. */
|
|
34
|
+
role?: OverlayRole | undefined;
|
|
35
|
+
/** Draw them dashed (4 3 screen px), so the two axes differ by more than colour. */
|
|
36
|
+
dashed?: boolean | undefined;
|
|
37
|
+
/** Draw them at all. Defaults to `true`. */
|
|
38
|
+
visible?: boolean | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Props of `GridLayer`. */
|
|
42
|
+
export interface GridLayerProps {
|
|
43
|
+
/** The nodes, in image coordinates, with their lattice indices. Gaps are allowed. */
|
|
44
|
+
nodes: readonly GridNode[];
|
|
45
|
+
/**
|
|
46
|
+
* How edges are drawn, by axis: `i` joins `(i, j)` to `(i + 1, j)` (the rows, when `i`
|
|
47
|
+
* counts columns), `j` joins `(i, j)` to `(i, j + 1)`. Both are solid `structure` by default.
|
|
48
|
+
*/
|
|
49
|
+
edges?: { i?: GridEdgeStyle | undefined; j?: GridEdgeStyle | undefined } | undefined;
|
|
50
|
+
/** The marker kind of a node that names none. Defaults to `"plus"`: a corner. */
|
|
51
|
+
kind?: string | undefined;
|
|
52
|
+
/** More marker kinds, or replacements for built-in ones, by name; see `PointSet`. */
|
|
53
|
+
markers?: Readonly<Record<string, MarkerShape>> | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Label every node without a `label` of its own with its lattice index, `i,j`, where the
|
|
56
|
+
* zoom leaves room (nodes at least 24 screen px apart). Off by default.
|
|
57
|
+
*/
|
|
58
|
+
indexLabels?: boolean | undefined;
|
|
59
|
+
/** Show the nodes' labels where the zoom leaves room for them. On by default. */
|
|
60
|
+
labels?: boolean | undefined;
|
|
61
|
+
/** The hovered node id, when the app controls hover. */
|
|
62
|
+
hoveredId?: PointId | null | undefined;
|
|
63
|
+
/** Called when the node under the pointer changes. `null` when it leaves every node. */
|
|
64
|
+
onHoverChange?: ((id: PointId | null) => void) | undefined;
|
|
65
|
+
/** The selected node ids, drawn at 2.5 px in the selection colour with a ring. */
|
|
66
|
+
selectedIds?: Iterable<PointId> | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Node ids drawn at 35 % opacity, as a set or a predicate. An edge is dimmed when both its
|
|
69
|
+
* nodes are. Pass a stable value: a new one re-batches the layer.
|
|
70
|
+
*/
|
|
71
|
+
dimmed?: Iterable<PointId> | ((id: PointId) => boolean) | undefined;
|
|
72
|
+
/** A press landed on a node; see `PointSet`. Setting it makes the layer claim presses on its nodes. */
|
|
73
|
+
onItemPress?: ((id: PointId, event: StagePointerEvent) => void) | undefined;
|
|
74
|
+
/** Draw the dark band under every mark. On by default; off for a fast, flat layer. */
|
|
75
|
+
halo?: boolean | undefined;
|
|
76
|
+
/** CSS colour of selected nodes and rings. Defaults to the overlay `selection` role. */
|
|
77
|
+
selectionStroke?: string | undefined;
|
|
78
|
+
/** The id hit-tests report for the nodes. Defaults to a generated one. */
|
|
79
|
+
layerId?: string | undefined;
|
|
80
|
+
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.point`. */
|
|
81
|
+
priority?: number | undefined;
|
|
82
|
+
/** The layer's accessible name. Defaults to "Grid". */
|
|
83
|
+
label?: string | undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const HALO = overlayRole("halo");
|
|
87
|
+
|
|
88
|
+
/** A style with its defaults filled in. */
|
|
89
|
+
function edgeStyle(style: GridEdgeStyle | undefined): { role: OverlayRole; dashed: boolean; visible: boolean } {
|
|
90
|
+
return { role: style?.role ?? "structure", dashed: style?.dashed ?? false, visible: style?.visible ?? true };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** One batched path of edges. */
|
|
94
|
+
interface EdgeBatch {
|
|
95
|
+
key: string;
|
|
96
|
+
axis: LatticeAxis;
|
|
97
|
+
dim: boolean;
|
|
98
|
+
d: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A detected lattice inside an `ImageStage`.
|
|
103
|
+
*
|
|
104
|
+
* - **Nodes** are a `PointSet`: `plus` markers by default, picked through the point index at
|
|
105
|
+
* point priority, with `onHoverChange`, `onItemPress`, selection (with ring) and dimming.
|
|
106
|
+
* - **Edges** join `(i, j)` to `(i + 1, j)` and to `(i, j + 1)` where both nodes exist
|
|
107
|
+
* (`latticeEdges`), in one path per axis and dimming, below the nodes. They are `structure`
|
|
108
|
+
* role by default, 1 screen px with a halo; `edges` restyles each axis.
|
|
109
|
+
* - **Labels** are the nodes' own `label`s, or with `indexLabels` their `i,j`, only where nodes
|
|
110
|
+
* are at least 24 screen px apart.
|
|
111
|
+
*
|
|
112
|
+
* The edges' SVG carries `data-edges`; the nodes' is the `PointSet`'s.
|
|
113
|
+
*/
|
|
114
|
+
export function GridLayer({
|
|
115
|
+
nodes,
|
|
116
|
+
edges,
|
|
117
|
+
kind = "plus",
|
|
118
|
+
markers,
|
|
119
|
+
indexLabels = false,
|
|
120
|
+
labels = true,
|
|
121
|
+
hoveredId,
|
|
122
|
+
onHoverChange,
|
|
123
|
+
selectedIds,
|
|
124
|
+
dimmed,
|
|
125
|
+
onItemPress,
|
|
126
|
+
halo = true,
|
|
127
|
+
selectionStroke,
|
|
128
|
+
layerId,
|
|
129
|
+
priority = STAGE_HIT_PRIORITY.point,
|
|
130
|
+
label = "Grid",
|
|
131
|
+
}: GridLayerProps) {
|
|
132
|
+
const stage = useStage();
|
|
133
|
+
const px = useScreenPx();
|
|
134
|
+
|
|
135
|
+
const styles = { i: edgeStyle(edges?.i), j: edgeStyle(edges?.j) };
|
|
136
|
+
const isDimmed = useMemo(() => {
|
|
137
|
+
if (typeof dimmed === "function") return dimmed;
|
|
138
|
+
const set = new Set(dimmed ?? []);
|
|
139
|
+
return (id: PointId) => set.has(id);
|
|
140
|
+
}, [dimmed]);
|
|
141
|
+
|
|
142
|
+
const batches = useMemo(() => {
|
|
143
|
+
const map = new Map<string, EdgeBatch>();
|
|
144
|
+
for (const edge of latticeEdges(nodes)) {
|
|
145
|
+
const a = nodes[edge.a]!;
|
|
146
|
+
const b = nodes[edge.b]!;
|
|
147
|
+
if (!Number.isFinite(a.x + a.y + b.x + b.y)) continue;
|
|
148
|
+
const dim = isDimmed(a.id) && isDimmed(b.id);
|
|
149
|
+
const k = `${edge.axis}|${dim ? 1 : 0}`;
|
|
150
|
+
let batch = map.get(k);
|
|
151
|
+
if (!batch) {
|
|
152
|
+
batch = { key: k, axis: edge.axis, dim, d: "" };
|
|
153
|
+
map.set(k, batch);
|
|
154
|
+
}
|
|
155
|
+
batch.d += `M${a.x} ${a.y}L${b.x} ${b.y}`;
|
|
156
|
+
}
|
|
157
|
+
return [...map.values()];
|
|
158
|
+
}, [nodes, isDimmed]);
|
|
159
|
+
|
|
160
|
+
// Without `indexLabels` the nodes go through as they are, so the PointSet's index is not rebuilt.
|
|
161
|
+
const items = useMemo<readonly PointSetItem[]>(
|
|
162
|
+
() => (indexLabels ? nodes.map((node) => (node.label ? node : { ...node, label: `${node.i},${node.j}` })) : nodes),
|
|
163
|
+
[nodes, indexLabels],
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<>
|
|
168
|
+
<svg
|
|
169
|
+
viewBox={imageViewBox(stage.image)}
|
|
170
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
171
|
+
data-edges=""
|
|
172
|
+
aria-hidden
|
|
173
|
+
>
|
|
174
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
175
|
+
{batches
|
|
176
|
+
.filter((batch) => styles[batch.axis].visible)
|
|
177
|
+
.map((batch) => {
|
|
178
|
+
const style = styles[batch.axis];
|
|
179
|
+
const thin = style.role === "model" || style.role === "structure";
|
|
180
|
+
const width = px(OVERLAY_STATE_WIDTH.default - (thin ? 0.5 : 0));
|
|
181
|
+
const dash = style.dashed ? `${px(4)} ${px(3)}` : undefined;
|
|
182
|
+
return (
|
|
183
|
+
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
184
|
+
{halo && <path d={batch.d} stroke={HALO} strokeWidth={width + px(2)} opacity={0.6} />}
|
|
185
|
+
<path data-edge-batch={batch.key} d={batch.d} stroke={overlayRole(style.role)} strokeWidth={width} strokeDasharray={dash} />
|
|
186
|
+
</g>
|
|
187
|
+
);
|
|
188
|
+
})}
|
|
189
|
+
</g>
|
|
190
|
+
</svg>
|
|
191
|
+
<PointSet
|
|
192
|
+
items={items}
|
|
193
|
+
kind={kind}
|
|
194
|
+
markers={markers}
|
|
195
|
+
hoveredId={hoveredId}
|
|
196
|
+
onHoverChange={onHoverChange}
|
|
197
|
+
selectedIds={selectedIds}
|
|
198
|
+
dimmed={dimmed}
|
|
199
|
+
onItemPress={onItemPress}
|
|
200
|
+
labels={labels}
|
|
201
|
+
halo={halo}
|
|
202
|
+
selectionStroke={selectionStroke}
|
|
203
|
+
layerId={layerId}
|
|
204
|
+
priority={priority}
|
|
205
|
+
label={label}
|
|
206
|
+
/>
|
|
207
|
+
</>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A scalar field or a pre-coloured buffer, drawn as one image over (or under) the overlays.
|
|
3
|
+
*
|
|
4
|
+
* A heatmap is a raster, not vector geometry (ADR-0004 rule 5): it is rendered once into an
|
|
5
|
+
* offscreen canvas, handed to the browser as an object URL, and shown by an `<img>` the stage
|
|
6
|
+
* positions and scales like the photograph. Nothing is drawn per pixel by React, so a 4-megapixel
|
|
7
|
+
* map costs one element. It takes no pointer events; to read the numbers under the pointer, use
|
|
8
|
+
* `valueAt` on the plane.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { useEffect, useRef, useState } from "react";
|
|
12
|
+
|
|
13
|
+
import { cn } from "@vitavision/ui";
|
|
14
|
+
import type { ValuePlane } from "../api/mapValues";
|
|
15
|
+
import { rasterizePlane, type Colormap, type ValueRange } from "./heatmapRaster";
|
|
16
|
+
import { useStage } from "./stage/ImageStage";
|
|
17
|
+
import { PIXEL_CENTRE, type Rect } from "./stage/view";
|
|
18
|
+
|
|
19
|
+
/** What every `HeatmapLayer` takes. */
|
|
20
|
+
export interface HeatmapLayerBaseProps {
|
|
21
|
+
/** How opaque the heatmap is, 0 to 1. Defaults to `1`. */
|
|
22
|
+
opacity?: number | undefined;
|
|
23
|
+
/** Draw it. Defaults to `true`. While `false` nothing is rasterised. */
|
|
24
|
+
visible?: boolean | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Where the raster is placed, in image coordinates: `x` and `y` the top-left edge of its
|
|
27
|
+
* first pixel's footprint (`-0.5, -0.5` is the image's own top-left corner), `width` and `height`
|
|
28
|
+
* the extent it is stretched over. Defaults to the whole image, so a raster of any
|
|
29
|
+
* resolution covers it 1:1 in position.
|
|
30
|
+
*/
|
|
31
|
+
rect?: Rect | undefined;
|
|
32
|
+
/** The zoom (CSS pixels per heatmap pixel) from which heatmap pixels are drawn as blocks. Defaults to 4. */
|
|
33
|
+
pixelatedAbove?: number | undefined;
|
|
34
|
+
/** A description for assistive technology. Defaults to `""`: decorative, as the legend carries the meaning. */
|
|
35
|
+
alt?: string | undefined;
|
|
36
|
+
/** Called once the raster is on screen (and again for each new one). */
|
|
37
|
+
onReady?: (() => void) | undefined;
|
|
38
|
+
/** Called when the raster could not be made: a canvas that could not be allocated, or an `rgba` buffer too short for its size. */
|
|
39
|
+
onError?: (() => void) | undefined;
|
|
40
|
+
/** Merged onto the image with `cn`. */
|
|
41
|
+
className?: string | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A heatmap from pixels that are already coloured. */
|
|
45
|
+
export interface HeatmapRgbaProps extends HeatmapLayerBaseProps {
|
|
46
|
+
/** Row-major RGBA, `width · height · 4` bytes. A `Uint8Array` is read in place, not copied. */
|
|
47
|
+
rgba: Uint8ClampedArray | Uint8Array;
|
|
48
|
+
/** The raster's width in pixels. */
|
|
49
|
+
width: number;
|
|
50
|
+
/** The raster's height in pixels. */
|
|
51
|
+
height: number;
|
|
52
|
+
/** Not used with `rgba`. */
|
|
53
|
+
plane?: undefined;
|
|
54
|
+
/** Not used with `rgba`. */
|
|
55
|
+
colormap?: undefined;
|
|
56
|
+
/** Not used with `rgba`. */
|
|
57
|
+
range?: undefined;
|
|
58
|
+
/** Not used with `rgba`. */
|
|
59
|
+
channel?: undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A heatmap from a scalar field and a colour map. */
|
|
63
|
+
export interface HeatmapPlaneProps extends HeatmapLayerBaseProps {
|
|
64
|
+
/** The field, as decoded by `decodePlane`. */
|
|
65
|
+
plane: ValuePlane;
|
|
66
|
+
/**
|
|
67
|
+
* The colour map. `NaN` pixels stay transparent. Pass a stable function: a new one
|
|
68
|
+
* re-rasterises the heatmap. `@vitavision/charts`' `(t) => colormapRgb("viridis", t)` is one.
|
|
69
|
+
*/
|
|
70
|
+
colormap: Colormap;
|
|
71
|
+
/** What the map spans. Defaults to the extent of the channel's finite values. */
|
|
72
|
+
range?: ValueRange | undefined;
|
|
73
|
+
/** Which channel of the plane to draw. Defaults to the first. */
|
|
74
|
+
channel?: number | undefined;
|
|
75
|
+
/** Not used with `plane`. */
|
|
76
|
+
rgba?: undefined;
|
|
77
|
+
/** Not used with `plane`. */
|
|
78
|
+
width?: undefined;
|
|
79
|
+
/** Not used with `plane`. */
|
|
80
|
+
height?: undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Props of `HeatmapLayer`: pre-coloured `rgba` pixels, or a `plane` with a `colormap`. */
|
|
84
|
+
export type HeatmapLayerProps = HeatmapRgbaProps | HeatmapPlaneProps;
|
|
85
|
+
|
|
86
|
+
/** A raster on screen and the object URL it came from. */
|
|
87
|
+
interface Raster {
|
|
88
|
+
url: string;
|
|
89
|
+
width: number;
|
|
90
|
+
height: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A heatmap inside an `ImageStage`: a scalar field coloured through a colour map, or an RGBA
|
|
95
|
+
* buffer, placed over the image (default) or any rectangle of it.
|
|
96
|
+
*
|
|
97
|
+
* - **Rasterised in an effect**, once per change of its data, into an offscreen canvas and an
|
|
98
|
+
* object URL that is revoked when replaced and on unmount. Server rendering draws nothing.
|
|
99
|
+
* - **Pixelated** from 4 CSS pixels per heatmap pixel, like `ImageLayer`, so the cells of a
|
|
100
|
+
* coarse map are not smoothed into a blur.
|
|
101
|
+
* - **Stacking:** it is drawn where it is placed among the stage's children. Before the
|
|
102
|
+
* overlays it sits under them; the image layer goes before it.
|
|
103
|
+
* - **Input:** none. It takes no pointer events, so presses reach the layers above and the stage.
|
|
104
|
+
*
|
|
105
|
+
* The image carries `data-heatmap`, `data-pixelated` while pixelated, and `data-ready`.
|
|
106
|
+
*/
|
|
107
|
+
export function HeatmapLayer(props: HeatmapLayerProps) {
|
|
108
|
+
const { opacity = 1, visible = true, rect, pixelatedAbove = 4, alt = "", onReady, onError, className } = props;
|
|
109
|
+
const stage = useStage();
|
|
110
|
+
const [raster, setRaster] = useState<Raster | null>(null);
|
|
111
|
+
const [ready, setReady] = useState(false);
|
|
112
|
+
const urlRef = useRef<string | null>(null);
|
|
113
|
+
const callbacksRef = useRef({ onReady, onError });
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
callbacksRef.current = { onReady, onError };
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// The inputs that change what is drawn, spelled out so the effect re-runs on exactly those.
|
|
119
|
+
const { rgba, width, height, plane, colormap, channel } = props;
|
|
120
|
+
const rangeLow = props.range?.low;
|
|
121
|
+
const rangeHigh = props.range?.high;
|
|
122
|
+
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
if (!visible) return;
|
|
125
|
+
let cancelled = false;
|
|
126
|
+
const range = rangeLow === undefined || rangeHigh === undefined ? undefined : { low: rangeLow, high: rangeHigh };
|
|
127
|
+
const source = rgba ? wrapRgba(rgba, width, height) : plane && colormap ? colourPlane(plane, colormap, range, channel) : null;
|
|
128
|
+
const canvas = document.createElement("canvas");
|
|
129
|
+
const context = source ? ((canvas.width = source.width), (canvas.height = source.height), canvas.getContext("2d")) : null;
|
|
130
|
+
if (!source || !context) {
|
|
131
|
+
callbacksRef.current.onError?.();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const { width: w, height: h } = source;
|
|
135
|
+
context.putImageData(new ImageData(source.pixels, w, h), 0, 0);
|
|
136
|
+
canvas.toBlob((blob) => {
|
|
137
|
+
if (cancelled) return;
|
|
138
|
+
if (!blob) {
|
|
139
|
+
callbacksRef.current.onError?.();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const previous = urlRef.current;
|
|
143
|
+
const url = URL.createObjectURL(blob);
|
|
144
|
+
urlRef.current = url;
|
|
145
|
+
setReady(false);
|
|
146
|
+
setRaster({ url, width: w, height: h });
|
|
147
|
+
// The old image is replaced in this commit; the browser has already decoded it.
|
|
148
|
+
if (previous) URL.revokeObjectURL(previous);
|
|
149
|
+
});
|
|
150
|
+
return () => {
|
|
151
|
+
cancelled = true;
|
|
152
|
+
};
|
|
153
|
+
}, [visible, rgba, width, height, plane, colormap, rangeLow, rangeHigh, channel]);
|
|
154
|
+
|
|
155
|
+
useEffect(
|
|
156
|
+
() => () => {
|
|
157
|
+
if (urlRef.current) URL.revokeObjectURL(urlRef.current);
|
|
158
|
+
urlRef.current = null;
|
|
159
|
+
},
|
|
160
|
+
[],
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
if (!visible || raster === null) return null;
|
|
164
|
+
const box = rect ?? { x: -PIXEL_CENTRE, y: -PIXEL_CENTRE, width: stage.image.width, height: stage.image.height };
|
|
165
|
+
const pixelated = (stage.view.scale * box.width) / raster.width >= pixelatedAbove;
|
|
166
|
+
return (
|
|
167
|
+
<img
|
|
168
|
+
src={raster.url}
|
|
169
|
+
alt={alt}
|
|
170
|
+
draggable={false}
|
|
171
|
+
data-heatmap=""
|
|
172
|
+
data-ready={ready ? "" : undefined}
|
|
173
|
+
data-pixelated={pixelated ? "" : undefined}
|
|
174
|
+
onLoad={() => {
|
|
175
|
+
setReady(true);
|
|
176
|
+
callbacksRef.current.onReady?.();
|
|
177
|
+
}}
|
|
178
|
+
className={cn("pointer-events-none absolute max-w-none select-none", className)}
|
|
179
|
+
style={{
|
|
180
|
+
left: box.x + PIXEL_CENTRE,
|
|
181
|
+
top: box.y + PIXEL_CENTRE,
|
|
182
|
+
width: box.width,
|
|
183
|
+
height: box.height,
|
|
184
|
+
opacity,
|
|
185
|
+
pointerEvents: "none",
|
|
186
|
+
imageRendering: pixelated ? "pixelated" : undefined,
|
|
187
|
+
}}
|
|
188
|
+
/>
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Pixels to draw, and their size. */
|
|
193
|
+
interface Pixels {
|
|
194
|
+
pixels: Uint8ClampedArray<ArrayBuffer>;
|
|
195
|
+
width: number;
|
|
196
|
+
height: number;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The first `width · height · 4` bytes of an RGBA buffer, read in place; `null` if it is too short or empty. */
|
|
200
|
+
function wrapRgba(rgba: Uint8ClampedArray | Uint8Array, width: number, height: number): Pixels | null {
|
|
201
|
+
const length = width * height * 4;
|
|
202
|
+
if (!(length > 0) || rgba.length < length) return null;
|
|
203
|
+
return { pixels: new Uint8ClampedArray(rgba.buffer as ArrayBuffer, rgba.byteOffset, length), width, height };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** A plane through a colour map; `null` for an empty plane. */
|
|
207
|
+
function colourPlane(plane: ValuePlane, colormap: Colormap, range: ValueRange | undefined, channel: number | undefined): Pixels | null {
|
|
208
|
+
if (!(plane.width > 0 && plane.height > 0)) return null;
|
|
209
|
+
const pixels = rasterizePlane(plane, colormap, range, channel) as Uint8ClampedArray<ArrayBuffer>;
|
|
210
|
+
return { pixels, width: plane.width, height: plane.height };
|
|
211
|
+
}
|