@vitavision/stage2d 0.10.0 → 0.12.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 +12 -3
- package/dist/index.d.ts +437 -36
- package/dist/index.js +1406 -338
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/AreaSet.tsx +83 -52
- package/src/components/ContourEditor.tsx +12 -3
- package/src/components/DraftShape.tsx +56 -11
- package/src/components/EllipseSet.tsx +292 -0
- package/src/components/MaskEditor.tsx +22 -9
- package/src/components/PointSet.tsx +19 -9
- package/src/components/PolylineSet.tsx +56 -15
- package/src/components/RectRoiEditor.tsx +5 -4
- package/src/components/ShapeEditor.tsx +117 -37
- package/src/components/areaBatching.ts +111 -0
- package/src/components/areaIndex.ts +7 -4
- package/src/components/ellipseIndex.ts +279 -0
- package/src/components/markerShapes.ts +3 -1
- package/src/components/shapeEdit.ts +67 -1
- package/src/components/stage/ImageStage.tsx +250 -127
- package/src/components/stage/StageSurface.tsx +117 -17
- package/src/components/stage/gesture.ts +15 -0
- package/src/components/stage/touchWatch.ts +85 -0
- package/src/components/stage/useCoarsePointer.ts +30 -0
- package/src/components/stage/view.ts +88 -20
- package/src/index.ts +20 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Many ellipses over the image whose size is data, hoverable and selectable: fitted ring
|
|
3
|
+
* edges, edge-bit dots, drawn ellipse annotations.
|
|
4
|
+
*
|
|
5
|
+
* A `MarkerShape` is sized in screen pixels, so it cannot draw a ring whose radius is a
|
|
6
|
+
* measurement. This layer draws exact ellipses (two arcs each) in image pixels, batched by
|
|
7
|
+
* appearance into a handful of `<path>`s (ADR-0004 rule 1). Strokes stay screen-sized (rule 4).
|
|
8
|
+
* The pointer is resolved by `nearestEllipse` through the stage's hit registry, never by the
|
|
9
|
+
* DOM (rule 2); `pickable={false}` leaves a decorative layer out of it.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useMemo, useState } from "react";
|
|
13
|
+
|
|
14
|
+
import { buildEllipseIndex, ellipsePath, nearestEllipse, type Ellipse, type EllipseId, type EllipseIndex } from "./ellipseIndex";
|
|
15
|
+
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
16
|
+
import { useStage } from "./stage/ImageStage";
|
|
17
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
18
|
+
import { STAGE_HIT_PRIORITY } from "./stage/hitTest";
|
|
19
|
+
import { useStageHitLayer } from "./stage/useStageHitTest";
|
|
20
|
+
import { useScreenPx } from "./stage/useScreenPx";
|
|
21
|
+
import { imageViewBox } from "./stage/view";
|
|
22
|
+
|
|
23
|
+
/** One ellipse of an `EllipseSet`. */
|
|
24
|
+
export interface EllipseSetItem extends Ellipse {
|
|
25
|
+
/** The overlay role it is painted in. Defaults to the layer's `role`. */
|
|
26
|
+
role?: OverlayRole | undefined;
|
|
27
|
+
/** Draw it dashed (4 3 screen px), so two kinds differ by more than colour. */
|
|
28
|
+
dashed?: boolean | undefined;
|
|
29
|
+
/** Opacity in [0, 1], rounded to a tenth so ellipses still share paths. Defaults to 1. */
|
|
30
|
+
opacity?: number | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Props of `EllipseSet`. */
|
|
34
|
+
export interface EllipseSetProps {
|
|
35
|
+
/** The ellipses, in image coordinates. */
|
|
36
|
+
items: readonly EllipseSetItem[];
|
|
37
|
+
/** The role of an item that names none. Defaults to `"feature"`. */
|
|
38
|
+
role?: OverlayRole | undefined;
|
|
39
|
+
/** The hovered id, when the app controls hover: its ellipses are drawn at 2 px. */
|
|
40
|
+
hoveredId?: EllipseId | null | undefined;
|
|
41
|
+
/** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called when `pickable` is off. */
|
|
42
|
+
onHoverChange?: ((id: EllipseId | null) => void) | undefined;
|
|
43
|
+
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
44
|
+
selectedIds?: Iterable<EllipseId> | undefined;
|
|
45
|
+
/** Ids drawn at 35 % opacity, as a set or a predicate. Pass a stable value: a new one re-batches the layer. */
|
|
46
|
+
dimmed?: Iterable<EllipseId> | ((id: EllipseId) => boolean) | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* A press landed on an ellipse. Setting it makes the layer claim presses on its ellipses,
|
|
49
|
+
* so the stage does not pan from them. The event is the `pointerdown` (the `pointerup` for
|
|
50
|
+
* a touch tap), so `useShapeDrag` can start a drag from it.
|
|
51
|
+
*
|
|
52
|
+
* A mounted `StageSurface` hears a press first; there, ask `useStageHitTest` what is
|
|
53
|
+
* under it instead.
|
|
54
|
+
*/
|
|
55
|
+
onItemPress?: ((id: EllipseId, event: StagePointerEvent) => void) | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
|
|
58
|
+
* decorative layer (the ring outlines under a `PointSet` of centres), so it neither
|
|
59
|
+
* hovers, nor claims presses, nor answers `useStageHitTest`.
|
|
60
|
+
*/
|
|
61
|
+
pickable?: boolean | undefined;
|
|
62
|
+
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
63
|
+
layerId?: string | undefined;
|
|
64
|
+
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.area`. */
|
|
65
|
+
priority?: number | undefined;
|
|
66
|
+
/** Draw the dark band under every stroke. On by default. */
|
|
67
|
+
halo?: boolean | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* The smallest semi-axis, in screen pixels: a dot whose data size would vanish at the fitted
|
|
70
|
+
* zoom is still drawn this large. Omit for none.
|
|
71
|
+
*/
|
|
72
|
+
minRadius?: number | undefined;
|
|
73
|
+
/** CSS colour of selected ellipses. Defaults to the overlay `selection` role. */
|
|
74
|
+
selectionStroke?: string | undefined;
|
|
75
|
+
/** Name the layer as an image for assistive technology. Without it the layer is decorative (`aria-hidden`). */
|
|
76
|
+
label?: string | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const HALO = overlayRole("halo");
|
|
80
|
+
|
|
81
|
+
/** The outline of an item, its semi-axes raised to `floor` image pixels. */
|
|
82
|
+
function outlineOf(item: EllipseSetItem, floor: number): string {
|
|
83
|
+
return ellipsePath(item.x, item.y, Math.max(item.rx, floor), Math.max(item.ry, floor), item.angle ?? 0);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The concatenated outlines of the items at `indices`. */
|
|
87
|
+
function batchPath(items: readonly EllipseSetItem[], indices: readonly number[], floor: number): string {
|
|
88
|
+
let d = "";
|
|
89
|
+
for (const i of indices) d += outlineOf(items[i]!, floor);
|
|
90
|
+
return d;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The layer's entry in the stage's hit registry: a component, not a hook call in
|
|
95
|
+
* `EllipseSet`, so `pickable={false}` really unregisters it. Renders nothing.
|
|
96
|
+
*/
|
|
97
|
+
function EllipseHits({
|
|
98
|
+
index,
|
|
99
|
+
layerId,
|
|
100
|
+
priority,
|
|
101
|
+
onHover,
|
|
102
|
+
onPress,
|
|
103
|
+
}: {
|
|
104
|
+
index: EllipseIndex;
|
|
105
|
+
layerId: string | undefined;
|
|
106
|
+
priority: number;
|
|
107
|
+
onHover: (id: EllipseId | null) => void;
|
|
108
|
+
onPress: ((id: EllipseId, event: StagePointerEvent) => void) | undefined;
|
|
109
|
+
}) {
|
|
110
|
+
useStageHitLayer({
|
|
111
|
+
layerId,
|
|
112
|
+
priority,
|
|
113
|
+
pick: (point, radius) => {
|
|
114
|
+
const hit = nearestEllipse(index, point, radius);
|
|
115
|
+
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
116
|
+
},
|
|
117
|
+
onHover,
|
|
118
|
+
onPress: onPress ? (id, event) => onPress(id, event) : undefined,
|
|
119
|
+
});
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The ellipses of one batched path. */
|
|
124
|
+
interface Batch {
|
|
125
|
+
key: string;
|
|
126
|
+
role: OverlayRole;
|
|
127
|
+
dashed: boolean;
|
|
128
|
+
opacity: number;
|
|
129
|
+
dim: boolean;
|
|
130
|
+
/** Item positions, ascending. */
|
|
131
|
+
indices: number[];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A batched set of ellipses inside an `ImageStage`.
|
|
136
|
+
*
|
|
137
|
+
* - **Geometry** is data: `rx`, `ry` and the centre are image pixels and scale with the image;
|
|
138
|
+
* the stroke is a screen constant.
|
|
139
|
+
* - **States** follow visual-language §5: 1.5 screen px (1 for `model` and `structure`), hover
|
|
140
|
+
* 2, selected 2.5 in the selection colour, dimmed at 35 % opacity. Every stroke has a halo.
|
|
141
|
+
* - **Batching** is by (role, dash, opacity, dimming): a thousand rings are a dozen elements.
|
|
142
|
+
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
143
|
+
* an outline picks that ellipse, else the smallest ellipse containing the point.
|
|
144
|
+
* `onHoverChange` and `onItemPress` here, and `useStageHitTest` for an app that arbitrates
|
|
145
|
+
* between layers. `pickable={false}` turns it off.
|
|
146
|
+
*
|
|
147
|
+
* The SVG carries `data-ellipses` (the count) and `data-hovered` (the hovered id).
|
|
148
|
+
*/
|
|
149
|
+
export function EllipseSet({
|
|
150
|
+
items,
|
|
151
|
+
role = "feature",
|
|
152
|
+
hoveredId,
|
|
153
|
+
onHoverChange,
|
|
154
|
+
selectedIds,
|
|
155
|
+
dimmed,
|
|
156
|
+
onItemPress,
|
|
157
|
+
pickable = true,
|
|
158
|
+
layerId,
|
|
159
|
+
priority = STAGE_HIT_PRIORITY.area,
|
|
160
|
+
halo = true,
|
|
161
|
+
minRadius,
|
|
162
|
+
selectionStroke = overlayRole("selection"),
|
|
163
|
+
label,
|
|
164
|
+
}: EllipseSetProps) {
|
|
165
|
+
const stage = useStage();
|
|
166
|
+
const px = useScreenPx();
|
|
167
|
+
const unit = px(1);
|
|
168
|
+
|
|
169
|
+
const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
|
|
170
|
+
const isDimmed = useMemo(() => {
|
|
171
|
+
if (typeof dimmed === "function") return dimmed;
|
|
172
|
+
const set = new Set(dimmed ?? []);
|
|
173
|
+
return (id: EllipseId) => set.has(id);
|
|
174
|
+
}, [dimmed]);
|
|
175
|
+
|
|
176
|
+
const index = useMemo(() => (pickable ? buildEllipseIndex(items) : null), [items, pickable]);
|
|
177
|
+
const [ownHover, setOwnHover] = useState<EllipseId | null>(null);
|
|
178
|
+
const hoverId = hoveredId !== undefined ? hoveredId : pickable ? ownHover : null;
|
|
179
|
+
|
|
180
|
+
const batches = useMemo(() => {
|
|
181
|
+
const normal = new Map<string, Batch>();
|
|
182
|
+
const picked: Batch = { key: "selected", role: "selection", dashed: false, opacity: 1, dim: false, indices: [] };
|
|
183
|
+
items.forEach((item, i) => {
|
|
184
|
+
if (selectedSet.has(item.id)) {
|
|
185
|
+
picked.indices.push(i);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const itemRole = item.role ?? role;
|
|
189
|
+
const dashed = item.dashed ?? false;
|
|
190
|
+
const opacity = Math.round(Math.min(1, Math.max(0, item.opacity ?? 1)) * 10) / 10;
|
|
191
|
+
const dim = isDimmed(item.id);
|
|
192
|
+
const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}`;
|
|
193
|
+
let batch = normal.get(key);
|
|
194
|
+
if (!batch) {
|
|
195
|
+
batch = { key, role: itemRole, dashed, opacity, dim, indices: [] };
|
|
196
|
+
normal.set(key, batch);
|
|
197
|
+
}
|
|
198
|
+
batch.indices.push(i);
|
|
199
|
+
});
|
|
200
|
+
return { normal: [...normal.values()], picked: picked.indices.length > 0 ? picked : null };
|
|
201
|
+
}, [items, role, selectedSet, isDimmed]);
|
|
202
|
+
|
|
203
|
+
// Outlines depend on the zoom only through the screen-pixel floor.
|
|
204
|
+
const floor = minRadius !== undefined ? minRadius * unit : 0;
|
|
205
|
+
const normalPaths = useMemo(
|
|
206
|
+
() => batches.normal.map((batch) => ({ batch, d: batchPath(items, batch.indices, floor) })),
|
|
207
|
+
[batches, items, floor],
|
|
208
|
+
);
|
|
209
|
+
const pickedPath = useMemo(() => (batches.picked ? batchPath(items, batches.picked.indices, floor) : ""), [batches, items, floor]);
|
|
210
|
+
|
|
211
|
+
const hoverPositions: number[] = [];
|
|
212
|
+
if (hoverId !== null) {
|
|
213
|
+
items.forEach((item, i) => {
|
|
214
|
+
if (item.id === hoverId) hoverPositions.push(i);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
const hoverSelected = hoverId !== null && selectedSet.has(hoverId);
|
|
218
|
+
|
|
219
|
+
const widthOf = (itemRole: OverlayRole, state: "default" | "hover" | "selected"): number => {
|
|
220
|
+
const thin = itemRole === "model" || itemRole === "structure";
|
|
221
|
+
return px(OVERLAY_STATE_WIDTH[state] - (thin && state !== "selected" ? 0.5 : 0));
|
|
222
|
+
};
|
|
223
|
+
const dash = `${px(4)} ${px(3)}`;
|
|
224
|
+
|
|
225
|
+
return (
|
|
226
|
+
<svg
|
|
227
|
+
viewBox={imageViewBox(stage.image)}
|
|
228
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
229
|
+
{...(label !== undefined ? { role: "img", "aria-label": label } : { "aria-hidden": true })}
|
|
230
|
+
data-ellipses={items.length}
|
|
231
|
+
data-hovered={hoverId ?? undefined}
|
|
232
|
+
>
|
|
233
|
+
{index && (
|
|
234
|
+
<EllipseHits
|
|
235
|
+
index={index}
|
|
236
|
+
layerId={layerId}
|
|
237
|
+
priority={priority}
|
|
238
|
+
onHover={(id) => {
|
|
239
|
+
setOwnHover(id);
|
|
240
|
+
onHoverChange?.(id);
|
|
241
|
+
}}
|
|
242
|
+
onPress={onItemPress}
|
|
243
|
+
/>
|
|
244
|
+
)}
|
|
245
|
+
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
246
|
+
{normalPaths.map(({ batch, d }) => (
|
|
247
|
+
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
248
|
+
{halo && (
|
|
249
|
+
<path
|
|
250
|
+
d={d}
|
|
251
|
+
stroke={HALO}
|
|
252
|
+
strokeWidth={widthOf(batch.role, "default") + px(2)}
|
|
253
|
+
strokeDasharray={batch.dashed ? dash : undefined}
|
|
254
|
+
opacity={0.6 * batch.opacity}
|
|
255
|
+
/>
|
|
256
|
+
)}
|
|
257
|
+
<path
|
|
258
|
+
data-batch={batch.key}
|
|
259
|
+
d={d}
|
|
260
|
+
stroke={overlayRole(batch.role)}
|
|
261
|
+
strokeWidth={widthOf(batch.role, "default")}
|
|
262
|
+
strokeDasharray={batch.dashed ? dash : undefined}
|
|
263
|
+
opacity={batch.opacity}
|
|
264
|
+
/>
|
|
265
|
+
</g>
|
|
266
|
+
))}
|
|
267
|
+
{batches.picked && (
|
|
268
|
+
<>
|
|
269
|
+
{halo && <path d={pickedPath} stroke={HALO} strokeWidth={widthOf("selection", "selected") + px(2)} />}
|
|
270
|
+
<path data-selected-ellipses="" d={pickedPath} stroke={selectionStroke} strokeWidth={widthOf("selection", "selected")} />
|
|
271
|
+
</>
|
|
272
|
+
)}
|
|
273
|
+
{hoverPositions.map((position) => {
|
|
274
|
+
const item = items[position]!;
|
|
275
|
+
const itemRole = item.role ?? role;
|
|
276
|
+
const state = hoverSelected ? "selected" : "hover";
|
|
277
|
+
return (
|
|
278
|
+
<g key={position}>
|
|
279
|
+
{halo && <path d={outlineOf(item, floor)} stroke={HALO} strokeWidth={widthOf(itemRole, state) + px(2)} />}
|
|
280
|
+
<path
|
|
281
|
+
data-hovered-ellipse=""
|
|
282
|
+
d={outlineOf(item, floor)}
|
|
283
|
+
stroke={hoverSelected ? selectionStroke : overlayRole(itemRole)}
|
|
284
|
+
strokeWidth={widthOf(itemRole, state)}
|
|
285
|
+
/>
|
|
286
|
+
</g>
|
|
287
|
+
);
|
|
288
|
+
})}
|
|
289
|
+
</g>
|
|
290
|
+
</svg>
|
|
291
|
+
);
|
|
292
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { useEffect, useRef, useState, type FocusEvent, type KeyboardEvent, type PointerEvent } from "react";
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { DraftShape } from "./DraftShape";
|
|
6
6
|
import { useStage } from "./stage/ImageStage";
|
|
7
7
|
import type { Point } from "./measureGeometry";
|
|
8
8
|
|
|
@@ -49,7 +49,11 @@ export interface MaskEditorProps {
|
|
|
49
49
|
color?: readonly [number, number, number, number];
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Render and edit a binary raster mask within the enclosing ImageStage. While editing, a
|
|
54
|
+
* `DraftShape` brush footprint (a circle of the brush's diameter) follows the mouse or pen
|
|
55
|
+
* pointer, and the keyboard brush while the layer has keyboard focus.
|
|
56
|
+
*/
|
|
53
57
|
export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRadius = 3, mode = "paint", label = "Mask brush", color = [0, 190, 220, 128] }: MaskEditorProps) {
|
|
54
58
|
const stage = useStage();
|
|
55
59
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
@@ -59,6 +63,8 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
59
63
|
const cursorRef = useRef<Point | null>(null);
|
|
60
64
|
// Where the keyboard brush is drawn, while the layer has keyboard focus.
|
|
61
65
|
const [brush, setBrush] = useState<Point | null>(null);
|
|
66
|
+
// Where the pointer hovers (or paints); the footprint follows it. Not set for touch.
|
|
67
|
+
const [hover, setHover] = useState<Point | null>(null);
|
|
62
68
|
|
|
63
69
|
function cursor(): Point {
|
|
64
70
|
return cursorRef.current ?? { x: Math.floor(stage.image.width / 2), y: Math.floor(stage.image.height / 2) };
|
|
@@ -91,13 +97,21 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
91
97
|
lastRef.current = point;
|
|
92
98
|
cursorRef.current = point;
|
|
93
99
|
setBrush(null);
|
|
100
|
+
if (event.pointerType !== "touch") setHover(point);
|
|
94
101
|
apply(point, point);
|
|
95
102
|
}
|
|
96
103
|
|
|
97
104
|
function move(event: PointerEvent<HTMLCanvasElement>) {
|
|
98
|
-
if (!lastRef.current)
|
|
105
|
+
if (!lastRef.current) {
|
|
106
|
+
if (editable && !stage.panMode && event.pointerType !== "touch") {
|
|
107
|
+
setHover(stage.toImage({ x: event.clientX, y: event.clientY }));
|
|
108
|
+
setBrush(null);
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
99
112
|
event.stopPropagation();
|
|
100
113
|
const point = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
114
|
+
if (event.pointerType !== "touch") setHover(point);
|
|
101
115
|
apply(lastRef.current, point);
|
|
102
116
|
lastRef.current = point;
|
|
103
117
|
cursorRef.current = point;
|
|
@@ -107,6 +121,7 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
107
121
|
if (!lastRef.current) return;
|
|
108
122
|
event.stopPropagation();
|
|
109
123
|
lastRef.current = null;
|
|
124
|
+
if (event.pointerType === "touch") setHover(null);
|
|
110
125
|
onCommit?.();
|
|
111
126
|
}
|
|
112
127
|
|
|
@@ -126,12 +141,14 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
126
141
|
y: Math.max(0, Math.min(stage.image.height - 1, from.y + delta.y)),
|
|
127
142
|
};
|
|
128
143
|
setBrush(cursorRef.current);
|
|
144
|
+
setHover(null);
|
|
129
145
|
} else if (event.key === "Enter") {
|
|
130
146
|
event.preventDefault();
|
|
131
147
|
event.stopPropagation();
|
|
132
148
|
const at = cursor();
|
|
133
149
|
draftRef.current = mask;
|
|
134
150
|
setBrush(at);
|
|
151
|
+
setHover(null);
|
|
135
152
|
apply(at, at);
|
|
136
153
|
onCommit?.();
|
|
137
154
|
}
|
|
@@ -143,12 +160,8 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
143
160
|
|
|
144
161
|
return (
|
|
145
162
|
<>
|
|
146
|
-
<canvas ref={canvasRef} width={stage.image.width} height={stage.image.height} className={`absolute inset-0 h-full w-full ${editable ? "pointer-events-auto cursor-crosshair" : "pointer-events-none"}`} style={{ imageRendering: "pixelated" }} role={editable ? "button" : "img"} tabIndex={editable ? 0 : undefined} aria-label={label} aria-description={editable ? "Arrow keys move the brush; Enter paints or erases." : undefined} onPointerDown={down} onPointerMove={move} onPointerUp={up} onLostPointerCapture={up} onDoubleClick={(event) => { if (editable && !stage.panMode) event.stopPropagation(); }} onKeyDown={keyDown} onFocus={focus} onBlur={() => setBrush(null)} />
|
|
147
|
-
{brush && (
|
|
148
|
-
<svg viewBox={imageViewBox(stage.image)} className="pointer-events-none absolute inset-0 h-full w-full overflow-visible" aria-hidden="true">
|
|
149
|
-
<circle cx={brush.x} cy={brush.y} r={Math.max(0.75, brushRadius)} fill="none" stroke="var(--signal)" strokeWidth={stage.imageLength(1.5)} />
|
|
150
|
-
</svg>
|
|
151
|
-
)}
|
|
163
|
+
<canvas ref={canvasRef} width={stage.image.width} height={stage.image.height} className={`absolute inset-0 h-full w-full ${editable ? "pointer-events-auto cursor-crosshair" : "pointer-events-none"}`} style={{ imageRendering: "pixelated" }} role={editable ? "button" : "img"} tabIndex={editable ? 0 : undefined} aria-label={label} aria-description={editable ? "Arrow keys move the brush; Enter paints or erases." : undefined} onPointerDown={down} onPointerMove={move} onPointerUp={up} onLostPointerCapture={up} onDoubleClick={(event) => { if (editable && !stage.panMode) event.stopPropagation(); }} onKeyDown={keyDown} onFocus={focus} onBlur={() => setBrush(null)} onPointerLeave={() => { if (!lastRef.current) setHover(null); }} />
|
|
164
|
+
{(hover ?? brush) && editable && <DraftShape shape={{ kind: "brush", x: (hover ?? brush)!.x, y: (hover ?? brush)!.y, diameter: 2 * Math.max(0.75, brushRadius) }} stroke="var(--signal)" />}
|
|
152
165
|
</>
|
|
153
166
|
);
|
|
154
167
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Many points over the image, hoverable and selectable: detected corners, ring centres,
|
|
3
3
|
* keypoints, labelled landmarks.
|
|
4
4
|
*
|
|
5
|
-
* Drawn as a handful of batched paths, one per (state, style, marker kind), never one element
|
|
5
|
+
* Drawn as a handful of batched paths, one per (state, style, colour, marker kind), never one element
|
|
6
6
|
* per point (ADR-0004 rules 1 and 3); the pointer is resolved by `nearestPoint` through the
|
|
7
7
|
* stage's hit registry, never by the DOM (rule 2); markers are sized in screen pixels (rule
|
|
8
8
|
* 4). A scene of 20,000 points is under a dozen DOM nodes plus at most 200 labels.
|
|
@@ -30,8 +30,15 @@ export interface PointSetItem extends PointItem {
|
|
|
30
30
|
kind?: string | undefined;
|
|
31
31
|
/** The overlay role it is painted in. Defaults to the marker kind's role, usually `feature`. */
|
|
32
32
|
role?: OverlayRole | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* CSS colour of its mark (`#hex`, `var(--x)`, `color-mix(…)`; used as given). Overrides the
|
|
35
|
+
* role; selection still paints in `selectionStroke`. Points sharing a colour share a batch.
|
|
36
|
+
*/
|
|
37
|
+
color?: string | undefined;
|
|
33
38
|
/** Its orientation in radians, clockwise from +x; read by `directed`. */
|
|
34
39
|
angle?: number | undefined;
|
|
40
|
+
/** A second angle in radians, for a glyph with two axes. `MarkerShape.path` gets `undefined` without it. */
|
|
41
|
+
angle2?: number | undefined;
|
|
35
42
|
/** Text drawn beside it, when the zoom leaves room (points at least 24 screen px apart). */
|
|
36
43
|
label?: string | undefined;
|
|
37
44
|
}
|
|
@@ -91,6 +98,8 @@ interface Batch {
|
|
|
91
98
|
key: string;
|
|
92
99
|
shape: MarkerShape;
|
|
93
100
|
role: OverlayRole;
|
|
101
|
+
/** The custom colour, when the items of this batch have one. */
|
|
102
|
+
color: string | undefined;
|
|
94
103
|
dim: boolean;
|
|
95
104
|
/** Item positions, ascending. */
|
|
96
105
|
indices: number[];
|
|
@@ -174,7 +183,7 @@ export function PointSet({
|
|
|
174
183
|
const key = kindName;
|
|
175
184
|
let batch = picked.get(key);
|
|
176
185
|
if (!batch) {
|
|
177
|
-
batch = { key, shape, role: "selection", dim: false, indices: [], cache: null };
|
|
186
|
+
batch = { key, shape, role: "selection", color: undefined, dim: false, indices: [], cache: null };
|
|
178
187
|
picked.set(key, batch);
|
|
179
188
|
}
|
|
180
189
|
batch.indices.push(i);
|
|
@@ -182,10 +191,11 @@ export function PointSet({
|
|
|
182
191
|
}
|
|
183
192
|
const role = item.role ?? shape.role ?? "feature";
|
|
184
193
|
const dim = isDimmed(item.id);
|
|
185
|
-
const
|
|
194
|
+
const color = item.color || undefined;
|
|
195
|
+
const key = `${dim ? 1 : 0}|${role}|${kindName}${color ? `|${color}` : ""}`;
|
|
186
196
|
let batch = normal.get(key);
|
|
187
197
|
if (!batch) {
|
|
188
|
-
batch = { key, shape, role, dim, indices: [], cache: null };
|
|
198
|
+
batch = { key, shape, role, color, dim, indices: [], cache: null };
|
|
189
199
|
normal.set(key, batch);
|
|
190
200
|
}
|
|
191
201
|
batch.indices.push(i);
|
|
@@ -270,7 +280,7 @@ export function PointSet({
|
|
|
270
280
|
key={batch.key}
|
|
271
281
|
data-batch={batch.key}
|
|
272
282
|
d={d}
|
|
273
|
-
stroke={overlayRole(batch.role)}
|
|
283
|
+
stroke={batch.color ?? overlayRole(batch.role)}
|
|
274
284
|
strokeWidth={widthOf(batch.shape, batch.role, "default")}
|
|
275
285
|
opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : 1}
|
|
276
286
|
/>
|
|
@@ -299,15 +309,15 @@ export function PointSet({
|
|
|
299
309
|
<>
|
|
300
310
|
{halo && (
|
|
301
311
|
<path
|
|
302
|
-
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0)}
|
|
312
|
+
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0, hoverItem.angle2)}
|
|
303
313
|
stroke={HALO}
|
|
304
314
|
strokeWidth={widthOf(hoverShape, hoverRole, hoverSelected ? "selected" : "hover") + px(2)}
|
|
305
315
|
/>
|
|
306
316
|
)}
|
|
307
317
|
<path
|
|
308
318
|
data-hovered-point=""
|
|
309
|
-
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0)}
|
|
310
|
-
stroke={hoverSelected ? selectionStroke : overlayRole(hoverRole)}
|
|
319
|
+
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0, hoverItem.angle2)}
|
|
320
|
+
stroke={hoverSelected ? selectionStroke : (hoverItem?.color || overlayRole(hoverRole))}
|
|
311
321
|
strokeWidth={widthOf(hoverShape, hoverRole, hoverSelected ? "selected" : "hover")}
|
|
312
322
|
/>
|
|
313
323
|
</>
|
|
@@ -342,7 +352,7 @@ function batchPath(batch: Batch, items: readonly PointSetItem[], unit: number, c
|
|
|
342
352
|
for (const i of batch.indices) {
|
|
343
353
|
const item = items[i]!;
|
|
344
354
|
if (window && (item.x < window.x || item.x > window.x + window.width || item.y < window.y || item.y > window.y + window.height)) continue;
|
|
345
|
-
d += batch.shape.path(item.x, item.y, unit, item.angle ?? 0);
|
|
355
|
+
d += batch.shape.path(item.x, item.y, unit, item.angle ?? 0, item.angle2);
|
|
346
356
|
}
|
|
347
357
|
batch.cache = { key, d };
|
|
348
358
|
return d;
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* `nearestPolyline`, not by the DOM, which keeps this layer engine-agnostic for L6-1.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { useMemo, useState, type PointerEvent as ReactPointerEvent } from "react";
|
|
12
|
+
import { useEffect, useMemo, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
|
|
13
13
|
|
|
14
14
|
import type { Point } from "./measureGeometry";
|
|
15
15
|
import {
|
|
@@ -26,6 +26,7 @@ import { useStageDrag } 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";
|
|
29
|
+
import { watchTouch } from "./stage/touchWatch";
|
|
29
30
|
import { imageViewBox, type Rect } from "./stage/view";
|
|
30
31
|
|
|
31
32
|
/** One polyline of a `PolylineSet`, with optional styling. */
|
|
@@ -76,6 +77,17 @@ export interface PolylineSetProps {
|
|
|
76
77
|
* and to the layers below.
|
|
77
78
|
*/
|
|
78
79
|
marquee?: boolean | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Whether the layer takes presses and hover. Defaults to `true` when any of `onItemPress`,
|
|
82
|
+
* `onSelect`, `onHover`, `onHoverChange` or `marquee` is given. When `false` the layer
|
|
83
|
+
* renders no press target at all: a press on a line falls through to whatever is below (a
|
|
84
|
+
* draw tool's `StageSurface`, say), while `useStageHitTest` still finds the lines. Set it
|
|
85
|
+
* `false` while a tool other than selection is active.
|
|
86
|
+
*
|
|
87
|
+
* A touch on an interactive layer is not claimed: the stage still pans and pinches, and the
|
|
88
|
+
* line is selected when the finger lifts after a tap.
|
|
89
|
+
*/
|
|
90
|
+
interactive?: boolean | undefined;
|
|
79
91
|
/** CSS colour of the lines. Defaults to the overlay `feature` role: they are detected lines. */
|
|
80
92
|
stroke?: string | undefined;
|
|
81
93
|
/** CSS colour of a selected line. Defaults to the overlay `selection` role. */
|
|
@@ -107,6 +119,8 @@ const MAX_VERTEX_DOTS = 5000;
|
|
|
107
119
|
* - **Halo.** Every line has a halo, so it holds on any image.
|
|
108
120
|
* - **Points.** Above `vertexScale`, the points of the hovered and selected lines are drawn.
|
|
109
121
|
* - **Panning.** The hand tool and a held space bar still pan.
|
|
122
|
+
* - **Tools.** Without a handler (or with `interactive={false}`) the layer takes no presses
|
|
123
|
+
* and only answers `useStageHitTest`, so a draw tool's surface below receives them.
|
|
110
124
|
*
|
|
111
125
|
* The SVG carries `data-hovered` (the hovered id) and `data-sweeping` while a band is drawn.
|
|
112
126
|
*/
|
|
@@ -120,6 +134,7 @@ export function PolylineSet({
|
|
|
120
134
|
onItemPress,
|
|
121
135
|
onSelect,
|
|
122
136
|
marquee = false,
|
|
137
|
+
interactive,
|
|
123
138
|
stroke = overlayRole("feature"),
|
|
124
139
|
selectionStroke = overlayRole("selection"),
|
|
125
140
|
hitWidth = 14,
|
|
@@ -130,6 +145,12 @@ export function PolylineSet({
|
|
|
130
145
|
}: PolylineSetProps) {
|
|
131
146
|
const stage = useStage();
|
|
132
147
|
const startDrag = useStageDrag();
|
|
148
|
+
const watchRef = useRef<(() => void) | null>(null);
|
|
149
|
+
// A touch still being watched when the layer goes away is cancelled, not left listening.
|
|
150
|
+
useEffect(() => () => watchRef.current?.(), []);
|
|
151
|
+
const takesPresses =
|
|
152
|
+
interactive ??
|
|
153
|
+
(onItemPress !== undefined || onSelect !== undefined || onHover !== undefined || onHoverChange !== undefined || marquee);
|
|
133
154
|
const px = useScreenPx();
|
|
134
155
|
const { hitTestAll } = useStageHitTest();
|
|
135
156
|
|
|
@@ -230,7 +251,9 @@ export function PolylineSet({
|
|
|
230
251
|
};
|
|
231
252
|
|
|
232
253
|
const onLinePointerDown = (event: ReactPointerEvent<SVGPathElement>) => {
|
|
233
|
-
|
|
254
|
+
const touch = event.pointerType === "touch";
|
|
255
|
+
if (stage.panMode || (!touch && event.button !== 0)) return;
|
|
256
|
+
if (touch && watchRef.current) return; // a second finger: the watch cancels itself, the stage pinches
|
|
234
257
|
// A band has to be able to start anywhere, and on a frame that is mostly lines "anywhere"
|
|
235
258
|
// is usually on one. Declining here would hand the press to the stage (which pans), not to
|
|
236
259
|
// a surface below, because only the topmost element is a press's target.
|
|
@@ -244,6 +267,22 @@ export function PolylineSet({
|
|
|
244
267
|
// hands it to the point layer.
|
|
245
268
|
const point = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
246
269
|
if (hitTestAll(point, hitWidth / 2, { pressable: true }).some((hit) => hit.layerId !== ownId && hit.priority > priority)) return;
|
|
270
|
+
if (touch && !marquee) {
|
|
271
|
+
// Not claimed: the press may become a pan or a pinch, so the line is selected on the tap.
|
|
272
|
+
const additive = event.metaKey || event.ctrlKey;
|
|
273
|
+
watchRef.current = watchTouch(event, {
|
|
274
|
+
onEnd: (_e, _moved, tap) => {
|
|
275
|
+
watchRef.current = null;
|
|
276
|
+
if (!tap) return;
|
|
277
|
+
onItemPress?.(id, event);
|
|
278
|
+
onSelect?.([id], additive ? "toggle" : "replace");
|
|
279
|
+
},
|
|
280
|
+
onCancel: () => {
|
|
281
|
+
watchRef.current = null;
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
247
286
|
// Claimed, and selected on the press: a selection that waits for the release feels like
|
|
248
287
|
// lag on a canvas where every other gesture is immediate.
|
|
249
288
|
event.stopPropagation();
|
|
@@ -261,7 +300,7 @@ export function PolylineSet({
|
|
|
261
300
|
data-hovered={hoverId ?? undefined}
|
|
262
301
|
data-sweeping={band ? "" : undefined}
|
|
263
302
|
>
|
|
264
|
-
{marquee && (
|
|
303
|
+
{takesPresses && marquee && (
|
|
265
304
|
<rect
|
|
266
305
|
data-marquee-surface=""
|
|
267
306
|
x={-0.5}
|
|
@@ -308,18 +347,20 @@ export function PolylineSet({
|
|
|
308
347
|
{dots && <path data-points="" d={dots} stroke={selectionStroke} strokeWidth={px(3)} />}
|
|
309
348
|
{/* The one hit target: every line, transparent, wide. Which line it was is the
|
|
310
349
|
index's answer, not the DOM's. */}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
350
|
+
{takesPresses && (
|
|
351
|
+
<path
|
|
352
|
+
data-hit=""
|
|
353
|
+
d={groups.hit}
|
|
354
|
+
stroke="transparent"
|
|
355
|
+
strokeWidth={px(hitWidth)}
|
|
356
|
+
style={{ pointerEvents: "stroke", cursor: stage.panMode ? undefined : marquee ? "crosshair" : "pointer" }}
|
|
357
|
+
onPointerDown={onLinePointerDown}
|
|
358
|
+
onPointerMove={(event) => {
|
|
359
|
+
if (!band && event.pointerType !== "touch") setHover(pickAt(event));
|
|
360
|
+
}}
|
|
361
|
+
onPointerLeave={() => setHover(null)}
|
|
362
|
+
/>
|
|
363
|
+
)}
|
|
323
364
|
</g>
|
|
324
365
|
{band && (
|
|
325
366
|
<rect
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
import { overlayRole } from "./overlayRole";
|
|
25
25
|
import { useStage } from "./stage/ImageStage";
|
|
26
26
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
27
|
-
import { imageViewBox, type Rect } from "./stage/view";
|
|
27
|
+
import { PIXEL_CENTRE, imageViewBox, type Rect } from "./stage/view";
|
|
28
28
|
|
|
29
29
|
/** Handle square side, in screen pixels. */
|
|
30
30
|
const HANDLE_PX = 9;
|
|
@@ -50,8 +50,9 @@ export interface RectRoiEditorProps {
|
|
|
50
50
|
*/
|
|
51
51
|
draw?: boolean | undefined;
|
|
52
52
|
/**
|
|
53
|
-
* The area the region must stay in
|
|
54
|
-
*
|
|
53
|
+
* The area the region must stay in, in the stage's centre convention. Defaults to
|
|
54
|
+
* `{ x: -0.5, y: -0.5, width, height }` of the image: its full extent, so a region that
|
|
55
|
+
* fills it outlines the image edges.
|
|
55
56
|
*/
|
|
56
57
|
bounds?: Rect | undefined;
|
|
57
58
|
/** The smallest side, in image pixels; a smaller drawn box is a click, not a region. Defaults to 4. */
|
|
@@ -93,7 +94,7 @@ export function RectRoiEditor({
|
|
|
93
94
|
stroke = overlayRole("selection"),
|
|
94
95
|
}: RectRoiEditorProps) {
|
|
95
96
|
const stage = useStage();
|
|
96
|
-
const area = bounds ?? { x:
|
|
97
|
+
const area = bounds ?? { x: -PIXEL_CENTRE, y: -PIXEL_CENTRE, width: stage.image.width, height: stage.image.height };
|
|
97
98
|
const dragRef = useRef<Drag | null>(null);
|
|
98
99
|
const lastRef = useRef<Rect | null>(null);
|
|
99
100
|
const [drawing, setDrawing] = useState<Rect | null>(null);
|