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