@vitavision/stage2d 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -6
- package/dist/index.d.ts +806 -74
- package/dist/index.js +6669 -4568
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/AreaSet.tsx +87 -56
- package/src/components/ContourEditor.tsx +297 -39
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/DraftShape.tsx +56 -11
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +1 -2
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MaskEditor.tsx +22 -9
- package/src/components/MeasureOverlay.tsx +320 -89
- package/src/components/PointSet.tsx +18 -10
- package/src/components/PolylineSet.tsx +81 -17
- package/src/components/RectRoiEditor.tsx +193 -88
- package/src/components/ShapeEditor.tsx +14 -4
- package/src/components/areaBatching.ts +111 -0
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +2 -3
- package/src/components/markerShapes.ts +1 -2
- package/src/components/measureGeometry.ts +34 -1
- package/src/components/measureHit.ts +371 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +2 -3
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +228 -0
- package/src/components/stage/ImageLayer.tsx +56 -15
- package/src/components/stage/ImageStage.tsx +231 -128
- package/src/components/stage/StageSurface.tsx +44 -18
- package/src/components/stage/StageToolbar.tsx +26 -8
- package/src/components/stage/compareStyle.ts +155 -0
- package/src/components/stage/gesture.ts +5 -0
- package/src/components/stage/hitTest.ts +19 -0
- package/src/components/stage/useStageHitTest.ts +11 -0
- package/src/components/stage/view.ts +33 -6
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +34 -2
|
@@ -8,16 +8,39 @@
|
|
|
8
8
|
* or pan. The overlay never touches the DOM to find out where it is; it is told, in
|
|
9
9
|
* `nativeWidth` × `nativeHeight` pixel coordinates, via `primitives`.
|
|
10
10
|
*
|
|
11
|
-
* This component holds **no app state**: no fetch, no selection
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
11
|
+
* This component holds **no app state**: no fetch, no selection. It draws its props, on
|
|
12
|
+
* purpose — the app decides what a "caliper box" or a "detected edge" *is*; this only knows
|
|
13
|
+
* how to draw one. The one piece of arithmetic it owns is `strokeScale` compensation (see
|
|
14
|
+
* `measureGeometry.ts`'s `strokeWidthFor`), because getting that wrong is invisible on screen
|
|
15
|
+
* until someone zooms in and a "1px" edge marker has silently become 8px wide.
|
|
16
|
+
*
|
|
17
|
+
* Given a hover or press handler it becomes pickable: it answers the stage's hit-test with
|
|
18
|
+
* `measureHit.ts`'s geometry and tracks the hover the app does not control, while its SVG
|
|
19
|
+
* still takes no pointer events.
|
|
17
20
|
*/
|
|
18
21
|
|
|
22
|
+
import { useEffect, useState } from "react";
|
|
23
|
+
|
|
24
|
+
import { nearestMeasurePrimitive } from "./measureHit";
|
|
25
|
+
import type { StagePointerEvent } from "./stage/hitContext";
|
|
26
|
+
import { STAGE_HIT_PRIORITY } from "./stage/hitTest";
|
|
27
|
+
import { useStageHitLayer } from "./stage/useStageHitTest";
|
|
19
28
|
import { imageViewBox } from "./stage/view";
|
|
20
|
-
import {
|
|
29
|
+
import {
|
|
30
|
+
arcPath,
|
|
31
|
+
arrowHeadPoints,
|
|
32
|
+
caliperArrow,
|
|
33
|
+
caliperCorners,
|
|
34
|
+
crossSegments,
|
|
35
|
+
dimensionGeometry,
|
|
36
|
+
MEASURE_ARROW_HEAD_PX,
|
|
37
|
+
MEASURE_CROSS_PX,
|
|
38
|
+
MEASURE_DIMENSION_OFFSET,
|
|
39
|
+
MEASURE_POINT_RADIUS_PX,
|
|
40
|
+
polygonPath,
|
|
41
|
+
segmentsPath,
|
|
42
|
+
strokeWidthFor,
|
|
43
|
+
} from "./measureGeometry";
|
|
21
44
|
import { OVERLAY_STATE_OPACITY, overlayRole, type OverlayRole, type OverlayState } from "./overlayRole";
|
|
22
45
|
import { polylinePath } from "./polylineIndex";
|
|
23
46
|
import { cn, toneColor, type MeasureTone } from "@vitavision/ui";
|
|
@@ -168,10 +191,35 @@ export interface PolylinePrimitive extends PrimitiveCommon {
|
|
|
168
191
|
label?: string;
|
|
169
192
|
}
|
|
170
193
|
|
|
171
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* Many unconnected segments drawn as one element, e.g. the tick marks of a shape model whose
|
|
196
|
+
* points are stored in no particular order, so that a `polyline` cannot join them. Thousands
|
|
197
|
+
* of segments cost one path, not one element each.
|
|
198
|
+
*/
|
|
199
|
+
export interface SegmentsPrimitive extends PrimitiveCommon {
|
|
200
|
+
/** Discriminant. */
|
|
201
|
+
kind: "segments";
|
|
202
|
+
/**
|
|
203
|
+
* `[x1, y1, x2, y2, …]` in image coordinates: four numbers per segment. A trailing partial
|
|
204
|
+
* segment is ignored, and a segment with a non-finite coordinate is skipped.
|
|
205
|
+
*/
|
|
206
|
+
points: number[];
|
|
207
|
+
/** Verdict colour. Defaults to the neutral tone. */
|
|
208
|
+
tone?: MeasureTone;
|
|
209
|
+
/** A short dash pattern. */
|
|
210
|
+
dashed?: boolean;
|
|
211
|
+
/** Text drawn just above the first segment's start. */
|
|
212
|
+
label?: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Anything `MeasureOverlay` can draw, discriminated by `kind`. Kinds are added in minor
|
|
217
|
+
* releases, so code that switches over `kind` should keep a default case.
|
|
218
|
+
*/
|
|
172
219
|
export type MeasurePrimitive =
|
|
173
220
|
| PointPrimitive
|
|
174
221
|
| SegmentPrimitive
|
|
222
|
+
| SegmentsPrimitive
|
|
175
223
|
| CirclePrimitive
|
|
176
224
|
| ArcPrimitive
|
|
177
225
|
| CaliperPrimitive
|
|
@@ -179,9 +227,15 @@ export type MeasurePrimitive =
|
|
|
179
227
|
| PolylinePrimitive;
|
|
180
228
|
|
|
181
229
|
const DEFAULT_LABEL_SIZE = 11;
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
const
|
|
230
|
+
/** How much wider than the mark above it a halo is, in screen pixels. */
|
|
231
|
+
const HALO_PX = 2;
|
|
232
|
+
const HALO_OPACITY = 0.6;
|
|
233
|
+
/** The halo behind a label's glyphs, in screen pixels. */
|
|
234
|
+
const LABEL_HALO_PX = 3;
|
|
235
|
+
/** How much wider than the mark a selection ring is, and how much a selected dot's ring grows its radius. */
|
|
236
|
+
const RING_PX = 3;
|
|
237
|
+
const RING_GROW_PX = 2;
|
|
238
|
+
const HALO = overlayRole("halo");
|
|
185
239
|
|
|
186
240
|
/** Props of `MeasureOverlay`. */
|
|
187
241
|
export interface MeasureOverlayProps {
|
|
@@ -198,6 +252,40 @@ export interface MeasureOverlayProps {
|
|
|
198
252
|
* pixels) this is simply `useStage().view.scale`. See `strokeWidthFor`.
|
|
199
253
|
*/
|
|
200
254
|
strokeScale: number;
|
|
255
|
+
/**
|
|
256
|
+
* Which primitives get a halo: a dark band under the mark (2 screen px wider, at 60 %
|
|
257
|
+
* opacity) and behind its label's glyphs (3 screen px), so it holds on a bright part of the
|
|
258
|
+
* image as well as a dark one.
|
|
259
|
+
* - `"role"` (the default): primitives that carry a `role`.
|
|
260
|
+
* - `"all"`: every primitive, verdict tones included.
|
|
261
|
+
* - `"none"`: none.
|
|
262
|
+
*/
|
|
263
|
+
halo?: "role" | "all" | "none" | undefined;
|
|
264
|
+
/**
|
|
265
|
+
* The hovered primitive's `id`, when the app controls hover (a table beside the stage hovers
|
|
266
|
+
* rows too). That primitive is drawn in the hover state unless it is selected. Without it, a
|
|
267
|
+
* pickable overlay tracks the pointer's hover itself.
|
|
268
|
+
*/
|
|
269
|
+
hoveredId?: string | null | undefined;
|
|
270
|
+
/**
|
|
271
|
+
* Called when the primitive under the pointer changes: its `id`, or `null` when the pointer
|
|
272
|
+
* leaves every primitive. Only primitives with an `id` are picked.
|
|
273
|
+
*
|
|
274
|
+
* Setting this or `onItemPress` makes the overlay pickable: it answers the stage's hit-test,
|
|
275
|
+
* so it must then sit inside an `ImageStage`. Its SVG still takes no pointer events.
|
|
276
|
+
*/
|
|
277
|
+
onHoverChange?: ((id: string | null) => void) | undefined;
|
|
278
|
+
/**
|
|
279
|
+
* A press landed on a primitive with an `id`: the nearest within the pointer's tolerance, the
|
|
280
|
+
* later one on a tie. The press is claimed, so the stage does not pan from it. Return `false`
|
|
281
|
+
* to decline it: the press then goes to the next layer under the pointer, or to the stage.
|
|
282
|
+
* The event is the `pointerdown` (the `pointerup` for a touch tap).
|
|
283
|
+
*/
|
|
284
|
+
onItemPress?: ((id: string, event: StagePointerEvent) => boolean | void) | undefined;
|
|
285
|
+
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
286
|
+
layerId?: string | undefined;
|
|
287
|
+
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.line`. */
|
|
288
|
+
priority?: number | undefined;
|
|
201
289
|
/** Merged onto the `<svg>` with `cn`. */
|
|
202
290
|
className?: string;
|
|
203
291
|
}
|
|
@@ -212,110 +300,221 @@ export interface MeasureOverlayProps {
|
|
|
212
300
|
*
|
|
213
301
|
* A primitive's colour is its `role` (overlay grammar) if given, else its verdict `tone`.
|
|
214
302
|
* Its `state` follows the overlay grammar: `hover` thickens it, `selected` thickens it and
|
|
215
|
-
* rings it in the selection colour, and `dimmed` fades it.
|
|
216
|
-
*
|
|
303
|
+
* rings it in the selection colour, and `dimmed` fades it. A primitive with a `role` is drawn
|
|
304
|
+
* over a halo, and so is its label; `halo` widens or drops that.
|
|
305
|
+
*
|
|
306
|
+
* **Picking.** With `onHoverChange` or `onItemPress` the overlay answers the stage's hit-test
|
|
307
|
+
* (`useStageHitTest` finds its primitives too): the pointer picks the nearest primitive with
|
|
308
|
+
* an `id`, a dot, a filled circle and a caliper box by their inside, every other kind by its
|
|
309
|
+
* strokes. The hovered one is drawn in the hover state.
|
|
310
|
+
*
|
|
311
|
+
* Each primitive is a `<g>` carrying `data-kind`, plus `data-id` and `data-state` (`hover`
|
|
312
|
+
* while hovered) when given; its halo is the `<g data-halo>` inside it. The SVG carries
|
|
313
|
+
* `data-hovered` (the hovered id).
|
|
217
314
|
*/
|
|
218
315
|
export function MeasureOverlay({
|
|
219
316
|
nativeWidth,
|
|
220
317
|
nativeHeight,
|
|
221
318
|
primitives,
|
|
222
319
|
strokeScale,
|
|
320
|
+
halo = "role",
|
|
321
|
+
hoveredId,
|
|
322
|
+
onHoverChange,
|
|
323
|
+
onItemPress,
|
|
324
|
+
layerId,
|
|
325
|
+
priority = STAGE_HIT_PRIORITY.line,
|
|
223
326
|
className,
|
|
224
327
|
}: MeasureOverlayProps) {
|
|
328
|
+
const pickable = onHoverChange !== undefined || onItemPress !== undefined;
|
|
329
|
+
const [ownHover, setOwnHover] = useState<string | null>(null);
|
|
330
|
+
const hoverId = hoveredId !== undefined ? hoveredId : pickable ? ownHover : null;
|
|
225
331
|
const hairline = strokeWidthFor(strokeScale, 1);
|
|
226
332
|
const thick = strokeWidthFor(strokeScale, 1.5);
|
|
227
333
|
const labelSize = strokeWidthFor(strokeScale, DEFAULT_LABEL_SIZE);
|
|
334
|
+
const labelHalo = strokeWidthFor(strokeScale, LABEL_HALO_PX);
|
|
228
335
|
|
|
229
336
|
return (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
<
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
337
|
+
<>
|
|
338
|
+
{pickable && (
|
|
339
|
+
<MeasureHitLayer
|
|
340
|
+
primitives={primitives}
|
|
341
|
+
strokeScale={strokeScale}
|
|
342
|
+
layerId={layerId}
|
|
343
|
+
priority={priority}
|
|
344
|
+
setOwnHover={setOwnHover}
|
|
345
|
+
onHoverChange={onHoverChange}
|
|
346
|
+
onItemPress={onItemPress}
|
|
347
|
+
/>
|
|
348
|
+
)}
|
|
349
|
+
<svg
|
|
350
|
+
role="presentation"
|
|
351
|
+
aria-hidden
|
|
352
|
+
viewBox={imageViewBox({ width: nativeWidth, height: nativeHeight })}
|
|
353
|
+
preserveAspectRatio="none"
|
|
354
|
+
className={cn("pointer-events-none absolute inset-0 h-full w-full overflow-visible", className)}
|
|
355
|
+
data-hovered={hoverId ?? undefined}
|
|
356
|
+
>
|
|
357
|
+
{primitives.map((primitive, index) => {
|
|
358
|
+
// Hover is drawn on any primitive but a selected one, which keeps its selection.
|
|
359
|
+
const hovered = hoverId !== null && primitive.id === hoverId && primitive.state !== "selected";
|
|
360
|
+
const shown = hovered ? "hover" : primitive.state;
|
|
361
|
+
const state = shown ?? "default";
|
|
362
|
+
const colour = primitive.role ? overlayRole(primitive.role) : toneColor(primitive.tone);
|
|
363
|
+
// Hover and selected widen the strokes by the overlay grammar's ratios (2 and 2.5 to the
|
|
364
|
+
// default's 1.5).
|
|
365
|
+
const widen = state === "hover" ? 2 / 1.5 : state === "selected" ? 2.5 / 1.5 : 1;
|
|
366
|
+
const haloed = halo === "all" || (halo === "role" && primitive.role !== undefined);
|
|
367
|
+
// Measured off the mark's own width, so the bands under a dashed mark break where it does.
|
|
368
|
+
const dash = `${hairline * widen * 4} ${hairline * widen * 3}`;
|
|
369
|
+
// A selected mark's outermost band is its ring: the halo goes under that and outgrows it.
|
|
370
|
+
const ring = state === "selected";
|
|
371
|
+
const haloPx = (ring ? RING_PX : 0) + HALO_PX;
|
|
372
|
+
return (
|
|
373
|
+
<g
|
|
374
|
+
// Primitives carry no required identity; a position is the only stable key.
|
|
375
|
+
// eslint-disable-next-line @eslint-react/no-array-index-key
|
|
376
|
+
key={index}
|
|
377
|
+
data-kind={primitive.kind}
|
|
378
|
+
data-id={primitive.id}
|
|
379
|
+
data-state={shown}
|
|
380
|
+
opacity={OVERLAY_STATE_OPACITY[state] === 1 ? undefined : OVERLAY_STATE_OPACITY[state]}
|
|
381
|
+
>
|
|
382
|
+
{haloed && (
|
|
383
|
+
<g data-halo="" opacity={HALO_OPACITY}>
|
|
384
|
+
<Primitive
|
|
385
|
+
primitive={primitive}
|
|
386
|
+
colour={HALO}
|
|
387
|
+
hairline={hairline * widen + strokeWidthFor(strokeScale, haloPx)}
|
|
388
|
+
thick={thick * widen + strokeWidthFor(strokeScale, haloPx)}
|
|
389
|
+
dash={dash}
|
|
390
|
+
labelSize={0}
|
|
391
|
+
labelHalo={0}
|
|
392
|
+
strokeScale={strokeScale}
|
|
393
|
+
grow={(ring ? RING_GROW_PX : 0) + HALO_PX / 2}
|
|
394
|
+
/>
|
|
395
|
+
</g>
|
|
396
|
+
)}
|
|
397
|
+
{ring && (
|
|
398
|
+
<g opacity={0.6}>
|
|
399
|
+
<Primitive
|
|
400
|
+
primitive={primitive}
|
|
401
|
+
colour={overlayRole("selection")}
|
|
402
|
+
hairline={hairline * widen + strokeWidthFor(strokeScale, RING_PX)}
|
|
403
|
+
thick={thick * widen + strokeWidthFor(strokeScale, RING_PX)}
|
|
404
|
+
dash={dash}
|
|
405
|
+
labelSize={0}
|
|
406
|
+
labelHalo={0}
|
|
407
|
+
strokeScale={strokeScale}
|
|
408
|
+
grow={RING_GROW_PX}
|
|
409
|
+
/>
|
|
410
|
+
</g>
|
|
411
|
+
)}
|
|
412
|
+
<Primitive
|
|
413
|
+
primitive={primitive}
|
|
414
|
+
colour={colour}
|
|
415
|
+
hairline={hairline * widen}
|
|
416
|
+
thick={thick * widen}
|
|
417
|
+
dash={dash}
|
|
418
|
+
labelSize={labelSize}
|
|
419
|
+
labelHalo={haloed ? labelHalo : 0}
|
|
420
|
+
strokeScale={strokeScale}
|
|
421
|
+
/>
|
|
422
|
+
</g>
|
|
423
|
+
);
|
|
424
|
+
})}
|
|
425
|
+
</svg>
|
|
426
|
+
</>
|
|
278
427
|
);
|
|
279
428
|
}
|
|
280
429
|
|
|
430
|
+
/**
|
|
431
|
+
* The overlay's registration with the stage's hit-test. A component of its own so that it is
|
|
432
|
+
* mounted only when the overlay is pickable: a plain overlay needs no `ImageStage` around it.
|
|
433
|
+
*/
|
|
434
|
+
function MeasureHitLayer({
|
|
435
|
+
primitives,
|
|
436
|
+
strokeScale,
|
|
437
|
+
layerId,
|
|
438
|
+
priority,
|
|
439
|
+
setOwnHover,
|
|
440
|
+
onHoverChange,
|
|
441
|
+
onItemPress,
|
|
442
|
+
}: {
|
|
443
|
+
primitives: readonly MeasurePrimitive[];
|
|
444
|
+
strokeScale: number;
|
|
445
|
+
layerId: string | undefined;
|
|
446
|
+
priority: number;
|
|
447
|
+
setOwnHover: (id: string | null) => void;
|
|
448
|
+
onHoverChange: ((id: string | null) => void) | undefined;
|
|
449
|
+
onItemPress: ((id: string, event: StagePointerEvent) => boolean | void) | undefined;
|
|
450
|
+
}) {
|
|
451
|
+
useStageHitLayer({
|
|
452
|
+
layerId,
|
|
453
|
+
priority,
|
|
454
|
+
pick: (point, radius) => {
|
|
455
|
+
const hit = nearestMeasurePrimitive(primitives, point, radius, strokeScale);
|
|
456
|
+
return hit ? { id: hit.id, dist: hit.distance } : null;
|
|
457
|
+
},
|
|
458
|
+
onHover: (id) => {
|
|
459
|
+
const next = id === null ? null : String(id);
|
|
460
|
+
setOwnHover(next);
|
|
461
|
+
onHoverChange?.(next);
|
|
462
|
+
},
|
|
463
|
+
onPress: onItemPress ? (id, event) => onItemPress(String(id), event) : undefined,
|
|
464
|
+
});
|
|
465
|
+
// A hover tracked here ends with the layer, so it is not drawn when the overlay is made pickable again.
|
|
466
|
+
useEffect(() => () => setOwnHover(null), [setOwnHover]);
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
|
|
281
470
|
function Primitive({
|
|
282
471
|
primitive,
|
|
283
472
|
colour,
|
|
284
473
|
hairline,
|
|
285
474
|
thick,
|
|
475
|
+
dash,
|
|
286
476
|
labelSize,
|
|
477
|
+
labelHalo,
|
|
287
478
|
strokeScale,
|
|
288
|
-
|
|
479
|
+
grow,
|
|
289
480
|
}: {
|
|
290
481
|
primitive: MeasurePrimitive;
|
|
291
482
|
colour: string;
|
|
292
483
|
hairline: number;
|
|
293
484
|
thick: number;
|
|
485
|
+
/** The dash pattern of a dashed mark. */
|
|
486
|
+
dash: string;
|
|
487
|
+
/** Label font size; 0 draws no label. */
|
|
294
488
|
labelSize: number;
|
|
489
|
+
/** Width of the halo behind a label's glyphs; 0 draws none. */
|
|
490
|
+
labelHalo: number;
|
|
295
491
|
strokeScale: number;
|
|
296
|
-
/**
|
|
297
|
-
|
|
492
|
+
/**
|
|
493
|
+
* Set for a band drawn under the mark (its halo, its selection ring): screen pixels a dot's
|
|
494
|
+
* radius grows by. A filled circle is then outlined rather than filled.
|
|
495
|
+
*/
|
|
496
|
+
grow?: number | undefined;
|
|
298
497
|
}) {
|
|
498
|
+
const band = grow !== undefined;
|
|
299
499
|
|
|
300
500
|
switch (primitive.kind) {
|
|
301
501
|
case "point": {
|
|
302
502
|
if (primitive.cross) {
|
|
303
|
-
const size = strokeWidthFor(strokeScale,
|
|
503
|
+
const size = strokeWidthFor(strokeScale, MEASURE_CROSS_PX);
|
|
304
504
|
const [h, v] = crossSegments(primitive.x, primitive.y, size);
|
|
305
505
|
return (
|
|
306
506
|
<g stroke={colour} strokeWidth={hairline}>
|
|
307
507
|
<line x1={h[0].x} y1={h[0].y} x2={h[1].x} y2={h[1].y} />
|
|
308
508
|
<line x1={v[0].x} y1={v[0].y} x2={v[1].x} y2={v[1].y} />
|
|
309
|
-
<Label x={primitive.x} y={primitive.y - size - 2} text={primitive.label} colour={colour} size={labelSize} />
|
|
509
|
+
<Label x={primitive.x} y={primitive.y - size - 2} text={primitive.label} colour={colour} size={labelSize} halo={labelHalo} />
|
|
310
510
|
</g>
|
|
311
511
|
);
|
|
312
512
|
}
|
|
313
|
-
|
|
314
|
-
const radius = strokeWidthFor(strokeScale, (primitive.radius ?? DEFAULT_POINT_RADIUS) + (ring ? 2 : 0));
|
|
513
|
+
const radius = strokeWidthFor(strokeScale, (primitive.radius ?? MEASURE_POINT_RADIUS_PX) + (grow ?? 0));
|
|
315
514
|
return (
|
|
316
515
|
<g>
|
|
317
516
|
<circle cx={primitive.x} cy={primitive.y} r={radius} fill={colour} />
|
|
318
|
-
<Label x={primitive.x} y={primitive.y - radius - 2} text={primitive.label} colour={colour} size={labelSize} />
|
|
517
|
+
<Label x={primitive.x} y={primitive.y - radius - 2} text={primitive.label} colour={colour} size={labelSize} halo={labelHalo} />
|
|
319
518
|
</g>
|
|
320
519
|
);
|
|
321
520
|
}
|
|
@@ -329,18 +528,30 @@ function Primitive({
|
|
|
329
528
|
y2={primitive.y2}
|
|
330
529
|
stroke={colour}
|
|
331
530
|
strokeWidth={hairline}
|
|
332
|
-
strokeDasharray={primitive.dashed ?
|
|
531
|
+
strokeDasharray={primitive.dashed ? dash : undefined}
|
|
333
532
|
/>
|
|
334
533
|
);
|
|
335
534
|
|
|
535
|
+
case "segments": {
|
|
536
|
+
const p = primitive.points;
|
|
537
|
+
return (
|
|
538
|
+
<g>
|
|
539
|
+
<path d={segmentsPath(p)} fill="none" stroke={colour} strokeWidth={hairline} strokeDasharray={primitive.dashed ? dash : undefined} />
|
|
540
|
+
{p.length >= 4 && (
|
|
541
|
+
<Label x={p[0]!} y={p[1]! - strokeWidthFor(strokeScale, 4)} text={primitive.label} colour={colour} size={labelSize} halo={labelHalo} />
|
|
542
|
+
)}
|
|
543
|
+
</g>
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
|
|
336
547
|
case "circle":
|
|
337
548
|
return (
|
|
338
549
|
<circle
|
|
339
550
|
cx={primitive.cx}
|
|
340
551
|
cy={primitive.cy}
|
|
341
552
|
r={primitive.r}
|
|
342
|
-
fill={primitive.filled && !
|
|
343
|
-
stroke={primitive.filled && !
|
|
553
|
+
fill={primitive.filled && !band ? colour : "none"}
|
|
554
|
+
stroke={primitive.filled && !band ? "none" : colour}
|
|
344
555
|
strokeWidth={hairline}
|
|
345
556
|
/>
|
|
346
557
|
);
|
|
@@ -359,7 +570,7 @@ function Primitive({
|
|
|
359
570
|
case "caliper": {
|
|
360
571
|
const corners = caliperCorners(primitive.cx, primitive.cy, primitive.width, primitive.height, primitive.angle);
|
|
361
572
|
const arrow = caliperArrow(primitive.cx, primitive.cy, primitive.width, primitive.angle);
|
|
362
|
-
const head = arrowHeadPoints(arrow.to, primitive.angle, strokeWidthFor(strokeScale,
|
|
573
|
+
const head = arrowHeadPoints(arrow.to, primitive.angle, strokeWidthFor(strokeScale, MEASURE_ARROW_HEAD_PX));
|
|
363
574
|
return (
|
|
364
575
|
<g>
|
|
365
576
|
<path d={polygonPath(corners)} fill="none" stroke={colour} strokeWidth={hairline} />
|
|
@@ -369,7 +580,7 @@ function Primitive({
|
|
|
369
580
|
<polyline points={head.map((p) => `${p.x},${p.y}`).join(" ")} />
|
|
370
581
|
</g>
|
|
371
582
|
)}
|
|
372
|
-
<Label x={primitive.cx} y={primitive.cy} text={primitive.label} colour={colour} size={labelSize} />
|
|
583
|
+
<Label x={primitive.cx} y={primitive.cy} text={primitive.label} colour={colour} size={labelSize} halo={labelHalo} />
|
|
373
584
|
</g>
|
|
374
585
|
);
|
|
375
586
|
}
|
|
@@ -384,7 +595,7 @@ function Primitive({
|
|
|
384
595
|
strokeWidth={hairline}
|
|
385
596
|
strokeLinecap="round"
|
|
386
597
|
strokeLinejoin="round"
|
|
387
|
-
strokeDasharray={primitive.dashed ?
|
|
598
|
+
strokeDasharray={primitive.dashed ? dash : undefined}
|
|
388
599
|
/>
|
|
389
600
|
{primitive.points.length >= 2 && (
|
|
390
601
|
<Label
|
|
@@ -393,31 +604,37 @@ function Primitive({
|
|
|
393
604
|
text={primitive.label}
|
|
394
605
|
colour={colour}
|
|
395
606
|
size={labelSize}
|
|
607
|
+
halo={labelHalo}
|
|
396
608
|
/>
|
|
397
609
|
)}
|
|
398
610
|
</g>
|
|
399
611
|
);
|
|
400
612
|
|
|
401
613
|
case "dimension": {
|
|
402
|
-
const offset = primitive.offset ??
|
|
614
|
+
const offset = primitive.offset ?? MEASURE_DIMENSION_OFFSET;
|
|
403
615
|
const geometry = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, offset);
|
|
404
616
|
return (
|
|
405
617
|
<g stroke={colour} strokeWidth={hairline}>
|
|
406
618
|
<line x1={geometry.extensionLine1[0].x} y1={geometry.extensionLine1[0].y} x2={geometry.extensionLine1[1].x} y2={geometry.extensionLine1[1].y} opacity={0.5} />
|
|
407
619
|
<line x1={geometry.extensionLine2[0].x} y1={geometry.extensionLine2[0].y} x2={geometry.extensionLine2[1].x} y2={geometry.extensionLine2[1].y} opacity={0.5} />
|
|
408
620
|
<line x1={geometry.dimensionLine[0].x} y1={geometry.dimensionLine[0].y} x2={geometry.dimensionLine[1].x} y2={geometry.dimensionLine[1].y} />
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
621
|
+
{labelSize > 0 && (
|
|
622
|
+
<text
|
|
623
|
+
x={geometry.labelAnchor.x}
|
|
624
|
+
y={geometry.labelAnchor.y}
|
|
625
|
+
fontSize={labelSize}
|
|
626
|
+
fill={colour}
|
|
627
|
+
stroke={labelHalo > 0 ? HALO : "none"}
|
|
628
|
+
strokeWidth={labelHalo > 0 ? labelHalo : undefined}
|
|
629
|
+
strokeLinejoin={labelHalo > 0 ? "round" : undefined}
|
|
630
|
+
paintOrder={labelHalo > 0 ? "stroke" : undefined}
|
|
631
|
+
textAnchor="middle"
|
|
632
|
+
dominantBaseline="text-after-edge"
|
|
633
|
+
transform={`rotate(${geometry.angleDegrees} ${geometry.labelAnchor.x} ${geometry.labelAnchor.y})`}
|
|
634
|
+
>
|
|
635
|
+
{primitive.label}
|
|
636
|
+
</text>
|
|
637
|
+
)}
|
|
421
638
|
</g>
|
|
422
639
|
);
|
|
423
640
|
}
|
|
@@ -433,16 +650,30 @@ function Label({
|
|
|
433
650
|
text,
|
|
434
651
|
colour,
|
|
435
652
|
size,
|
|
653
|
+
halo,
|
|
436
654
|
}: {
|
|
437
655
|
x: number;
|
|
438
656
|
y: number;
|
|
439
657
|
text?: string | undefined;
|
|
440
658
|
colour: string;
|
|
441
659
|
size: number;
|
|
660
|
+
/** Width of the halo behind the glyphs; 0 draws none, and the text keeps the stroke it inherits. */
|
|
661
|
+
halo: number;
|
|
442
662
|
}) {
|
|
443
663
|
if (!text || !(size > 0)) return null;
|
|
444
664
|
return (
|
|
445
|
-
<text
|
|
665
|
+
<text
|
|
666
|
+
x={x}
|
|
667
|
+
y={y}
|
|
668
|
+
fontSize={size}
|
|
669
|
+
fill={colour}
|
|
670
|
+
stroke={halo > 0 ? HALO : undefined}
|
|
671
|
+
strokeWidth={halo > 0 ? halo : undefined}
|
|
672
|
+
strokeLinejoin={halo > 0 ? "round" : undefined}
|
|
673
|
+
paintOrder={halo > 0 ? "stroke" : undefined}
|
|
674
|
+
textAnchor="middle"
|
|
675
|
+
dominantBaseline="text-after-edge"
|
|
676
|
+
>
|
|
446
677
|
{text}
|
|
447
678
|
</text>
|
|
448
679
|
);
|
|
@@ -2,10 +2,10 @@
|
|
|
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
|
|
6
|
-
* per point
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* Drawn as a handful of batched paths, one per (state, style, colour, marker kind), never one element
|
|
6
|
+
* per point; the pointer is resolved by `nearestPoint` through the stage's hit registry, never by
|
|
7
|
+
* the DOM; markers are sized in screen pixels. A scene of 20,000 points is under a dozen DOM nodes
|
|
8
|
+
* plus at most 200 labels.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { useMemo, useState } from "react";
|
|
@@ -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[];
|
|
@@ -105,7 +112,7 @@ interface Batch {
|
|
|
105
112
|
*
|
|
106
113
|
* - **Markers** by kind, from the overlay grammar: `dot`, `plus` (corner), `cross`, `square`,
|
|
107
114
|
* `hollow` (predicted), `directed`. Add your own through `markers`.
|
|
108
|
-
* - **States
|
|
115
|
+
* - **States**: default 1.5 screen px, hover 2, selected 2.5 in the
|
|
109
116
|
* selection colour with a ring, dimmed at 35 % opacity. Every mark has a halo.
|
|
110
117
|
* - **Labels** only where points are at least 24 screen px apart, at most 200 at a time.
|
|
111
118
|
* - **Picking** goes through the stage's hit registry: `onHoverChange` and `onItemPress` here,
|
|
@@ -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
|
</>
|