@vitavision/stage2d 0.12.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/README.md +3 -6
- package/dist/index.d.ts +669 -57
- package/dist/index.js +5455 -3621
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/mapValues.ts +1 -4
- package/src/components/AreaSet.tsx +32 -9
- package/src/components/ContourEditor.tsx +325 -40
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/DraftShape.tsx +37 -5
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +8 -3
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MaskEditor.tsx +57 -9
- package/src/components/MeasureOverlay.tsx +353 -92
- package/src/components/PointSet.tsx +23 -12
- package/src/components/PolylineSet.tsx +83 -27
- package/src/components/RectRoiEditor.tsx +189 -93
- package/src/components/ShapeEditor.tsx +25 -10
- package/src/components/areaBatching.ts +23 -3
- package/src/components/areaIndex.ts +2 -1
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +3 -7
- package/src/components/markerShapes.ts +2 -5
- package/src/components/measureGeometry.ts +46 -28
- package/src/components/measureHit.ts +383 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +14 -5
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +231 -0
- package/src/components/stage/ImageLayer.tsx +56 -24
- package/src/components/stage/ImageStage.tsx +33 -38
- package/src/components/stage/StageSurface.tsx +34 -34
- package/src/components/stage/StageToolbar.tsx +35 -37
- 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 +17 -12
- package/src/components/stage/view.ts +3 -15
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +34 -65
|
@@ -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
|
+
// oxlint-disable-next-line 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,43 @@ 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
|
|
540
|
+
d={segmentsPath(p)}
|
|
541
|
+
fill="none"
|
|
542
|
+
stroke={colour}
|
|
543
|
+
strokeWidth={hairline}
|
|
544
|
+
strokeDasharray={primitive.dashed ? dash : undefined}
|
|
545
|
+
/>
|
|
546
|
+
{p.length >= 4 && (
|
|
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
|
+
/>
|
|
555
|
+
)}
|
|
556
|
+
</g>
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
336
560
|
case "circle":
|
|
337
561
|
return (
|
|
338
562
|
<circle
|
|
339
563
|
cx={primitive.cx}
|
|
340
564
|
cy={primitive.cy}
|
|
341
565
|
r={primitive.r}
|
|
342
|
-
fill={primitive.filled && !
|
|
343
|
-
stroke={primitive.filled && !
|
|
566
|
+
fill={primitive.filled && !band ? colour : "none"}
|
|
567
|
+
stroke={primitive.filled && !band ? "none" : colour}
|
|
344
568
|
strokeWidth={hairline}
|
|
345
569
|
/>
|
|
346
570
|
);
|
|
@@ -359,7 +583,7 @@ function Primitive({
|
|
|
359
583
|
case "caliper": {
|
|
360
584
|
const corners = caliperCorners(primitive.cx, primitive.cy, primitive.width, primitive.height, primitive.angle);
|
|
361
585
|
const arrow = caliperArrow(primitive.cx, primitive.cy, primitive.width, primitive.angle);
|
|
362
|
-
const head = arrowHeadPoints(arrow.to, primitive.angle, strokeWidthFor(strokeScale,
|
|
586
|
+
const head = arrowHeadPoints(arrow.to, primitive.angle, strokeWidthFor(strokeScale, MEASURE_ARROW_HEAD_PX));
|
|
363
587
|
return (
|
|
364
588
|
<g>
|
|
365
589
|
<path d={polygonPath(corners)} fill="none" stroke={colour} strokeWidth={hairline} />
|
|
@@ -369,7 +593,7 @@ function Primitive({
|
|
|
369
593
|
<polyline points={head.map((p) => `${p.x},${p.y}`).join(" ")} />
|
|
370
594
|
</g>
|
|
371
595
|
)}
|
|
372
|
-
<Label x={primitive.cx} y={primitive.cy} text={primitive.label} colour={colour} size={labelSize} />
|
|
596
|
+
<Label x={primitive.cx} y={primitive.cy} text={primitive.label} colour={colour} size={labelSize} halo={labelHalo} />
|
|
373
597
|
</g>
|
|
374
598
|
);
|
|
375
599
|
}
|
|
@@ -384,7 +608,7 @@ function Primitive({
|
|
|
384
608
|
strokeWidth={hairline}
|
|
385
609
|
strokeLinecap="round"
|
|
386
610
|
strokeLinejoin="round"
|
|
387
|
-
strokeDasharray={primitive.dashed ?
|
|
611
|
+
strokeDasharray={primitive.dashed ? dash : undefined}
|
|
388
612
|
/>
|
|
389
613
|
{primitive.points.length >= 2 && (
|
|
390
614
|
<Label
|
|
@@ -393,31 +617,54 @@ function Primitive({
|
|
|
393
617
|
text={primitive.label}
|
|
394
618
|
colour={colour}
|
|
395
619
|
size={labelSize}
|
|
620
|
+
halo={labelHalo}
|
|
396
621
|
/>
|
|
397
622
|
)}
|
|
398
623
|
</g>
|
|
399
624
|
);
|
|
400
625
|
|
|
401
626
|
case "dimension": {
|
|
402
|
-
const offset = primitive.offset ??
|
|
627
|
+
const offset = primitive.offset ?? MEASURE_DIMENSION_OFFSET;
|
|
403
628
|
const geometry = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, offset);
|
|
404
629
|
return (
|
|
405
630
|
<g stroke={colour} strokeWidth={hairline}>
|
|
406
|
-
<line
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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
|
+
/>
|
|
651
|
+
{labelSize > 0 && (
|
|
652
|
+
<text
|
|
653
|
+
x={geometry.labelAnchor.x}
|
|
654
|
+
y={geometry.labelAnchor.y}
|
|
655
|
+
fontSize={labelSize}
|
|
656
|
+
fill={colour}
|
|
657
|
+
stroke={labelHalo > 0 ? HALO : "none"}
|
|
658
|
+
strokeWidth={labelHalo > 0 ? labelHalo : undefined}
|
|
659
|
+
strokeLinejoin={labelHalo > 0 ? "round" : undefined}
|
|
660
|
+
paintOrder={labelHalo > 0 ? "stroke" : undefined}
|
|
661
|
+
textAnchor="middle"
|
|
662
|
+
dominantBaseline="text-after-edge"
|
|
663
|
+
transform={`rotate(${geometry.angleDegrees} ${geometry.labelAnchor.x} ${geometry.labelAnchor.y})`}
|
|
664
|
+
>
|
|
665
|
+
{primitive.label}
|
|
666
|
+
</text>
|
|
667
|
+
)}
|
|
421
668
|
</g>
|
|
422
669
|
);
|
|
423
670
|
}
|
|
@@ -433,16 +680,30 @@ function Label({
|
|
|
433
680
|
text,
|
|
434
681
|
colour,
|
|
435
682
|
size,
|
|
683
|
+
halo,
|
|
436
684
|
}: {
|
|
437
685
|
x: number;
|
|
438
686
|
y: number;
|
|
439
687
|
text?: string | undefined;
|
|
440
688
|
colour: string;
|
|
441
689
|
size: number;
|
|
690
|
+
/** Width of the halo behind the glyphs; 0 draws none, and the text keeps the stroke it inherits. */
|
|
691
|
+
halo: number;
|
|
442
692
|
}) {
|
|
443
693
|
if (!text || !(size > 0)) return null;
|
|
444
694
|
return (
|
|
445
|
-
<text
|
|
695
|
+
<text
|
|
696
|
+
x={x}
|
|
697
|
+
y={y}
|
|
698
|
+
fontSize={size}
|
|
699
|
+
fill={colour}
|
|
700
|
+
stroke={halo > 0 ? HALO : undefined}
|
|
701
|
+
strokeWidth={halo > 0 ? halo : undefined}
|
|
702
|
+
strokeLinejoin={halo > 0 ? "round" : undefined}
|
|
703
|
+
paintOrder={halo > 0 ? "stroke" : undefined}
|
|
704
|
+
textAnchor="middle"
|
|
705
|
+
dominantBaseline="text-after-edge"
|
|
706
|
+
>
|
|
446
707
|
{text}
|
|
447
708
|
</text>
|
|
448
709
|
);
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* keypoints, labelled landmarks.
|
|
4
4
|
*
|
|
5
5
|
* Drawn as a handful of batched paths, one per (state, style, colour, marker kind), never one element
|
|
6
|
-
* per point
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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";
|
|
@@ -112,7 +112,7 @@ interface Batch {
|
|
|
112
112
|
*
|
|
113
113
|
* - **Markers** by kind, from the overlay grammar: `dot`, `plus` (corner), `cross`, `square`,
|
|
114
114
|
* `hollow` (predicted), `directed`. Add your own through `markers`.
|
|
115
|
-
* - **States
|
|
115
|
+
* - **States**: default 1.5 screen px, hover 2, selected 2.5 in the
|
|
116
116
|
* selection colour with a ring, dimmed at 35 % opacity. Every mark has a halo.
|
|
117
117
|
* - **Labels** only where points are at least 24 screen px apart, at most 200 at a time.
|
|
118
118
|
* - **Picking** goes through the stage's hit registry: `onHoverChange` and `onItemPress` here,
|
|
@@ -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 };
|