@vitavision/stage2d 0.7.1 → 0.8.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 +34 -1
- package/dist/index.d.ts +538 -14
- package/dist/index.js +1276 -50
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/MeasureOverlay.tsx +128 -32
- package/src/components/PolylineSet.tsx +308 -0
- package/src/components/RectRoiEditor.tsx +292 -0
- package/src/components/overlayRole.ts +53 -0
- package/src/components/polylineIndex.ts +288 -0
- package/src/components/roiEdit.ts +154 -0
- package/src/components/stage/ImageLayer.tsx +118 -0
- package/src/components/stage/ImageStage.tsx +49 -10
- package/src/components/stage/StageSurface.tsx +177 -0
- package/src/components/stage/StageToolbar.tsx +105 -6
- package/src/components/stage/useScreenPx.ts +16 -0
- package/src/components/stage/view.ts +7 -2
- package/src/index.ts +55 -0
- package/src/styles.css +27 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/stage2d",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "The vitavision 2D image stage: view transform, zoom/pan, layered overlays, measurement.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"react-dom": "^19.2.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@vitavision/ui": "^0.
|
|
49
|
+
"@vitavision/ui": "^0.10.0",
|
|
50
50
|
"lucide-react": "^1.48.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
* A vector overlay layer for measurement primitives, drawn in source-image pixel
|
|
3
3
|
* coordinates.
|
|
4
4
|
*
|
|
5
|
-
* Meant to sit as one of the
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* `nativeHeight` pixel coordinates, via `primitives`.
|
|
5
|
+
* Meant to sit as one of the layers inside `ImageStage`, alongside the image itself and any
|
|
6
|
+
* raster layers (a mask, a false-colour value plane). Because the whole stack is transformed
|
|
7
|
+
* together, a caliper box drawn here stays registered with the pixel it measures at any zoom
|
|
8
|
+
* or pan. The overlay never touches the DOM to find out where it is; it is told, in
|
|
9
|
+
* `nativeWidth` × `nativeHeight` pixel coordinates, via `primitives`.
|
|
11
10
|
*
|
|
12
11
|
* This component holds **no app state**: no fetch, no selection, no hover tracking. It is a
|
|
13
12
|
* pure function of its props, on purpose — the app decides what a "caliper box" or a
|
|
@@ -19,12 +18,27 @@
|
|
|
19
18
|
|
|
20
19
|
import { imageViewBox } from "./stage/view";
|
|
21
20
|
import { arcPath, arrowHeadPoints, caliperArrow, caliperCorners, crossSegments, dimensionGeometry, polygonPath, strokeWidthFor } from "./measureGeometry";
|
|
21
|
+
import { OVERLAY_STATE_OPACITY, overlayRole, type OverlayRole, type OverlayState } from "./overlayRole";
|
|
22
|
+
import { polylinePath } from "./polylineIndex";
|
|
22
23
|
import { cn, toneColor, type MeasureTone } from "@vitavision/ui";
|
|
23
24
|
|
|
24
25
|
export type { MeasureTone };
|
|
25
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Optional fields every primitive takes. All are additive: a primitive without them draws
|
|
29
|
+
* exactly as before.
|
|
30
|
+
*/
|
|
31
|
+
export interface PrimitiveCommon {
|
|
32
|
+
/** An identity, written to the primitive's `data-id`, for linking a mark to a row in a list. */
|
|
33
|
+
id?: string | undefined;
|
|
34
|
+
/** Interaction state: hover thickens, selected adds a ring, dimmed fades (overlay grammar). */
|
|
35
|
+
state?: OverlayState | undefined;
|
|
36
|
+
/** An overlay role colour, instead of `tone`: `feature`, `model`, `structure`. */
|
|
37
|
+
role?: OverlayRole | undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
26
40
|
/** A marked location: a dot, or a cross for a subpixel edge. */
|
|
27
|
-
export interface PointPrimitive {
|
|
41
|
+
export interface PointPrimitive extends PrimitiveCommon {
|
|
28
42
|
/** Discriminant. */
|
|
29
43
|
kind: "point";
|
|
30
44
|
/** Image x of the point. */
|
|
@@ -42,7 +56,7 @@ export interface PointPrimitive {
|
|
|
42
56
|
}
|
|
43
57
|
|
|
44
58
|
/** A straight line between two image points. */
|
|
45
|
-
export interface SegmentPrimitive {
|
|
59
|
+
export interface SegmentPrimitive extends PrimitiveCommon {
|
|
46
60
|
/** Discriminant. */
|
|
47
61
|
kind: "segment";
|
|
48
62
|
/** Image x of the first end. */
|
|
@@ -60,7 +74,7 @@ export interface SegmentPrimitive {
|
|
|
60
74
|
}
|
|
61
75
|
|
|
62
76
|
/** A circle — a fitted bore, a tolerance ring. */
|
|
63
|
-
export interface CirclePrimitive {
|
|
77
|
+
export interface CirclePrimitive extends PrimitiveCommon {
|
|
64
78
|
/** Discriminant. */
|
|
65
79
|
kind: "circle";
|
|
66
80
|
/** Image x of the centre. */
|
|
@@ -76,7 +90,7 @@ export interface CirclePrimitive {
|
|
|
76
90
|
}
|
|
77
91
|
|
|
78
92
|
/** A circular arc, swept forward (clockwise on screen) from `startAngle` to `endAngle`. */
|
|
79
|
-
export interface ArcPrimitive {
|
|
93
|
+
export interface ArcPrimitive extends PrimitiveCommon {
|
|
80
94
|
/** Discriminant. */
|
|
81
95
|
kind: "arc";
|
|
82
96
|
/** Image x of the centre. */
|
|
@@ -94,7 +108,7 @@ export interface ArcPrimitive {
|
|
|
94
108
|
}
|
|
95
109
|
|
|
96
110
|
/** A caliper search box: a rotated rectangle with an arrow along its scan direction. */
|
|
97
|
-
export interface CaliperPrimitive {
|
|
111
|
+
export interface CaliperPrimitive extends PrimitiveCommon {
|
|
98
112
|
/** Discriminant. */
|
|
99
113
|
kind: "caliper";
|
|
100
114
|
/** Image x of the box centre. */
|
|
@@ -116,7 +130,7 @@ export interface CaliperPrimitive {
|
|
|
116
130
|
}
|
|
117
131
|
|
|
118
132
|
/** A dimension annotation: extension lines, an offset dimension line, and its value as text. */
|
|
119
|
-
export interface DimensionPrimitive {
|
|
133
|
+
export interface DimensionPrimitive extends PrimitiveCommon {
|
|
120
134
|
/** Discriminant. */
|
|
121
135
|
kind: "dimension";
|
|
122
136
|
/** Image x of the first measured point. */
|
|
@@ -135,6 +149,25 @@ export interface DimensionPrimitive {
|
|
|
135
149
|
offset?: number;
|
|
136
150
|
}
|
|
137
151
|
|
|
152
|
+
/**
|
|
153
|
+
* A polyline through image points, e.g. a model's contour. It replaces one `segment` per
|
|
154
|
+
* edge, which runs to thousands of elements for a model outline.
|
|
155
|
+
*/
|
|
156
|
+
export interface PolylinePrimitive extends PrimitiveCommon {
|
|
157
|
+
/** Discriminant. */
|
|
158
|
+
kind: "polyline";
|
|
159
|
+
/** `[x0, y0, x1, y1, …]` in image coordinates. */
|
|
160
|
+
points: number[];
|
|
161
|
+
/** Join the last point to the first. */
|
|
162
|
+
closed?: boolean;
|
|
163
|
+
/** Verdict colour. Defaults to the neutral tone. */
|
|
164
|
+
tone?: MeasureTone;
|
|
165
|
+
/** A short dash pattern. */
|
|
166
|
+
dashed?: boolean;
|
|
167
|
+
/** Text drawn just above the first point. */
|
|
168
|
+
label?: string;
|
|
169
|
+
}
|
|
170
|
+
|
|
138
171
|
/** Anything `MeasureOverlay` can draw, discriminated by `kind`. */
|
|
139
172
|
export type MeasurePrimitive =
|
|
140
173
|
| PointPrimitive
|
|
@@ -142,7 +175,8 @@ export type MeasurePrimitive =
|
|
|
142
175
|
| CirclePrimitive
|
|
143
176
|
| ArcPrimitive
|
|
144
177
|
| CaliperPrimitive
|
|
145
|
-
| DimensionPrimitive
|
|
178
|
+
| DimensionPrimitive
|
|
179
|
+
| PolylinePrimitive;
|
|
146
180
|
|
|
147
181
|
const DEFAULT_LABEL_SIZE = 11;
|
|
148
182
|
const DEFAULT_POINT_RADIUS = 3;
|
|
@@ -169,12 +203,17 @@ export interface MeasureOverlayProps {
|
|
|
169
203
|
}
|
|
170
204
|
|
|
171
205
|
/**
|
|
172
|
-
* A vector overlay of measurement primitives — points, segments, circles, arcs,
|
|
173
|
-
* boxes, dimensions — drawn in source-image pixel coordinates.
|
|
206
|
+
* A vector overlay of measurement primitives — points, segments, polylines, circles, arcs,
|
|
207
|
+
* caliper boxes, dimensions — drawn in source-image pixel coordinates.
|
|
174
208
|
*
|
|
175
209
|
* Place it as a layer inside `ImageStage` so it moves with the image. It is a pure function
|
|
176
210
|
* of its props and decorative to assistive technology (`aria-hidden`), so a result drawn
|
|
177
211
|
* only here must also be stated in text.
|
|
212
|
+
*
|
|
213
|
+
* A primitive's colour is its `role` (overlay grammar) if given, else its verdict `tone`.
|
|
214
|
+
* Its `state` follows the overlay grammar: `hover` thickens it, `selected` thickens it and
|
|
215
|
+
* rings it in the selection colour, and `dimmed` fades it. Each primitive is a `<g>` carrying
|
|
216
|
+
* `data-kind`, plus `data-id` and `data-state` when given.
|
|
178
217
|
*/
|
|
179
218
|
export function MeasureOverlay({
|
|
180
219
|
nativeWidth,
|
|
@@ -195,36 +234,68 @@ export function MeasureOverlay({
|
|
|
195
234
|
preserveAspectRatio="none"
|
|
196
235
|
className={cn("pointer-events-none absolute inset-0 h-full w-full overflow-visible", className)}
|
|
197
236
|
>
|
|
198
|
-
{primitives.map((primitive, index) =>
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
237
|
+
{primitives.map((primitive, index) => {
|
|
238
|
+
const state = primitive.state ?? "default";
|
|
239
|
+
const colour = primitive.role ? overlayRole(primitive.role) : toneColor(primitive.tone);
|
|
240
|
+
// Hover and selected widen the strokes by the overlay grammar's ratios (2 and 2.5 to the
|
|
241
|
+
// default's 1.5).
|
|
242
|
+
const widen = state === "hover" ? 2 / 1.5 : state === "selected" ? 2.5 / 1.5 : 1;
|
|
243
|
+
return (
|
|
244
|
+
<g
|
|
245
|
+
// Primitives carry no required identity; a position is the only stable key.
|
|
246
|
+
// eslint-disable-next-line @eslint-react/no-array-index-key
|
|
247
|
+
key={index}
|
|
248
|
+
data-kind={primitive.kind}
|
|
249
|
+
data-id={primitive.id}
|
|
250
|
+
data-state={primitive.state}
|
|
251
|
+
opacity={OVERLAY_STATE_OPACITY[state] === 1 ? undefined : OVERLAY_STATE_OPACITY[state]}
|
|
252
|
+
>
|
|
253
|
+
{state === "selected" && (
|
|
254
|
+
<g opacity={0.6}>
|
|
255
|
+
<Primitive
|
|
256
|
+
primitive={primitive}
|
|
257
|
+
colour={overlayRole("selection")}
|
|
258
|
+
hairline={hairline * widen + strokeWidthFor(strokeScale, 3)}
|
|
259
|
+
thick={thick * widen + strokeWidthFor(strokeScale, 3)}
|
|
260
|
+
labelSize={0}
|
|
261
|
+
strokeScale={strokeScale}
|
|
262
|
+
ring
|
|
263
|
+
/>
|
|
264
|
+
</g>
|
|
265
|
+
)}
|
|
266
|
+
<Primitive
|
|
267
|
+
primitive={primitive}
|
|
268
|
+
colour={colour}
|
|
269
|
+
hairline={hairline * widen}
|
|
270
|
+
thick={thick * widen}
|
|
271
|
+
labelSize={labelSize}
|
|
272
|
+
strokeScale={strokeScale}
|
|
273
|
+
/>
|
|
274
|
+
</g>
|
|
275
|
+
);
|
|
276
|
+
})}
|
|
210
277
|
</svg>
|
|
211
278
|
);
|
|
212
279
|
}
|
|
213
280
|
|
|
214
281
|
function Primitive({
|
|
215
282
|
primitive,
|
|
283
|
+
colour,
|
|
216
284
|
hairline,
|
|
217
285
|
thick,
|
|
218
286
|
labelSize,
|
|
219
287
|
strokeScale,
|
|
288
|
+
ring = false,
|
|
220
289
|
}: {
|
|
221
290
|
primitive: MeasurePrimitive;
|
|
291
|
+
colour: string;
|
|
222
292
|
hairline: number;
|
|
223
293
|
thick: number;
|
|
224
294
|
labelSize: number;
|
|
225
295
|
strokeScale: number;
|
|
296
|
+
/** Drawn as the selection ring under the mark: no fill on a filled circle, a larger dot. */
|
|
297
|
+
ring?: boolean;
|
|
226
298
|
}) {
|
|
227
|
-
const colour = toneColor(primitive.tone);
|
|
228
299
|
|
|
229
300
|
switch (primitive.kind) {
|
|
230
301
|
case "point": {
|
|
@@ -239,7 +310,8 @@ function Primitive({
|
|
|
239
310
|
</g>
|
|
240
311
|
);
|
|
241
312
|
}
|
|
242
|
-
|
|
313
|
+
// The ring under a selected dot is the dot grown by 2 screen px.
|
|
314
|
+
const radius = strokeWidthFor(strokeScale, (primitive.radius ?? DEFAULT_POINT_RADIUS) + (ring ? 2 : 0));
|
|
243
315
|
return (
|
|
244
316
|
<g>
|
|
245
317
|
<circle cx={primitive.x} cy={primitive.y} r={radius} fill={colour} />
|
|
@@ -267,8 +339,8 @@ function Primitive({
|
|
|
267
339
|
cx={primitive.cx}
|
|
268
340
|
cy={primitive.cy}
|
|
269
341
|
r={primitive.r}
|
|
270
|
-
fill={primitive.filled ? colour : "none"}
|
|
271
|
-
stroke={primitive.filled ? "none" : colour}
|
|
342
|
+
fill={primitive.filled && !ring ? colour : "none"}
|
|
343
|
+
stroke={primitive.filled && !ring ? "none" : colour}
|
|
272
344
|
strokeWidth={hairline}
|
|
273
345
|
/>
|
|
274
346
|
);
|
|
@@ -302,6 +374,30 @@ function Primitive({
|
|
|
302
374
|
);
|
|
303
375
|
}
|
|
304
376
|
|
|
377
|
+
case "polyline":
|
|
378
|
+
return (
|
|
379
|
+
<g>
|
|
380
|
+
<path
|
|
381
|
+
d={polylinePath(primitive.points, primitive.closed === true)}
|
|
382
|
+
fill="none"
|
|
383
|
+
stroke={colour}
|
|
384
|
+
strokeWidth={hairline}
|
|
385
|
+
strokeLinecap="round"
|
|
386
|
+
strokeLinejoin="round"
|
|
387
|
+
strokeDasharray={primitive.dashed ? `${hairline * 4} ${hairline * 3}` : undefined}
|
|
388
|
+
/>
|
|
389
|
+
{primitive.points.length >= 2 && (
|
|
390
|
+
<Label
|
|
391
|
+
x={primitive.points[0]!}
|
|
392
|
+
y={primitive.points[1]! - strokeWidthFor(strokeScale, 4)}
|
|
393
|
+
text={primitive.label}
|
|
394
|
+
colour={colour}
|
|
395
|
+
size={labelSize}
|
|
396
|
+
/>
|
|
397
|
+
)}
|
|
398
|
+
</g>
|
|
399
|
+
);
|
|
400
|
+
|
|
305
401
|
case "dimension": {
|
|
306
402
|
const offset = primitive.offset ?? DEFAULT_DIMENSION_OFFSET;
|
|
307
403
|
const geometry = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, offset);
|
|
@@ -344,7 +440,7 @@ function Label({
|
|
|
344
440
|
colour: string;
|
|
345
441
|
size: number;
|
|
346
442
|
}) {
|
|
347
|
-
if (!text) return null;
|
|
443
|
+
if (!text || !(size > 0)) return null;
|
|
348
444
|
return (
|
|
349
445
|
<text x={x} y={y} fontSize={size} fill={colour} textAnchor="middle" dominantBaseline="text-after-edge">
|
|
350
446
|
{text}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Many polylines over the image, selectable: contours a model may be built from, a
|
|
3
|
+
* document's segments, a batch's matches.
|
|
4
|
+
*
|
|
5
|
+
* Drawn as a handful of batched paths, one per state and colour, not one element per line. The
|
|
6
|
+
* L0-3 benchmark shows the per-element form failing the 2 ms hit-test gate at scale, while the
|
|
7
|
+
* batched form plus a spatial index passes on every candidate engine
|
|
8
|
+
* (`docs/measurements/stage2d-bench-analysis.md`). The pointer is resolved to a line by
|
|
9
|
+
* `nearestPolyline`, not by the DOM, which keeps this layer engine-agnostic for L6-1.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useMemo, useState, type PointerEvent as ReactPointerEvent } from "react";
|
|
13
|
+
|
|
14
|
+
import type { Point } from "./measureGeometry";
|
|
15
|
+
import {
|
|
16
|
+
buildPolylineIndex,
|
|
17
|
+
nearestPolyline,
|
|
18
|
+
polylinePath,
|
|
19
|
+
polylinesInRect,
|
|
20
|
+
type Polyline,
|
|
21
|
+
type PolylineId,
|
|
22
|
+
} from "./polylineIndex";
|
|
23
|
+
import { overlayRole } from "./overlayRole";
|
|
24
|
+
import { useStage } from "./stage/ImageStage";
|
|
25
|
+
import { useStageDrag } from "./stage/StageSurface";
|
|
26
|
+
import { useScreenPx } from "./stage/useScreenPx";
|
|
27
|
+
import { imageViewBox, type Rect } from "./stage/view";
|
|
28
|
+
|
|
29
|
+
/** One polyline of a `PolylineSet`, with optional styling. */
|
|
30
|
+
export interface PolylineSetItem extends Polyline {
|
|
31
|
+
/** CSS colour for this line, instead of the set's `stroke`. */
|
|
32
|
+
stroke?: string | undefined;
|
|
33
|
+
/** Draw dashed: a line that is present but not in play (a dropped contour). */
|
|
34
|
+
dashed?: boolean | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** How a gesture combines with the current selection. */
|
|
38
|
+
export type PolylineSelectMode = "replace" | "toggle" | "add";
|
|
39
|
+
|
|
40
|
+
/** Props of `PolylineSet`. */
|
|
41
|
+
export interface PolylineSetProps {
|
|
42
|
+
/** The polylines, in image coordinates. */
|
|
43
|
+
items: readonly PolylineSetItem[];
|
|
44
|
+
/** The selected ids. */
|
|
45
|
+
selected?: Iterable<PolylineId> | undefined;
|
|
46
|
+
/** Ids drawn at 35 % opacity, e.g. outside the current filter. They still take hover and clicks. */
|
|
47
|
+
dimmed?: Iterable<PolylineId> | undefined;
|
|
48
|
+
/** The hovered id, when the app controls hover (e.g. a list beside the stage hovers lines too). */
|
|
49
|
+
hovered?: PolylineId | null | undefined;
|
|
50
|
+
/** Called when the line under the pointer changes. */
|
|
51
|
+
onHover?: ((id: PolylineId | null) => void) | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Called on a selection gesture.
|
|
54
|
+
* - A click on a line → `[id]`, "replace".
|
|
55
|
+
* - ⌘/Ctrl-click → `[id]`, "toggle".
|
|
56
|
+
* - A rubber band → every line it touches, "replace", or "add" with ⌘/Ctrl held. An empty
|
|
57
|
+
* band clears.
|
|
58
|
+
*/
|
|
59
|
+
onSelect?: ((ids: PolylineId[], mode: PolylineSelectMode) => void) | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* The sweep tool is active: a drag anywhere on the image draws a rubber band. Without it a
|
|
62
|
+
* band starts only with Shift held on a line, and a press on bare image is left to the stage
|
|
63
|
+
* and to the layers below.
|
|
64
|
+
*/
|
|
65
|
+
marquee?: boolean | undefined;
|
|
66
|
+
/** CSS colour of the lines. Defaults to the overlay `feature` role: they are detected lines. */
|
|
67
|
+
stroke?: string | undefined;
|
|
68
|
+
/** CSS colour of a selected line. Defaults to the overlay `selection` role. */
|
|
69
|
+
selectionStroke?: string | undefined;
|
|
70
|
+
/** How close the pointer must be to pick a line, in screen pixels (the hit band's full width). Defaults to 14. */
|
|
71
|
+
hitWidth?: number | undefined;
|
|
72
|
+
/** The zoom from which a hovered or selected line's points are drawn. Defaults to 3. */
|
|
73
|
+
vertexScale?: number | undefined;
|
|
74
|
+
/** The layer's accessible name. Defaults to "Lines". */
|
|
75
|
+
label?: string | undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const HALO = overlayRole("halo");
|
|
79
|
+
/** The most points drawn as dots at once; past this the dots are noise and a cost. */
|
|
80
|
+
const MAX_VERTEX_DOTS = 5000;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A selectable set of polylines inside an `ImageStage`.
|
|
84
|
+
*
|
|
85
|
+
* - **States**, from visual-language §5:
|
|
86
|
+
* - default 1.5 screen px;
|
|
87
|
+
* - hover 2 px;
|
|
88
|
+
* - selected 2.5 px in `selectionStroke`;
|
|
89
|
+
* - dimmed at 35 % opacity.
|
|
90
|
+
* - **Halo.** Every line has a halo, so it holds on any image.
|
|
91
|
+
* - **Points.** Above `vertexScale`, the points of the hovered and selected lines are drawn.
|
|
92
|
+
* - **Panning.** The hand tool and a held space bar still pan.
|
|
93
|
+
*
|
|
94
|
+
* The SVG carries `data-hovered` (the hovered id) and `data-sweeping` while a band is drawn.
|
|
95
|
+
*/
|
|
96
|
+
export function PolylineSet({
|
|
97
|
+
items,
|
|
98
|
+
selected,
|
|
99
|
+
dimmed,
|
|
100
|
+
hovered,
|
|
101
|
+
onHover,
|
|
102
|
+
onSelect,
|
|
103
|
+
marquee = false,
|
|
104
|
+
stroke = overlayRole("feature"),
|
|
105
|
+
selectionStroke = overlayRole("selection"),
|
|
106
|
+
hitWidth = 14,
|
|
107
|
+
vertexScale = 3,
|
|
108
|
+
label = "Lines",
|
|
109
|
+
}: PolylineSetProps) {
|
|
110
|
+
const stage = useStage();
|
|
111
|
+
const startDrag = useStageDrag();
|
|
112
|
+
const px = useScreenPx();
|
|
113
|
+
|
|
114
|
+
const index = useMemo(() => buildPolylineIndex(items), [items]);
|
|
115
|
+
const selectedSet = useMemo(() => new Set(selected ?? []), [selected]);
|
|
116
|
+
const dimmedSet = useMemo(() => new Set(dimmed ?? []), [dimmed]);
|
|
117
|
+
const byId = useMemo(() => new Map(items.map((item) => [item.id, item])), [items]);
|
|
118
|
+
|
|
119
|
+
const [ownHover, setOwnHover] = useState<PolylineId | null>(null);
|
|
120
|
+
const hoverId = hovered !== undefined ? hovered : ownHover;
|
|
121
|
+
const [band, setBand] = useState<Rect | null>(null);
|
|
122
|
+
|
|
123
|
+
// One path per (state, colour, dash) group, rebuilt only when the set or the selection
|
|
124
|
+
// changes. Hover is drawn on top, separately, so moving the pointer never rebuilds these.
|
|
125
|
+
const groups = useMemo(() => {
|
|
126
|
+
const map = new Map<string, { d: string[]; stroke: string; dashed: boolean; dim: boolean }>();
|
|
127
|
+
const hitParts: string[] = [];
|
|
128
|
+
for (const item of items) {
|
|
129
|
+
const d = polylinePath(item.points, item.closed === true);
|
|
130
|
+
if (d === "") continue;
|
|
131
|
+
hitParts.push(d);
|
|
132
|
+
if (selectedSet.has(item.id)) continue;
|
|
133
|
+
const colour = item.stroke ?? stroke;
|
|
134
|
+
const dashed = item.dashed === true;
|
|
135
|
+
const dim = dimmedSet.has(item.id);
|
|
136
|
+
const key = `${dim ? 1 : 0}|${dashed ? 1 : 0}|${colour}`;
|
|
137
|
+
let group = map.get(key);
|
|
138
|
+
if (!group) {
|
|
139
|
+
group = { d: [], stroke: colour, dashed, dim };
|
|
140
|
+
map.set(key, group);
|
|
141
|
+
}
|
|
142
|
+
group.d.push(d);
|
|
143
|
+
}
|
|
144
|
+
const selectedPath = items
|
|
145
|
+
.filter((item) => selectedSet.has(item.id))
|
|
146
|
+
.map((item) => polylinePath(item.points, item.closed === true))
|
|
147
|
+
.join("");
|
|
148
|
+
return {
|
|
149
|
+
batches: [...map.values()].map((g) => ({ ...g, path: g.d.join("") })),
|
|
150
|
+
hit: hitParts.join(""),
|
|
151
|
+
selected: selectedPath,
|
|
152
|
+
};
|
|
153
|
+
}, [items, selectedSet, dimmedSet, stroke]);
|
|
154
|
+
|
|
155
|
+
const hoverItem = hoverId !== null ? byId.get(hoverId) : undefined;
|
|
156
|
+
const hoverPath = hoverItem ? polylinePath(hoverItem.points, hoverItem.closed === true) : "";
|
|
157
|
+
|
|
158
|
+
const dots = useMemo(() => {
|
|
159
|
+
if (stage.view.scale < vertexScale) return "";
|
|
160
|
+
const shown: PolylineSetItem[] = [];
|
|
161
|
+
if (hoverItem) shown.push(hoverItem);
|
|
162
|
+
for (const item of items) if (selectedSet.has(item.id) && item !== hoverItem) shown.push(item);
|
|
163
|
+
let d = "";
|
|
164
|
+
let count = 0;
|
|
165
|
+
for (const item of shown) {
|
|
166
|
+
const p = item.points;
|
|
167
|
+
for (let i = 0; i + 1 < p.length && count < MAX_VERTEX_DOTS; i += 2, count++) {
|
|
168
|
+
d += `M${p[i]} ${p[i + 1]}h0`;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return d;
|
|
172
|
+
}, [stage.view.scale, vertexScale, hoverItem, items, selectedSet]);
|
|
173
|
+
|
|
174
|
+
const pickAt = (event: { clientX: number; clientY: number }): PolylineId | null =>
|
|
175
|
+
nearestPolyline(index, stage.toImage({ x: event.clientX, y: event.clientY }), px(hitWidth / 2))?.id ?? null;
|
|
176
|
+
|
|
177
|
+
const setHover = (id: PolylineId | null) => {
|
|
178
|
+
if (id === hoverId) return;
|
|
179
|
+
setOwnHover(id);
|
|
180
|
+
onHover?.(id);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const sweep = (event: ReactPointerEvent<SVGElement>) => {
|
|
184
|
+
const from = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
185
|
+
const additive = event.metaKey || event.ctrlKey;
|
|
186
|
+
setBand({ x: from.x, y: from.y, width: 0, height: 0 });
|
|
187
|
+
setHover(null);
|
|
188
|
+
startDrag(event, {
|
|
189
|
+
onMove: (p) => setBand(boxBetween(from, p)),
|
|
190
|
+
onEnd: (p) => {
|
|
191
|
+
setBand(null);
|
|
192
|
+
onSelect?.(polylinesInRect(index, boxBetween(from, p)), additive ? "add" : "replace");
|
|
193
|
+
},
|
|
194
|
+
onCancel: () => setBand(null),
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const onLinePointerDown = (event: ReactPointerEvent<SVGPathElement>) => {
|
|
199
|
+
if (stage.panMode || event.button !== 0) return;
|
|
200
|
+
// A band has to be able to start anywhere, and on a frame that is mostly lines "anywhere"
|
|
201
|
+
// is usually on one. Declining here would hand the press to the stage (which pans), not to
|
|
202
|
+
// a surface below, because only the topmost element is a press's target.
|
|
203
|
+
if (event.shiftKey || marquee) {
|
|
204
|
+
sweep(event);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const id = pickAt(event);
|
|
208
|
+
if (id === null) return;
|
|
209
|
+
// Claimed, and selected on the press: a selection that waits for the release feels like
|
|
210
|
+
// lag on a canvas where every other gesture is immediate.
|
|
211
|
+
event.stopPropagation();
|
|
212
|
+
onSelect?.([id], event.metaKey || event.ctrlKey ? "toggle" : "replace");
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const hairline = px(1.5);
|
|
216
|
+
return (
|
|
217
|
+
<svg
|
|
218
|
+
viewBox={imageViewBox(stage.image)}
|
|
219
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
220
|
+
role="img"
|
|
221
|
+
aria-label={`${label}: ${items.length} ${items.length === 1 ? "line" : "lines"}, ${selectedSet.size} selected`}
|
|
222
|
+
data-hovered={hoverId ?? undefined}
|
|
223
|
+
data-sweeping={band ? "" : undefined}
|
|
224
|
+
>
|
|
225
|
+
{marquee && (
|
|
226
|
+
<rect
|
|
227
|
+
data-marquee-surface=""
|
|
228
|
+
x={-0.5}
|
|
229
|
+
y={-0.5}
|
|
230
|
+
width={stage.image.width}
|
|
231
|
+
height={stage.image.height}
|
|
232
|
+
fill="transparent"
|
|
233
|
+
className="pointer-events-auto cursor-crosshair"
|
|
234
|
+
onPointerDown={(event) => {
|
|
235
|
+
if (stage.panMode || event.button !== 0) return;
|
|
236
|
+
sweep(event);
|
|
237
|
+
}}
|
|
238
|
+
/>
|
|
239
|
+
)}
|
|
240
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
241
|
+
<path d={groups.hit} stroke={HALO} strokeWidth={px(1.5 + 2)} opacity={0.6} />
|
|
242
|
+
{groups.batches.map((batch) => (
|
|
243
|
+
<path
|
|
244
|
+
key={`${batch.dim}|${batch.dashed}|${batch.stroke}`}
|
|
245
|
+
d={batch.path}
|
|
246
|
+
stroke={batch.stroke}
|
|
247
|
+
strokeWidth={hairline}
|
|
248
|
+
strokeDasharray={batch.dashed ? `${px(4)} ${px(4)}` : undefined}
|
|
249
|
+
opacity={batch.dim ? 0.35 : 1}
|
|
250
|
+
/>
|
|
251
|
+
))}
|
|
252
|
+
{groups.selected && (
|
|
253
|
+
<>
|
|
254
|
+
<path d={groups.selected} stroke={HALO} strokeWidth={px(2.5 + 2)} />
|
|
255
|
+
<path data-selected-lines="" d={groups.selected} stroke={selectionStroke} strokeWidth={px(2.5)} />
|
|
256
|
+
</>
|
|
257
|
+
)}
|
|
258
|
+
{hoverPath && !band && (
|
|
259
|
+
<>
|
|
260
|
+
<path d={hoverPath} stroke={HALO} strokeWidth={px(2 + 2)} />
|
|
261
|
+
<path
|
|
262
|
+
data-hovered-line=""
|
|
263
|
+
d={hoverPath}
|
|
264
|
+
stroke={hoverItem && selectedSet.has(hoverItem.id) ? selectionStroke : (hoverItem?.stroke ?? stroke)}
|
|
265
|
+
strokeWidth={px(hoverItem && selectedSet.has(hoverItem.id) ? 2.5 : 2)}
|
|
266
|
+
/>
|
|
267
|
+
</>
|
|
268
|
+
)}
|
|
269
|
+
{dots && <path data-points="" d={dots} stroke={selectionStroke} strokeWidth={px(3)} />}
|
|
270
|
+
{/* The one hit target: every line, transparent, wide. Which line it was is the
|
|
271
|
+
index's answer, not the DOM's. */}
|
|
272
|
+
<path
|
|
273
|
+
data-hit=""
|
|
274
|
+
d={groups.hit}
|
|
275
|
+
stroke="transparent"
|
|
276
|
+
strokeWidth={px(hitWidth)}
|
|
277
|
+
style={{ pointerEvents: "stroke", cursor: stage.panMode ? undefined : marquee ? "crosshair" : "pointer" }}
|
|
278
|
+
onPointerDown={onLinePointerDown}
|
|
279
|
+
onPointerMove={(event) => {
|
|
280
|
+
if (!band) setHover(pickAt(event));
|
|
281
|
+
}}
|
|
282
|
+
onPointerLeave={() => setHover(null)}
|
|
283
|
+
/>
|
|
284
|
+
</g>
|
|
285
|
+
{band && (
|
|
286
|
+
<rect
|
|
287
|
+
x={band.x}
|
|
288
|
+
y={band.y}
|
|
289
|
+
width={band.width}
|
|
290
|
+
height={band.height}
|
|
291
|
+
fill={selectionStroke}
|
|
292
|
+
fillOpacity={0.12}
|
|
293
|
+
stroke={selectionStroke}
|
|
294
|
+
strokeWidth={px(1)}
|
|
295
|
+
/>
|
|
296
|
+
)}
|
|
297
|
+
</svg>
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function boxBetween(a: Point, b: Point): Rect {
|
|
302
|
+
return {
|
|
303
|
+
x: Math.min(a.x, b.x),
|
|
304
|
+
y: Math.min(a.y, b.y),
|
|
305
|
+
width: Math.abs(b.x - a.x),
|
|
306
|
+
height: Math.abs(b.y - a.y),
|
|
307
|
+
};
|
|
308
|
+
}
|