@vitavision/stage2d 0.13.0 → 0.14.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 +32 -7
- package/dist/index.js +46 -47
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/mapValues.ts +1 -4
- package/src/components/AreaSet.tsx +41 -9
- package/src/components/ContourEditor.tsx +48 -12
- package/src/components/DraftShape.tsx +37 -5
- package/src/components/EllipseSet.tsx +33 -51
- package/src/components/GridLayer.tsx +7 -1
- package/src/components/MaskEditor.tsx +57 -9
- package/src/components/MeasureOverlay.tsx +37 -9
- package/src/components/PointSet.tsx +30 -11
- package/src/components/PolylineSet.tsx +2 -10
- package/src/components/RectRoiEditor.tsx +1 -9
- package/src/components/ShapeEditor.tsx +12 -7
- package/src/components/areaBatching.ts +22 -2
- package/src/components/areaIndex.ts +2 -1
- package/src/components/labelLod.ts +1 -4
- package/src/components/markerShapes.ts +1 -3
- package/src/components/measureGeometry.ts +12 -27
- package/src/components/measureHit.ts +16 -4
- package/src/components/pointIndex.ts +12 -2
- package/src/components/stage/CompareLayer.tsx +4 -1
- package/src/components/stage/ImageLayer.tsx +1 -10
- package/src/components/stage/ImageStage.tsx +4 -30
- package/src/components/stage/StageSurface.tsx +33 -30
- package/src/components/stage/StageToolbar.tsx +9 -29
- package/src/components/stage/hitTest.ts +6 -2
- package/src/components/stage/useStageHitTest.ts +16 -14
- package/src/components/stage/view.ts +3 -15
- package/src/index.ts +11 -72
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/stage2d",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.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.14.0",
|
|
50
50
|
"lucide-react": "^1.48.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
package/src/api/mapValues.ts
CHANGED
|
@@ -107,10 +107,7 @@ export function valuesAt(plane: ValuePlane, u: number, v: number): number[] {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/** Where a value sits in a range, as a fraction — `null` for a degenerate range. */
|
|
110
|
-
export function fractionOf(
|
|
111
|
-
value: number,
|
|
112
|
-
range: { low: number; high: number } | null | undefined,
|
|
113
|
-
): number | null {
|
|
110
|
+
export function fractionOf(value: number, range: { low: number; high: number } | null | undefined): number | null {
|
|
114
111
|
if (!range || range.high <= range.low) return null;
|
|
115
112
|
return (value - range.low) / (range.high - range.low);
|
|
116
113
|
}
|
|
@@ -44,7 +44,7 @@ export interface AreaSetProps {
|
|
|
44
44
|
role?: OverlayRole | undefined;
|
|
45
45
|
/** The hovered id, when the app controls hover (a list beside the stage hovers regions too). */
|
|
46
46
|
hoveredId?: AreaId | null | undefined;
|
|
47
|
-
/** Called when the region under the pointer changes. `null` when it leaves every region. */
|
|
47
|
+
/** Called when the region under the pointer changes. `null` when it leaves every region. Not called while `pickable` is off; turning it off ends a hover with `null`. */
|
|
48
48
|
onHoverChange?: ((id: AreaId | null) => void) | undefined;
|
|
49
49
|
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
50
50
|
selectedIds?: Iterable<AreaId> | undefined;
|
|
@@ -97,6 +97,12 @@ export interface AreaSetProps {
|
|
|
97
97
|
halo?: boolean | undefined;
|
|
98
98
|
/** CSS colour of selected regions. Defaults to the overlay `selection` role. */
|
|
99
99
|
selectionStroke?: string | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
|
|
102
|
+
* decorative layer (a shaded backdrop under the regions that matter), so it neither hovers,
|
|
103
|
+
* nor claims presses, nor answers `useStageHitTest`.
|
|
104
|
+
*/
|
|
105
|
+
pickable?: boolean | undefined;
|
|
100
106
|
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
101
107
|
layerId?: string | undefined;
|
|
102
108
|
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.area`. */
|
|
@@ -127,6 +133,7 @@ const TICK_PX = 8;
|
|
|
127
133
|
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
128
134
|
* an outline picks that region, else the smallest region containing the point. `onHoverChange`
|
|
129
135
|
* and `onItemPress` here, and `useStageHitTest` for an app that arbitrates between layers.
|
|
136
|
+
* `pickable={false}` turns it off.
|
|
130
137
|
*
|
|
131
138
|
* The SVG carries `data-hovered` (the hovered id).
|
|
132
139
|
*/
|
|
@@ -138,6 +145,7 @@ export function AreaSet({
|
|
|
138
145
|
selectedIds,
|
|
139
146
|
dimmed,
|
|
140
147
|
onItemPress,
|
|
148
|
+
pickable = true,
|
|
141
149
|
labels = true,
|
|
142
150
|
firstVertexTick = false,
|
|
143
151
|
stroke,
|
|
@@ -155,7 +163,7 @@ export function AreaSet({
|
|
|
155
163
|
const px = useScreenPx();
|
|
156
164
|
const unit = px(1);
|
|
157
165
|
|
|
158
|
-
const index = useMemo(() => buildAreaIndex(items), [items]);
|
|
166
|
+
const index = useMemo(() => (pickable ? buildAreaIndex(items) : null), [items, pickable]);
|
|
159
167
|
const selectedSet = useMemo(() => new Set(selectedIds ?? []), [selectedIds]);
|
|
160
168
|
const isDimmed = useMemo(() => {
|
|
161
169
|
if (typeof dimmed === "function") return dimmed;
|
|
@@ -170,15 +178,16 @@ export function AreaSet({
|
|
|
170
178
|
useStageHitLayer({
|
|
171
179
|
layerId,
|
|
172
180
|
priority,
|
|
181
|
+
enabled: pickable,
|
|
173
182
|
pick: (point, radius) => {
|
|
174
|
-
const hit = nearestArea(index, point, radius);
|
|
183
|
+
const hit = index && nearestArea(index, point, radius);
|
|
175
184
|
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
176
185
|
},
|
|
177
186
|
onHover: (id) => {
|
|
178
187
|
setOwnHover(id);
|
|
179
188
|
onHoverChange?.(id);
|
|
180
189
|
},
|
|
181
|
-
onPress: onItemPress
|
|
190
|
+
onPress: onItemPress,
|
|
182
191
|
});
|
|
183
192
|
|
|
184
193
|
// Regions grouped by appearance. Rebuilt when the items or the selection change, never on
|
|
@@ -245,7 +254,9 @@ export function AreaSet({
|
|
|
245
254
|
/** Each region's fill as its own even-odd path, or all of them as one path. */
|
|
246
255
|
const fillPaths = (batch: AreaBatch, colour: string, tag: string) =>
|
|
247
256
|
evenOdd ? (
|
|
248
|
-
batch.fills!.map((d, k) =>
|
|
257
|
+
batch.fills!.map((d, k) => (
|
|
258
|
+
<path key={batch.indices[k]} data-fill={tag} d={d} fill={colour} fillOpacity={fillOpacity} fillRule="evenodd" />
|
|
259
|
+
))
|
|
249
260
|
) : (
|
|
250
261
|
<path data-fill={tag} d={batch.fills!.join("")} fill={colour} fillOpacity={fillOpacity} />
|
|
251
262
|
);
|
|
@@ -261,7 +272,9 @@ export function AreaSet({
|
|
|
261
272
|
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
262
273
|
{groups.normal.map((batch) => (
|
|
263
274
|
<g key={batch.id} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
264
|
-
{halo && (!separate || batch.d !== "") &&
|
|
275
|
+
{halo && (!separate || batch.d !== "") && (
|
|
276
|
+
<path d={batch.d} stroke={HALO} strokeWidth={widthOf(batch.role, "default") + px(2)} opacity={0.6} />
|
|
277
|
+
)}
|
|
265
278
|
{separate && fillPaths(batch, batch.fill ?? strokeOf(batch), batch.key)}
|
|
266
279
|
{(!separate || batch.d !== "") && (
|
|
267
280
|
<path
|
|
@@ -292,7 +305,11 @@ export function AreaSet({
|
|
|
292
305
|
{hoverItem && (
|
|
293
306
|
<>
|
|
294
307
|
{halo && (
|
|
295
|
-
<path
|
|
308
|
+
<path
|
|
309
|
+
d={areaPath(hoverItem.points)}
|
|
310
|
+
stroke={HALO}
|
|
311
|
+
strokeWidth={widthOf(hoverRole, hoverSelected ? "selected" : "hover") + px(2)}
|
|
312
|
+
/>
|
|
296
313
|
)}
|
|
297
314
|
<path
|
|
298
315
|
data-hovered-area=""
|
|
@@ -315,9 +332,24 @@ export function AreaSet({
|
|
|
315
332
|
))}
|
|
316
333
|
</g>
|
|
317
334
|
{shownLabels.length > 0 && (
|
|
318
|
-
<g
|
|
335
|
+
<g
|
|
336
|
+
data-labels=""
|
|
337
|
+
fontSize={px(11)}
|
|
338
|
+
className="font-mono"
|
|
339
|
+
stroke={HALO}
|
|
340
|
+
strokeWidth={px(3)}
|
|
341
|
+
paintOrder="stroke"
|
|
342
|
+
strokeLinejoin="round"
|
|
343
|
+
>
|
|
319
344
|
{shownLabels.map((i) => (
|
|
320
|
-
<text
|
|
345
|
+
<text
|
|
346
|
+
key={items[i]!.id}
|
|
347
|
+
x={anchors.xy[2 * i]}
|
|
348
|
+
y={anchors.xy[2 * i + 1]}
|
|
349
|
+
fill={LABEL}
|
|
350
|
+
textAnchor="middle"
|
|
351
|
+
dominantBaseline="central"
|
|
352
|
+
>
|
|
321
353
|
{items[i]!.label}
|
|
322
354
|
</text>
|
|
323
355
|
))}
|
|
@@ -107,7 +107,10 @@ function nearestSegment(points: Point[], point: Point, closed: boolean): { index
|
|
|
107
107
|
const dy = b.y - a.y;
|
|
108
108
|
const t = Math.max(0, Math.min(1, ((point.x - a.x) * dx + (point.y - a.y) * dy) / (dx * dx + dy * dy || 1)));
|
|
109
109
|
const d = (point.x - a.x - t * dx) ** 2 + (point.y - a.y - t * dy) ** 2;
|
|
110
|
-
if (d < distance) {
|
|
110
|
+
if (d < distance) {
|
|
111
|
+
distance = d;
|
|
112
|
+
nearest = i;
|
|
113
|
+
}
|
|
111
114
|
}
|
|
112
115
|
return { index: nearest, distance: Math.sqrt(distance) };
|
|
113
116
|
}
|
|
@@ -243,8 +246,10 @@ export function ContourEditor({
|
|
|
243
246
|
const p = points[index];
|
|
244
247
|
if (!p) return;
|
|
245
248
|
const shift: Record<string, Point> = {
|
|
246
|
-
ArrowLeft: { x: -delta, y: 0 },
|
|
247
|
-
|
|
249
|
+
ArrowLeft: { x: -delta, y: 0 },
|
|
250
|
+
ArrowRight: { x: delta, y: 0 },
|
|
251
|
+
ArrowUp: { x: 0, y: -delta },
|
|
252
|
+
ArrowDown: { x: 0, y: delta },
|
|
248
253
|
};
|
|
249
254
|
if (shift[event.key]) {
|
|
250
255
|
event.preventDefault();
|
|
@@ -281,8 +286,7 @@ export function ContourEditor({
|
|
|
281
286
|
const touch = event.pointerType === "touch";
|
|
282
287
|
let moved = false;
|
|
283
288
|
/** Whether the pointer has left the click slop: before that, the press edits nothing. */
|
|
284
|
-
const past = (e: globalThis.PointerEvent) =>
|
|
285
|
-
moved || (moved = Math.hypot(e.clientX - client.x, e.clientY - client.y) > CLICK_SLOP);
|
|
289
|
+
const past = (e: globalThis.PointerEvent) => moved || (moved = Math.hypot(e.clientX - client.x, e.clientY - client.y) > CLICK_SLOP);
|
|
286
290
|
setFootprint(from);
|
|
287
291
|
if (mode === "brush") {
|
|
288
292
|
const snapshot = points;
|
|
@@ -366,17 +370,49 @@ export function ContourEditor({
|
|
|
366
370
|
{preview && (
|
|
367
371
|
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
368
372
|
<polyline points={toAttribute(preview)} stroke={overlayRole("halo")} strokeWidth={px(4)} />
|
|
369
|
-
<polyline
|
|
373
|
+
<polyline
|
|
374
|
+
data-erase-preview=""
|
|
375
|
+
points={toAttribute(preview)}
|
|
376
|
+
stroke={ERASE}
|
|
377
|
+
strokeWidth={px(2.5)}
|
|
378
|
+
strokeDasharray={`${px(6)} ${px(4)}`}
|
|
379
|
+
/>
|
|
370
380
|
</g>
|
|
371
381
|
)}
|
|
372
|
-
{editable &&
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
382
|
+
{editable &&
|
|
383
|
+
mode === "vertex" &&
|
|
384
|
+
points.map((point, index) => (
|
|
385
|
+
// The contour API is an ordered point list without vertex IDs; indices are stable across drag edits.
|
|
386
|
+
<circle
|
|
387
|
+
// oxlint-disable-next-line react/no-array-index-key
|
|
388
|
+
key={index}
|
|
389
|
+
cx={point.x}
|
|
390
|
+
cy={point.y}
|
|
391
|
+
r={selected === index ? radius * 1.3 : radius}
|
|
392
|
+
fill="var(--surface)"
|
|
393
|
+
stroke={stroke}
|
|
394
|
+
strokeWidth={hairline}
|
|
395
|
+
className="pointer-events-auto cursor-move"
|
|
396
|
+
tabIndex={0}
|
|
397
|
+
role="button"
|
|
398
|
+
aria-label={`${label} point ${index + 1}`}
|
|
399
|
+
onPointerDown={(event) => pointerDown(event, index)}
|
|
400
|
+
onPointerMove={pointerMove}
|
|
401
|
+
onPointerUp={pointerUp}
|
|
402
|
+
onLostPointerCapture={pointerUp}
|
|
403
|
+
onDoubleClick={(event) => {
|
|
404
|
+
if (!stage.panMode) event.stopPropagation();
|
|
405
|
+
}}
|
|
406
|
+
onKeyDown={(event) => keyDown(event, index)}
|
|
407
|
+
onFocus={() => setSelected(index)}
|
|
408
|
+
/>
|
|
409
|
+
))}
|
|
377
410
|
</svg>
|
|
378
411
|
{tool && footprint && (
|
|
379
|
-
<DraftShape
|
|
412
|
+
<DraftShape
|
|
413
|
+
shape={{ kind: "brush", x: footprint.x, y: footprint.y, diameter: 2 * brushRadius }}
|
|
414
|
+
stroke={mode === "erase" ? ERASE : undefined}
|
|
415
|
+
/>
|
|
380
416
|
)}
|
|
381
417
|
</>
|
|
382
418
|
);
|
|
@@ -93,10 +93,21 @@ export function DraftShape({ shape, role = "selection", stroke, halo = true }: D
|
|
|
93
93
|
{parts.outline && halo && <path d={parts.outline} stroke={HALO} strokeWidth={width + px(2)} opacity={0.6} />}
|
|
94
94
|
{parts.outline && parts.region && <path d={parts.outline} fill={colour} fillOpacity={0.12} stroke="none" />}
|
|
95
95
|
{parts.outline && <path data-draft-outline="" d={parts.outline} stroke={colour} strokeWidth={width} strokeDasharray={dash} />}
|
|
96
|
-
{parts.trail &&
|
|
96
|
+
{parts.trail && (
|
|
97
|
+
<path data-draft-stroke="" d={parts.trail.d} stroke={colour} strokeWidth={Math.max(parts.trail.width, px(1))} opacity={0.4} />
|
|
98
|
+
)}
|
|
97
99
|
{parts.footprint && halo && <path d={parts.footprint} stroke={HALO} strokeWidth={px(3.5)} opacity={0.6} />}
|
|
98
100
|
{parts.footprint && <path data-draft-brush="" d={parts.footprint} stroke={colour} strokeWidth={px(1.5)} />}
|
|
99
|
-
{parts.closing &&
|
|
101
|
+
{parts.closing && (
|
|
102
|
+
<path
|
|
103
|
+
data-draft-closing=""
|
|
104
|
+
d={parts.closing}
|
|
105
|
+
stroke={colour}
|
|
106
|
+
strokeWidth={parts.armed ? width : px(1)}
|
|
107
|
+
strokeDasharray={parts.armed ? undefined : dash}
|
|
108
|
+
opacity={parts.armed ? 1 : 0.5}
|
|
109
|
+
/>
|
|
110
|
+
)}
|
|
100
111
|
{parts.first && <path data-draft-first="" d={parts.first} stroke={colour} strokeWidth={width} fill={colour} fillOpacity={0.35} />}
|
|
101
112
|
{parts.vertices && <path data-draft-vertices="" d={parts.vertices} stroke={colour} strokeWidth={px(VERTEX_PX)} />}
|
|
102
113
|
</g>
|
|
@@ -125,7 +136,16 @@ interface DraftParts {
|
|
|
125
136
|
footprint: string | null;
|
|
126
137
|
}
|
|
127
138
|
|
|
128
|
-
const NONE: DraftParts = {
|
|
139
|
+
const NONE: DraftParts = {
|
|
140
|
+
outline: null,
|
|
141
|
+
region: false,
|
|
142
|
+
closing: null,
|
|
143
|
+
vertices: null,
|
|
144
|
+
armed: false,
|
|
145
|
+
first: null,
|
|
146
|
+
trail: null,
|
|
147
|
+
footprint: null,
|
|
148
|
+
};
|
|
129
149
|
|
|
130
150
|
/** An ellipse with the given centre and radii as two arcs. */
|
|
131
151
|
function ellipsePath(cx: number, cy: number, rx: number, ry: number): string {
|
|
@@ -148,7 +168,10 @@ function draftParts(shape: DraftShapeSpec, unit: number): DraftParts {
|
|
|
148
168
|
return { ...NONE, outline, region: true };
|
|
149
169
|
}
|
|
150
170
|
case "brush":
|
|
151
|
-
return {
|
|
171
|
+
return {
|
|
172
|
+
...NONE,
|
|
173
|
+
footprint: ellipsePath(shape.x, shape.y, Math.max(shape.diameter, 2 * unit) / 2, Math.max(shape.diameter, 2 * unit) / 2),
|
|
174
|
+
};
|
|
152
175
|
case "stroke": {
|
|
153
176
|
const n = Math.floor(shape.points.length / 2);
|
|
154
177
|
if (n === 0) return NONE;
|
|
@@ -213,7 +236,16 @@ export function MarqueeRect({ rect, stroke = overlayRole("selection") }: Marquee
|
|
|
213
236
|
data-marquee={rect ? "" : undefined}
|
|
214
237
|
>
|
|
215
238
|
{rect && (
|
|
216
|
-
<rect
|
|
239
|
+
<rect
|
|
240
|
+
x={rect.x}
|
|
241
|
+
y={rect.y}
|
|
242
|
+
width={rect.width}
|
|
243
|
+
height={rect.height}
|
|
244
|
+
fill={stroke}
|
|
245
|
+
fillOpacity={0.12}
|
|
246
|
+
stroke={stroke}
|
|
247
|
+
strokeWidth={px(1)}
|
|
248
|
+
/>
|
|
217
249
|
)}
|
|
218
250
|
</svg>
|
|
219
251
|
);
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { useMemo, useState } from "react";
|
|
13
13
|
|
|
14
|
-
import { buildEllipseIndex, ellipsePath, nearestEllipse, type Ellipse, type EllipseId
|
|
14
|
+
import { buildEllipseIndex, ellipsePath, nearestEllipse, type Ellipse, type EllipseId } from "./ellipseIndex";
|
|
15
15
|
import { overlayRole, OVERLAY_STATE_OPACITY, OVERLAY_STATE_WIDTH, type OverlayRole } from "./overlayRole";
|
|
16
16
|
import { useStage } from "./stage/ImageStage";
|
|
17
17
|
import type { StagePointerEvent } from "./stage/hitContext";
|
|
@@ -24,6 +24,12 @@ import { imageViewBox } from "./stage/view";
|
|
|
24
24
|
export interface EllipseSetItem extends Ellipse {
|
|
25
25
|
/** The overlay role it is painted in. Defaults to the layer's `role`. */
|
|
26
26
|
role?: OverlayRole | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* CSS colour of its outline (`#hex`, `var(--x)`, `color-mix(…)`; used as given), so a layer can
|
|
29
|
+
* encode a scalar per ellipse. Overrides the role; selection still paints in `selectionStroke`.
|
|
30
|
+
* Ellipses sharing a colour share a path, so quantise a continuous scale to a few dozen steps.
|
|
31
|
+
*/
|
|
32
|
+
color?: string | undefined;
|
|
27
33
|
/** Draw it dashed (4 3 screen px), so two kinds differ by more than colour. */
|
|
28
34
|
dashed?: boolean | undefined;
|
|
29
35
|
/** Opacity in [0, 1], rounded to a tenth so ellipses still share paths. Defaults to 1. */
|
|
@@ -38,7 +44,7 @@ export interface EllipseSetProps {
|
|
|
38
44
|
role?: OverlayRole | undefined;
|
|
39
45
|
/** The hovered id, when the app controls hover: its ellipses are drawn at 2 px. */
|
|
40
46
|
hoveredId?: EllipseId | null | undefined;
|
|
41
|
-
/** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called
|
|
47
|
+
/** Called when the ellipse under the pointer changes. `null` when it leaves every ellipse. Not called while `pickable` is off; turning it off ends a hover with `null`. */
|
|
42
48
|
onHoverChange?: ((id: EllipseId | null) => void) | undefined;
|
|
43
49
|
/** The selected ids, drawn at 2.5 px in the selection colour. */
|
|
44
50
|
selectedIds?: Iterable<EllipseId> | undefined;
|
|
@@ -90,40 +96,12 @@ function batchPath(items: readonly EllipseSetItem[], indices: readonly number[],
|
|
|
90
96
|
return d;
|
|
91
97
|
}
|
|
92
98
|
|
|
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
99
|
/** The ellipses of one batched path. */
|
|
124
100
|
interface Batch {
|
|
125
101
|
key: string;
|
|
126
102
|
role: OverlayRole;
|
|
103
|
+
/** The custom colour, when the items of this batch have one. */
|
|
104
|
+
color: string | undefined;
|
|
127
105
|
dashed: boolean;
|
|
128
106
|
opacity: number;
|
|
129
107
|
dim: boolean;
|
|
@@ -138,7 +116,7 @@ interface Batch {
|
|
|
138
116
|
* the stroke is a screen constant.
|
|
139
117
|
* - **States**: 1.5 screen px (1 for `model` and `structure`), hover
|
|
140
118
|
* 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.
|
|
119
|
+
* - **Batching** is by (role, colour, dash, opacity, dimming): a thousand rings are a dozen elements.
|
|
142
120
|
* - **Picking** goes through the stage's hit registry: a press within the pointer's tolerance of
|
|
143
121
|
* an outline picks that ellipse, else the smallest ellipse containing the point.
|
|
144
122
|
* `onHoverChange` and `onItemPress` here, and `useStageHitTest` for an app that arbitrates
|
|
@@ -175,24 +153,40 @@ export function EllipseSet({
|
|
|
175
153
|
|
|
176
154
|
const index = useMemo(() => (pickable ? buildEllipseIndex(items) : null), [items, pickable]);
|
|
177
155
|
const [ownHover, setOwnHover] = useState<EllipseId | null>(null);
|
|
178
|
-
const hoverId = hoveredId !== undefined ? hoveredId :
|
|
156
|
+
const hoverId = hoveredId !== undefined ? hoveredId : ownHover;
|
|
157
|
+
|
|
158
|
+
useStageHitLayer({
|
|
159
|
+
layerId,
|
|
160
|
+
priority,
|
|
161
|
+
enabled: pickable,
|
|
162
|
+
pick: (point, radius) => {
|
|
163
|
+
const hit = index && nearestEllipse(index, point, radius);
|
|
164
|
+
return hit ? { id: hit.id, dist: hit.inside ? 0 : hit.distance } : null;
|
|
165
|
+
},
|
|
166
|
+
onHover: (id) => {
|
|
167
|
+
setOwnHover(id);
|
|
168
|
+
onHoverChange?.(id);
|
|
169
|
+
},
|
|
170
|
+
onPress: onItemPress,
|
|
171
|
+
});
|
|
179
172
|
|
|
180
173
|
const batches = useMemo(() => {
|
|
181
174
|
const normal = new Map<string, Batch>();
|
|
182
|
-
const picked: Batch = { key: "selected", role: "selection", dashed: false, opacity: 1, dim: false, indices: [] };
|
|
175
|
+
const picked: Batch = { key: "selected", role: "selection", color: undefined, dashed: false, opacity: 1, dim: false, indices: [] };
|
|
183
176
|
items.forEach((item, i) => {
|
|
184
177
|
if (selectedSet.has(item.id)) {
|
|
185
178
|
picked.indices.push(i);
|
|
186
179
|
return;
|
|
187
180
|
}
|
|
188
181
|
const itemRole = item.role ?? role;
|
|
182
|
+
const color = item.color || undefined;
|
|
189
183
|
const dashed = item.dashed ?? false;
|
|
190
184
|
const opacity = Math.round(Math.min(1, Math.max(0, item.opacity ?? 1)) * 10) / 10;
|
|
191
185
|
const dim = isDimmed(item.id);
|
|
192
|
-
const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}`;
|
|
186
|
+
const key = `${dim ? 1 : 0}|${itemRole}|${dashed ? "dashed" : "solid"}|${opacity}${color ? `|${color}` : ""}`;
|
|
193
187
|
let batch = normal.get(key);
|
|
194
188
|
if (!batch) {
|
|
195
|
-
batch = { key, role: itemRole, dashed, opacity, dim, indices: [] };
|
|
189
|
+
batch = { key, role: itemRole, color, dashed, opacity, dim, indices: [] };
|
|
196
190
|
normal.set(key, batch);
|
|
197
191
|
}
|
|
198
192
|
batch.indices.push(i);
|
|
@@ -230,18 +224,6 @@ export function EllipseSet({
|
|
|
230
224
|
data-ellipses={items.length}
|
|
231
225
|
data-hovered={hoverId ?? undefined}
|
|
232
226
|
>
|
|
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
227
|
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
246
228
|
{normalPaths.map(({ batch, d }) => (
|
|
247
229
|
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
@@ -257,7 +239,7 @@ export function EllipseSet({
|
|
|
257
239
|
<path
|
|
258
240
|
data-batch={batch.key}
|
|
259
241
|
d={d}
|
|
260
|
-
stroke={overlayRole(batch.role)}
|
|
242
|
+
stroke={batch.color ?? overlayRole(batch.role)}
|
|
261
243
|
strokeWidth={widthOf(batch.role, "default")}
|
|
262
244
|
strokeDasharray={batch.dashed ? dash : undefined}
|
|
263
245
|
opacity={batch.opacity}
|
|
@@ -280,7 +262,7 @@ export function EllipseSet({
|
|
|
280
262
|
<path
|
|
281
263
|
data-hovered-ellipse=""
|
|
282
264
|
d={outlineOf(item, floor)}
|
|
283
|
-
stroke={hoverSelected ? selectionStroke : overlayRole(itemRole)}
|
|
265
|
+
stroke={hoverSelected ? selectionStroke : item.color || overlayRole(itemRole)}
|
|
284
266
|
strokeWidth={widthOf(itemRole, state)}
|
|
285
267
|
/>
|
|
286
268
|
</g>
|
|
@@ -181,7 +181,13 @@ export function GridLayer({
|
|
|
181
181
|
return (
|
|
182
182
|
<g key={batch.key} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
183
183
|
{halo && <path d={batch.d} stroke={HALO} strokeWidth={width + px(2)} opacity={0.6} />}
|
|
184
|
-
<path
|
|
184
|
+
<path
|
|
185
|
+
data-edge-batch={batch.key}
|
|
186
|
+
d={batch.d}
|
|
187
|
+
stroke={overlayRole(style.role)}
|
|
188
|
+
strokeWidth={width}
|
|
189
|
+
strokeDasharray={dash}
|
|
190
|
+
/>
|
|
185
191
|
</g>
|
|
186
192
|
);
|
|
187
193
|
})}
|
|
@@ -7,9 +7,18 @@ import { useStage } from "./stage/ImageStage";
|
|
|
7
7
|
import type { Point } from "./measureGeometry";
|
|
8
8
|
|
|
9
9
|
/** Paint a continuous round stroke into a copied binary mask. Pixel centers are integers. */
|
|
10
|
-
export function paintMask(
|
|
10
|
+
export function paintMask(
|
|
11
|
+
mask: Uint8Array,
|
|
12
|
+
width: number,
|
|
13
|
+
height: number,
|
|
14
|
+
from: Point,
|
|
15
|
+
to: Point,
|
|
16
|
+
radius: number,
|
|
17
|
+
value: 0 | 1,
|
|
18
|
+
): Uint8Array {
|
|
11
19
|
if (mask.length !== width * height) throw new RangeError("Mask dimensions do not match its pixel data.");
|
|
12
|
-
if (![from.x, from.y, to.x, to.y, radius].every(Number.isFinite) || radius < 0)
|
|
20
|
+
if (![from.x, from.y, to.x, to.y, radius].every(Number.isFinite) || radius < 0)
|
|
21
|
+
throw new RangeError("Brush coordinates and radius must be finite and nonnegative.");
|
|
13
22
|
const next = mask.slice();
|
|
14
23
|
const clamp = (value: number, limit: number) => Math.max(0, Math.min(limit - 1, value));
|
|
15
24
|
const start = { x: clamp(from.x, width), y: clamp(from.y, height) };
|
|
@@ -18,8 +27,8 @@ export function paintMask(mask: Uint8Array, width: number, height: number, from:
|
|
|
18
27
|
// A click between four pixel centers must still affect the nearest pixels.
|
|
19
28
|
const r = Math.max(0.75, radius);
|
|
20
29
|
for (let step = 0; step <= steps; step++) {
|
|
21
|
-
const x = start.x + (end.x - start.x) * step / steps;
|
|
22
|
-
const y = start.y + (end.y - start.y) * step / steps;
|
|
30
|
+
const x = start.x + ((end.x - start.x) * step) / steps;
|
|
31
|
+
const y = start.y + ((end.y - start.y) * step) / steps;
|
|
23
32
|
for (let row = Math.max(0, Math.ceil(y - r)); row <= Math.min(height - 1, Math.floor(y + r)); row++) {
|
|
24
33
|
for (let col = Math.max(0, Math.ceil(x - r)); col <= Math.min(width - 1, Math.floor(x + r)); col++) {
|
|
25
34
|
if ((col - x) ** 2 + (row - y) ** 2 <= r * r) next[row * width + col] = value;
|
|
@@ -54,7 +63,16 @@ export interface MaskEditorProps {
|
|
|
54
63
|
* `DraftShape` brush footprint (a circle of the brush's diameter) follows the mouse or pen
|
|
55
64
|
* pointer, and the keyboard brush while the layer has keyboard focus.
|
|
56
65
|
*/
|
|
57
|
-
export function MaskEditor({
|
|
66
|
+
export function MaskEditor({
|
|
67
|
+
mask,
|
|
68
|
+
onChange,
|
|
69
|
+
onCommit,
|
|
70
|
+
editable = false,
|
|
71
|
+
brushRadius = 3,
|
|
72
|
+
mode = "paint",
|
|
73
|
+
label = "Mask brush",
|
|
74
|
+
color = [0, 190, 220, 128],
|
|
75
|
+
}: MaskEditorProps) {
|
|
58
76
|
const stage = useStage();
|
|
59
77
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
60
78
|
const draftRef = useRef(mask);
|
|
@@ -128,8 +146,10 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
128
146
|
function keyDown(event: KeyboardEvent<HTMLCanvasElement>) {
|
|
129
147
|
if (!editable || stage.panMode) return;
|
|
130
148
|
const shift: Record<string, Point> = {
|
|
131
|
-
ArrowLeft: { x: -1, y: 0 },
|
|
132
|
-
|
|
149
|
+
ArrowLeft: { x: -1, y: 0 },
|
|
150
|
+
ArrowRight: { x: 1, y: 0 },
|
|
151
|
+
ArrowUp: { x: 0, y: -1 },
|
|
152
|
+
ArrowDown: { x: 0, y: 1 },
|
|
133
153
|
};
|
|
134
154
|
const delta = shift[event.key];
|
|
135
155
|
if (delta) {
|
|
@@ -160,8 +180,36 @@ export function MaskEditor({ mask, onChange, onCommit, editable = false, brushRa
|
|
|
160
180
|
|
|
161
181
|
return (
|
|
162
182
|
<>
|
|
163
|
-
<canvas
|
|
164
|
-
|
|
183
|
+
<canvas
|
|
184
|
+
ref={canvasRef}
|
|
185
|
+
width={stage.image.width}
|
|
186
|
+
height={stage.image.height}
|
|
187
|
+
className={`absolute inset-0 h-full w-full ${editable ? "pointer-events-auto cursor-crosshair" : "pointer-events-none"}`}
|
|
188
|
+
style={{ imageRendering: "pixelated" }}
|
|
189
|
+
role={editable ? "button" : "img"}
|
|
190
|
+
tabIndex={editable ? 0 : undefined}
|
|
191
|
+
aria-label={label}
|
|
192
|
+
aria-description={editable ? "Arrow keys move the brush; Enter paints or erases." : undefined}
|
|
193
|
+
onPointerDown={down}
|
|
194
|
+
onPointerMove={move}
|
|
195
|
+
onPointerUp={up}
|
|
196
|
+
onLostPointerCapture={up}
|
|
197
|
+
onDoubleClick={(event) => {
|
|
198
|
+
if (editable && !stage.panMode) event.stopPropagation();
|
|
199
|
+
}}
|
|
200
|
+
onKeyDown={keyDown}
|
|
201
|
+
onFocus={focus}
|
|
202
|
+
onBlur={() => setBrush(null)}
|
|
203
|
+
onPointerLeave={() => {
|
|
204
|
+
if (!lastRef.current) setHover(null);
|
|
205
|
+
}}
|
|
206
|
+
/>
|
|
207
|
+
{(hover ?? brush) && editable && (
|
|
208
|
+
<DraftShape
|
|
209
|
+
shape={{ kind: "brush", x: (hover ?? brush)!.x, y: (hover ?? brush)!.y, diameter: 2 * Math.max(0.75, brushRadius) }}
|
|
210
|
+
stroke="var(--signal)"
|
|
211
|
+
/>
|
|
212
|
+
)}
|
|
165
213
|
</>
|
|
166
214
|
);
|
|
167
215
|
}
|