@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
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* still takes no pointer events.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import {
|
|
22
|
+
import { useState } from "react";
|
|
23
23
|
|
|
24
24
|
import { nearestMeasurePrimitive } from "./measureHit";
|
|
25
25
|
import type { StagePointerEvent } from "./stage/hitContext";
|
|
@@ -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}
|
|
@@ -462,8 +462,6 @@ function MeasureHitLayer({
|
|
|
462
462
|
},
|
|
463
463
|
onPress: onItemPress ? (id, event) => onItemPress(String(id), event) : undefined,
|
|
464
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
465
|
return null;
|
|
468
466
|
}
|
|
469
467
|
|
|
@@ -536,9 +534,22 @@ function Primitive({
|
|
|
536
534
|
const p = primitive.points;
|
|
537
535
|
return (
|
|
538
536
|
<g>
|
|
539
|
-
<path
|
|
537
|
+
<path
|
|
538
|
+
d={segmentsPath(p)}
|
|
539
|
+
fill="none"
|
|
540
|
+
stroke={colour}
|
|
541
|
+
strokeWidth={hairline}
|
|
542
|
+
strokeDasharray={primitive.dashed ? dash : undefined}
|
|
543
|
+
/>
|
|
540
544
|
{p.length >= 4 && (
|
|
541
|
-
<Label
|
|
545
|
+
<Label
|
|
546
|
+
x={p[0]!}
|
|
547
|
+
y={p[1]! - strokeWidthFor(strokeScale, 4)}
|
|
548
|
+
text={primitive.label}
|
|
549
|
+
colour={colour}
|
|
550
|
+
size={labelSize}
|
|
551
|
+
halo={labelHalo}
|
|
552
|
+
/>
|
|
542
553
|
)}
|
|
543
554
|
</g>
|
|
544
555
|
);
|
|
@@ -615,9 +626,26 @@ function Primitive({
|
|
|
615
626
|
const geometry = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, offset);
|
|
616
627
|
return (
|
|
617
628
|
<g stroke={colour} strokeWidth={hairline}>
|
|
618
|
-
<line
|
|
619
|
-
|
|
620
|
-
|
|
629
|
+
<line
|
|
630
|
+
x1={geometry.extensionLine1[0].x}
|
|
631
|
+
y1={geometry.extensionLine1[0].y}
|
|
632
|
+
x2={geometry.extensionLine1[1].x}
|
|
633
|
+
y2={geometry.extensionLine1[1].y}
|
|
634
|
+
opacity={0.5}
|
|
635
|
+
/>
|
|
636
|
+
<line
|
|
637
|
+
x1={geometry.extensionLine2[0].x}
|
|
638
|
+
y1={geometry.extensionLine2[0].y}
|
|
639
|
+
x2={geometry.extensionLine2[1].x}
|
|
640
|
+
y2={geometry.extensionLine2[1].y}
|
|
641
|
+
opacity={0.5}
|
|
642
|
+
/>
|
|
643
|
+
<line
|
|
644
|
+
x1={geometry.dimensionLine[0].x}
|
|
645
|
+
y1={geometry.dimensionLine[0].y}
|
|
646
|
+
x2={geometry.dimensionLine[1].x}
|
|
647
|
+
y2={geometry.dimensionLine[1].y}
|
|
648
|
+
/>
|
|
621
649
|
{labelSize > 0 && (
|
|
622
650
|
<text
|
|
623
651
|
x={geometry.labelAnchor.x}
|
|
@@ -53,7 +53,7 @@ export interface PointSetProps {
|
|
|
53
53
|
markers?: Readonly<Record<string, MarkerShape>> | undefined;
|
|
54
54
|
/** The hovered id, when the app controls hover (a list beside the stage hovers points too). */
|
|
55
55
|
hoveredId?: PointId | null | undefined;
|
|
56
|
-
/** Called when the point under the pointer changes. `null` when it leaves every point. */
|
|
56
|
+
/** Called when the point under the pointer changes. `null` when it leaves every point. Not called while `pickable` is off; turning it off ends a hover with `null`. */
|
|
57
57
|
onHoverChange?: ((id: PointId | null) => void) | undefined;
|
|
58
58
|
/** The selected ids, drawn at 2.5 px in the selection colour with a ring. */
|
|
59
59
|
selectedIds?: Iterable<PointId> | undefined;
|
|
@@ -77,6 +77,12 @@ export interface PointSetProps {
|
|
|
77
77
|
halo?: boolean | undefined;
|
|
78
78
|
/** CSS colour of selected points and rings. Defaults to the overlay `selection` role. */
|
|
79
79
|
selectionStroke?: string | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Whether the layer takes part in the stage's hit-test. On by default. Turn it off for a
|
|
82
|
+
* decorative layer (a backdrop of markers under the points that matter), so it neither
|
|
83
|
+
* hovers, nor claims presses, nor answers `useStageHitTest`.
|
|
84
|
+
*/
|
|
85
|
+
pickable?: boolean | undefined;
|
|
80
86
|
/** The id hit-tests report for this layer. Defaults to a generated one. */
|
|
81
87
|
layerId?: string | undefined;
|
|
82
88
|
/** Rank against other layers in hit-tests. Defaults to `STAGE_HIT_PRIORITY.point`. */
|
|
@@ -116,7 +122,7 @@ interface Batch {
|
|
|
116
122
|
* selection colour with a ring, dimmed at 35 % opacity. Every mark has a halo.
|
|
117
123
|
* - **Labels** only where points are at least 24 screen px apart, at most 200 at a time.
|
|
118
124
|
* - **Picking** goes through the stage's hit registry: `onHoverChange` and `onItemPress` here,
|
|
119
|
-
* and `useStageHitTest` for an app that arbitrates between layers.
|
|
125
|
+
* and `useStageHitTest` for an app that arbitrates between layers. `pickable={false}` turns it off.
|
|
120
126
|
*
|
|
121
127
|
* Outlines (`plus`, `cross`, …) are regenerated when the zoom changes, since their size is in
|
|
122
128
|
* screen pixels; past 3,000 points only those near the viewport are generated. `dot` is a
|
|
@@ -133,6 +139,7 @@ export function PointSet({
|
|
|
133
139
|
selectedIds,
|
|
134
140
|
dimmed,
|
|
135
141
|
onItemPress,
|
|
142
|
+
pickable = true,
|
|
136
143
|
labels = true,
|
|
137
144
|
halo = true,
|
|
138
145
|
selectionStroke = overlayRole("selection"),
|
|
@@ -160,6 +167,7 @@ export function PointSet({
|
|
|
160
167
|
useStageHitLayer({
|
|
161
168
|
layerId,
|
|
162
169
|
priority,
|
|
170
|
+
enabled: pickable,
|
|
163
171
|
pick: (point, radius) => {
|
|
164
172
|
const hit = nearestPoint(index, point.x, point.y, radius);
|
|
165
173
|
return hit ? { id: hit.id, dist: hit.dist } : null;
|
|
@@ -168,7 +176,7 @@ export function PointSet({
|
|
|
168
176
|
setOwnHover(id);
|
|
169
177
|
onHoverChange?.(id);
|
|
170
178
|
},
|
|
171
|
-
onPress: onItemPress
|
|
179
|
+
onPress: onItemPress,
|
|
172
180
|
});
|
|
173
181
|
|
|
174
182
|
// Items grouped by appearance. Rebuilt when the items or the selection change, never on
|
|
@@ -287,12 +295,14 @@ export function PointSet({
|
|
|
287
295
|
))}
|
|
288
296
|
{halo &&
|
|
289
297
|
paths.picked.map(({ batch, d }) => (
|
|
290
|
-
<path
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
298
|
+
<path
|
|
299
|
+
key={`halo|selected|${batch.key}`}
|
|
300
|
+
d={d}
|
|
301
|
+
stroke={HALO}
|
|
302
|
+
strokeWidth={widthOf(batch.shape, batch.role, "selected") + px(2)}
|
|
303
|
+
/>
|
|
295
304
|
))}
|
|
305
|
+
{halo && paths.rings.map(({ batch, d }) => <path key={`halo|${batch.key}`} d={d} stroke={HALO} strokeWidth={ringWidth + px(2)} />)}
|
|
296
306
|
{paths.picked.map(({ batch, d }) => (
|
|
297
307
|
<path
|
|
298
308
|
key={`selected|${batch.key}`}
|
|
@@ -317,14 +327,22 @@ export function PointSet({
|
|
|
317
327
|
<path
|
|
318
328
|
data-hovered-point=""
|
|
319
329
|
d={hoverShape.path(hoverItem.x, hoverItem.y, unit, hoverItem.angle ?? 0, hoverItem.angle2)}
|
|
320
|
-
stroke={hoverSelected ? selectionStroke :
|
|
330
|
+
stroke={hoverSelected ? selectionStroke : hoverItem?.color || overlayRole(hoverRole)}
|
|
321
331
|
strokeWidth={widthOf(hoverShape, hoverRole, hoverSelected ? "selected" : "hover")}
|
|
322
332
|
/>
|
|
323
333
|
</>
|
|
324
334
|
)}
|
|
325
335
|
</g>
|
|
326
336
|
{shownLabels.length > 0 && (
|
|
327
|
-
<g
|
|
337
|
+
<g
|
|
338
|
+
data-labels=""
|
|
339
|
+
fontSize={px(11)}
|
|
340
|
+
className="font-mono"
|
|
341
|
+
stroke={HALO}
|
|
342
|
+
strokeWidth={px(3)}
|
|
343
|
+
paintOrder="stroke"
|
|
344
|
+
strokeLinejoin="round"
|
|
345
|
+
>
|
|
328
346
|
{shownLabels.map((i) => {
|
|
329
347
|
const item = items[i]!;
|
|
330
348
|
return (
|
|
@@ -351,7 +369,8 @@ function batchPath(batch: Batch, items: readonly PointSetItem[], unit: number, c
|
|
|
351
369
|
let d = "";
|
|
352
370
|
for (const i of batch.indices) {
|
|
353
371
|
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))
|
|
372
|
+
if (window && (item.x < window.x || item.x > window.x + window.width || item.y < window.y || item.y > window.y + window.height))
|
|
373
|
+
continue;
|
|
355
374
|
d += batch.shape.path(item.x, item.y, unit, item.angle ?? 0, item.angle2);
|
|
356
375
|
}
|
|
357
376
|
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
|
};
|
|
@@ -93,8 +93,14 @@ export function segmentsPath(points: ArrayLike<number>): string {
|
|
|
93
93
|
/** The two line segments of a cross mark centred on `(x, y)`, each `2·size` long. */
|
|
94
94
|
export function crossSegments(x: number, y: number, size: number): [[Point, Point], [Point, Point]] {
|
|
95
95
|
return [
|
|
96
|
-
[
|
|
97
|
-
|
|
96
|
+
[
|
|
97
|
+
{ x: x - size, y },
|
|
98
|
+
{ x: x + size, y },
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
{ x, y: y - size },
|
|
102
|
+
{ x, y: y + size },
|
|
103
|
+
],
|
|
98
104
|
];
|
|
99
105
|
}
|
|
100
106
|
|
|
@@ -159,13 +165,7 @@ export function arcPath(cx: number, cy: number, r: number, startAngle: number, e
|
|
|
159
165
|
* by `height`, centred at `(cx, cy)` and rotated by `angle` radians. Corner order is
|
|
160
166
|
* suitable for `polygonPath` — around the box, not crossed.
|
|
161
167
|
*/
|
|
162
|
-
export function caliperCorners(
|
|
163
|
-
cx: number,
|
|
164
|
-
cy: number,
|
|
165
|
-
width: number,
|
|
166
|
-
height: number,
|
|
167
|
-
angle: number,
|
|
168
|
-
): [Point, Point, Point, Point] {
|
|
168
|
+
export function caliperCorners(cx: number, cy: number, width: number, height: number, angle: number): [Point, Point, Point, Point] {
|
|
169
169
|
const hw = width / 2;
|
|
170
170
|
const hh = height / 2;
|
|
171
171
|
const local: [number, number][] = [
|
|
@@ -186,12 +186,7 @@ export function caliperCorners(
|
|
|
186
186
|
* along the box's own measurement axis. What the arrow points at is the scan direction the
|
|
187
187
|
* caliper was evaluated in, not merely a decoration on the box.
|
|
188
188
|
*/
|
|
189
|
-
export function caliperArrow(
|
|
190
|
-
cx: number,
|
|
191
|
-
cy: number,
|
|
192
|
-
width: number,
|
|
193
|
-
angle: number,
|
|
194
|
-
): { from: Point; to: Point } {
|
|
189
|
+
export function caliperArrow(cx: number, cy: number, width: number, angle: number): { from: Point; to: Point } {
|
|
195
190
|
const reach = width / 2 + Math.max(4, width * 0.2);
|
|
196
191
|
const tip = rotatePoint(reach, 0, angle);
|
|
197
192
|
return { from: { x: cx, y: cy }, to: { x: cx + tip.x, y: cy + tip.y } };
|
|
@@ -208,11 +203,7 @@ export function arrowHeadPoints(tip: Point, angle: number, size: number): [Point
|
|
|
208
203
|
const spread = size;
|
|
209
204
|
const left = rotatePoint(-back, spread, angle);
|
|
210
205
|
const right = rotatePoint(-back, -spread, angle);
|
|
211
|
-
return [
|
|
212
|
-
{ x: tip.x + left.x, y: tip.y + left.y },
|
|
213
|
-
tip,
|
|
214
|
-
{ x: tip.x + right.x, y: tip.y + right.y },
|
|
215
|
-
];
|
|
206
|
+
return [{ x: tip.x + left.x, y: tip.y + left.y }, tip, { x: tip.x + right.x, y: tip.y + right.y }];
|
|
216
207
|
}
|
|
217
208
|
|
|
218
209
|
/** What `dimensionGeometry` computes: the lines and label placement of a dimension annotation. */
|
|
@@ -237,13 +228,7 @@ export interface DimensionGeometry {
|
|
|
237
228
|
* Degenerate input (`x1 === x2 && y1 === y2`) falls back to an arbitrary rightward normal
|
|
238
229
|
* rather than dividing by zero, so a caller need not special-case a zero-length dimension.
|
|
239
230
|
*/
|
|
240
|
-
export function dimensionGeometry(
|
|
241
|
-
x1: number,
|
|
242
|
-
y1: number,
|
|
243
|
-
x2: number,
|
|
244
|
-
y2: number,
|
|
245
|
-
offset: number,
|
|
246
|
-
): DimensionGeometry {
|
|
231
|
+
export function dimensionGeometry(x1: number, y1: number, x2: number, y2: number, offset: number): DimensionGeometry {
|
|
247
232
|
const dx = x2 - x1;
|
|
248
233
|
const dy = y2 - y1;
|
|
249
234
|
const length = Math.hypot(dx, dy);
|
|
@@ -99,7 +99,8 @@ export function measurePrimitiveDistance(primitive: MeasurePrimitive, point: Poi
|
|
|
99
99
|
case "dimension": {
|
|
100
100
|
const g = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, primitive.offset ?? MEASURE_DIMENSION_OFFSET);
|
|
101
101
|
let best = Infinity;
|
|
102
|
-
for (const [a, b] of [g.extensionLine1, g.extensionLine2, g.dimensionLine])
|
|
102
|
+
for (const [a, b] of [g.extensionLine1, g.extensionLine2, g.dimensionLine])
|
|
103
|
+
best = Math.min(best, segmentDistance(p, a.x, a.y, b.x, b.y));
|
|
103
104
|
return best;
|
|
104
105
|
}
|
|
105
106
|
|
|
@@ -204,7 +205,12 @@ function indexOf(primitives: readonly MeasurePrimitive[]): MeasureIndex {
|
|
|
204
205
|
const counts = new Uint32Array(cols * rows + 1);
|
|
205
206
|
const spans: [number, number, number, number][] = [];
|
|
206
207
|
for (const i of gridded) {
|
|
207
|
-
const span: [number, number, number, number] = [
|
|
208
|
+
const span: [number, number, number, number] = [
|
|
209
|
+
cellX(boxes[i * 5]!),
|
|
210
|
+
cellY(boxes[i * 5 + 1]!),
|
|
211
|
+
cellX(boxes[i * 5 + 2]!),
|
|
212
|
+
cellY(boxes[i * 5 + 3]!),
|
|
213
|
+
];
|
|
208
214
|
spans.push(span);
|
|
209
215
|
if ((span[2] - span[0] + 1) * (span[3] - span[1] + 1) > LARGE_CELLS) {
|
|
210
216
|
large.push(i);
|
|
@@ -273,7 +279,8 @@ function boxOf(primitive: MeasurePrimitive): [number, number, number, number, nu
|
|
|
273
279
|
add(primitive.cx + primitive.r, primitive.cy + primitive.r);
|
|
274
280
|
break;
|
|
275
281
|
case "caliper": {
|
|
276
|
-
for (const corner of caliperCorners(primitive.cx, primitive.cy, primitive.width, primitive.height, primitive.angle))
|
|
282
|
+
for (const corner of caliperCorners(primitive.cx, primitive.cy, primitive.width, primitive.height, primitive.angle))
|
|
283
|
+
add(corner.x, corner.y);
|
|
277
284
|
const tip = caliperArrow(primitive.cx, primitive.cy, primitive.width, primitive.angle).to;
|
|
278
285
|
add(tip.x, tip.y);
|
|
279
286
|
// The head's back corners reach 1.6 × its size behind the tip and 1 × to the side.
|
|
@@ -342,7 +349,12 @@ export function nearestMeasurePrimitive(
|
|
|
342
349
|
seen[i] = query;
|
|
343
350
|
const reach = radius + boxes[i * 5 + 4]! * screen;
|
|
344
351
|
if (
|
|
345
|
-
!(
|
|
352
|
+
!(
|
|
353
|
+
point.x >= boxes[i * 5]! - reach &&
|
|
354
|
+
point.x <= boxes[i * 5 + 2]! + reach &&
|
|
355
|
+
point.y >= boxes[i * 5 + 1]! - reach &&
|
|
356
|
+
point.y <= boxes[i * 5 + 3]! + reach
|
|
357
|
+
)
|
|
346
358
|
) {
|
|
347
359
|
return;
|
|
348
360
|
}
|
|
@@ -122,8 +122,18 @@ export function buildPointIndex(xy: ArrayLike<number>, options: PointIndexOption
|
|
|
122
122
|
}
|
|
123
123
|
if (live === 0) {
|
|
124
124
|
return {
|
|
125
|
-
count,
|
|
126
|
-
|
|
125
|
+
count,
|
|
126
|
+
xy,
|
|
127
|
+
ids: ids,
|
|
128
|
+
radii,
|
|
129
|
+
maxRadius,
|
|
130
|
+
cell: 1,
|
|
131
|
+
x0: 0,
|
|
132
|
+
y0: 0,
|
|
133
|
+
cols: 1,
|
|
134
|
+
rows: 1,
|
|
135
|
+
starts: new Uint32Array(2),
|
|
136
|
+
items: new Uint32Array(0),
|
|
127
137
|
};
|
|
128
138
|
}
|
|
129
139
|
|
|
@@ -124,7 +124,10 @@ export function CompareLayer({
|
|
|
124
124
|
const extent = vertical ? image.width : image.height;
|
|
125
125
|
const at = current * extent;
|
|
126
126
|
const visible = visibleRect(stage.view, stage.box);
|
|
127
|
-
const along = knobPosition(
|
|
127
|
+
const along = knobPosition(
|
|
128
|
+
visible === null ? null : vertical ? [visible.y, visible.y + visible.height] : [visible.x, visible.x + visible.width],
|
|
129
|
+
length,
|
|
130
|
+
);
|
|
128
131
|
const knob = vertical ? { x: at, y: along } : { x: along, y: at };
|
|
129
132
|
const line = vertical ? { x1: at, y1: 0, x2: at, y2: image.height } : { x1: 0, y1: at, x2: image.width, y2: at };
|
|
130
133
|
|
|
@@ -83,16 +83,7 @@ export type ImageLayerProps = ImageLayerBaseProps &
|
|
|
83
83
|
* once it has loaded, the preview carries `data-wants-full` from the moment the full image is
|
|
84
84
|
* wanted until it has loaded, and both carry `data-pixelated` while pixelated.
|
|
85
85
|
*/
|
|
86
|
-
export function ImageLayer({
|
|
87
|
-
src,
|
|
88
|
-
preview,
|
|
89
|
-
alt,
|
|
90
|
-
pixelatedAbove = 4,
|
|
91
|
-
onFullNeeded,
|
|
92
|
-
onLoad,
|
|
93
|
-
onError,
|
|
94
|
-
className,
|
|
95
|
-
}: ImageLayerProps) {
|
|
86
|
+
export function ImageLayer({ src, preview, alt, pixelatedAbove = 4, onFullNeeded, onLoad, onError, className }: ImageLayerProps) {
|
|
96
87
|
const stage = useStage();
|
|
97
88
|
const scale = stage.view.scale;
|
|
98
89
|
// The full image is wanted once the preview would be magnified on screen: its own pixels
|
|
@@ -25,24 +25,8 @@
|
|
|
25
25
|
* with the image.
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
-
import {
|
|
29
|
-
|
|
30
|
-
use,
|
|
31
|
-
useCallback,
|
|
32
|
-
useEffect,
|
|
33
|
-
useImperativeHandle,
|
|
34
|
-
useLayoutEffect,
|
|
35
|
-
useMemo,
|
|
36
|
-
useRef,
|
|
37
|
-
useState,
|
|
38
|
-
} from "react";
|
|
39
|
-
import type {
|
|
40
|
-
CSSProperties,
|
|
41
|
-
KeyboardEvent as ReactKeyboardEvent,
|
|
42
|
-
PointerEvent as ReactPointerEvent,
|
|
43
|
-
ReactNode,
|
|
44
|
-
Ref,
|
|
45
|
-
} from "react";
|
|
28
|
+
import { createContext, use, useCallback, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
29
|
+
import type { CSSProperties, KeyboardEvent as ReactKeyboardEvent, PointerEvent as ReactPointerEvent, ReactNode, Ref } from "react";
|
|
46
30
|
|
|
47
31
|
import { cn } from "@vitavision/ui";
|
|
48
32
|
import type { Point } from "../measureGeometry";
|
|
@@ -960,18 +944,8 @@ function contentSize(element: HTMLElement): Box {
|
|
|
960
944
|
const style = getComputedStyle(element);
|
|
961
945
|
const px = (value: string) => Number.parseFloat(value) || 0;
|
|
962
946
|
return {
|
|
963
|
-
width:
|
|
964
|
-
|
|
965
|
-
px(style.borderLeftWidth) -
|
|
966
|
-
px(style.borderRightWidth) -
|
|
967
|
-
px(style.paddingLeft) -
|
|
968
|
-
px(style.paddingRight),
|
|
969
|
-
height:
|
|
970
|
-
rect.height -
|
|
971
|
-
px(style.borderTopWidth) -
|
|
972
|
-
px(style.borderBottomWidth) -
|
|
973
|
-
px(style.paddingTop) -
|
|
974
|
-
px(style.paddingBottom),
|
|
947
|
+
width: rect.width - px(style.borderLeftWidth) - px(style.borderRightWidth) - px(style.paddingLeft) - px(style.paddingRight),
|
|
948
|
+
height: rect.height - px(style.borderTopWidth) - px(style.borderBottomWidth) - px(style.paddingTop) - px(style.paddingBottom),
|
|
975
949
|
};
|
|
976
950
|
}
|
|
977
951
|
|
|
@@ -64,37 +64,40 @@ export function useStageDrag(): (event: ReactPointerEvent<Element>, drag: StageD
|
|
|
64
64
|
// A drag still in flight when the component unmounts is cancelled, not leaked.
|
|
65
65
|
useEffect(() => () => cleanupRef.current?.(), []);
|
|
66
66
|
|
|
67
|
-
return useCallback(
|
|
68
|
-
event
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
67
|
+
return useCallback(
|
|
68
|
+
(event: ReactPointerEvent<Element>, drag: StageDrag) => {
|
|
69
|
+
event.stopPropagation();
|
|
70
|
+
event.preventDefault();
|
|
71
|
+
cleanupRef.current?.();
|
|
72
|
+
const from = { x: event.clientX, y: event.clientY };
|
|
73
|
+
let moved = false;
|
|
74
|
+
const at = (e: PointerEvent) => toImage({ x: e.clientX, y: e.clientY });
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
76
|
+
const move = (e: PointerEvent) => {
|
|
77
|
+
if (!moved && Math.hypot(e.clientX - from.x, e.clientY - from.y) > CLICK_SLOP) moved = true;
|
|
78
|
+
drag.onMove?.(at(e), e);
|
|
79
|
+
};
|
|
80
|
+
const up = (e: PointerEvent) => {
|
|
81
|
+
stop();
|
|
82
|
+
drag.onEnd?.(at(e), e, moved);
|
|
83
|
+
};
|
|
84
|
+
const cancel = () => {
|
|
85
|
+
stop();
|
|
86
|
+
drag.onCancel?.();
|
|
87
|
+
};
|
|
88
|
+
const stop = () => {
|
|
89
|
+
window.removeEventListener("pointermove", move);
|
|
90
|
+
window.removeEventListener("pointerup", up);
|
|
91
|
+
window.removeEventListener("pointercancel", cancel);
|
|
92
|
+
cleanupRef.current = null;
|
|
93
|
+
};
|
|
94
|
+
window.addEventListener("pointermove", move);
|
|
95
|
+
window.addEventListener("pointerup", up);
|
|
96
|
+
window.addEventListener("pointercancel", cancel);
|
|
97
|
+
cleanupRef.current = cancel;
|
|
98
|
+
},
|
|
99
|
+
[toImage],
|
|
100
|
+
);
|
|
98
101
|
}
|
|
99
102
|
|
|
100
103
|
/** A press on the surface, as the app's `onPress` sees it. */
|