@vitavision/stage2d 0.13.2 → 0.15.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 +2 -1
- package/dist/index.d.ts +52 -8
- package/dist/index.js +134 -74
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/AreaSet.tsx +13 -4
- package/src/components/EllipseSet.tsx +33 -51
- package/src/components/MeasureOverlay.tsx +1 -3
- package/src/components/PointSet.tsx +11 -3
- package/src/components/stage/ImageLayer.tsx +63 -4
- package/src/components/stage/hitTest.ts +6 -2
- package/src/components/stage/useStageHitTest.ts +10 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/stage2d",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "The vitavision 2D image stage: view transform, zoom/pan, layered overlays, measurement.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"react-dom": "^19.2.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@vitavision/ui": "^0.14.
|
|
49
|
+
"@vitavision/ui": "^0.14.1",
|
|
50
50
|
"lucide-react": "^1.48.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
@@ -44,7 +44,7 @@ export interface AreaSetProps {
|
|
|
44
44
|
role?: OverlayRole | undefined;
|
|
45
45
|
/** The hovered id, when the app controls hover (a list beside the stage hovers regions too). */
|
|
46
46
|
hoveredId?: AreaId | null | undefined;
|
|
47
|
-
/** Called when the region under the pointer changes. `null` when it leaves every region. */
|
|
47
|
+
/** Called when the region under the pointer changes. `null` when it leaves every region. Not called while `pickable` is off; turning it off ends a hover with `null`. */
|
|
48
48
|
onHoverChange?: ((id: AreaId | null) => void) | undefined;
|
|
49
49
|
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
50
50
|
selectedIds?: Iterable<AreaId> | undefined;
|
|
@@ -97,6 +97,12 @@ export interface AreaSetProps {
|
|
|
97
97
|
halo?: boolean | undefined;
|
|
98
98
|
/** CSS colour of selected regions. Defaults to the overlay `selection` role. */
|
|
99
99
|
selectionStroke?: string | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
|
|
102
|
+
* decorative layer (a shaded backdrop under the regions that matter), so it neither hovers,
|
|
103
|
+
* nor claims presses, nor answers `useStageHitTest`.
|
|
104
|
+
*/
|
|
105
|
+
pickable?: boolean | undefined;
|
|
100
106
|
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
101
107
|
layerId?: string | undefined;
|
|
102
108
|
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.area`. */
|
|
@@ -127,6 +133,7 @@ const TICK_PX = 8;
|
|
|
127
133
|
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
128
134
|
* an outline picks that region, else the smallest region containing the point. `onHoverChange`
|
|
129
135
|
* and `onItemPress` here, and `useStageHitTest` for an app that arbitrates between layers.
|
|
136
|
+
* `pickable={false}` turns it off.
|
|
130
137
|
*
|
|
131
138
|
* The SVG carries `data-hovered` (the hovered id).
|
|
132
139
|
*/
|
|
@@ -138,6 +145,7 @@ export function AreaSet({
|
|
|
138
145
|
selectedIds,
|
|
139
146
|
dimmed,
|
|
140
147
|
onItemPress,
|
|
148
|
+
pickable = true,
|
|
141
149
|
labels = true,
|
|
142
150
|
firstVertexTick = false,
|
|
143
151
|
stroke,
|
|
@@ -155,7 +163,7 @@ export function AreaSet({
|
|
|
155
163
|
const px = useScreenPx();
|
|
156
164
|
const unit = px(1);
|
|
157
165
|
|
|
158
|
-
const index = useMemo(() => buildAreaIndex(items), [items]);
|
|
166
|
+
const index = useMemo(() => (pickable ? buildAreaIndex(items) : null), [items, pickable]);
|
|
159
167
|
const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
|
|
160
168
|
const isDimmed = useMemo(() => {
|
|
161
169
|
if (typeof dimmed === "function") return dimmed;
|
|
@@ -170,15 +178,16 @@ export function AreaSet({
|
|
|
170
178
|
useStageHitLayer({
|
|
171
179
|
layerId,
|
|
172
180
|
priority,
|
|
181
|
+
enabled: pickable,
|
|
173
182
|
pick: (point, radius) => {
|
|
174
|
-
const hit = nearestArea(index, point, radius);
|
|
183
|
+
const hit = index && nearestArea(index, point, radius);
|
|
175
184
|
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
176
185
|
},
|
|
177
186
|
onHover: (id) => {
|
|
178
187
|
setOwnHover(id);
|
|
179
188
|
onHoverChange?.(id);
|
|
180
189
|
},
|
|
181
|
-
onPress: onItemPress
|
|
190
|
+
onPress: onItemPress,
|
|
182
191
|
});
|
|
183
192
|
|
|
184
193
|
// Regions grouped by appearance. Rebuilt when the items or the selection change, never on
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { useMemo, useState } from "react";
|
|
13
13
|
|
|
14
|
-
import { buildEllipseIndex, ellipsePath, nearestEllipse, type Ellipse, type EllipseId
|
|
14
|
+
import { buildEllipseIndex, ellipsePath, nearestEllipse, type Ellipse, type EllipseId } from "./ellipseIndex";
|
|
15
15
|
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
16
16
|
import { useStage } from "./stage/ImageStage";
|
|
17
17
|
import type { StagePointerEvent } from "./stage/hitContext";
|
|
@@ -24,6 +24,12 @@ import { imageViewBox } from "./stage/view";
|
|
|
24
24
|
export interface EllipseSetItem extends Ellipse {
|
|
25
25
|
/** The overlay role it is painted in. Defaults to the layer's `role`. */
|
|
26
26
|
role?: OverlayRole | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* CSS colour of its outline (`#hex`, `var(--x)`, `color-mix(…)`; used as given), so a layer can
|
|
29
|
+
* encode a scalar per ellipse. Overrides the role; selection still paints in `selectionStroke`.
|
|
30
|
+
* Ellipses sharing a colour share a path, so quantise a continuous scale to a few dozen steps.
|
|
31
|
+
*/
|
|
32
|
+
color?: string | undefined;
|
|
27
33
|
/** Draw it dashed (4 3 screen px), so two kinds differ by more than colour. */
|
|
28
34
|
dashed?: boolean | undefined;
|
|
29
35
|
/** Opacity in [0, 1], rounded to a tenth so ellipses still share paths. Defaults to 1. */
|
|
@@ -38,7 +44,7 @@ export interface EllipseSetProps {
|
|
|
38
44
|
role?: OverlayRole | undefined;
|
|
39
45
|
/** The hovered id, when the app controls hover: its ellipses are drawn at 2 px. */
|
|
40
46
|
hoveredId?: EllipseId | null | undefined;
|
|
41
|
-
/** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called
|
|
47
|
+
/** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called while `pickable` is off; turning it off ends a hover with `null`. */
|
|
42
48
|
onHoverChange?: ((id: EllipseId | null) => void) | undefined;
|
|
43
49
|
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
44
50
|
selectedIds?: Iterable<EllipseId> | undefined;
|
|
@@ -90,40 +96,12 @@ function batchPath(items: readonly EllipseSetItem[], indices: readonly number[],
|
|
|
90
96
|
return d;
|
|
91
97
|
}
|
|
92
98
|
|
|
93
|
-
/**
|
|
94
|
-
* The layer's entry in the stage's hit registry: a component, not a hook call in
|
|
95
|
-
* `EllipseSet`, so `pickable={false}` really unregisters it. Renders nothing.
|
|
96
|
-
*/
|
|
97
|
-
function EllipseHits({
|
|
98
|
-
index,
|
|
99
|
-
layerId,
|
|
100
|
-
priority,
|
|
101
|
-
onHover,
|
|
102
|
-
onPress,
|
|
103
|
-
}: {
|
|
104
|
-
index: EllipseIndex;
|
|
105
|
-
layerId: string | undefined;
|
|
106
|
-
priority: number;
|
|
107
|
-
onHover: (id: EllipseId | null) => void;
|
|
108
|
-
onPress: ((id: EllipseId, event: StagePointerEvent) => void) | undefined;
|
|
109
|
-
}) {
|
|
110
|
-
useStageHitLayer({
|
|
111
|
-
layerId,
|
|
112
|
-
priority,
|
|
113
|
-
pick: (point, radius) => {
|
|
114
|
-
const hit = nearestEllipse(index, point, radius);
|
|
115
|
-
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
116
|
-
},
|
|
117
|
-
onHover,
|
|
118
|
-
onPress: onPress ? (id, event) => onPress(id, event) : undefined,
|
|
119
|
-
});
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
99
|
/** The ellipses of one batched path. */
|
|
124
100
|
interface Batch {
|
|
125
101
|
key: string;
|
|
126
102
|
role: OverlayRole;
|
|
103
|
+
/** The custom colour, when the items of this batch have one. */
|
|
104
|
+
color: string | undefined;
|
|
127
105
|
dashed: boolean;
|
|
128
106
|
opacity: number;
|
|
129
107
|
dim: boolean;
|
|
@@ -138,7 +116,7 @@ interface Batch {
|
|
|
138
116
|
* the stroke is a screen constant.
|
|
139
117
|
* - **States**: 1.5 screen px (1 for `model` and `structure`), hover
|
|
140
118
|
* 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every stroke has a halo.
|
|
141
|
-
* - **Batching** is by (role, dash, opacity, dimming): a thousand rings are a dozen elements.
|
|
119
|
+
* - **Batching** is by (role, colour, dash, opacity, dimming): a thousand rings are a dozen elements.
|
|
142
120
|
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
143
121
|
* an outline picks that ellipse, else the smallest ellipse containing the point.
|
|
144
122
|
* `onHoverChange` and `onItemPress` here, and `useStageHitTest` for an app that arbitrates
|
|
@@ -175,24 +153,40 @@ export function EllipseSet({
|
|
|
175
153
|
|
|
176
154
|
const index = useMemo(() => (pickable ? buildEllipseIndex(items) : null), [items, pickable]);
|
|
177
155
|
const [ownHover, setOwnHover] = useState<EllipseId | null>(null);
|
|
178
|
-
const hoverId = hoveredId !== undefined ? hoveredId :
|
|
156
|
+
const hoverId = hoveredId !== undefined ? hoveredId : ownHover;
|
|
157
|
+
|
|
158
|
+
useStageHitLayer({
|
|
159
|
+
layerId,
|
|
160
|
+
priority,
|
|
161
|
+
enabled: pickable,
|
|
162
|
+
pick: (point, radius) => {
|
|
163
|
+
const hit = index && nearestEllipse(index, point, radius);
|
|
164
|
+
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
165
|
+
},
|
|
166
|
+
onHover: (id) => {
|
|
167
|
+
setOwnHover(id);
|
|
168
|
+
onHoverChange?.(id);
|
|
169
|
+
},
|
|
170
|
+
onPress: onItemPress,
|
|
171
|
+
});
|
|
179
172
|
|
|
180
173
|
const batches = useMemo(() => {
|
|
181
174
|
const normal = new Map<string, Batch>();
|
|
182
|
-
const picked: Batch = { key: "selected", role: "selection", dashed: false, opacity: 1, dim: false, indices: [] };
|
|
175
|
+
const picked: Batch = { key: "selected", role: "selection", color: undefined, dashed: false, opacity: 1, dim: false, indices: [] };
|
|
183
176
|
items.forEach((item, i) => {
|
|
184
177
|
if (selectedSet.has(item.id)) {
|
|
185
178
|
picked.indices.push(i);
|
|
186
179
|
return;
|
|
187
180
|
}
|
|
188
181
|
const itemRole = item.role ?? role;
|
|
182
|
+
const color = item.color || undefined;
|
|
189
183
|
const dashed = item.dashed ?? false;
|
|
190
184
|
const opacity = Math.round(Math.min(1, Math.max(0, item.opacity ?? 1)) * 10) / 10;
|
|
191
185
|
const dim = isDimmed(item.id);
|
|
192
|
-
const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}`;
|
|
186
|
+
const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}${color ? `|${color}` : ""}`;
|
|
193
187
|
let batch = normal.get(key);
|
|
194
188
|
if (!batch) {
|
|
195
|
-
batch = { key, role: itemRole, dashed, opacity, dim, indices: [] };
|
|
189
|
+
batch = { key, role: itemRole, color, dashed, opacity, dim, indices: [] };
|
|
196
190
|
normal.set(key, batch);
|
|
197
191
|
}
|
|
198
192
|
batch.indices.push(i);
|
|
@@ -230,18 +224,6 @@ export function EllipseSet({
|
|
|
230
224
|
data-ellipses={items.length}
|
|
231
225
|
data-hovered={hoverId ?? undefined}
|
|
232
226
|
>
|
|
233
|
-
{index && (
|
|
234
|
-
<EllipseHits
|
|
235
|
-
index={index}
|
|
236
|
-
layerId={layerId}
|
|
237
|
-
priority={priority}
|
|
238
|
-
onHover={(id) => {
|
|
239
|
-
setOwnHover(id);
|
|
240
|
-
onHoverChange?.(id);
|
|
241
|
-
}}
|
|
242
|
-
onPress={onItemPress}
|
|
243
|
-
/>
|
|
244
|
-
)}
|
|
245
227
|
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
246
228
|
{normalPaths.map(({ batch, d }) => (
|
|
247
229
|
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
@@ -257,7 +239,7 @@ export function EllipseSet({
|
|
|
257
239
|
<path
|
|
258
240
|
data-batch={batch.key}
|
|
259
241
|
d={d}
|
|
260
|
-
stroke={overlayRole(batch.role)}
|
|
242
|
+
stroke={batch.color ?? overlayRole(batch.role)}
|
|
261
243
|
strokeWidth={widthOf(batch.role, "default")}
|
|
262
244
|
strokeDasharray={batch.dashed ? dash : undefined}
|
|
263
245
|
opacity={batch.opacity}
|
|
@@ -280,7 +262,7 @@ export function EllipseSet({
|
|
|
280
262
|
<path
|
|
281
263
|
data-hovered-ellipse=""
|
|
282
264
|
d={outlineOf(item, floor)}
|
|
283
|
-
stroke={hoverSelected ? selectionStroke : overlayRole(itemRole)}
|
|
265
|
+
stroke={hoverSelected ? selectionStroke : item.color || overlayRole(itemRole)}
|
|
284
266
|
strokeWidth={widthOf(itemRole, state)}
|
|
285
267
|
/>
|
|
286
268
|
</g>
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* still takes no pointer events.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import {
|
|
22
|
+
import { useState } from "react";
|
|
23
23
|
|
|
24
24
|
import { nearestMeasurePrimitive } from "./measureHit";
|
|
25
25
|
import type { StagePointerEvent } from "./stage/hitContext";
|
|
@@ -462,8 +462,6 @@ function MeasureHitLayer({
|
|
|
462
462
|
},
|
|
463
463
|
onPress: onItemPress ? (id, event) => onItemPress(String(id), event) : undefined,
|
|
464
464
|
});
|
|
465
|
-
// A hover tracked here ends with the layer, so it is not drawn when the overlay is made pickable again.
|
|
466
|
-
useEffect(() => () => setOwnHover(null), [setOwnHover]);
|
|
467
465
|
return null;
|
|
468
466
|
}
|
|
469
467
|
|
|
@@ -53,7 +53,7 @@ export interface PointSetProps {
|
|
|
53
53
|
markers?: Readonly<Record<string, MarkerShape>> | undefined;
|
|
54
54
|
/** The hovered id, when the app controls hover (a list beside the stage hovers points too). */
|
|
55
55
|
hoveredId?: PointId | null | undefined;
|
|
56
|
-
/** Called when the point under the pointer changes. `null` when it leaves every point. */
|
|
56
|
+
/** Called when the point under the pointer changes. `null` when it leaves every point. Not called while `pickable` is off; turning it off ends a hover with `null`. */
|
|
57
57
|
onHoverChange?: ((id: PointId | null) => void) | undefined;
|
|
58
58
|
/** The selected ids, drawn at 2.5 px in the selection colour with a ring. */
|
|
59
59
|
selectedIds?: Iterable<PointId> | undefined;
|
|
@@ -77,6 +77,12 @@ export interface PointSetProps {
|
|
|
77
77
|
halo?: boolean | undefined;
|
|
78
78
|
/** CSS colour of selected points and rings. Defaults to the overlay `selection` role. */
|
|
79
79
|
selectionStroke?: string | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
|
|
82
|
+
* decorative layer (a backdrop of markers under the points that matter), so it neither
|
|
83
|
+
* hovers, nor claims presses, nor answers `useStageHitTest`.
|
|
84
|
+
*/
|
|
85
|
+
pickable?: boolean | undefined;
|
|
80
86
|
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
81
87
|
layerId?: string | undefined;
|
|
82
88
|
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.point`. */
|
|
@@ -116,7 +122,7 @@ interface Batch {
|
|
|
116
122
|
* selection colour with a ring, dimmed at 35 % opacity. Every mark has a halo.
|
|
117
123
|
* - **Labels** only where points are at least 24 screen px apart, at most 200 at a time.
|
|
118
124
|
* - **Picking** goes through the stage's hit registry: `onHoverChange` and `onItemPress` here,
|
|
119
|
-
* and `useStageHitTest` for an app that arbitrates between layers.
|
|
125
|
+
* and `useStageHitTest` for an app that arbitrates between layers. `pickable={false}` turns it off.
|
|
120
126
|
*
|
|
121
127
|
* Outlines (`plus`, `cross`, …) are regenerated when the zoom changes, since their size is in
|
|
122
128
|
* screen pixels; past 3,000 points only those near the viewport are generated. `dot` is a
|
|
@@ -133,6 +139,7 @@ export function PointSet({
|
|
|
133
139
|
selectedIds,
|
|
134
140
|
dimmed,
|
|
135
141
|
onItemPress,
|
|
142
|
+
pickable = true,
|
|
136
143
|
labels = true,
|
|
137
144
|
halo = true,
|
|
138
145
|
selectionStroke = overlayRole("selection"),
|
|
@@ -160,6 +167,7 @@ export function PointSet({
|
|
|
160
167
|
useStageHitLayer({
|
|
161
168
|
layerId,
|
|
162
169
|
priority,
|
|
170
|
+
enabled: pickable,
|
|
163
171
|
pick: (point, radius) => {
|
|
164
172
|
const hit = nearestPoint(index, point.x, point.y, radius);
|
|
165
173
|
return hit ? { id: hit.id, dist: hit.dist } : null;
|
|
@@ -168,7 +176,7 @@ export function PointSet({
|
|
|
168
176
|
setOwnHover(id);
|
|
169
177
|
onHoverChange?.(id);
|
|
170
178
|
},
|
|
171
|
-
onPress: onItemPress
|
|
179
|
+
onPress: onItemPress,
|
|
172
180
|
});
|
|
173
181
|
|
|
174
182
|
// Items grouped by appearance. Rebuilt when the items or the selection change, never on
|
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
* - **Pixelated past a zoom.** Above about 4× the browser's smoothing paints detail the
|
|
16
16
|
* sensor never recorded; on a metrology bench the sensor's own samples are what is worth
|
|
17
17
|
* looking at.
|
|
18
|
+
* - **A display tone curve.** A dark or low-contrast frame is read with a gain and a gamma
|
|
19
|
+
* applied on screen only (`gain`, `gamma`): the pixels the app measures are untouched.
|
|
18
20
|
*/
|
|
19
21
|
|
|
20
|
-
import { useEffect, useRef, useState } from "react";
|
|
22
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
21
23
|
|
|
22
24
|
import { cn } from "@vitavision/ui";
|
|
23
25
|
import { useStage } from "./ImageStage";
|
|
@@ -47,6 +49,20 @@ export interface ImageLayerBaseProps {
|
|
|
47
49
|
onLoad?: (() => void) | undefined;
|
|
48
50
|
/** Called when the full image fails to load. */
|
|
49
51
|
onError?: (() => void) | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Display gain: each channel is multiplied by it and clipped at white, after `gamma`.
|
|
54
|
+
* Defaults to 1. A negative value counts as 0, one that is not finite as 1.
|
|
55
|
+
*
|
|
56
|
+
* Like `gamma`, it changes what is drawn, never the image data: a way to read a dark or
|
|
57
|
+
* low-contrast frame, not a correction to measure on.
|
|
58
|
+
*/
|
|
59
|
+
gain?: number | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Display gamma, as an image editor's levels control: each channel `v` (0 to 1, as encoded in
|
|
62
|
+
* the file) is drawn as `gain × v^(1 / gamma)`. Above 1 it lifts the shadows and mid-tones,
|
|
63
|
+
* below 1 it darkens them. Defaults to 1; a value that is not positive counts as 1.
|
|
64
|
+
*/
|
|
65
|
+
gamma?: number | undefined;
|
|
50
66
|
/** Merged onto both images with `cn`. */
|
|
51
67
|
className?: string | undefined;
|
|
52
68
|
}
|
|
@@ -79,12 +95,29 @@ export type ImageLayerProps = ImageLayerBaseProps &
|
|
|
79
95
|
* With a `preview`, the full image is wanted once the stage would magnify the preview, and
|
|
80
96
|
* `onFullNeeded` says so; `src` can be supplied then rather than up front.
|
|
81
97
|
*
|
|
98
|
+
* With a `gain` or `gamma` other than 1, both images are drawn through one SVG tone-curve filter
|
|
99
|
+
* (sRGB values, per channel) and carry `data-adjusted`.
|
|
100
|
+
*
|
|
82
101
|
* Each image carries `data-tier` (`preview` or `full`); the full image carries `data-loaded`
|
|
83
102
|
* once it has loaded, the preview carries `data-wants-full` from the moment the full image is
|
|
84
103
|
* wanted until it has loaded, and both carry `data-pixelated` while pixelated.
|
|
85
104
|
*/
|
|
86
|
-
export function ImageLayer({
|
|
105
|
+
export function ImageLayer({
|
|
106
|
+
src,
|
|
107
|
+
preview,
|
|
108
|
+
alt,
|
|
109
|
+
pixelatedAbove = 4,
|
|
110
|
+
onFullNeeded,
|
|
111
|
+
onLoad,
|
|
112
|
+
onError,
|
|
113
|
+
gain,
|
|
114
|
+
gamma,
|
|
115
|
+
className,
|
|
116
|
+
}: ImageLayerProps) {
|
|
87
117
|
const stage = useStage();
|
|
118
|
+
const curve = toneCurve(gain, gamma);
|
|
119
|
+
// `useId` may contain characters that are not valid in a CSS `url(#…)` reference.
|
|
120
|
+
const filterId = `stage-image-tone-${useId().replace(/[^A-Za-z0-9_-]/g, "")}`;
|
|
88
121
|
const scale = stage.view.scale;
|
|
89
122
|
// The full image is wanted once the preview would be magnified on screen: its own pixels
|
|
90
123
|
// per image pixel are `preview.width / image.width`. Not before the viewport is measured:
|
|
@@ -112,9 +145,22 @@ export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded
|
|
|
112
145
|
const pixelated = scale >= pixelatedAbove;
|
|
113
146
|
const shared = cn("pointer-events-none absolute inset-0 h-full w-full select-none", className);
|
|
114
147
|
const rendering = pixelated ? ("pixelated" as const) : undefined;
|
|
148
|
+
const style = { imageRendering: rendering, filter: curve ? `url(#${filterId})` : undefined };
|
|
149
|
+
const adjusted = curve ? "" : undefined;
|
|
115
150
|
|
|
116
151
|
return (
|
|
117
152
|
<>
|
|
153
|
+
{curve && (
|
|
154
|
+
<svg aria-hidden="true" width="0" height="0" className="pointer-events-none absolute" focusable="false">
|
|
155
|
+
<filter id={filterId} colorInterpolationFilters="sRGB" data-tone-curve="">
|
|
156
|
+
<feComponentTransfer>
|
|
157
|
+
<feFuncR type="gamma" amplitude={curve.amplitude} exponent={curve.exponent} offset={0} />
|
|
158
|
+
<feFuncG type="gamma" amplitude={curve.amplitude} exponent={curve.exponent} offset={0} />
|
|
159
|
+
<feFuncB type="gamma" amplitude={curve.amplitude} exponent={curve.exponent} offset={0} />
|
|
160
|
+
</feComponentTransfer>
|
|
161
|
+
</filter>
|
|
162
|
+
</svg>
|
|
163
|
+
)}
|
|
118
164
|
{preview && !fullReady && (
|
|
119
165
|
<img
|
|
120
166
|
src={preview.src}
|
|
@@ -124,8 +170,9 @@ export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded
|
|
|
124
170
|
data-tier="preview"
|
|
125
171
|
data-wants-full={wanted ? "" : undefined}
|
|
126
172
|
data-pixelated={pixelated ? "" : undefined}
|
|
173
|
+
data-adjusted={adjusted}
|
|
127
174
|
className={shared}
|
|
128
|
-
style={
|
|
175
|
+
style={style}
|
|
129
176
|
/>
|
|
130
177
|
)}
|
|
131
178
|
{showFull && (
|
|
@@ -136,15 +183,27 @@ export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded
|
|
|
136
183
|
data-tier="full"
|
|
137
184
|
data-loaded={fullReady ? "" : undefined}
|
|
138
185
|
data-pixelated={pixelated ? "" : undefined}
|
|
186
|
+
data-adjusted={adjusted}
|
|
139
187
|
onLoad={() => {
|
|
140
188
|
setLoaded(src);
|
|
141
189
|
onLoad?.();
|
|
142
190
|
}}
|
|
143
191
|
onError={() => onError?.()}
|
|
144
192
|
className={cn(shared, preview && !fullReady && "opacity-0")}
|
|
145
|
-
style={
|
|
193
|
+
style={style}
|
|
146
194
|
/>
|
|
147
195
|
)}
|
|
148
196
|
</>
|
|
149
197
|
);
|
|
150
198
|
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The `feFuncX type="gamma"` parameters (`amplitude × v^exponent`) for a gain and a gamma, or
|
|
202
|
+
* `null` when the curve is the identity and no filter is needed.
|
|
203
|
+
*/
|
|
204
|
+
function toneCurve(gain: number | undefined, gamma: number | undefined): { amplitude: number; exponent: number } | null {
|
|
205
|
+
const amplitude = gain === undefined || !Number.isFinite(gain) ? 1 : Math.max(0, gain);
|
|
206
|
+
const g = gamma === undefined || !(gamma > 0) || !Number.isFinite(gamma) ? 1 : gamma;
|
|
207
|
+
if (amplitude === 1 && g === 1) return null;
|
|
208
|
+
return { amplitude, exponent: 1 / g };
|
|
209
|
+
}
|
|
@@ -115,7 +115,8 @@ export interface HitRegistry<E = unknown> {
|
|
|
115
115
|
/**
|
|
116
116
|
* Add a layer.
|
|
117
117
|
*
|
|
118
|
-
* @returns The function that removes it.
|
|
118
|
+
* @returns The function that removes it. A layer removed while it holds the hover is told
|
|
119
|
+
* the hover ended (`hover(null)`).
|
|
119
120
|
*/
|
|
120
121
|
register: (layer: StageHitLayerSpec<E>) => () => void;
|
|
121
122
|
/** Every layer's best answer at `point`, sorted best first. */
|
|
@@ -173,7 +174,10 @@ export function createHitRegistry<E = unknown>(): HitRegistry<E> {
|
|
|
173
174
|
layers.add(layer);
|
|
174
175
|
return () => {
|
|
175
176
|
layers.delete(layer);
|
|
176
|
-
if (hovered?.layer === layer)
|
|
177
|
+
if (hovered?.layer === layer) {
|
|
178
|
+
hovered = null;
|
|
179
|
+
layer.hover?.(null);
|
|
180
|
+
}
|
|
177
181
|
};
|
|
178
182
|
},
|
|
179
183
|
hitTestAll: (point, radius, options) => answers(point, radius, options).map((a) => a.hit),
|
|
@@ -60,6 +60,12 @@ export interface StageHitLayerOptions {
|
|
|
60
60
|
layerId?: string | undefined;
|
|
61
61
|
/** Rank against other layers; see `STAGE_HIT_PRIORITY`. */
|
|
62
62
|
priority: number;
|
|
63
|
+
/**
|
|
64
|
+
* Whether the layer is in the hit-test. Defaults to `true`. Off, it answers no hit-test and
|
|
65
|
+
* takes no hover or press; turning it off while it holds the hover calls `onHover(null)`.
|
|
66
|
+
* The layer id stays the same either way.
|
|
67
|
+
*/
|
|
68
|
+
enabled?: boolean | undefined;
|
|
63
69
|
/**
|
|
64
70
|
* The item under `point`, if any, within `radius` image pixels. Use the layer's index
|
|
65
71
|
* (`nearestPoint`, `nearestPolyline`), not the DOM.
|
|
@@ -102,9 +108,11 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
|
|
|
102
108
|
latestRef.current = options;
|
|
103
109
|
});
|
|
104
110
|
const priority = options.priority;
|
|
111
|
+
const enabled = options.enabled ?? true;
|
|
105
112
|
useLayoutEffect(() => {
|
|
113
|
+
if (!enabled) return;
|
|
106
114
|
// Everything the registry reads goes through `latestRef`, so one registration serves the
|
|
107
|
-
// layer's whole life; `priority` and `
|
|
115
|
+
// layer's whole life; `priority`, `id` and `enabled` are the only things that re-register it.
|
|
108
116
|
const layer: StageHitLayerSpec<StagePointerEvent> = {
|
|
109
117
|
id,
|
|
110
118
|
priority,
|
|
@@ -127,6 +135,6 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
|
|
|
127
135
|
},
|
|
128
136
|
};
|
|
129
137
|
return registry.register(layer);
|
|
130
|
-
}, [registry, id, priority]);
|
|
138
|
+
}, [registry, id, priority, enabled]);
|
|
131
139
|
return id;
|
|
132
140
|
}
|