@vitavision/stage2d 0.12.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 +669 -57
- package/dist/index.js +5455 -3621
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/AreaSet.tsx +4 -4
- package/src/components/ContourEditor.tsx +286 -37
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +1 -2
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MeasureOverlay.tsx +320 -89
- package/src/components/PointSet.tsx +4 -4
- package/src/components/PolylineSet.tsx +81 -17
- package/src/components/RectRoiEditor.tsx +188 -84
- package/src/components/ShapeEditor.tsx +14 -4
- package/src/components/areaBatching.ts +1 -1
- 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 +29 -8
- package/src/components/stage/StageSurface.tsx +1 -4
- 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/useShapeDrag.ts +2 -4
- package/src/index.ts +32 -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
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. */
|
|
@@ -49,6 +80,20 @@ export interface RectRoiEditorProps {
|
|
|
49
80
|
* region tool is active: in draw mode the layer claims presses that would otherwise pan.
|
|
50
81
|
*/
|
|
51
82
|
draw?: boolean | undefined;
|
|
83
|
+
/**
|
|
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;
|
|
52
97
|
/**
|
|
53
98
|
* The area the region must stay in, in the stage's centre convention. Defaults to
|
|
54
99
|
* `{ x: -0.5, y: -0.5, width, height }` of the image: its full extent, so a region that
|
|
@@ -61,26 +106,39 @@ export interface RectRoiEditorProps {
|
|
|
61
106
|
label?: string | undefined;
|
|
62
107
|
/** CSS colour of the outline and handles. Defaults to the overlay `selection` role. */
|
|
63
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;
|
|
64
115
|
}
|
|
65
116
|
|
|
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
117
|
/**
|
|
72
118
|
* An editable axis-aligned region inside an `ImageStage`.
|
|
73
119
|
*
|
|
74
|
-
* - **Handles.** The eight handles resize the region, and the interior moves it
|
|
75
|
-
*
|
|
76
|
-
*
|
|
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`.
|
|
77
132
|
* - **Keyboard.** The region is focusable: arrow keys move it by one image pixel (ten with
|
|
78
133
|
* Shift), and with Alt they grow or shrink it from the bottom-right corner.
|
|
79
134
|
* - **Limits.** The region stays inside `bounds` and never gets smaller than `minSize`.
|
|
80
135
|
* - **Rendering.** Handles and the outline are a constant size on screen at every zoom.
|
|
81
136
|
* - **Panning.** A press with the hand tool, or with space held, still pans.
|
|
82
137
|
*
|
|
83
|
-
*
|
|
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`.
|
|
84
142
|
*/
|
|
85
143
|
export function RectRoiEditor({
|
|
86
144
|
value,
|
|
@@ -88,71 +146,102 @@ export function RectRoiEditor({
|
|
|
88
146
|
onCommit,
|
|
89
147
|
editable = true,
|
|
90
148
|
draw = false,
|
|
149
|
+
drawSurface = true,
|
|
150
|
+
interior = "move",
|
|
91
151
|
bounds,
|
|
92
152
|
minSize = 4,
|
|
93
153
|
label = "Region",
|
|
94
154
|
stroke = overlayRole("selection"),
|
|
155
|
+
fill = stroke,
|
|
156
|
+
fillOpacity = 0.06,
|
|
157
|
+
ref,
|
|
95
158
|
}: RectRoiEditorProps) {
|
|
96
159
|
const stage = useStage();
|
|
97
160
|
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
161
|
const [drawing, setDrawing] = useState<Rect | null>(null);
|
|
162
|
+
const startShapeDrag = useShapeDrag();
|
|
163
|
+
const coarse = useCoarsePointer();
|
|
101
164
|
|
|
102
165
|
const px = useScreenPx();
|
|
103
166
|
const shown = drawing ?? value;
|
|
104
167
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
+
});
|
|
108
194
|
};
|
|
109
195
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
+
};
|
|
117
214
|
};
|
|
118
215
|
|
|
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
|
-
}
|
|
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
|
+
});
|
|
136
225
|
};
|
|
137
226
|
|
|
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);
|
|
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
|
+
};
|
|
154
241
|
};
|
|
155
242
|
|
|
243
|
+
useImperativeHandle(ref, () => ({ startDraw, drawDrag }));
|
|
244
|
+
|
|
156
245
|
const onKeyDown = (event: KeyboardEvent<SVGElement>) => {
|
|
157
246
|
if (!editable || value === null) return;
|
|
158
247
|
const step = event.shiftKey ? 10 : 1;
|
|
@@ -173,6 +262,8 @@ export function RectRoiEditor({
|
|
|
173
262
|
onCommit?.(next);
|
|
174
263
|
};
|
|
175
264
|
|
|
265
|
+
const keepDoubleClick = (event: { stopPropagation: () => void }) => event.stopPropagation();
|
|
266
|
+
|
|
176
267
|
const outline = shown && (
|
|
177
268
|
<>
|
|
178
269
|
<rect
|
|
@@ -189,8 +280,8 @@ export function RectRoiEditor({
|
|
|
189
280
|
y={shown.y}
|
|
190
281
|
width={shown.width}
|
|
191
282
|
height={shown.height}
|
|
192
|
-
fill={
|
|
193
|
-
fillOpacity={0
|
|
283
|
+
fill={fillOpacity > 0 ? fill : "none"}
|
|
284
|
+
fillOpacity={fillOpacity > 0 ? fillOpacity : undefined}
|
|
194
285
|
stroke={stroke}
|
|
195
286
|
strokeWidth={px(STROKE_PX)}
|
|
196
287
|
strokeDasharray={`${px(6)} ${px(4)}`}
|
|
@@ -205,7 +296,7 @@ export function RectRoiEditor({
|
|
|
205
296
|
data-editable={editable ? "" : undefined}
|
|
206
297
|
data-drawing={drawing ? "" : undefined}
|
|
207
298
|
>
|
|
208
|
-
{draw && editable && (
|
|
299
|
+
{draw && editable && drawSurface && (
|
|
209
300
|
<rect
|
|
210
301
|
data-draw-surface=""
|
|
211
302
|
x={area.x}
|
|
@@ -214,17 +305,35 @@ export function RectRoiEditor({
|
|
|
214
305
|
height={area.height}
|
|
215
306
|
fill="transparent"
|
|
216
307
|
className="pointer-events-auto cursor-crosshair"
|
|
217
|
-
onPointerDown={(event) =>
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
onPointerMove={onMove}
|
|
221
|
-
onPointerUp={onEnd}
|
|
222
|
-
onLostPointerCapture={onEnd}
|
|
308
|
+
onPointerDown={(event) => {
|
|
309
|
+
if (event.button === 0) startDraw(event);
|
|
310
|
+
}}
|
|
223
311
|
/>
|
|
224
312
|
)}
|
|
225
313
|
{outline}
|
|
226
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.
|
|
227
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=""
|
|
228
337
|
x={value.x}
|
|
229
338
|
y={value.y}
|
|
230
339
|
width={value.width}
|
|
@@ -234,20 +343,13 @@ export function RectRoiEditor({
|
|
|
234
343
|
tabIndex={0}
|
|
235
344
|
aria-label={`${label}: ${fmt(value.x)}, ${fmt(value.y)}, ${fmt(value.width)} × ${fmt(value.height)} px`}
|
|
236
345
|
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}
|
|
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}
|
|
249
351
|
onKeyDown={onKeyDown}
|
|
250
|
-
onDoubleClick={
|
|
352
|
+
onDoubleClick={keepDoubleClick}
|
|
251
353
|
/>
|
|
252
354
|
)}
|
|
253
355
|
{editable &&
|
|
@@ -269,11 +371,8 @@ export function RectRoiEditor({
|
|
|
269
371
|
strokeWidth={px(1)}
|
|
270
372
|
className="pointer-events-auto"
|
|
271
373
|
style={{ cursor: ROI_HANDLE_CURSOR[handle] }}
|
|
272
|
-
onPointerDown={
|
|
273
|
-
|
|
274
|
-
onPointerUp={onEnd}
|
|
275
|
-
onLostPointerCapture={onEnd}
|
|
276
|
-
onDoubleClick={(event) => event.stopPropagation()}
|
|
374
|
+
onPointerDown={press}
|
|
375
|
+
onDoubleClick={keepDoubleClick}
|
|
277
376
|
/>
|
|
278
377
|
);
|
|
279
378
|
})}
|
|
@@ -281,6 +380,11 @@ export function RectRoiEditor({
|
|
|
281
380
|
);
|
|
282
381
|
}
|
|
283
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
|
+
|
|
284
388
|
function clampPoint(p: Point, area: Rect): Point {
|
|
285
389
|
return {
|
|
286
390
|
x: Math.min(area.x + area.width, Math.max(area.x, p.x)),
|
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
SHAPE_ROTATE_RADIUS_PX,
|
|
27
27
|
type RotatedShape,
|
|
28
28
|
} from "./shapeEdit";
|
|
29
|
-
import { POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
29
|
+
import { CLICK_SLOP, POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
30
30
|
import { useStage } from "./stage/ImageStage";
|
|
31
31
|
import { useCoarsePointer } from "./stage/useCoarsePointer";
|
|
32
32
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
@@ -34,7 +34,7 @@ import { imageViewBox } from "./stage/view";
|
|
|
34
34
|
|
|
35
35
|
/** Outline width, in screen pixels. */
|
|
36
36
|
const STROKE_PX = 1.5;
|
|
37
|
-
/** The halo under the outline
|
|
37
|
+
/** The dark halo under the outline, in screen pixels on each side. */
|
|
38
38
|
const HALO_PX = 1;
|
|
39
39
|
/** Shift while rotating rounds to this step, and `[` / `]` turn by 1° (15° with Shift). */
|
|
40
40
|
const SNAP = Math.PI / 12;
|
|
@@ -71,7 +71,13 @@ type Drag = (
|
|
|
71
71
|
| { kind: "move"; from: Point }
|
|
72
72
|
| { kind: "resize"; handle: RoiHandle }
|
|
73
73
|
| { kind: "rotate" }
|
|
74
|
-
) & {
|
|
74
|
+
) & {
|
|
75
|
+
start: RotatedShape;
|
|
76
|
+
/** Where the press landed, in client coordinates: a move counts once it is past the click slop. */
|
|
77
|
+
client: Point;
|
|
78
|
+
moved: boolean;
|
|
79
|
+
pointerId: number;
|
|
80
|
+
};
|
|
75
81
|
|
|
76
82
|
/**
|
|
77
83
|
* An editable rotated rectangle or ellipse inside an `ImageStage`.
|
|
@@ -87,6 +93,8 @@ type Drag = (
|
|
|
87
93
|
* then the interior or outline band (move), then nothing. A small shape's interior does not
|
|
88
94
|
* steal a press meant for a handle, and a press that hits none of them is not claimed, so
|
|
89
95
|
* it reaches whatever is below, or the stage.
|
|
96
|
+
* - **Click slop.** A press becomes an edit only once the pointer has moved 3 screen pixels
|
|
97
|
+
* from where it landed, so a jittery click changes nothing and commits nothing.
|
|
90
98
|
* - **Touch.** A press that grabs the shape is claimed; a second finger landing during an edit
|
|
91
99
|
* cancels it (the shape reverts, nothing is committed) and is left to the stage.
|
|
92
100
|
* - **Rotation.** The handle past the top side turns the shape about its centre; hold Shift to
|
|
@@ -163,7 +171,7 @@ export function ShapeEditor({
|
|
|
163
171
|
} catch {
|
|
164
172
|
// The pointer ended before this handler ran; the edit still follows the events the editor receives.
|
|
165
173
|
}
|
|
166
|
-
const base = { start: value, moved: false, pointerId: event.pointerId };
|
|
174
|
+
const base = { start: value, client: { x: event.clientX, y: event.clientY }, moved: false, pointerId: event.pointerId };
|
|
167
175
|
dragRef.current =
|
|
168
176
|
decision.kind === "move"
|
|
169
177
|
? { ...base, kind: "move", from: at }
|
|
@@ -185,6 +193,8 @@ export function ShapeEditor({
|
|
|
185
193
|
const drag = dragRef.current;
|
|
186
194
|
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
187
195
|
event.stopPropagation();
|
|
196
|
+
// A jittery click is not an edit: nothing changes until the pointer leaves the click slop.
|
|
197
|
+
if (!drag.moved && Math.hypot(event.clientX - drag.client.x, event.clientY - drag.client.y) <= CLICK_SLOP) return;
|
|
188
198
|
const p = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
189
199
|
drag.moved = true;
|
|
190
200
|
if (drag.kind === "move") report(moveShape(drag.start, p.x - drag.from.x, p.y - drag.from.y));
|