@vitavision/stage2d 0.12.0 → 0.13.2
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 +3 -6
- package/dist/index.d.ts +669 -57
- package/dist/index.js +5455 -3621
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/mapValues.ts +1 -4
- package/src/components/AreaSet.tsx +32 -9
- package/src/components/ContourEditor.tsx +325 -40
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/DraftShape.tsx +37 -5
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +8 -3
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MaskEditor.tsx +57 -9
- package/src/components/MeasureOverlay.tsx +353 -92
- package/src/components/PointSet.tsx +23 -12
- package/src/components/PolylineSet.tsx +83 -27
- package/src/components/RectRoiEditor.tsx +189 -93
- package/src/components/ShapeEditor.tsx +25 -10
- package/src/components/areaBatching.ts +23 -3
- package/src/components/areaIndex.ts +2 -1
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +3 -7
- package/src/components/markerShapes.ts +2 -5
- package/src/components/measureGeometry.ts +46 -28
- package/src/components/measureHit.ts +383 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +14 -5
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +231 -0
- package/src/components/stage/ImageLayer.tsx +56 -24
- package/src/components/stage/ImageStage.tsx +33 -38
- package/src/components/stage/StageSurface.tsx +34 -34
- package/src/components/stage/StageToolbar.tsx +35 -37
- package/src/components/stage/compareStyle.ts +155 -0
- package/src/components/stage/gesture.ts +5 -0
- package/src/components/stage/hitTest.ts +19 -0
- package/src/components/stage/useStageHitTest.ts +17 -12
- package/src/components/stage/view.ts +3 -15
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +34 -65
|
@@ -2,27 +2,20 @@
|
|
|
2
2
|
* Many polylines over the image, selectable: contours a model may be built from, a
|
|
3
3
|
* document's segments, a batch's matches.
|
|
4
4
|
*
|
|
5
|
-
* Drawn as a handful of batched paths, one per state and colour, not one element per line.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* `nearestPolyline`, not by the DOM, which keeps this layer engine-agnostic for L6-1.
|
|
5
|
+
* Drawn as a handful of batched paths, one per state and colour, not one element per line. A
|
|
6
|
+
* per-element form fails a 2 ms hit-test budget at scale, while the batched form plus a spatial
|
|
7
|
+
* index meets it. The pointer is resolved to a line by `nearestPolyline`, not by the DOM, which
|
|
8
|
+
* keeps this layer independent of how the lines are drawn.
|
|
10
9
|
*/
|
|
11
10
|
|
|
12
|
-
import { useEffect, useMemo, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
|
|
11
|
+
import { useEffect, useImperativeHandle, useMemo, useRef, useState, type PointerEvent as ReactPointerEvent, type Ref } from "react";
|
|
13
12
|
|
|
14
13
|
import type { Point } from "./measureGeometry";
|
|
15
|
-
import {
|
|
16
|
-
buildPolylineIndex,
|
|
17
|
-
nearestPolyline,
|
|
18
|
-
polylinePath,
|
|
19
|
-
polylinesInRect,
|
|
20
|
-
type Polyline,
|
|
21
|
-
type PolylineId,
|
|
22
|
-
} from "./polylineIndex";
|
|
14
|
+
import { buildPolylineIndex, nearestPolyline, polylinePath, polylinesInRect, type Polyline, type PolylineId } from "./polylineIndex";
|
|
23
15
|
import { overlayRole } from "./overlayRole";
|
|
16
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
24
17
|
import { useStage } from "./stage/ImageStage";
|
|
25
|
-
import { useStageDrag } from "./stage/StageSurface";
|
|
18
|
+
import { useStageDrag, type StageDrag, type StagePress } from "./stage/StageSurface";
|
|
26
19
|
import { STAGE_HIT_PRIORITY } from "./stage/hitTest";
|
|
27
20
|
import { useStageHitLayer, useStageHitTest } from "./stage/useStageHitTest";
|
|
28
21
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
@@ -40,6 +33,26 @@ export interface PolylineSetItem extends Polyline {
|
|
|
40
33
|
/** How a gesture combines with the current selection. */
|
|
41
34
|
export type PolylineSelectMode = "replace" | "toggle" | "add";
|
|
42
35
|
|
|
36
|
+
/**
|
|
37
|
+
* What `PolylineSet`'s `ref` exposes: a rubber band started from a press somewhere else, such
|
|
38
|
+
* as the app's `StageSurface` or a region editor's inside. It works whether or not the layer
|
|
39
|
+
* is `interactive`, and ends in `onSelect` like a band started on the set itself.
|
|
40
|
+
*/
|
|
41
|
+
export interface PolylineSetHandle {
|
|
42
|
+
/**
|
|
43
|
+
* Start a band from a `pointerdown`: the press is claimed (the stage does not pan) and
|
|
44
|
+
* followed on `window`. `mode` defaults to `"add"` with ⌘/Ctrl held and `"replace"` without.
|
|
45
|
+
* Another event type (a touch tap reported on release) does nothing, and so does a press
|
|
46
|
+
* while the stage is in pan mode.
|
|
47
|
+
*/
|
|
48
|
+
startSweep(event: StagePointerEvent, mode?: "replace" | "add"): void;
|
|
49
|
+
/**
|
|
50
|
+
* The same band as a `StageDrag`, to return from `StageSurface`'s `onPress`. `mode` defaults
|
|
51
|
+
* from the press's ⌘/Ctrl. It claims a touch, so a finger sweeps instead of panning.
|
|
52
|
+
*/
|
|
53
|
+
sweepDrag(press: StagePress, mode?: "replace" | "add"): StageDrag;
|
|
54
|
+
}
|
|
55
|
+
|
|
43
56
|
/** Props of `PolylineSet`. */
|
|
44
57
|
export interface PolylineSetProps {
|
|
45
58
|
/** The polylines, in image coordinates. */
|
|
@@ -77,6 +90,13 @@ export interface PolylineSetProps {
|
|
|
77
90
|
* and to the layers below.
|
|
78
91
|
*/
|
|
79
92
|
marquee?: boolean | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Whether `marquee` puts a full-frame sweep target (`data-marquee-surface`) in this layer.
|
|
95
|
+
* Defaults to `true`. Off, a press on a line still starts a band, and a press on bare image
|
|
96
|
+
* reaches the layers below: the app starts a band from there with the `ref`'s `sweepDrag`
|
|
97
|
+
* or `startSweep`, so a band can begin anywhere without this layer covering the others.
|
|
98
|
+
*/
|
|
99
|
+
marqueeSurface?: boolean | undefined;
|
|
80
100
|
/**
|
|
81
101
|
* Whether the layer takes presses and hover. Defaults to `true` when any of `onItemPress`,
|
|
82
102
|
* `onSelect`, `onHover`, `onHoverChange` or `marquee` is given. When `false` the layer
|
|
@@ -96,12 +116,22 @@ export interface PolylineSetProps {
|
|
|
96
116
|
hitWidth?: number | undefined;
|
|
97
117
|
/** The zoom from which a hovered or selected line's points are drawn. Defaults to 3. */
|
|
98
118
|
vertexScale?: number | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* CSS colour of the point dots. Defaults to the overlay `label` role, a near-white that stands
|
|
121
|
+
* apart from both the line colours and the selection colour, so the points of a selected line
|
|
122
|
+
* show on it. Each dot sits on a dark halo.
|
|
123
|
+
*/
|
|
124
|
+
vertexColor?: string | undefined;
|
|
125
|
+
/** Diameter of a point dot, in screen pixels, without its 1 px halo. Defaults to 4. */
|
|
126
|
+
vertexSize?: number | undefined;
|
|
99
127
|
/** The layer's accessible name. Defaults to "Lines". */
|
|
100
128
|
label?: string | undefined;
|
|
101
129
|
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
102
130
|
layerId?: string | undefined;
|
|
103
131
|
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.line`. */
|
|
104
132
|
priority?: number | undefined;
|
|
133
|
+
/** Start a rubber band from a press elsewhere; see `PolylineSetHandle`. */
|
|
134
|
+
ref?: Ref<PolylineSetHandle> | undefined;
|
|
105
135
|
}
|
|
106
136
|
|
|
107
137
|
const HALO = overlayRole("halo");
|
|
@@ -111,18 +141,23 @@ const MAX_VERTEX_DOTS = 5000;
|
|
|
111
141
|
/**
|
|
112
142
|
* A selectable set of polylines inside an `ImageStage`.
|
|
113
143
|
*
|
|
114
|
-
* - **States
|
|
144
|
+
* - **States**:
|
|
115
145
|
* - default 1.5 screen px;
|
|
116
146
|
* - hover 2 px;
|
|
117
147
|
* - selected 2.5 px in `selectionStroke`;
|
|
118
148
|
* - dimmed at 35 % opacity.
|
|
119
149
|
* - **Halo.** Every line has a halo, so it holds on any image.
|
|
120
|
-
* - **Points.** Above `vertexScale`, the points of the hovered and selected lines are drawn
|
|
150
|
+
* - **Points.** Above `vertexScale`, the points of the hovered and selected lines are drawn as
|
|
151
|
+
* `vertexColor` dots on a dark halo, so they show on a selected line too.
|
|
121
152
|
* - **Panning.** The hand tool and a held space bar still pan.
|
|
122
153
|
* - **Tools.** Without a handler (or with `interactive={false}`) the layer takes no presses
|
|
123
154
|
* and only answers `useStageHitTest`, so a draw tool's surface below receives them.
|
|
155
|
+
* - **Sweeps from elsewhere.** The `ref`'s `startSweep` and `sweepDrag` start a band from a
|
|
156
|
+
* press another target received; with `marqueeSurface={false}` the layer keeps no
|
|
157
|
+
* full-frame target of its own.
|
|
124
158
|
*
|
|
125
|
-
* The SVG carries `data-hovered` (the hovered id) and `data-sweeping` while a band is drawn
|
|
159
|
+
* The SVG carries `data-hovered` (the hovered id) and `data-sweeping` while a band is drawn;
|
|
160
|
+
* the point dots are `data-points`.
|
|
126
161
|
*/
|
|
127
162
|
export function PolylineSet({
|
|
128
163
|
items,
|
|
@@ -134,14 +169,18 @@ export function PolylineSet({
|
|
|
134
169
|
onItemPress,
|
|
135
170
|
onSelect,
|
|
136
171
|
marquee = false,
|
|
172
|
+
marqueeSurface = true,
|
|
137
173
|
interactive,
|
|
138
174
|
stroke = overlayRole("feature"),
|
|
139
175
|
selectionStroke = overlayRole("selection"),
|
|
140
176
|
hitWidth = 14,
|
|
141
177
|
vertexScale = 3,
|
|
178
|
+
vertexColor = overlayRole("label"),
|
|
179
|
+
vertexSize = 4,
|
|
142
180
|
label = "Lines",
|
|
143
181
|
layerId,
|
|
144
182
|
priority = STAGE_HIT_PRIORITY.line,
|
|
183
|
+
ref,
|
|
145
184
|
}: PolylineSetProps) {
|
|
146
185
|
const stage = useStage();
|
|
147
186
|
const startDrag = useStageDrag();
|
|
@@ -149,8 +188,7 @@ export function PolylineSet({
|
|
|
149
188
|
// A touch still being watched when the layer goes away is cancelled, not left listening.
|
|
150
189
|
useEffect(() => () => watchRef.current?.(), []);
|
|
151
190
|
const takesPresses =
|
|
152
|
-
interactive ??
|
|
153
|
-
(onItemPress !== undefined || onSelect !== undefined || onHover !== undefined || onHoverChange !== undefined || marquee);
|
|
191
|
+
interactive ?? (onItemPress !== undefined || onSelect !== undefined || onHover !== undefined || onHoverChange !== undefined || marquee);
|
|
154
192
|
const px = useScreenPx();
|
|
155
193
|
const { hitTestAll } = useStageHitTest();
|
|
156
194
|
|
|
@@ -235,21 +273,34 @@ export function PolylineSet({
|
|
|
235
273
|
onHoverChange?.(id);
|
|
236
274
|
};
|
|
237
275
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const additive = event.metaKey || event.ctrlKey;
|
|
276
|
+
/** A band from `from` (image coordinates): drawn as the pointer moves, selecting on release. */
|
|
277
|
+
const bandFrom = (from: Point, additive: boolean): StageDrag => {
|
|
241
278
|
setBand({ x: from.x, y: from.y, width: 0, height: 0 });
|
|
242
279
|
setHover(null);
|
|
243
|
-
|
|
280
|
+
return {
|
|
244
281
|
onMove: (p) => setBand(boxBetween(from, p)),
|
|
245
282
|
onEnd: (p) => {
|
|
246
283
|
setBand(null);
|
|
247
284
|
onSelect?.(polylinesInRect(index, boxBetween(from, p)), additive ? "add" : "replace");
|
|
248
285
|
},
|
|
249
286
|
onCancel: () => setBand(null),
|
|
250
|
-
}
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const sweep = (event: StagePointerEvent, mode?: "replace" | "add") => {
|
|
291
|
+
if (event.type !== "pointerdown" || stage.panMode) return;
|
|
292
|
+
const additive = mode === undefined ? event.metaKey || event.ctrlKey : mode === "add";
|
|
293
|
+
startDrag(event, bandFrom(stage.toImage({ x: event.clientX, y: event.clientY }), additive));
|
|
251
294
|
};
|
|
252
295
|
|
|
296
|
+
useImperativeHandle(ref, () => ({
|
|
297
|
+
startSweep: sweep,
|
|
298
|
+
sweepDrag: (press, mode) => ({
|
|
299
|
+
...bandFrom(press.point, mode === undefined ? press.metaKey : mode === "add"),
|
|
300
|
+
claimsTouch: true,
|
|
301
|
+
}),
|
|
302
|
+
}));
|
|
303
|
+
|
|
253
304
|
const onLinePointerDown = (event: ReactPointerEvent<SVGPathElement>) => {
|
|
254
305
|
const touch = event.pointerType === "touch";
|
|
255
306
|
if (stage.panMode || (!touch && event.button !== 0)) return;
|
|
@@ -300,7 +351,7 @@ export function PolylineSet({
|
|
|
300
351
|
data-hovered={hoverId ?? undefined}
|
|
301
352
|
data-sweeping={band ? "" : undefined}
|
|
302
353
|
>
|
|
303
|
-
{takesPresses && marquee && (
|
|
354
|
+
{takesPresses && marquee && marqueeSurface && (
|
|
304
355
|
<rect
|
|
305
356
|
data-marquee-surface=""
|
|
306
357
|
x={-0.5}
|
|
@@ -344,7 +395,12 @@ export function PolylineSet({
|
|
|
344
395
|
/>
|
|
345
396
|
</>
|
|
346
397
|
)}
|
|
347
|
-
{dots &&
|
|
398
|
+
{dots && (
|
|
399
|
+
<>
|
|
400
|
+
<path d={dots} stroke={HALO} strokeWidth={px(vertexSize + 2)} />
|
|
401
|
+
<path data-points="" d={dots} stroke={vertexColor} strokeWidth={px(vertexSize)} />
|
|
402
|
+
</>
|
|
403
|
+
)}
|
|
348
404
|
{/* The one hit target: every line, transparent, wide. Which line it was is the
|
|
349
405
|
index's answer, not the DOM's. */}
|
|
350
406
|
{takesPresses && (
|
|
@@ -6,34 +6,57 @@
|
|
|
6
6
|
* corner, so a box that was nearly right had to be redrawn, and one drawn at fit could not
|
|
7
7
|
* be refined by zooming in first. Here the box has eight handles and an interior, and it lives
|
|
8
8
|
* inside `ImageStage`'s transform, so it stays on the pixels it encloses at any zoom.
|
|
9
|
+
*
|
|
10
|
+
* Every press on the box is decided once, by `shapePress`: the nearest handle first, then the
|
|
11
|
+
* band along the outline, then (unless `interior` is `"none"`) the inside. Every gesture then
|
|
12
|
+
* runs through `useShapeDrag`, so a press that stays within the click slop edits nothing.
|
|
9
13
|
*/
|
|
10
14
|
|
|
11
|
-
import {
|
|
15
|
+
import { useImperativeHandle, useState, type KeyboardEvent, type PointerEvent, type Ref } from "react";
|
|
12
16
|
|
|
17
|
+
import { cn } from "@vitavision/ui";
|
|
13
18
|
import type { Point } from "./measureGeometry";
|
|
14
|
-
import {
|
|
15
|
-
ROI_HANDLES,
|
|
16
|
-
ROI_HANDLE_CURSOR,
|
|
17
|
-
clampRect,
|
|
18
|
-
moveRect,
|
|
19
|
-
rectFromCorners,
|
|
20
|
-
resizeRect,
|
|
21
|
-
roiHandlePoint,
|
|
22
|
-
type RoiHandle,
|
|
23
|
-
} from "./roiEdit";
|
|
19
|
+
import { ROI_HANDLES, ROI_HANDLE_CURSOR, clampRect, moveRect, rectFromCorners, resizeRect, roiHandlePoint } from "./roiEdit";
|
|
24
20
|
import { overlayRole } from "./overlayRole";
|
|
21
|
+
import { shapePress, type RotatedShape } from "./shapeEdit";
|
|
22
|
+
import { CLICK_SLOP, POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
23
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
25
24
|
import { useStage } from "./stage/ImageStage";
|
|
25
|
+
import type { StageDrag, StagePress } from "./stage/StageSurface";
|
|
26
|
+
import { useCoarsePointer } from "./stage/useCoarsePointer";
|
|
26
27
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
27
28
|
import { PIXEL_CENTRE, imageViewBox, type Rect } from "./stage/view";
|
|
29
|
+
import { useShapeDrag } from "./useShapeDrag";
|
|
28
30
|
|
|
29
31
|
/** Handle square side, in screen pixels. */
|
|
30
32
|
const HANDLE_PX = 9;
|
|
31
33
|
/** Outline width, in screen pixels. */
|
|
32
34
|
const STROKE_PX = 1.5;
|
|
33
|
-
/** The halo under the outline
|
|
35
|
+
/** The dark halo under the outline, in screen pixels on each side. */
|
|
34
36
|
const HALO_PX = 1;
|
|
35
37
|
const HALO = overlayRole("halo");
|
|
36
38
|
|
|
39
|
+
/**
|
|
40
|
+
* What `RectRoiEditor`'s `ref` exposes: a new region drawn from a press the app's own target
|
|
41
|
+
* received, for an app whose one `StageSurface` decides what a press means.
|
|
42
|
+
*/
|
|
43
|
+
export interface RectRoiEditorHandle {
|
|
44
|
+
/**
|
|
45
|
+
* Draw a new region from a press: call it from a `pointerdown` handler, such as a layer's
|
|
46
|
+
* `onItemPress`. The press is claimed (the stage does not pan) and followed on `window`; a
|
|
47
|
+
* drag shorter than the click slop, or a box smaller than `minSize`, draws nothing. Another
|
|
48
|
+
* event type (a touch tap reported on release) does nothing. Works whether or not `draw` is
|
|
49
|
+
* set, and does nothing while `editable` is off or the stage is in pan mode.
|
|
50
|
+
*/
|
|
51
|
+
startDraw(event: StagePointerEvent): void;
|
|
52
|
+
/**
|
|
53
|
+
* The same draw as a `StageDrag`, to return from `StageSurface`'s `onPress`. It claims a
|
|
54
|
+
* touch, so a finger draws instead of panning. While `editable` is off it is an empty drag:
|
|
55
|
+
* the press is claimed and nothing is drawn.
|
|
56
|
+
*/
|
|
57
|
+
drawDrag(press: StagePress): StageDrag;
|
|
58
|
+
}
|
|
59
|
+
|
|
37
60
|
/** Props of `RectRoiEditor`. */
|
|
38
61
|
export interface RectRoiEditorProps {
|
|
39
62
|
/** The region in image coordinates, or `null` for none. */
|
|
@@ -49,6 +72,20 @@ export interface RectRoiEditorProps {
|
|
|
49
72
|
* region tool is active: in draw mode the layer claims presses that would otherwise pan.
|
|
50
73
|
*/
|
|
51
74
|
draw?: boolean | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Whether `draw` puts a full-frame draw target (`data-draw-surface`) in this layer. Defaults
|
|
77
|
+
* to `true`. Turn it off when the app's own `StageSurface` decides what a press means, so
|
|
78
|
+
* other layers can sit between the target and the region's handles, and start the draw from
|
|
79
|
+
* there with the `ref`'s `drawDrag` or `startDraw`.
|
|
80
|
+
*/
|
|
81
|
+
drawSurface?: boolean | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* What a press inside the region does. `"move"` (the default) moves it. `"none"` leaves the
|
|
84
|
+
* inside to the layers below (a press there selects what lies under the region, say): only
|
|
85
|
+
* the handles and the band along the outline grab the region. The region stays focusable and
|
|
86
|
+
* the arrow keys still move it.
|
|
87
|
+
*/
|
|
88
|
+
interior?: "move" | "none" | undefined;
|
|
52
89
|
/**
|
|
53
90
|
* The area the region must stay in, in the stage's centre convention. Defaults to
|
|
54
91
|
* `{ x: -0.5, y: -0.5, width, height }` of the image: its full extent, so a region that
|
|
@@ -61,26 +98,39 @@ export interface RectRoiEditorProps {
|
|
|
61
98
|
label?: string | undefined;
|
|
62
99
|
/** CSS colour of the outline and handles. Defaults to the overlay `selection` role. */
|
|
63
100
|
stroke?: string | undefined;
|
|
101
|
+
/** CSS colour of the tint inside the region. Defaults to `stroke`. */
|
|
102
|
+
fill?: string | undefined;
|
|
103
|
+
/** Opacity of the tint inside the region, 0 to 1. Defaults to 0.06; `0` draws no tint. */
|
|
104
|
+
fillOpacity?: number | undefined;
|
|
105
|
+
/** Draw from the app's own press target; see `RectRoiEditorHandle`. */
|
|
106
|
+
ref?: Ref<RectRoiEditorHandle> | undefined;
|
|
64
107
|
}
|
|
65
108
|
|
|
66
|
-
type Drag =
|
|
67
|
-
| { kind: "move"; from: Point; start: Rect; moved: boolean }
|
|
68
|
-
| { kind: "resize"; handle: RoiHandle; start: Rect; moved: boolean }
|
|
69
|
-
| { kind: "draw"; from: Point; moved: boolean };
|
|
70
|
-
|
|
71
109
|
/**
|
|
72
110
|
* An editable axis-aligned region inside an `ImageStage`.
|
|
73
111
|
*
|
|
74
|
-
* - **Handles.** The eight handles resize the region, and the interior moves it
|
|
75
|
-
*
|
|
76
|
-
*
|
|
112
|
+
* - **Handles.** The eight handles resize the region, and the interior moves it, as does the
|
|
113
|
+
* band along the outline (the pointer's tolerance: 6 px for a mouse, 12 px for a finger). A
|
|
114
|
+
* handle dragged through the opposite edge flips the box.
|
|
115
|
+
* - **Press order.** One decision serves every part of the editor: the nearest handle first,
|
|
116
|
+
* then the outline band, then the interior (unless `interior` is `"none"`). A small region's
|
|
117
|
+
* interior does not steal a press meant for a handle, and a press that grabs nothing is not
|
|
118
|
+
* claimed.
|
|
119
|
+
* - **Click slop.** A press becomes an edit only once the pointer has moved 3 screen pixels, so
|
|
120
|
+
* a jittery click calls neither `onValueChange` nor `onCommit`. An interrupted drag
|
|
121
|
+
* (`pointercancel`) puts the region back and commits nothing.
|
|
122
|
+
* - **Drawing.** With `draw`, a drag elsewhere on the image draws a new region; with
|
|
123
|
+
* `drawSurface={false}` the app starts it through the `ref`.
|
|
77
124
|
* - **Keyboard.** The region is focusable: arrow keys move it by one image pixel (ten with
|
|
78
125
|
* Shift), and with Alt they grow or shrink it from the bottom-right corner.
|
|
79
126
|
* - **Limits.** The region stays inside `bounds` and never gets smaller than `minSize`.
|
|
80
127
|
* - **Rendering.** Handles and the outline are a constant size on screen at every zoom.
|
|
81
128
|
* - **Panning.** A press with the hand tool, or with space held, still pans.
|
|
82
129
|
*
|
|
83
|
-
*
|
|
130
|
+
* Data attributes, for styling and tests: the SVG carries `data-editable`, and `data-drawing`
|
|
131
|
+
* while a draw is in progress; the full-frame draw target is `data-draw-surface`; the region's
|
|
132
|
+
* focusable inside is `data-roi-interior`; the band along its outline is `data-roi-band`; and
|
|
133
|
+
* each handle is `data-handle`, one of `nw`, `n`, `ne`, `e`, `se`, `s`, `sw`, `w`.
|
|
84
134
|
*/
|
|
85
135
|
export function RectRoiEditor({
|
|
86
136
|
value,
|
|
@@ -88,71 +138,102 @@ export function RectRoiEditor({
|
|
|
88
138
|
onCommit,
|
|
89
139
|
editable = true,
|
|
90
140
|
draw = false,
|
|
141
|
+
drawSurface = true,
|
|
142
|
+
interior = "move",
|
|
91
143
|
bounds,
|
|
92
144
|
minSize = 4,
|
|
93
145
|
label = "Region",
|
|
94
146
|
stroke = overlayRole("selection"),
|
|
147
|
+
fill = stroke,
|
|
148
|
+
fillOpacity = 0.06,
|
|
149
|
+
ref,
|
|
95
150
|
}: RectRoiEditorProps) {
|
|
96
151
|
const stage = useStage();
|
|
97
152
|
const area = bounds ?? { x: -PIXEL_CENTRE, y: -PIXEL_CENTRE, width: stage.image.width, height: stage.image.height };
|
|
98
|
-
const dragRef = useRef<Drag | null>(null);
|
|
99
|
-
const lastRef = useRef<Rect | null>(null);
|
|
100
153
|
const [drawing, setDrawing] = useState<Rect | null>(null);
|
|
154
|
+
const startShapeDrag = useShapeDrag();
|
|
155
|
+
const coarse = useCoarsePointer();
|
|
101
156
|
|
|
102
157
|
const px = useScreenPx();
|
|
103
158
|
const shown = drawing ?? value;
|
|
104
159
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
160
|
+
/**
|
|
161
|
+
* The one press decision for the band, the interior and every handle: the nearest handle
|
|
162
|
+
* wins over the band and the interior. A declined press is not claimed and bubbles on.
|
|
163
|
+
*/
|
|
164
|
+
const press = (event: PointerEvent<SVGElement>) => {
|
|
165
|
+
if (!value || !editable || stage.panMode || event.button !== 0) return;
|
|
166
|
+
const at = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
167
|
+
const decision = shapePress(asShape(value), "rect", stage.view.scale, at, hitRadiusPx(event.pointerType), false, interior === "move");
|
|
168
|
+
if (!decision) return;
|
|
169
|
+
const start = value;
|
|
170
|
+
let last: Rect | null = null;
|
|
171
|
+
startShapeDrag(event, {
|
|
172
|
+
onMove: (delta) => {
|
|
173
|
+
last =
|
|
174
|
+
decision.kind === "resize"
|
|
175
|
+
? resizeRect(start, decision.handle, { x: at.x + delta.x, y: at.y + delta.y }, area, minSize)
|
|
176
|
+
: moveRect(start, delta.x, delta.y, area);
|
|
177
|
+
onValueChange(last);
|
|
178
|
+
},
|
|
179
|
+
onEnd: (_delta, moved) => {
|
|
180
|
+
if (moved && last) onCommit?.(last);
|
|
181
|
+
},
|
|
182
|
+
onCancel: () => {
|
|
183
|
+
if (last) onValueChange(start);
|
|
184
|
+
},
|
|
185
|
+
});
|
|
108
186
|
};
|
|
109
187
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
188
|
+
/** A draw from `from`: the box to the pointer, clamped, shown while in flight and kept on release. */
|
|
189
|
+
const drawFrom = (from: Point) => {
|
|
190
|
+
let drawn: Rect | null = null;
|
|
191
|
+
return {
|
|
192
|
+
to: (p: Point) => {
|
|
193
|
+
drawn = clampRect(rectFromCorners(clampPoint(from, area), clampPoint(p, area)), area, 0);
|
|
194
|
+
setDrawing(drawn);
|
|
195
|
+
},
|
|
196
|
+
end: (moved: boolean) => {
|
|
197
|
+
setDrawing(null);
|
|
198
|
+
// A drawn box smaller than `minSize` is a click, not a region.
|
|
199
|
+
if (moved && drawn && drawn.width >= minSize && drawn.height >= minSize) {
|
|
200
|
+
onValueChange(drawn);
|
|
201
|
+
onCommit?.(drawn);
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
cancel: () => setDrawing(null),
|
|
205
|
+
};
|
|
117
206
|
};
|
|
118
207
|
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
report(resizeRect(drag.start, drag.handle, p, area, minSize));
|
|
129
|
-
} else {
|
|
130
|
-
const drawn = clampRect(rectFromCorners(clampPoint(drag.from, area), clampPoint(p, area)), area, 0);
|
|
131
|
-
// Kept in the ref too: a pointer-up can arrive before the move's render, and the end of
|
|
132
|
-
// the gesture must see the box the last move drew.
|
|
133
|
-
lastRef.current = drawn;
|
|
134
|
-
setDrawing(drawn);
|
|
135
|
-
}
|
|
208
|
+
const startDraw = (event: StagePointerEvent) => {
|
|
209
|
+
if (!editable || stage.panMode) return;
|
|
210
|
+
const from = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
211
|
+
const gesture = drawFrom(from);
|
|
212
|
+
startShapeDrag(event, {
|
|
213
|
+
onMove: (delta) => gesture.to({ x: from.x + delta.x, y: from.y + delta.y }),
|
|
214
|
+
onEnd: (_delta, moved) => gesture.end(moved),
|
|
215
|
+
onCancel: gesture.cancel,
|
|
216
|
+
});
|
|
136
217
|
};
|
|
137
218
|
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
if (drag.moved && lastRef.current) onCommit?.(lastRef.current);
|
|
219
|
+
const drawDrag = (pressed: StagePress): StageDrag => {
|
|
220
|
+
if (!editable) return {};
|
|
221
|
+
const gesture = drawFrom(pressed.point);
|
|
222
|
+
let moved = false;
|
|
223
|
+
return {
|
|
224
|
+
claimsTouch: true,
|
|
225
|
+
onMove: (point, event) => {
|
|
226
|
+
if (!moved && Math.hypot(event.clientX - pressed.client.x, event.clientY - pressed.client.y) <= CLICK_SLOP) return;
|
|
227
|
+
moved = true;
|
|
228
|
+
gesture.to(point);
|
|
229
|
+
},
|
|
230
|
+
onEnd: () => gesture.end(moved),
|
|
231
|
+
onCancel: gesture.cancel,
|
|
232
|
+
};
|
|
154
233
|
};
|
|
155
234
|
|
|
235
|
+
useImperativeHandle(ref, () => ({ startDraw, drawDrag }));
|
|
236
|
+
|
|
156
237
|
const onKeyDown = (event: KeyboardEvent<SVGElement>) => {
|
|
157
238
|
if (!editable || value === null) return;
|
|
158
239
|
const step = event.shiftKey ? 10 : 1;
|
|
@@ -173,6 +254,8 @@ export function RectRoiEditor({
|
|
|
173
254
|
onCommit?.(next);
|
|
174
255
|
};
|
|
175
256
|
|
|
257
|
+
const keepDoubleClick = (event: { stopPropagation: () => void }) => event.stopPropagation();
|
|
258
|
+
|
|
176
259
|
const outline = shown && (
|
|
177
260
|
<>
|
|
178
261
|
<rect
|
|
@@ -189,8 +272,8 @@ export function RectRoiEditor({
|
|
|
189
272
|
y={shown.y}
|
|
190
273
|
width={shown.width}
|
|
191
274
|
height={shown.height}
|
|
192
|
-
fill={
|
|
193
|
-
fillOpacity={0
|
|
275
|
+
fill={fillOpacity > 0 ? fill : "none"}
|
|
276
|
+
fillOpacity={fillOpacity > 0 ? fillOpacity : undefined}
|
|
194
277
|
stroke={stroke}
|
|
195
278
|
strokeWidth={px(STROKE_PX)}
|
|
196
279
|
strokeDasharray={`${px(6)} ${px(4)}`}
|
|
@@ -205,7 +288,7 @@ export function RectRoiEditor({
|
|
|
205
288
|
data-editable={editable ? "" : undefined}
|
|
206
289
|
data-drawing={drawing ? "" : undefined}
|
|
207
290
|
>
|
|
208
|
-
{draw && editable && (
|
|
291
|
+
{draw && editable && drawSurface && (
|
|
209
292
|
<rect
|
|
210
293
|
data-draw-surface=""
|
|
211
294
|
x={area.x}
|
|
@@ -214,17 +297,35 @@ export function RectRoiEditor({
|
|
|
214
297
|
height={area.height}
|
|
215
298
|
fill="transparent"
|
|
216
299
|
className="pointer-events-auto cursor-crosshair"
|
|
217
|
-
onPointerDown={(event) =>
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
onPointerMove={onMove}
|
|
221
|
-
onPointerUp={onEnd}
|
|
222
|
-
onLostPointerCapture={onEnd}
|
|
300
|
+
onPointerDown={(event) => {
|
|
301
|
+
if (event.button === 0) startDraw(event);
|
|
302
|
+
}}
|
|
223
303
|
/>
|
|
224
304
|
)}
|
|
225
305
|
{outline}
|
|
226
306
|
{editable && value && !drawing && (
|
|
307
|
+
// The outline band: a wide transparent stroke, so a press a few pixels either side of
|
|
308
|
+
// the outline still grabs the region, even when its inside is left to the layers
|
|
309
|
+
// below. Sized for the device's primary pointer; `press` decides with the event's own.
|
|
227
310
|
<rect
|
|
311
|
+
data-roi-band=""
|
|
312
|
+
x={value.x}
|
|
313
|
+
y={value.y}
|
|
314
|
+
width={value.width}
|
|
315
|
+
height={value.height}
|
|
316
|
+
fill="none"
|
|
317
|
+
stroke="transparent"
|
|
318
|
+
strokeWidth={px(2 * (coarse ? TOUCH_RADIUS_PX : POINTER_RADIUS_PX))}
|
|
319
|
+
aria-hidden
|
|
320
|
+
className="pointer-events-auto cursor-move"
|
|
321
|
+
style={{ pointerEvents: "stroke" }}
|
|
322
|
+
onPointerDown={press}
|
|
323
|
+
onDoubleClick={keepDoubleClick}
|
|
324
|
+
/>
|
|
325
|
+
)}
|
|
326
|
+
{editable && value && !drawing && (
|
|
327
|
+
<rect
|
|
328
|
+
data-roi-interior=""
|
|
228
329
|
x={value.x}
|
|
229
330
|
y={value.y}
|
|
230
331
|
width={value.width}
|
|
@@ -234,20 +335,13 @@ export function RectRoiEditor({
|
|
|
234
335
|
tabIndex={0}
|
|
235
336
|
aria-label={`${label}: ${fmt(value.x)}, ${fmt(value.y)}, ${fmt(value.width)} × ${fmt(value.height)} px`}
|
|
236
337
|
aria-roledescription="region"
|
|
237
|
-
className=
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
start: value,
|
|
243
|
-
moved: false,
|
|
244
|
-
})
|
|
245
|
-
}
|
|
246
|
-
onPointerMove={onMove}
|
|
247
|
-
onPointerUp={onEnd}
|
|
248
|
-
onLostPointerCapture={onEnd}
|
|
338
|
+
className={cn(
|
|
339
|
+
"outline-none focus-visible:stroke-signal",
|
|
340
|
+
interior === "move" ? "pointer-events-auto cursor-move" : "pointer-events-none",
|
|
341
|
+
)}
|
|
342
|
+
onPointerDown={press}
|
|
249
343
|
onKeyDown={onKeyDown}
|
|
250
|
-
onDoubleClick={
|
|
344
|
+
onDoubleClick={keepDoubleClick}
|
|
251
345
|
/>
|
|
252
346
|
)}
|
|
253
347
|
{editable &&
|
|
@@ -269,11 +363,8 @@ export function RectRoiEditor({
|
|
|
269
363
|
strokeWidth={px(1)}
|
|
270
364
|
className="pointer-events-auto"
|
|
271
365
|
style={{ cursor: ROI_HANDLE_CURSOR[handle] }}
|
|
272
|
-
onPointerDown={
|
|
273
|
-
|
|
274
|
-
onPointerUp={onEnd}
|
|
275
|
-
onLostPointerCapture={onEnd}
|
|
276
|
-
onDoubleClick={(event) => event.stopPropagation()}
|
|
366
|
+
onPointerDown={press}
|
|
367
|
+
onDoubleClick={keepDoubleClick}
|
|
277
368
|
/>
|
|
278
369
|
);
|
|
279
370
|
})}
|
|
@@ -281,6 +372,11 @@ export function RectRoiEditor({
|
|
|
281
372
|
);
|
|
282
373
|
}
|
|
283
374
|
|
|
375
|
+
/** The region as an unrotated shape, for the shared press decision. */
|
|
376
|
+
function asShape(rect: Rect): RotatedShape {
|
|
377
|
+
return { cx: rect.x + rect.width / 2, cy: rect.y + rect.height / 2, width: rect.width, height: rect.height, rotation: 0 };
|
|
378
|
+
}
|
|
379
|
+
|
|
284
380
|
function clampPoint(p: Point, area: Rect): Point {
|
|
285
381
|
return {
|
|
286
382
|
x: Math.min(area.x + area.width, Math.max(area.x, p.x)),
|