@vitavision/stage2d 0.11.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/dist/index.d.ts +139 -19
- package/dist/index.js +456 -189
- 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/MaskEditor.tsx +22 -9
- package/src/components/PointSet.tsx +14 -6
- package/src/components/RectRoiEditor.tsx +5 -4
- package/src/components/areaBatching.ts +111 -0
- package/src/components/stage/ImageStage.tsx +202 -120
- package/src/components/stage/StageSurface.tsx +43 -14
- package/src/components/stage/view.ts +33 -6
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Many closed regions over the image, hoverable and selectable: marker quads, drawn polygons,
|
|
3
3
|
* region annotations, defect outlines.
|
|
4
4
|
*
|
|
5
|
-
* Drawn as a handful of batched paths, one per (state, role), each outline with its 12 % fill
|
|
5
|
+
* Drawn as a handful of batched paths, one per (state, role, colours), each outline with its 12 % fill
|
|
6
6
|
* in one element (ADR-0004 rule 1); the pointer is resolved by `nearestArea` through the
|
|
7
7
|
* stage's hit registry, never by the DOM (rule 2); strokes and labels are sized in screen
|
|
8
8
|
* pixels (rule 4). A scene of 5,000 quads is a dozen DOM nodes plus at most 200 labels.
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import { useMemo, useState } from "react";
|
|
12
12
|
|
|
13
|
+
import { batchAreas, type AreaBatch } from "./areaBatching";
|
|
13
14
|
import { areaCentre, areaPath, buildAreaIndex, nearestArea, type Area, type AreaId } from "./areaIndex";
|
|
14
15
|
import { useLabelIndices } from "./labelLod";
|
|
15
16
|
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
@@ -26,6 +27,13 @@ export interface AreaSetItem extends Area {
|
|
|
26
27
|
role?: OverlayRole | undefined;
|
|
27
28
|
/** Text drawn at its centre (the mean of its vertices), when the zoom leaves room. */
|
|
28
29
|
label?: string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* CSS colour of its outline (`#hex`, `var(--x)`, `color-mix(…)`; used as given). Overrides
|
|
32
|
+
* the role and the layer's `stroke`. Items sharing a colour share a batch.
|
|
33
|
+
*/
|
|
34
|
+
stroke?: string | undefined;
|
|
35
|
+
/** CSS colour of its fill. Defaults to its outline colour. */
|
|
36
|
+
fill?: string | undefined;
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
/** Props of `AreaSet`. */
|
|
@@ -61,6 +69,30 @@ export interface AreaSetProps {
|
|
|
61
69
|
* of a marker quad reads (the overlay grammar's "corner 0 ticked"). Off by default.
|
|
62
70
|
*/
|
|
63
71
|
firstVertexTick?: boolean | undefined;
|
|
72
|
+
/** CSS colour of every outline that names none of its own, in place of its role's. */
|
|
73
|
+
stroke?: string | undefined;
|
|
74
|
+
/** The fill's alpha, 0 to 1. Defaults to 0.12. */
|
|
75
|
+
fillOpacity?: number | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* How a self-overlapping ring is filled. `"nonzero"` (the default) fills everything a ring
|
|
78
|
+
* encloses; `"evenodd"` leaves the doubly-wound part of a self-intersecting ring open, as
|
|
79
|
+
* a rasteriser would. With `"evenodd"` every region's fill is its own path (even-odd across
|
|
80
|
+
* a batched path would cut a hole where two regions overlap); outlines stay batched.
|
|
81
|
+
*/
|
|
82
|
+
fillRule?: "nonzero" | "evenodd" | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The paint order. `"appearance"` (the default) draws one batch per appearance, in order of
|
|
85
|
+
* first appearance. `"items"` draws consecutive runs in item order, so a later region
|
|
86
|
+
* covers an earlier one of another appearance (a subtract region over an add region); more
|
|
87
|
+
* runs mean more paths, so use it when the order matters.
|
|
88
|
+
*/
|
|
89
|
+
paintOrder?: "appearance" | "items" | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* What a selected region looks like. `"selection"` (the default) paints it in the
|
|
92
|
+
* selection colour, fill and outline. `"item"` keeps its own fill (and its place in the
|
|
93
|
+
* paint order) and restyles only the outline.
|
|
94
|
+
*/
|
|
95
|
+
selectionFill?: "selection" | "item" | undefined;
|
|
64
96
|
/** Draw the dark band under every outline. On by default; off for a fast, flat layer. */
|
|
65
97
|
halo?: boolean | undefined;
|
|
66
98
|
/** CSS colour of selected regions. Defaults to the overlay `selection` role. */
|
|
@@ -80,21 +112,11 @@ const FILL_OPACITY = 0.12;
|
|
|
80
112
|
/** How long the first-vertex tick is, in screen pixels, at most. */
|
|
81
113
|
const TICK_PX = 8;
|
|
82
114
|
|
|
83
|
-
/** The regions of one batched path. */
|
|
84
|
-
interface Batch {
|
|
85
|
-
key: string;
|
|
86
|
-
role: OverlayRole;
|
|
87
|
-
dim: boolean;
|
|
88
|
-
/** Item positions, ascending. */
|
|
89
|
-
indices: number[];
|
|
90
|
-
/** The outlines, concatenated. */
|
|
91
|
-
d: string;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
115
|
/**
|
|
95
116
|
* A selectable set of closed regions inside an `ImageStage`.
|
|
96
117
|
*
|
|
97
|
-
* - **Rendering**: an outline and its 12 % fill, in the item's role (`feature` by default)
|
|
118
|
+
* - **Rendering**: an outline and its 12 % fill, in the item's role (`feature` by default) or its own
|
|
119
|
+
* `stroke` / `fill` colours (`fillOpacity`, `fillRule`, `paintOrder` and `selectionFill` tune the fill).
|
|
98
120
|
* Rings are wound alike before they are batched, so overlapping regions never cut a hole in
|
|
99
121
|
* each other.
|
|
100
122
|
* - **States**, from visual-language §5: default 1.5 screen px (1 for `model` and
|
|
@@ -118,6 +140,11 @@ export function AreaSet({
|
|
|
118
140
|
onItemPress,
|
|
119
141
|
labels = true,
|
|
120
142
|
firstVertexTick = false,
|
|
143
|
+
stroke,
|
|
144
|
+
fillOpacity = FILL_OPACITY,
|
|
145
|
+
fillRule = "nonzero",
|
|
146
|
+
paintOrder = "appearance",
|
|
147
|
+
selectionFill = "selection",
|
|
121
148
|
halo = true,
|
|
122
149
|
selectionStroke = overlayRole("selection"),
|
|
123
150
|
layerId,
|
|
@@ -156,28 +183,13 @@ export function AreaSet({
|
|
|
156
183
|
|
|
157
184
|
// Regions grouped by appearance. Rebuilt when the items or the selection change, never on
|
|
158
185
|
// hover or on a view change.
|
|
159
|
-
const groups = useMemo(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
const itemRole = item.role ?? role;
|
|
169
|
-
const dim = isDimmed(item.id);
|
|
170
|
-
const key = `${dim ? 1 : 0}|${itemRole}`;
|
|
171
|
-
let batch = normal.get(key);
|
|
172
|
-
if (!batch) {
|
|
173
|
-
batch = { key, role: itemRole, dim, indices: [], d: "" };
|
|
174
|
-
normal.set(key, batch);
|
|
175
|
-
}
|
|
176
|
-
batch.indices.push(i);
|
|
177
|
-
batch.d += areaPath(item.points);
|
|
178
|
-
});
|
|
179
|
-
return { normal: [...normal.values()], picked: picked.indices.length > 0 ? picked : null };
|
|
180
|
-
}, [items, role, selectedSet, isDimmed]);
|
|
186
|
+
const groups = useMemo(
|
|
187
|
+
() => batchAreas(items, { role, stroke, paintOrder, fillRule, selectionFill, selected: selectedSet, isDimmed }),
|
|
188
|
+
[items, role, stroke, paintOrder, fillRule, selectionFill, selectedSet, isDimmed],
|
|
189
|
+
);
|
|
190
|
+
const keepFill = selectionFill === "item";
|
|
191
|
+
const evenOdd = fillRule === "evenodd";
|
|
192
|
+
const separate = evenOdd || keepFill;
|
|
181
193
|
|
|
182
194
|
// The tick is a line a few screen pixels long, so it is the one thing here that follows
|
|
183
195
|
// the zoom: regenerated per zoom step, in O(regions).
|
|
@@ -186,14 +198,19 @@ export function AreaSet({
|
|
|
186
198
|
const batches = groups.picked ? [...groups.normal, groups.picked] : groups.normal;
|
|
187
199
|
return batches.map((batch) => {
|
|
188
200
|
let d = "";
|
|
189
|
-
for (const i of batch.indices)
|
|
201
|
+
for (const i of batch.indices) {
|
|
202
|
+
// A selected item keeping its fill is ticked in the selection batch only.
|
|
203
|
+
if (keepFill && batch !== groups.picked && selectedSet.has(items[i]!.id)) continue;
|
|
204
|
+
d += tickPath(items[i]!.points, TICK_PX * unit);
|
|
205
|
+
}
|
|
190
206
|
return { batch, d };
|
|
191
207
|
});
|
|
192
|
-
}, [firstVertexTick, groups, items, unit]);
|
|
208
|
+
}, [firstVertexTick, groups, items, unit, keepFill, selectedSet]);
|
|
193
209
|
|
|
194
210
|
const hoverItem = hoverId !== null ? items[positionOf.get(hoverId) ?? -1] : undefined;
|
|
195
211
|
const hoverSelected = hoverItem ? selectedSet.has(hoverItem.id) : false;
|
|
196
212
|
const hoverRole = hoverItem?.role ?? role;
|
|
213
|
+
const hoverStroke = hoverItem ? (hoverItem.stroke ?? stroke ?? overlayRole(hoverRole)) : undefined;
|
|
197
214
|
|
|
198
215
|
// Labels sit at the centre of a region; the layout is the shared level-of-detail rule.
|
|
199
216
|
const anchors = useMemo(() => {
|
|
@@ -223,6 +240,16 @@ export function AreaSet({
|
|
|
223
240
|
return px(OVERLAY_STATE_WIDTH[state] - (thin && state !== "selected" ? 0.5 : 0));
|
|
224
241
|
};
|
|
225
242
|
|
|
243
|
+
const strokeOf = (batch: AreaBatch) => batch.stroke ?? overlayRole(batch.role);
|
|
244
|
+
|
|
245
|
+
/** Each region's fill as its own even-odd path, or all of them as one path. */
|
|
246
|
+
const fillPaths = (batch: AreaBatch, colour: string, tag: string) =>
|
|
247
|
+
evenOdd ? (
|
|
248
|
+
batch.fills!.map((d, k) => <path key={batch.indices[k]} data-fill={tag} d={d} fill={colour} fillOpacity={fillOpacity} fillRule="evenodd" />)
|
|
249
|
+
) : (
|
|
250
|
+
<path data-fill={tag} d={batch.fills!.join("")} fill={colour} fillOpacity={fillOpacity} />
|
|
251
|
+
);
|
|
252
|
+
|
|
226
253
|
return (
|
|
227
254
|
<svg
|
|
228
255
|
viewBox={imageViewBox(stage.image)}
|
|
@@ -233,26 +260,30 @@ export function AreaSet({
|
|
|
233
260
|
>
|
|
234
261
|
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
235
262
|
{groups.normal.map((batch) => (
|
|
236
|
-
<g key={batch.
|
|
237
|
-
{halo && <path d={batch.d} stroke={HALO} strokeWidth={widthOf(batch.role, "default") + px(2)} opacity={0.6} />}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
263
|
+
<g key={batch.id} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
264
|
+
{halo && (!separate || batch.d !== "") && <path d={batch.d} stroke={HALO} strokeWidth={widthOf(batch.role, "default") + px(2)} opacity={0.6} />}
|
|
265
|
+
{separate && fillPaths(batch, batch.fill ?? strokeOf(batch), batch.key)}
|
|
266
|
+
{(!separate || batch.d !== "") && (
|
|
267
|
+
<path
|
|
268
|
+
data-batch={batch.key}
|
|
269
|
+
d={batch.d}
|
|
270
|
+
fill={separate ? undefined : (batch.fill ?? strokeOf(batch))}
|
|
271
|
+
fillOpacity={separate ? undefined : fillOpacity}
|
|
272
|
+
stroke={strokeOf(batch)}
|
|
273
|
+
strokeWidth={widthOf(batch.role, "default")}
|
|
274
|
+
/>
|
|
275
|
+
)}
|
|
246
276
|
</g>
|
|
247
277
|
))}
|
|
248
278
|
{groups.picked && (
|
|
249
279
|
<>
|
|
250
280
|
{halo && <path d={groups.picked.d} stroke={HALO} strokeWidth={widthOf("selection", "selected") + px(2)} />}
|
|
281
|
+
{evenOdd && !keepFill && fillPaths(groups.picked, selectionStroke, "selected")}
|
|
251
282
|
<path
|
|
252
283
|
data-selected-areas=""
|
|
253
284
|
d={groups.picked.d}
|
|
254
|
-
fill={selectionStroke}
|
|
255
|
-
fillOpacity={
|
|
285
|
+
fill={evenOdd || keepFill ? undefined : selectionStroke}
|
|
286
|
+
fillOpacity={evenOdd || keepFill ? undefined : fillOpacity}
|
|
256
287
|
stroke={selectionStroke}
|
|
257
288
|
strokeWidth={widthOf("selection", "selected")}
|
|
258
289
|
/>
|
|
@@ -266,18 +297,18 @@ export function AreaSet({
|
|
|
266
297
|
<path
|
|
267
298
|
data-hovered-area=""
|
|
268
299
|
d={areaPath(hoverItem.points)}
|
|
269
|
-
stroke={hoverSelected ? selectionStroke :
|
|
300
|
+
stroke={hoverSelected ? selectionStroke : hoverStroke}
|
|
270
301
|
strokeWidth={widthOf(hoverRole, hoverSelected ? "selected" : "hover")}
|
|
271
302
|
/>
|
|
272
303
|
</>
|
|
273
304
|
)}
|
|
274
305
|
{ticks.map(({ batch, d }) => (
|
|
275
|
-
<g key={`tick|${batch.
|
|
306
|
+
<g key={`tick|${batch.id}`} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
276
307
|
{halo && <path d={d} stroke={HALO} strokeWidth={px(OVERLAY_STATE_WIDTH.selected + 2)} opacity={0.6} />}
|
|
277
308
|
<path
|
|
278
309
|
data-ticks={batch.key}
|
|
279
310
|
d={d}
|
|
280
|
-
stroke={batch === groups.picked ? selectionStroke :
|
|
311
|
+
stroke={batch === groups.picked ? selectionStroke : strokeOf(batch)}
|
|
281
312
|
strokeWidth={px(OVERLAY_STATE_WIDTH.selected)}
|
|
282
313
|
/>
|
|
283
314
|
</g>
|
|
@@ -5,6 +5,7 @@ import { useRef, useState, type KeyboardEvent, type PointerEvent } from "react";
|
|
|
5
5
|
import { imageViewBox } from "./stage/view";
|
|
6
6
|
import { useStage } from "./stage/ImageStage";
|
|
7
7
|
import type { Point } from "./measureGeometry";
|
|
8
|
+
import type { Rect } from "./stage/view";
|
|
8
9
|
|
|
9
10
|
/** An ordered, closed contour drawn in source-image pixel-center coordinates. */
|
|
10
11
|
export interface ContourEditorProps {
|
|
@@ -20,6 +21,13 @@ export interface ContourEditorProps {
|
|
|
20
21
|
label?: string;
|
|
21
22
|
/** CSS stroke color. */
|
|
22
23
|
stroke?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The area, in image coordinates, every vertex is kept inside: a drag, a keyboard nudge and
|
|
26
|
+
* an inserted vertex are clamped to it. Defaults to the pixel centres,
|
|
27
|
+
* `{ x: 0, y: 0, width: w - 1, height: h - 1 }`. Pass `{ x: -0.5, y: -0.5, width: w, height: h }`
|
|
28
|
+
* for polygons in the area convention, which may lie exactly on the image border.
|
|
29
|
+
*/
|
|
30
|
+
bounds?: Rect | undefined;
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
/** Return the index of the closest segment, with the last vertex joined to the first. */
|
|
@@ -40,7 +48,7 @@ export function nearestContourSegment(points: Point[], point: Point): number {
|
|
|
40
48
|
}
|
|
41
49
|
|
|
42
50
|
/** Edit contour vertices over an ImageStage using its shared source-image transform. */
|
|
43
|
-
export function ContourEditor({ points, onChange, onCommit, editable = false, label = "Contour", stroke = "var(--signal)" }: ContourEditorProps) {
|
|
51
|
+
export function ContourEditor({ points, onChange, onCommit, editable = false, label = "Contour", stroke = "var(--signal)", bounds }: ContourEditorProps) {
|
|
44
52
|
const stage = useStage();
|
|
45
53
|
const draggingRef = useRef<number | null>(null);
|
|
46
54
|
const movedRef = useRef(false);
|
|
@@ -50,9 +58,10 @@ export function ContourEditor({ points, onChange, onCommit, editable = false, la
|
|
|
50
58
|
const hairline = stage.imageLength(1.5);
|
|
51
59
|
|
|
52
60
|
function clamp(point: Point): Point {
|
|
61
|
+
const area = bounds ?? { x: 0, y: 0, width: stage.image.width - 1, height: stage.image.height - 1 };
|
|
53
62
|
return {
|
|
54
|
-
x: Math.max(
|
|
55
|
-
y: Math.max(
|
|
63
|
+
x: Math.max(area.x, Math.min(area.x + area.width, point.x)),
|
|
64
|
+
y: Math.max(area.y, Math.min(area.y + area.height, point.y)),
|
|
56
65
|
};
|
|
57
66
|
}
|
|
58
67
|
|
|
@@ -19,7 +19,12 @@ import { imageViewBox, type Rect } from "./stage/view";
|
|
|
19
19
|
* - `point`: a position.
|
|
20
20
|
* - `line`: a segment from `from` to `to`.
|
|
21
21
|
* - `polyline` and `polygon`: the vertices placed so far, flat `[x0, y0, x1, y1, …]`, and
|
|
22
|
-
* the pointer as `cursor`, which the next vertex would land on.
|
|
22
|
+
* the pointer as `cursor`, which the next vertex would land on. A polygon with `closing`
|
|
23
|
+
* set has the pointer on its first vertex: a click there closes it, so the first vertex is
|
|
24
|
+
* highlighted and the closing edge drawn solid.
|
|
25
|
+
* - `stroke`: a brush trail, the flat `points` swept by a round brush of `width` image pixels
|
|
26
|
+
* (never thinner than 1 screen pixel), as a translucent filled band without dashes.
|
|
27
|
+
* - `brush`: a brush footprint cursor, a circle of `diameter` image pixels centred on `x`, `y`.
|
|
23
28
|
* - `rect` and `ellipse`: two opposite corners of the axis-aligned box (the drag's start and
|
|
24
29
|
* the pointer).
|
|
25
30
|
*/
|
|
@@ -27,7 +32,9 @@ export type DraftShapeSpec =
|
|
|
27
32
|
| { kind: "point"; x: number; y: number }
|
|
28
33
|
| { kind: "line"; from: Point; to: Point }
|
|
29
34
|
| { kind: "polyline"; points: ArrayLike<number>; cursor?: Point | undefined }
|
|
30
|
-
| { kind: "polygon"; points: ArrayLike<number>; cursor?: Point | undefined }
|
|
35
|
+
| { kind: "polygon"; points: ArrayLike<number>; cursor?: Point | undefined; closing?: boolean | undefined }
|
|
36
|
+
| { kind: "stroke"; points: ArrayLike<number>; width: number }
|
|
37
|
+
| { kind: "brush"; x: number; y: number; diameter: number }
|
|
31
38
|
| { kind: "rect"; from: Point; to: Point }
|
|
32
39
|
| { kind: "ellipse"; from: Point; to: Point };
|
|
33
40
|
|
|
@@ -37,6 +44,8 @@ export interface DraftShapeProps {
|
|
|
37
44
|
shape: DraftShapeSpec | null;
|
|
38
45
|
/** The overlay role it is drawn in. Defaults to `"selection"`, the accent. */
|
|
39
46
|
role?: OverlayRole | undefined;
|
|
47
|
+
/** A CSS colour that overrides `role`, for example `"var(--defect)"` for an eraser. */
|
|
48
|
+
stroke?: string | undefined;
|
|
40
49
|
/** Draw the dark band under the outline. On by default. */
|
|
41
50
|
halo?: boolean | undefined;
|
|
42
51
|
}
|
|
@@ -54,15 +63,20 @@ const VERTEX_PX = 6;
|
|
|
54
63
|
* The outline is 1.5 screen px, dashed 6 4 screen px, in the accent (`selection`) role by
|
|
55
64
|
* default, with a halo; regions (`polygon`, `rect`, `ellipse`) carry the 12 % fill. A polyline
|
|
56
65
|
* or polygon shows its placed vertices as dots and a segment on to the pointer; a polygon also
|
|
57
|
-
* shows, fainter, the segment that would close it
|
|
66
|
+
* shows, fainter, the segment that would close it; with `closing` the first vertex gets a
|
|
67
|
+
* ring and that segment turns solid and full strength: "click here to close". A `stroke`
|
|
68
|
+
* (brush trail) is a translucent band of its own width with round caps and joins and no
|
|
69
|
+
* dashes, and a `brush` is a footprint ring: a dark ring under a light one, so it reads on
|
|
70
|
+
* any image. `points` may be a typed-array view that changes identity every move: the draft
|
|
71
|
+
* is rebuilt in one O(n) pass per render, with no per-vertex elements. It takes no pointer events and is hidden
|
|
58
72
|
* from assistive technology: it is transient feedback for a gesture.
|
|
59
73
|
*
|
|
60
74
|
* The SVG carries `data-draft` (the shape's kind).
|
|
61
75
|
*/
|
|
62
|
-
export function DraftShape({ shape, role = "selection", halo = true }: DraftShapeProps) {
|
|
76
|
+
export function DraftShape({ shape, role = "selection", stroke, halo = true }: DraftShapeProps) {
|
|
63
77
|
const stage = useStage();
|
|
64
78
|
const px = useScreenPx();
|
|
65
|
-
const colour = overlayRole(role);
|
|
79
|
+
const colour = stroke ?? overlayRole(role);
|
|
66
80
|
const dash = `${px(DASH)} ${px(GAP)}`;
|
|
67
81
|
const width = px(STROKE_PX);
|
|
68
82
|
const parts = shape ? draftParts(shape, px(1)) : null;
|
|
@@ -79,7 +93,11 @@ export function DraftShape({ shape, role = "selection", halo = true }: DraftShap
|
|
|
79
93
|
{parts.outline && halo && <path d={parts.outline} stroke={HALO} strokeWidth={width + px(2)} opacity={0.6} />}
|
|
80
94
|
{parts.outline && parts.region && <path d={parts.outline} fill={colour} fillOpacity={0.12} stroke="none" />}
|
|
81
95
|
{parts.outline && <path data-draft-outline="" d={parts.outline} stroke={colour} strokeWidth={width} strokeDasharray={dash} />}
|
|
82
|
-
{parts.
|
|
96
|
+
{parts.trail && <path data-draft-stroke="" d={parts.trail.d} stroke={colour} strokeWidth={Math.max(parts.trail.width, px(1))} opacity={0.4} />}
|
|
97
|
+
{parts.footprint && halo && <path d={parts.footprint} stroke={HALO} strokeWidth={px(3.5)} opacity={0.6} />}
|
|
98
|
+
{parts.footprint && <path data-draft-brush="" d={parts.footprint} stroke={colour} strokeWidth={px(1.5)} />}
|
|
99
|
+
{parts.closing && <path data-draft-closing="" d={parts.closing} stroke={colour} strokeWidth={parts.armed ? width : px(1)} strokeDasharray={parts.armed ? undefined : dash} opacity={parts.armed ? 1 : 0.5} />}
|
|
100
|
+
{parts.first && <path data-draft-first="" d={parts.first} stroke={colour} strokeWidth={width} fill={colour} fillOpacity={0.35} />}
|
|
83
101
|
{parts.vertices && <path data-draft-vertices="" d={parts.vertices} stroke={colour} strokeWidth={px(VERTEX_PX)} />}
|
|
84
102
|
</g>
|
|
85
103
|
)}
|
|
@@ -97,8 +115,18 @@ interface DraftParts {
|
|
|
97
115
|
closing: string | null;
|
|
98
116
|
/** The placed vertices, as zero-length segments drawn with round caps. */
|
|
99
117
|
vertices: string | null;
|
|
118
|
+
/** Whether the closing segment is the live "click to close" cue: solid, full strength. */
|
|
119
|
+
armed: boolean;
|
|
120
|
+
/** The ring around the first vertex while the pointer is on it. */
|
|
121
|
+
first: string | null;
|
|
122
|
+
/** A brush trail: its path and the width in image pixels. */
|
|
123
|
+
trail: { d: string; width: number } | null;
|
|
124
|
+
/** A brush footprint circle. */
|
|
125
|
+
footprint: string | null;
|
|
100
126
|
}
|
|
101
127
|
|
|
128
|
+
const NONE: DraftParts = { outline: null, region: false, closing: null, vertices: null, armed: false, first: null, trail: null, footprint: null };
|
|
129
|
+
|
|
102
130
|
/** An ellipse with the given centre and radii as two arcs. */
|
|
103
131
|
function ellipsePath(cx: number, cy: number, rx: number, ry: number): string {
|
|
104
132
|
return `M${cx + rx} ${cy}a${rx} ${ry} 0 1 0 ${-2 * rx} 0a${rx} ${ry} 0 1 0 ${2 * rx} 0Z`;
|
|
@@ -107,9 +135,9 @@ function ellipsePath(cx: number, cy: number, rx: number, ry: number): string {
|
|
|
107
135
|
function draftParts(shape: DraftShapeSpec, unit: number): DraftParts {
|
|
108
136
|
switch (shape.kind) {
|
|
109
137
|
case "point":
|
|
110
|
-
return { outline: ellipsePath(shape.x, shape.y, 8 * unit, 8 * unit),
|
|
138
|
+
return { ...NONE, outline: ellipsePath(shape.x, shape.y, 8 * unit, 8 * unit), vertices: `M${shape.x} ${shape.y}h0` };
|
|
111
139
|
case "line":
|
|
112
|
-
return { outline: `M${shape.from.x} ${shape.from.y}L${shape.to.x} ${shape.to.y}
|
|
140
|
+
return { ...NONE, outline: `M${shape.from.x} ${shape.from.y}L${shape.to.x} ${shape.to.y}` };
|
|
113
141
|
case "rect":
|
|
114
142
|
case "ellipse": {
|
|
115
143
|
const box = rectFromCorners(shape.from, shape.to);
|
|
@@ -117,12 +145,22 @@ function draftParts(shape: DraftShapeSpec, unit: number): DraftParts {
|
|
|
117
145
|
shape.kind === "rect"
|
|
118
146
|
? `M${box.x} ${box.y}h${box.width}v${box.height}h${-box.width}Z`
|
|
119
147
|
: ellipsePath(box.x + box.width / 2, box.y + box.height / 2, box.width / 2, box.height / 2);
|
|
120
|
-
return { outline, region: true
|
|
148
|
+
return { ...NONE, outline, region: true };
|
|
149
|
+
}
|
|
150
|
+
case "brush":
|
|
151
|
+
return { ...NONE, footprint: ellipsePath(shape.x, shape.y, Math.max(shape.diameter, 2 * unit) / 2, Math.max(shape.diameter, 2 * unit) / 2) };
|
|
152
|
+
case "stroke": {
|
|
153
|
+
const n = Math.floor(shape.points.length / 2);
|
|
154
|
+
if (n === 0) return NONE;
|
|
155
|
+
let d = "";
|
|
156
|
+
for (let i = 0; i < n; i++) d += `${i === 0 ? "M" : "L"}${shape.points[2 * i]} ${shape.points[2 * i + 1]}`;
|
|
157
|
+
// A single point is a zero-length segment: the round caps make it a dot.
|
|
158
|
+
return { ...NONE, trail: { d: n === 1 ? `${d}h0` : d, width: shape.width } };
|
|
121
159
|
}
|
|
122
160
|
case "polyline":
|
|
123
161
|
case "polygon": {
|
|
124
162
|
const n = Math.floor(shape.points.length / 2);
|
|
125
|
-
if (n === 0) return
|
|
163
|
+
if (n === 0) return NONE;
|
|
126
164
|
let outline = "";
|
|
127
165
|
let vertices = "";
|
|
128
166
|
for (let i = 0; i < n; i++) {
|
|
@@ -131,11 +169,18 @@ function draftParts(shape: DraftShapeSpec, unit: number): DraftParts {
|
|
|
131
169
|
}
|
|
132
170
|
if (shape.cursor) outline += `L${shape.cursor.x} ${shape.cursor.y}`;
|
|
133
171
|
const polygon = shape.kind === "polygon";
|
|
172
|
+
const armed = polygon && shape.closing === true && n >= 2;
|
|
173
|
+
let closing: string | null = null;
|
|
174
|
+
if (armed) closing = `M${shape.points[2 * n - 2]} ${shape.points[2 * n - 1]}L${shape.points[0]} ${shape.points[1]}`;
|
|
175
|
+
else if (polygon && shape.cursor) closing = `M${shape.cursor.x} ${shape.cursor.y}L${shape.points[0]} ${shape.points[1]}`;
|
|
134
176
|
return {
|
|
177
|
+
...NONE,
|
|
135
178
|
outline,
|
|
136
179
|
region: polygon && n + (shape.cursor ? 1 : 0) >= 3,
|
|
137
|
-
closing
|
|
180
|
+
closing,
|
|
138
181
|
vertices,
|
|
182
|
+
armed,
|
|
183
|
+
first: armed ? ellipsePath(shape.points[0]!, shape.points[1]!, 5 * unit, 5 * unit) : null,
|
|
139
184
|
};
|
|
140
185
|
}
|
|
141
186
|
}
|
|
@@ -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,6 +30,11 @@ 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;
|
|
35
40
|
/** A second angle in radians, for a glyph with two axes. `MarkerShape.path` gets `undefined` without it. */
|
|
@@ -93,6 +98,8 @@ interface Batch {
|
|
|
93
98
|
key: string;
|
|
94
99
|
shape: MarkerShape;
|
|
95
100
|
role: OverlayRole;
|
|
101
|
+
/** The custom colour, when the items of this batch have one. */
|
|
102
|
+
color: string | undefined;
|
|
96
103
|
dim: boolean;
|
|
97
104
|
/** Item positions, ascending. */
|
|
98
105
|
indices: number[];
|
|
@@ -176,7 +183,7 @@ export function PointSet({
|
|
|
176
183
|
const key = kindName;
|
|
177
184
|
let batch = picked.get(key);
|
|
178
185
|
if (!batch) {
|
|
179
|
-
batch = { key, shape, role: "selection", dim: false, indices: [], cache: null };
|
|
186
|
+
batch = { key, shape, role: "selection", color: undefined, dim: false, indices: [], cache: null };
|
|
180
187
|
picked.set(key, batch);
|
|
181
188
|
}
|
|
182
189
|
batch.indices.push(i);
|
|
@@ -184,10 +191,11 @@ export function PointSet({
|
|
|
184
191
|
}
|
|
185
192
|
const role = item.role ?? shape.role ?? "feature";
|
|
186
193
|
const dim = isDimmed(item.id);
|
|
187
|
-
const
|
|
194
|
+
const color = item.color || undefined;
|
|
195
|
+
const key = `${dim ? 1 : 0}|${role}|${kindName}${color ? `|${color}` : ""}`;
|
|
188
196
|
let batch = normal.get(key);
|
|
189
197
|
if (!batch) {
|
|
190
|
-
batch = { key, shape, role, dim, indices: [], cache: null };
|
|
198
|
+
batch = { key, shape, role, color, dim, indices: [], cache: null };
|
|
191
199
|
normal.set(key, batch);
|
|
192
200
|
}
|
|
193
201
|
batch.indices.push(i);
|
|
@@ -272,7 +280,7 @@ export function PointSet({
|
|
|
272
280
|
key={batch.key}
|
|
273
281
|
data-batch={batch.key}
|
|
274
282
|
d={d}
|
|
275
|
-
stroke={overlayRole(batch.role)}
|
|
283
|
+
stroke={batch.color ?? overlayRole(batch.role)}
|
|
276
284
|
strokeWidth={widthOf(batch.shape, batch.role, "default")}
|
|
277
285
|
opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : 1}
|
|
278
286
|
/>
|
|
@@ -309,7 +317,7 @@ export function PointSet({
|
|
|
309
317
|
<path
|
|
310
318
|
data-hovered-point=""
|
|
311
319
|
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0, hoverItem.angle2)}
|
|
312
|
-
stroke={hoverSelected ? selectionStroke : overlayRole(hoverRole)}
|
|
320
|
+
stroke={hoverSelected ? selectionStroke : (hoverItem?.color || overlayRole(hoverRole))}
|
|
313
321
|
strokeWidth={widthOf(hoverShape, hoverRole, hoverSelected ? "selected" : "hover")}
|
|
314
322
|
/>
|
|
315
323
|
</>
|
|
@@ -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);
|