@vitavision/stage2d 0.13.0 → 0.13.2
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.js.map +1 -1
- package/package.json +2 -2
- package/src/api/mapValues.ts +1 -4
- package/src/components/AreaSet.tsx +28 -5
- package/src/components/ContourEditor.tsx +48 -12
- package/src/components/DraftShape.tsx +37 -5
- package/src/components/GridLayer.tsx +7 -1
- package/src/components/MaskEditor.tsx +57 -9
- package/src/components/MeasureOverlay.tsx +36 -6
- package/src/components/PointSet.tsx +19 -8
- 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/useStageHitTest.ts +6 -12
- 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.13.
|
|
3
|
+
"version": "0.13.2",
|
|
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
|
}
|
|
@@ -245,7 +245,9 @@ export function AreaSet({
|
|
|
245
245
|
/** Each region's fill as its own even-odd path, or all of them as one path. */
|
|
246
246
|
const fillPaths = (batch: AreaBatch, colour: string, tag: string) =>
|
|
247
247
|
evenOdd ? (
|
|
248
|
-
batch.fills!.map((d, k) =>
|
|
248
|
+
batch.fills!.map((d, k) => (
|
|
249
|
+
<path key={batch.indices[k]} data-fill={tag} d={d} fill={colour} fillOpacity={fillOpacity} fillRule="evenodd" />
|
|
250
|
+
))
|
|
249
251
|
) : (
|
|
250
252
|
<path data-fill={tag} d={batch.fills!.join("")} fill={colour} fillOpacity={fillOpacity} />
|
|
251
253
|
);
|
|
@@ -261,7 +263,9 @@ export function AreaSet({
|
|
|
261
263
|
<g fill="none" strokeLinecap="round" strokeLinejoin="round">
|
|
262
264
|
{groups.normal.map((batch) => (
|
|
263
265
|
<g key={batch.id} opacity={batch.dim ? OVERLAY_STATE_OPACITY.dimmed : undefined}>
|
|
264
|
-
{halo && (!separate || batch.d !== "") &&
|
|
266
|
+
{halo && (!separate || batch.d !== "") && (
|
|
267
|
+
<path d={batch.d} stroke={HALO} strokeWidth={widthOf(batch.role, "default") + px(2)} opacity={0.6} />
|
|
268
|
+
)}
|
|
265
269
|
{separate && fillPaths(batch, batch.fill ?? strokeOf(batch), batch.key)}
|
|
266
270
|
{(!separate || batch.d !== "") && (
|
|
267
271
|
<path
|
|
@@ -292,7 +296,11 @@ export function AreaSet({
|
|
|
292
296
|
{hoverItem && (
|
|
293
297
|
<>
|
|
294
298
|
{halo && (
|
|
295
|
-
<path
|
|
299
|
+
<path
|
|
300
|
+
d={areaPath(hoverItem.points)}
|
|
301
|
+
stroke={HALO}
|
|
302
|
+
strokeWidth={widthOf(hoverRole, hoverSelected ? "selected" : "hover") + px(2)}
|
|
303
|
+
/>
|
|
296
304
|
)}
|
|
297
305
|
<path
|
|
298
306
|
data-hovered-area=""
|
|
@@ -315,9 +323,24 @@ export function AreaSet({
|
|
|
315
323
|
))}
|
|
316
324
|
</g>
|
|
317
325
|
{shownLabels.length > 0 && (
|
|
318
|
-
<g
|
|
326
|
+
<g
|
|
327
|
+
data-labels=""
|
|
328
|
+
fontSize={px(11)}
|
|
329
|
+
className="font-mono"
|
|
330
|
+
stroke={HALO}
|
|
331
|
+
strokeWidth={px(3)}
|
|
332
|
+
paintOrder="stroke"
|
|
333
|
+
strokeLinejoin="round"
|
|
334
|
+
>
|
|
319
335
|
{shownLabels.map((i) => (
|
|
320
|
-
<text
|
|
336
|
+
<text
|
|
337
|
+
key={items[i]!.id}
|
|
338
|
+
x={anchors.xy[2 * i]}
|
|
339
|
+
y={anchors.xy[2 * i + 1]}
|
|
340
|
+
fill={LABEL}
|
|
341
|
+
textAnchor="middle"
|
|
342
|
+
dominantBaseline="central"
|
|
343
|
+
>
|
|
321
344
|
{items[i]!.label}
|
|
322
345
|
</text>
|
|
323
346
|
))}
|
|
@@ -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
|
);
|
|
@@ -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
|
}
|
|
@@ -372,7 +372,7 @@ export function MeasureOverlay({
|
|
|
372
372
|
return (
|
|
373
373
|
<g
|
|
374
374
|
// Primitives carry no required identity; a position is the only stable key.
|
|
375
|
-
//
|
|
375
|
+
// oxlint-disable-next-line react/no-array-index-key
|
|
376
376
|
key={index}
|
|
377
377
|
data-kind={primitive.kind}
|
|
378
378
|
data-id={primitive.id}
|
|
@@ -536,9 +536,22 @@ function Primitive({
|
|
|
536
536
|
const p = primitive.points;
|
|
537
537
|
return (
|
|
538
538
|
<g>
|
|
539
|
-
<path
|
|
539
|
+
<path
|
|
540
|
+
d={segmentsPath(p)}
|
|
541
|
+
fill="none"
|
|
542
|
+
stroke={colour}
|
|
543
|
+
strokeWidth={hairline}
|
|
544
|
+
strokeDasharray={primitive.dashed ? dash : undefined}
|
|
545
|
+
/>
|
|
540
546
|
{p.length >= 4 && (
|
|
541
|
-
<Label
|
|
547
|
+
<Label
|
|
548
|
+
x={p[0]!}
|
|
549
|
+
y={p[1]! - strokeWidthFor(strokeScale, 4)}
|
|
550
|
+
text={primitive.label}
|
|
551
|
+
colour={colour}
|
|
552
|
+
size={labelSize}
|
|
553
|
+
halo={labelHalo}
|
|
554
|
+
/>
|
|
542
555
|
)}
|
|
543
556
|
</g>
|
|
544
557
|
);
|
|
@@ -615,9 +628,26 @@ function Primitive({
|
|
|
615
628
|
const geometry = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, offset);
|
|
616
629
|
return (
|
|
617
630
|
<g stroke={colour} strokeWidth={hairline}>
|
|
618
|
-
<line
|
|
619
|
-
|
|
620
|
-
|
|
631
|
+
<line
|
|
632
|
+
x1={geometry.extensionLine1[0].x}
|
|
633
|
+
y1={geometry.extensionLine1[0].y}
|
|
634
|
+
x2={geometry.extensionLine1[1].x}
|
|
635
|
+
y2={geometry.extensionLine1[1].y}
|
|
636
|
+
opacity={0.5}
|
|
637
|
+
/>
|
|
638
|
+
<line
|
|
639
|
+
x1={geometry.extensionLine2[0].x}
|
|
640
|
+
y1={geometry.extensionLine2[0].y}
|
|
641
|
+
x2={geometry.extensionLine2[1].x}
|
|
642
|
+
y2={geometry.extensionLine2[1].y}
|
|
643
|
+
opacity={0.5}
|
|
644
|
+
/>
|
|
645
|
+
<line
|
|
646
|
+
x1={geometry.dimensionLine[0].x}
|
|
647
|
+
y1={geometry.dimensionLine[0].y}
|
|
648
|
+
x2={geometry.dimensionLine[1].x}
|
|
649
|
+
y2={geometry.dimensionLine[1].y}
|
|
650
|
+
/>
|
|
621
651
|
{labelSize > 0 && (
|
|
622
652
|
<text
|
|
623
653
|
x={geometry.labelAnchor.x}
|
|
@@ -287,12 +287,14 @@ export function PointSet({
|
|
|
287
287
|
))}
|
|
288
288
|
{halo &&
|
|
289
289
|
paths.picked.map(({ batch, d }) => (
|
|
290
|
-
<path
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
290
|
+
<path
|
|
291
|
+
key={`halo|selected|${batch.key}`}
|
|
292
|
+
d={d}
|
|
293
|
+
stroke={HALO}
|
|
294
|
+
strokeWidth={widthOf(batch.shape, batch.role, "selected") + px(2)}
|
|
295
|
+
/>
|
|
295
296
|
))}
|
|
297
|
+
{halo && paths.rings.map(({ batch, d }) => <path key={`halo|${batch.key}`} d={d} stroke={HALO} strokeWidth={ringWidth + px(2)} />)}
|
|
296
298
|
{paths.picked.map(({ batch, d }) => (
|
|
297
299
|
<path
|
|
298
300
|
key={`selected|${batch.key}`}
|
|
@@ -317,14 +319,22 @@ export function PointSet({
|
|
|
317
319
|
<path
|
|
318
320
|
data-hovered-point=""
|
|
319
321
|
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0, hoverItem.angle2)}
|
|
320
|
-
stroke={hoverSelected ? selectionStroke :
|
|
322
|
+
stroke={hoverSelected ? selectionStroke : hoverItem?.color || overlayRole(hoverRole)}
|
|
321
323
|
strokeWidth={widthOf(hoverShape, hoverRole, hoverSelected ? "selected" : "hover")}
|
|
322
324
|
/>
|
|
323
325
|
</>
|
|
324
326
|
)}
|
|
325
327
|
</g>
|
|
326
328
|
{shownLabels.length > 0 && (
|
|
327
|
-
<g
|
|
329
|
+
<g
|
|
330
|
+
data-labels=""
|
|
331
|
+
fontSize={px(11)}
|
|
332
|
+
className="font-mono"
|
|
333
|
+
stroke={HALO}
|
|
334
|
+
strokeWidth={px(3)}
|
|
335
|
+
paintOrder="stroke"
|
|
336
|
+
strokeLinejoin="round"
|
|
337
|
+
>
|
|
328
338
|
{shownLabels.map((i) => {
|
|
329
339
|
const item = items[i]!;
|
|
330
340
|
return (
|
|
@@ -351,7 +361,8 @@ function batchPath(batch: Batch, items: readonly PointSetItem[], unit: number, c
|
|
|
351
361
|
let d = "";
|
|
352
362
|
for (const i of batch.indices) {
|
|
353
363
|
const item = items[i]!;
|
|
354
|
-
if (window && (item.x < window.x || item.x > window.x + window.width || item.y < window.y || item.y > window.y + window.height))
|
|
364
|
+
if (window && (item.x < window.x || item.x > window.x + window.width || item.y < window.y || item.y > window.y + window.height))
|
|
365
|
+
continue;
|
|
355
366
|
d += batch.shape.path(item.x, item.y, unit, item.angle ?? 0, item.angle2);
|
|
356
367
|
}
|
|
357
368
|
batch.cache = { key, d };
|
|
@@ -11,14 +11,7 @@
|
|
|
11
11
|
import { useEffect, useImperativeHandle, useMemo, useRef, useState, type PointerEvent as ReactPointerEvent, type Ref } from "react";
|
|
12
12
|
|
|
13
13
|
import type { Point } from "./measureGeometry";
|
|
14
|
-
import {
|
|
15
|
-
buildPolylineIndex,
|
|
16
|
-
nearestPolyline,
|
|
17
|
-
polylinePath,
|
|
18
|
-
polylinesInRect,
|
|
19
|
-
type Polyline,
|
|
20
|
-
type PolylineId,
|
|
21
|
-
} from "./polylineIndex";
|
|
14
|
+
import { buildPolylineIndex, nearestPolyline, polylinePath, polylinesInRect, type Polyline, type PolylineId } from "./polylineIndex";
|
|
22
15
|
import { overlayRole } from "./overlayRole";
|
|
23
16
|
import type { StagePointerEvent } from "./stage/hitContext";
|
|
24
17
|
import { useStage } from "./stage/ImageStage";
|
|
@@ -195,8 +188,7 @@ export function PolylineSet({
|
|
|
195
188
|
// A touch still being watched when the layer goes away is cancelled, not left listening.
|
|
196
189
|
useEffect(() => () => watchRef.current?.(), []);
|
|
197
190
|
const takesPresses =
|
|
198
|
-
interactive ??
|
|
199
|
-
(onItemPress !== undefined || onSelect !== undefined || onHover !== undefined || onHoverChange !== undefined || marquee);
|
|
191
|
+
interactive ?? (onItemPress !== undefined || onSelect !== undefined || onHover !== undefined || onHoverChange !== undefined || marquee);
|
|
200
192
|
const px = useScreenPx();
|
|
201
193
|
const { hitTestAll } = useStageHitTest();
|
|
202
194
|
|
|
@@ -16,15 +16,7 @@ import { useImperativeHandle, useState, type KeyboardEvent, type PointerEvent, t
|
|
|
16
16
|
|
|
17
17
|
import { cn } from "@vitavision/ui";
|
|
18
18
|
import type { Point } from "./measureGeometry";
|
|
19
|
-
import {
|
|
20
|
-
ROI_HANDLES,
|
|
21
|
-
ROI_HANDLE_CURSOR,
|
|
22
|
-
clampRect,
|
|
23
|
-
moveRect,
|
|
24
|
-
rectFromCorners,
|
|
25
|
-
resizeRect,
|
|
26
|
-
roiHandlePoint,
|
|
27
|
-
} from "./roiEdit";
|
|
19
|
+
import { ROI_HANDLES, ROI_HANDLE_CURSOR, clampRect, moveRect, rectFromCorners, resizeRect, roiHandlePoint } from "./roiEdit";
|
|
28
20
|
import { overlayRole } from "./overlayRole";
|
|
29
21
|
import { shapePress, type RotatedShape } from "./shapeEdit";
|
|
30
22
|
import { CLICK_SLOP, POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
@@ -67,11 +67,7 @@ export interface ShapeEditorProps {
|
|
|
67
67
|
stroke?: string | undefined;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
type Drag = (
|
|
71
|
-
| { kind: "move"; from: Point }
|
|
72
|
-
| { kind: "resize"; handle: RoiHandle }
|
|
73
|
-
| { kind: "rotate" }
|
|
74
|
-
) & {
|
|
70
|
+
type Drag = ({ kind: "move"; from: Point } | { kind: "resize"; handle: RoiHandle } | { kind: "rotate" }) & {
|
|
75
71
|
start: RotatedShape;
|
|
76
72
|
/** Where the press landed, in client coordinates: a move counts once it is past the click slop. */
|
|
77
73
|
client: Point;
|
|
@@ -265,7 +261,9 @@ export function ShapeEditor({
|
|
|
265
261
|
data-editable={editable ? "" : undefined}
|
|
266
262
|
>
|
|
267
263
|
{value && <Outline kind={kind} shape={value} fill="none" stroke={HALO} strokeWidth={px(STROKE_PX + 2 * HALO_PX)} />}
|
|
268
|
-
{value &&
|
|
264
|
+
{value && (
|
|
265
|
+
<Outline kind={kind} shape={value} data-shape={kind} fill={stroke} fillOpacity={0.12} stroke={stroke} strokeWidth={px(STROKE_PX)} />
|
|
266
|
+
)}
|
|
269
267
|
{kind === "ellipse" && value && (
|
|
270
268
|
<rect
|
|
271
269
|
x={value.cx - value.width / 2}
|
|
@@ -364,7 +362,14 @@ type OutlineProps = Omit<SVGProps<SVGElement>, "ref"> & Record<`data-${string}`,
|
|
|
364
362
|
function Outline({ kind, shape, ...rest }: OutlineProps & { kind: "rect" | "ellipse"; shape: RotatedShape }) {
|
|
365
363
|
const transform = turned(shape);
|
|
366
364
|
return kind === "rect" ? (
|
|
367
|
-
<rect
|
|
365
|
+
<rect
|
|
366
|
+
x={shape.cx - shape.width / 2}
|
|
367
|
+
y={shape.cy - shape.height / 2}
|
|
368
|
+
width={shape.width}
|
|
369
|
+
height={shape.height}
|
|
370
|
+
transform={transform}
|
|
371
|
+
{...rest}
|
|
372
|
+
/>
|
|
368
373
|
) : (
|
|
369
374
|
<ellipse cx={shape.cx} cy={shape.cy} rx={shape.width / 2} ry={shape.height / 2} transform={transform} {...rest} />
|
|
370
375
|
);
|
|
@@ -81,7 +81,17 @@ export function batchAreas(items: readonly BatchableArea[], options: AreaBatchOp
|
|
|
81
81
|
const separate = fillRule === "evenodd" || keepSelected;
|
|
82
82
|
const byKey = new Map<string, AreaBatch>();
|
|
83
83
|
const runs: AreaBatch[] = [];
|
|
84
|
-
const picked: AreaBatch = {
|
|
84
|
+
const picked: AreaBatch = {
|
|
85
|
+
key: "selected",
|
|
86
|
+
id: "selected",
|
|
87
|
+
role: "selection",
|
|
88
|
+
stroke: undefined,
|
|
89
|
+
fill: undefined,
|
|
90
|
+
dim: false,
|
|
91
|
+
indices: [],
|
|
92
|
+
d: "",
|
|
93
|
+
fills: fillRule === "evenodd" && !keepSelected ? [] : null,
|
|
94
|
+
};
|
|
85
95
|
|
|
86
96
|
items.forEach((item, i) => {
|
|
87
97
|
const isSelected = selected.has(item.id);
|
|
@@ -98,7 +108,17 @@ export function batchAreas(items: readonly BatchableArea[], options: AreaBatchOp
|
|
|
98
108
|
const key = areaBatchKey(dim, itemRole, itemStroke, item.fill);
|
|
99
109
|
let batch = paintOrder === "items" ? runs[runs.length - 1] : byKey.get(key);
|
|
100
110
|
if (!batch || batch.key !== key) {
|
|
101
|
-
batch = {
|
|
111
|
+
batch = {
|
|
112
|
+
key,
|
|
113
|
+
id: paintOrder === "items" ? `${key}#${runs.length}` : key,
|
|
114
|
+
role: itemRole,
|
|
115
|
+
stroke: itemStroke,
|
|
116
|
+
fill: item.fill,
|
|
117
|
+
dim,
|
|
118
|
+
indices: [],
|
|
119
|
+
d: "",
|
|
120
|
+
fills: separate ? [] : null,
|
|
121
|
+
};
|
|
102
122
|
byKey.set(key, batch);
|
|
103
123
|
runs.push(batch);
|
|
104
124
|
}
|
|
@@ -232,7 +232,8 @@ export function buildAreaIndex(items: readonly Area[], cell?: number): AreaIndex
|
|
|
232
232
|
let total = 0;
|
|
233
233
|
for (let a = 0; a < count; a++) {
|
|
234
234
|
if (Number.isNaN(bounds[4 * a]!)) continue;
|
|
235
|
-
total +=
|
|
235
|
+
total +=
|
|
236
|
+
(Math.floor((bounds[4 * a + 2]! - bounds[4 * a]!) / s) + 1) * (Math.floor((bounds[4 * a + 3]! - bounds[4 * a + 1]!) / s) + 1);
|
|
236
237
|
}
|
|
237
238
|
return total;
|
|
238
239
|
};
|
|
@@ -71,10 +71,7 @@ export function useLabelIndices({ enabled, xy, labelled, forced }: LabelAnchors)
|
|
|
71
71
|
const stage = useStage();
|
|
72
72
|
const unit = useScreenPx()(1);
|
|
73
73
|
const spacing = LABEL_SPACING * unit;
|
|
74
|
-
const spaced = useMemo(
|
|
75
|
-
() => (enabled && labelled.length > 0 ? thinPoints(xy, spacing, labelled) : []),
|
|
76
|
-
[enabled, labelled, xy, spacing],
|
|
77
|
-
);
|
|
74
|
+
const spaced = useMemo(() => (enabled && labelled.length > 0 ? thinPoints(xy, spacing, labelled) : []), [enabled, labelled, xy, spacing]);
|
|
78
75
|
return useMemo(() => {
|
|
79
76
|
if (!enabled || (spaced.length === 0 && forced.length === 0)) return [];
|
|
80
77
|
const view = visibleRect(stage.view, stage.box);
|
|
@@ -112,9 +112,7 @@ export const MARKER_SHAPES: Readonly<Record<BuiltinMarkerKind, MarkerShape>> = {
|
|
|
112
112
|
path: (x, y, u, angle) => {
|
|
113
113
|
const c = Math.cos(angle);
|
|
114
114
|
const s = Math.sin(angle);
|
|
115
|
-
return (
|
|
116
|
-
`${circlePath(x, y, 2 * u)}M${n(x + 2 * u * c)} ${n(y + 2 * u * s)}L${n(x + 8 * u * c)} ${n(y + 8 * u * s)}`
|
|
117
|
-
);
|
|
115
|
+
return `${circlePath(x, y, 2 * u)}M${n(x + 2 * u * c)} ${n(y + 2 * u * s)}L${n(x + 8 * u * c)} ${n(y + 8 * u * s)}`;
|
|
118
116
|
},
|
|
119
117
|
},
|
|
120
118
|
};
|