@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
|
@@ -61,10 +61,7 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
|
|
|
61
61
|
return (
|
|
62
62
|
<div
|
|
63
63
|
data-fit={stage.isFit ? "" : undefined}
|
|
64
|
-
className={cn(
|
|
65
|
-
"flex items-center gap-0.5 rounded-panel border border-line bg-overlay/85 p-1 shadow-lg backdrop-blur",
|
|
66
|
-
className,
|
|
67
|
-
)}
|
|
64
|
+
className={cn("flex items-center gap-0.5 rounded-panel border border-line bg-overlay/85 p-1 shadow-lg backdrop-blur", className)}
|
|
68
65
|
// Every press here is a control, never the start of a pan.
|
|
69
66
|
onPointerDown={(event) => event.stopPropagation()}
|
|
70
67
|
onDoubleClick={(event) => event.stopPropagation()}
|
|
@@ -88,18 +85,12 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
|
|
|
88
85
|
aria-expanded={menuOpen}
|
|
89
86
|
data-state={menuOpen ? "open" : "closed"}
|
|
90
87
|
onClick={() => setMenuOpen((open) => !open)}
|
|
91
|
-
className={cn(
|
|
92
|
-
"h-7 min-w-14 rounded-control px-2 font-mono text-xs text-fg tabular-nums hover:bg-raised",
|
|
93
|
-
focusRing,
|
|
94
|
-
)}
|
|
88
|
+
className={cn("h-7 min-w-14 rounded-control px-2 font-mono text-xs text-fg tabular-nums hover:bg-raised", focusRing)}
|
|
95
89
|
>
|
|
96
90
|
{stage.isFit ? "Fit" : formatScale(stage.view.scale)}
|
|
97
91
|
</button>
|
|
98
92
|
{menuOpen && (
|
|
99
|
-
<div
|
|
100
|
-
role="menu"
|
|
101
|
-
className="absolute bottom-9 left-0 z-20 min-w-28 rounded-panel border border-line bg-overlay p-1 shadow-lg"
|
|
102
|
-
>
|
|
93
|
+
<div role="menu" className="absolute bottom-9 left-0 z-20 min-w-28 rounded-panel border border-line bg-overlay p-1 shadow-lg">
|
|
103
94
|
<MenuItem
|
|
104
95
|
onClick={() => {
|
|
105
96
|
stage.fit();
|
|
@@ -159,8 +150,10 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
|
|
|
159
150
|
}
|
|
160
151
|
|
|
161
152
|
/** Props of `StageButton`: a `<button>`'s, less the ones it sets itself. */
|
|
162
|
-
export interface StageButtonProps
|
|
163
|
-
|
|
153
|
+
export interface StageButtonProps extends Omit<
|
|
154
|
+
ComponentPropsWithRef<"button">,
|
|
155
|
+
"type" | "title" | "aria-label" | "aria-pressed" | "children"
|
|
156
|
+
> {
|
|
164
157
|
/** The accessible name, also shown as the tooltip. */
|
|
165
158
|
label: string;
|
|
166
159
|
/** A toggle's state; omit for a plain action button. */
|
|
@@ -229,8 +222,16 @@ export function StageButton({ label, pressed, hint, shortcut, className, childre
|
|
|
229
222
|
export interface StageLayer {
|
|
230
223
|
/** Its identity, passed back to `onVisibleChange`. */
|
|
231
224
|
id: string;
|
|
232
|
-
/**
|
|
233
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Its name in the menu: text, or richer content such as a name and a count. It names the
|
|
227
|
+
* menu item for assistive technology, so keep its text meaningful.
|
|
228
|
+
*/
|
|
229
|
+
label: ReactNode;
|
|
230
|
+
/**
|
|
231
|
+
* A CSS colour shown as a small dot before the label, matching the layer's colour on the
|
|
232
|
+
* canvas. Decorative (`aria-hidden`), so the label must name the layer on its own.
|
|
233
|
+
*/
|
|
234
|
+
swatch?: string | undefined;
|
|
234
235
|
/** Whether it is shown. */
|
|
235
236
|
visible: boolean;
|
|
236
237
|
/** Shown but not switchable, e.g. a layer that has nothing to draw yet. */
|
|
@@ -245,17 +246,19 @@ export interface StageLayersMenuProps {
|
|
|
245
246
|
layers: readonly StageLayer[];
|
|
246
247
|
/** Called with a layer's id and its new visibility. */
|
|
247
248
|
onVisibleChange: (id: string, visible: boolean) => void;
|
|
248
|
-
/** The trigger's accessible name and
|
|
249
|
+
/** The trigger's accessible name and tooltip. Defaults to "Layers". */
|
|
249
250
|
label?: string | undefined;
|
|
251
|
+
/** The heading over the menu's items. Defaults to `label`. */
|
|
252
|
+
heading?: ReactNode;
|
|
250
253
|
}
|
|
251
254
|
|
|
252
255
|
/**
|
|
253
256
|
* A `StageButton`-shaped trigger opening a menu of layer toggles, for `StageToolbar`'s
|
|
254
|
-
* children. Each layer is a `menuitemcheckbox
|
|
255
|
-
* next. The trigger shows as pressed while any layer
|
|
256
|
-
* says so before the menu is opened.
|
|
257
|
+
* children. Each layer is a `menuitemcheckbox`, with its `swatch` as a dot before its label;
|
|
258
|
+
* toggling one keeps the menu open for the next. The trigger shows as pressed while any layer
|
|
259
|
+
* is hidden, so a canvas missing a layer says so before the menu is opened.
|
|
257
260
|
*/
|
|
258
|
-
export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: StageLayersMenuProps) {
|
|
261
|
+
export function StageLayersMenu({ layers, onVisibleChange, label = "Layers", heading = label }: StageLayersMenuProps) {
|
|
259
262
|
const hidden = layers.some((layer) => !layer.visible && !layer.disabled);
|
|
260
263
|
return (
|
|
261
264
|
<DropdownMenu
|
|
@@ -266,7 +269,7 @@ export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: S
|
|
|
266
269
|
</StageButton>
|
|
267
270
|
}
|
|
268
271
|
>
|
|
269
|
-
<MenuLabel>{
|
|
272
|
+
<MenuLabel>{heading}</MenuLabel>
|
|
270
273
|
{layers.map((layer) => (
|
|
271
274
|
<MenuCheckboxItem
|
|
272
275
|
key={layer.id}
|
|
@@ -275,6 +278,14 @@ export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: S
|
|
|
275
278
|
disabled={layer.disabled}
|
|
276
279
|
shortcut={layer.shortcut}
|
|
277
280
|
>
|
|
281
|
+
{layer.swatch !== undefined && (
|
|
282
|
+
<span
|
|
283
|
+
data-swatch=""
|
|
284
|
+
aria-hidden
|
|
285
|
+
className="mr-2 inline-block size-2 rounded-full align-middle ring-1 ring-line"
|
|
286
|
+
style={{ backgroundColor: layer.swatch }}
|
|
287
|
+
/>
|
|
288
|
+
)}
|
|
278
289
|
{layer.label}
|
|
279
290
|
</MenuCheckboxItem>
|
|
280
291
|
))}
|
|
@@ -293,15 +304,7 @@ export function StageToolbarDivider({ className }: StageToolbarDividerProps) {
|
|
|
293
304
|
return <span className={cn("mx-0.5 h-5 w-px bg-line", className)} aria-hidden />;
|
|
294
305
|
}
|
|
295
306
|
|
|
296
|
-
function MenuItem({
|
|
297
|
-
onClick,
|
|
298
|
-
active,
|
|
299
|
-
children,
|
|
300
|
-
}: {
|
|
301
|
-
onClick: () => void;
|
|
302
|
-
active?: boolean;
|
|
303
|
-
children: ReactNode;
|
|
304
|
-
}) {
|
|
307
|
+
function MenuItem({ onClick, active, children }: { onClick: () => void; active?: boolean; children: ReactNode }) {
|
|
305
308
|
return (
|
|
306
309
|
<button
|
|
307
310
|
type="button"
|
|
@@ -338,12 +341,7 @@ export interface StageReadoutProps {
|
|
|
338
341
|
export function StageReadout({ cursor, extra, className }: StageReadoutProps) {
|
|
339
342
|
const stage = useStage();
|
|
340
343
|
return (
|
|
341
|
-
<span
|
|
342
|
-
className={cn(
|
|
343
|
-
"rounded-control bg-black/60 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums",
|
|
344
|
-
className,
|
|
345
|
-
)}
|
|
346
|
-
>
|
|
344
|
+
<span className={cn("rounded-control bg-black/60 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums", className)}>
|
|
347
345
|
{cursor ? `${cursor.x.toFixed(1)}, ${cursor.y.toFixed(1)} px` : `${stage.image.width}×${stage.image.height}`}
|
|
348
346
|
{" · "}
|
|
349
347
|
{stage.isFit ? "fit" : formatScale(stage.view.scale)}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of `CompareLayer`: how image B is masked, clipped or blended over image A
|
|
3
|
+
* for each mode, and how the wipe's split moves. Pure CSS values, no canvas, so a comparison
|
|
4
|
+
* of two large frames costs the browser's compositor and nothing else.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { CSSProperties } from "react";
|
|
8
|
+
|
|
9
|
+
/** How two images are compared. */
|
|
10
|
+
export type CompareMode = "checker" | "wipe" | "difference";
|
|
11
|
+
|
|
12
|
+
/** Which way the wipe's divider runs: `vertical` splits left from right, `horizontal` top from bottom. */
|
|
13
|
+
export type CompareOrientation = "vertical" | "horizontal";
|
|
14
|
+
|
|
15
|
+
/** The styles of one comparison: the wrapper of both images, and image B. */
|
|
16
|
+
export interface CompareStyles {
|
|
17
|
+
/** On the element holding both images. */
|
|
18
|
+
wrapper: CSSProperties;
|
|
19
|
+
/** On image B, drawn over image A. */
|
|
20
|
+
b: CSSProperties;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A split as a fraction in `[0, 1]`.
|
|
25
|
+
*
|
|
26
|
+
* @param split - Any number.
|
|
27
|
+
* @returns It clamped to `[0, 1]`; `0.5` for a non-finite value.
|
|
28
|
+
*/
|
|
29
|
+
export function clampSplit(split: number): number {
|
|
30
|
+
if (!Number.isFinite(split)) return 0.5;
|
|
31
|
+
return Math.min(1, Math.max(0, split));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The styles that compare B with A.
|
|
36
|
+
*
|
|
37
|
+
* - `checker`: B is masked by a checkerboard of `cell`-pixel squares (a repeating conic
|
|
38
|
+
* gradient), so A shows through every other square, starting with A at the top-left.
|
|
39
|
+
* - `wipe`: B is clipped to the far side of the divider, so A shows before `split` (left of it,
|
|
40
|
+
* or above it) and B after.
|
|
41
|
+
* - `difference`: B is blended over A with `mix-blend-mode: difference`, isolated so nothing
|
|
42
|
+
* behind the layer joins the blend, then brightened by `gain`. The difference is per
|
|
43
|
+
* channel, of the sRGB-encoded values the browser composites, not of linear light.
|
|
44
|
+
*
|
|
45
|
+
* @param mode - The comparison.
|
|
46
|
+
* @param options - `split` (0..1), `orientation`, `cell` (image pixels) and `gain`.
|
|
47
|
+
* @returns The wrapper's and B's styles.
|
|
48
|
+
*/
|
|
49
|
+
export function compareStyles(
|
|
50
|
+
mode: CompareMode,
|
|
51
|
+
options: { split: number; orientation: CompareOrientation; cell: number; gain: number },
|
|
52
|
+
): CompareStyles {
|
|
53
|
+
if (mode === "checker") {
|
|
54
|
+
const size = 2 * Math.max(1, options.cell);
|
|
55
|
+
const mask = "conic-gradient(black 0 25%, transparent 0 50%, black 0 75%, transparent 0)";
|
|
56
|
+
return {
|
|
57
|
+
wrapper: {},
|
|
58
|
+
b: {
|
|
59
|
+
maskImage: mask,
|
|
60
|
+
WebkitMaskImage: mask,
|
|
61
|
+
maskSize: `${size}px ${size}px`,
|
|
62
|
+
WebkitMaskSize: `${size}px ${size}px`,
|
|
63
|
+
maskRepeat: "repeat",
|
|
64
|
+
WebkitMaskRepeat: "repeat",
|
|
65
|
+
maskPosition: "0 0",
|
|
66
|
+
WebkitMaskPosition: "0 0",
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (mode === "wipe") return { wrapper: {}, b: { clipPath: wipeClip(options.split, options.orientation) } };
|
|
71
|
+
return {
|
|
72
|
+
wrapper: { isolation: "isolate", filter: options.gain === 1 ? undefined : `brightness(${Math.max(0, options.gain)})` },
|
|
73
|
+
b: { mixBlendMode: "difference" },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The clip that leaves B on the far side of the divider.
|
|
79
|
+
*
|
|
80
|
+
* @param split - Where the divider is, as a fraction of the width (vertical) or height (horizontal).
|
|
81
|
+
* @param orientation - Which way the divider runs.
|
|
82
|
+
* @returns A CSS `clip-path`.
|
|
83
|
+
*/
|
|
84
|
+
export function wipeClip(split: number, orientation: CompareOrientation): string {
|
|
85
|
+
const at = `${round(clampSplit(split) * 100)}%`;
|
|
86
|
+
return orientation === "vertical" ? `inset(0 0 0 ${at})` : `inset(${at} 0 0 0)`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* What a key does to the split: the arrows move it 1 % (10 % with Shift), Page Up / Page Down
|
|
91
|
+
* 10 %, Home and End to either edge. Right always moves it right and Left left; on a vertical
|
|
92
|
+
* divider Up raises the value (the slider convention), on a horizontal one Up and Down move it
|
|
93
|
+
* up and down.
|
|
94
|
+
*
|
|
95
|
+
* @param split - The split now.
|
|
96
|
+
* @param key - `KeyboardEvent.key`.
|
|
97
|
+
* @param shift - Whether Shift is held.
|
|
98
|
+
* @param orientation - Which way the divider runs.
|
|
99
|
+
* @returns The new split, clamped; `null` for a key that does nothing.
|
|
100
|
+
*/
|
|
101
|
+
export function splitForKey(split: number, key: string, shift: boolean, orientation: CompareOrientation): number | null {
|
|
102
|
+
const step = shift ? 0.1 : 0.01;
|
|
103
|
+
const vertical = orientation === "vertical";
|
|
104
|
+
switch (key) {
|
|
105
|
+
case "ArrowRight":
|
|
106
|
+
return clampSplit(split + step);
|
|
107
|
+
case "ArrowLeft":
|
|
108
|
+
return clampSplit(split - step);
|
|
109
|
+
case "ArrowUp":
|
|
110
|
+
return clampSplit(split + (vertical ? step : -step));
|
|
111
|
+
case "ArrowDown":
|
|
112
|
+
return clampSplit(split + (vertical ? -step : step));
|
|
113
|
+
case "PageUp":
|
|
114
|
+
return clampSplit(split + 0.1);
|
|
115
|
+
case "PageDown":
|
|
116
|
+
return clampSplit(split - 0.1);
|
|
117
|
+
case "Home":
|
|
118
|
+
return 0;
|
|
119
|
+
case "End":
|
|
120
|
+
return 1;
|
|
121
|
+
default:
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The divider's value as text, for a screen reader: how much of A shows, e.g. `"40 % A"`.
|
|
128
|
+
*
|
|
129
|
+
* @param split - The split.
|
|
130
|
+
* @param labelA - Image A's name.
|
|
131
|
+
* @returns The text.
|
|
132
|
+
*/
|
|
133
|
+
export function splitText(split: number, labelA: string): string {
|
|
134
|
+
return `${Math.round(clampSplit(split) * 100)} % ${labelA}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Where along the divider its knob sits: the middle of the part of the divider on screen, so the
|
|
139
|
+
* knob stays in view however the image is zoomed and panned.
|
|
140
|
+
*
|
|
141
|
+
* @param visible - The visible span along the divider, in image pixels (`[start, end]`), or `null` before the viewport is measured.
|
|
142
|
+
* @param length - The image's extent along the divider.
|
|
143
|
+
* @returns The knob's position along the divider, in image pixels.
|
|
144
|
+
*/
|
|
145
|
+
export function knobPosition(visible: readonly [number, number] | null, length: number): number {
|
|
146
|
+
if (visible === null) return length / 2;
|
|
147
|
+
const start = Math.max(0, visible[0]);
|
|
148
|
+
const end = Math.min(length, visible[1]);
|
|
149
|
+
if (!(end >= start)) return Math.min(length, Math.max(0, (visible[0] + visible[1]) / 2));
|
|
150
|
+
return (start + end) / 2;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function round(value: number): number {
|
|
154
|
+
return Math.round(value * 1000) / 1000;
|
|
155
|
+
}
|
|
@@ -11,6 +11,11 @@ import { zoomAbout, type StageView } from "./view";
|
|
|
11
11
|
/** A mouse button that can pan the stage. */
|
|
12
12
|
export type StageMouseButton = "left" | "middle" | "right";
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* How far a press may travel and still be a click rather than a drag, in CSS pixels. A pan,
|
|
16
|
+
* a handle drag and a vertex drag all start only past it, so a jittery click edits nothing.
|
|
17
|
+
*/
|
|
18
|
+
export const CLICK_SLOP = 3;
|
|
14
19
|
/** How far a touch may travel and still be a tap, in CSS pixels. */
|
|
15
20
|
export const TAP_SLOP = 3;
|
|
16
21
|
/** The longest a touch may last and still be a tap, in milliseconds. */
|
|
@@ -64,6 +64,11 @@ export interface StageHitLayerSpec<E = unknown> {
|
|
|
64
64
|
hover?: ((id: HitId | null) => void) | undefined;
|
|
65
65
|
/** A press landed on an item. Return `false` to decline it. */
|
|
66
66
|
press?: ((id: HitId, event: E) => boolean | void) | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* A double-click landed on an item, at `point` (image coordinates). Return `false` to
|
|
69
|
+
* decline it.
|
|
70
|
+
*/
|
|
71
|
+
doubleClick?: ((id: HitId, point: Point) => boolean | void) | undefined;
|
|
67
72
|
}
|
|
68
73
|
|
|
69
74
|
/** Options of a hit-test. */
|
|
@@ -131,6 +136,13 @@ export interface HitRegistry<E = unknown> {
|
|
|
131
136
|
* @returns Whether a layer claimed it.
|
|
132
137
|
*/
|
|
133
138
|
routePress: (point: Point, radius: number, event: E) => boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Offer a double-click to the best item under `point` among layers that take double-clicks,
|
|
141
|
+
* then the next, until one claims it.
|
|
142
|
+
*
|
|
143
|
+
* @returns Whether a layer claimed it.
|
|
144
|
+
*/
|
|
145
|
+
routeDoubleClick: (point: Point, radius: number) => boolean;
|
|
134
146
|
}
|
|
135
147
|
|
|
136
148
|
/** Make an empty registry. */
|
|
@@ -177,5 +189,12 @@ export function createHitRegistry<E = unknown>(): HitRegistry<E> {
|
|
|
177
189
|
}
|
|
178
190
|
return false;
|
|
179
191
|
},
|
|
192
|
+
routeDoubleClick(point, radius) {
|
|
193
|
+
for (const { hit, layer } of answers(point, radius)) {
|
|
194
|
+
const doubleClick = layer.doubleClick;
|
|
195
|
+
if (doubleClick && doubleClick(hit.id, point) !== false) return true;
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
198
|
+
},
|
|
180
199
|
};
|
|
181
200
|
}
|
|
@@ -8,13 +8,7 @@ import { use, useId, useLayoutEffect, useMemo, useRef } from "react";
|
|
|
8
8
|
import type { Point } from "../measureGeometry";
|
|
9
9
|
import { useStage } from "./ImageStage";
|
|
10
10
|
import { StageHitContext, type StagePointerEvent } from "./hitContext";
|
|
11
|
-
import {
|
|
12
|
-
type HitCandidate,
|
|
13
|
-
type HitId,
|
|
14
|
-
type StageHit,
|
|
15
|
-
type StageHitLayerSpec,
|
|
16
|
-
type StageHitOptions,
|
|
17
|
-
} from "./hitTest";
|
|
11
|
+
import { type HitCandidate, type HitId, type StageHit, type StageHitLayerSpec, type StageHitOptions } from "./hitTest";
|
|
18
12
|
|
|
19
13
|
/** The default pointer tolerance of `hitTest`, in screen pixels. */
|
|
20
14
|
const DEFAULT_RADIUS_PX = 6;
|
|
@@ -53,10 +47,8 @@ export function useStageHitTest(): StageHitTestApi {
|
|
|
53
47
|
const { imageLength } = useStage();
|
|
54
48
|
return useMemo(
|
|
55
49
|
() => ({
|
|
56
|
-
hitTest: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) =>
|
|
57
|
-
|
|
58
|
-
hitTestAll: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) =>
|
|
59
|
-
registry.hitTestAll(point, imageLength(radiusScreenPx), options),
|
|
50
|
+
hitTest: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) => registry.hitTest(point, imageLength(radiusScreenPx), options),
|
|
51
|
+
hitTestAll: (point, radiusScreenPx = DEFAULT_RADIUS_PX, options) => registry.hitTestAll(point, imageLength(radiusScreenPx), options),
|
|
60
52
|
}),
|
|
61
53
|
[registry, imageLength],
|
|
62
54
|
);
|
|
@@ -84,6 +76,12 @@ export interface StageHitLayerOptions {
|
|
|
84
76
|
* decline one. For a touch tap this is the `pointerup`.
|
|
85
77
|
*/
|
|
86
78
|
onPress?: ((id: HitId, event: StagePointerEvent) => boolean | void) | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* A double-click landed on an item, at `point` in image coordinates, and nothing above it
|
|
81
|
+
* claimed it. The stage offers a double-click to its layers before its own double-click to
|
|
82
|
+
* fit, so a layer that handles one keeps the view where it is. Return `false` to decline it.
|
|
83
|
+
*/
|
|
84
|
+
onDoubleClick?: ((id: HitId, point: Point) => boolean | void) | undefined;
|
|
87
85
|
}
|
|
88
86
|
|
|
89
87
|
/**
|
|
@@ -118,7 +116,14 @@ export function useStageHitLayer(options: StageHitLayerOptions): string {
|
|
|
118
116
|
return latestRef.current.onHover === undefined ? undefined : (hovered: HitId | null) => latestRef.current.onHover?.(hovered);
|
|
119
117
|
},
|
|
120
118
|
get press() {
|
|
121
|
-
return latestRef.current.onPress === undefined
|
|
119
|
+
return latestRef.current.onPress === undefined
|
|
120
|
+
? undefined
|
|
121
|
+
: (hit: HitId, event: StagePointerEvent) => latestRef.current.onPress?.(hit, event);
|
|
122
|
+
},
|
|
123
|
+
get doubleClick() {
|
|
124
|
+
return latestRef.current.onDoubleClick === undefined
|
|
125
|
+
? undefined
|
|
126
|
+
: (hit: HitId, point: Point) => latestRef.current.onDoubleClick?.(hit, point);
|
|
122
127
|
},
|
|
123
128
|
};
|
|
124
129
|
return registry.register(layer);
|
|
@@ -96,9 +96,7 @@ export const MAX_SCALE = 32;
|
|
|
96
96
|
export const MIN_SCALE_VS_FIT = 0.25;
|
|
97
97
|
|
|
98
98
|
/** Zoom in/out steps, as the percentages a readout can show without apologising. */
|
|
99
|
-
const SCALE_LADDER = [
|
|
100
|
-
0.05, 0.0833, 0.125, 0.1667, 0.25, 0.3333, 0.5, 0.6667, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32,
|
|
101
|
-
];
|
|
99
|
+
const SCALE_LADDER = [0.05, 0.0833, 0.125, 0.1667, 0.25, 0.3333, 0.5, 0.6667, 1, 1.5, 2, 3, 4, 6, 8, 12, 16, 24, 32];
|
|
102
100
|
|
|
103
101
|
/** Relative tolerance for "is this the same scale" — a hair under a tenth of a percent. */
|
|
104
102
|
const SCALE_EPSILON = 1e-3;
|
|
@@ -203,12 +201,7 @@ export function toImage(view: StageView, p: Point): Point {
|
|
|
203
201
|
|
|
204
202
|
/** Whether an image coordinate is inside the image, in the pixel-centre convention. */
|
|
205
203
|
export function insideImage(p: Point, image: Box): boolean {
|
|
206
|
-
return
|
|
207
|
-
p.x >= -PIXEL_CENTRE &&
|
|
208
|
-
p.y >= -PIXEL_CENTRE &&
|
|
209
|
-
p.x < image.width - PIXEL_CENTRE &&
|
|
210
|
-
p.y < image.height - PIXEL_CENTRE
|
|
211
|
-
);
|
|
204
|
+
return p.x >= -PIXEL_CENTRE && p.y >= -PIXEL_CENTRE && p.x < image.width - PIXEL_CENTRE && p.y < image.height - PIXEL_CENTRE;
|
|
212
205
|
}
|
|
213
206
|
|
|
214
207
|
/**
|
|
@@ -277,12 +270,7 @@ export function scaleRange(box: Box, image: Box, options: ClampOptions = {}): [n
|
|
|
277
270
|
* larger is held so the viewport stays covered. With `"center"`, each axis only keeps some
|
|
278
271
|
* image point at or past the viewport's centre.
|
|
279
272
|
*/
|
|
280
|
-
export function clampView(
|
|
281
|
-
view: StageView,
|
|
282
|
-
box: Box,
|
|
283
|
-
image: Box,
|
|
284
|
-
options: ClampOptions = {},
|
|
285
|
-
): StageView {
|
|
273
|
+
export function clampView(view: StageView, box: Box, image: Box, options: ClampOptions = {}): StageView {
|
|
286
274
|
const [min, max] = scaleRange(box, image, options);
|
|
287
275
|
const scale = clamp(view.scale, min, max);
|
|
288
276
|
const axis = options.panBounds === "center" ? centreAxis : clampAxis;
|
|
@@ -22,6 +22,7 @@ import { useCallback } from "react";
|
|
|
22
22
|
|
|
23
23
|
import type { Point } from "./measureGeometry";
|
|
24
24
|
import type { StagePointerEvent } from "./stage/hitContext";
|
|
25
|
+
import { CLICK_SLOP } from "./stage/gesture";
|
|
25
26
|
import { useStage } from "./stage/ImageStage";
|
|
26
27
|
import { useStageDrag } from "./stage/StageSurface";
|
|
27
28
|
|
|
@@ -47,9 +48,6 @@ export interface ShapeDragOptions {
|
|
|
47
48
|
slop?: number | undefined;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
/** The default click slop, in screen pixels (the stage's own). */
|
|
51
|
-
const DEFAULT_SLOP = 3;
|
|
52
|
-
|
|
53
51
|
/**
|
|
54
52
|
* Start moving a shape from a press, inside an `ImageStage`.
|
|
55
53
|
*
|
|
@@ -70,7 +68,7 @@ export function useShapeDrag(): (event: StagePointerEvent, handlers: ShapeDragHa
|
|
|
70
68
|
handlers.onEnd?.({ x: 0, y: 0 }, false, event.nativeEvent);
|
|
71
69
|
return;
|
|
72
70
|
}
|
|
73
|
-
const slop = options?.slop ??
|
|
71
|
+
const slop = options?.slop ?? CLICK_SLOP;
|
|
74
72
|
const from = { x: event.clientX, y: event.clientY };
|
|
75
73
|
// Where the press landed, in image pixels: displacements are measured from it.
|
|
76
74
|
const press = toImage(from);
|
package/src/index.ts
CHANGED
|
@@ -14,30 +14,16 @@ export {
|
|
|
14
14
|
|
|
15
15
|
export type { StageMouseButton } from "./components/stage/gesture";
|
|
16
16
|
|
|
17
|
-
export {
|
|
18
|
-
STAGE_HIT_PRIORITY,
|
|
19
|
-
type HitCandidate,
|
|
20
|
-
type HitId,
|
|
21
|
-
type StageHit,
|
|
22
|
-
type StageHitOptions,
|
|
23
|
-
} from "./components/stage/hitTest";
|
|
17
|
+
export { STAGE_HIT_PRIORITY, type HitCandidate, type HitId, type StageHit, type StageHitOptions } from "./components/stage/hitTest";
|
|
24
18
|
export type { StagePointerEvent } from "./components/stage/hitContext";
|
|
25
|
-
export {
|
|
26
|
-
useStageHitLayer,
|
|
27
|
-
useStageHitTest,
|
|
28
|
-
type StageHitLayerOptions,
|
|
29
|
-
type StageHitTestApi,
|
|
30
|
-
} from "./components/stage/useStageHitTest";
|
|
19
|
+
export { useStageHitLayer, useStageHitTest, type StageHitLayerOptions, type StageHitTestApi } from "./components/stage/useStageHitTest";
|
|
31
20
|
|
|
32
|
-
export {
|
|
21
|
+
export { CompareLayer, type CompareLayerProps } from "./components/stage/CompareLayer";
|
|
22
|
+
export type { CompareMode, CompareOrientation } from "./components/stage/compareStyle";
|
|
33
23
|
|
|
34
|
-
export {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
type StageDrag,
|
|
38
|
-
type StagePress,
|
|
39
|
-
type StageSurfaceProps,
|
|
40
|
-
} from "./components/stage/StageSurface";
|
|
24
|
+
export { ImageLayer, type ImageLayerBaseProps, type ImageLayerProps, type ImageTier } from "./components/stage/ImageLayer";
|
|
25
|
+
|
|
26
|
+
export { StageSurface, useStageDrag, type StageDrag, type StagePress, type StageSurfaceProps } from "./components/stage/StageSurface";
|
|
41
27
|
|
|
42
28
|
export {
|
|
43
29
|
StageButton,
|
|
@@ -94,15 +80,7 @@ export {
|
|
|
94
80
|
type ZoomPanCanvasProps,
|
|
95
81
|
} from "./components/ZoomPanCanvas";
|
|
96
82
|
|
|
97
|
-
export {
|
|
98
|
-
PlaneFormatError,
|
|
99
|
-
decodePlane,
|
|
100
|
-
fetchPlane,
|
|
101
|
-
fractionOf,
|
|
102
|
-
valueAt,
|
|
103
|
-
valuesAt,
|
|
104
|
-
type ValuePlane,
|
|
105
|
-
} from "./api/mapValues";
|
|
83
|
+
export { PlaneFormatError, decodePlane, fetchPlane, fractionOf, valueAt, valuesAt, type ValuePlane } from "./api/mapValues";
|
|
106
84
|
|
|
107
85
|
export {
|
|
108
86
|
MeasureOverlay,
|
|
@@ -116,7 +94,9 @@ export {
|
|
|
116
94
|
type PolylinePrimitive,
|
|
117
95
|
type PrimitiveCommon,
|
|
118
96
|
type SegmentPrimitive,
|
|
97
|
+
type SegmentsPrimitive,
|
|
119
98
|
} from "./components/MeasureOverlay";
|
|
99
|
+
export { measurePrimitiveDistance, nearestMeasurePrimitive } from "./components/measureHit";
|
|
120
100
|
|
|
121
101
|
export {
|
|
122
102
|
OVERLAY_ROLES,
|
|
@@ -129,10 +109,12 @@ export {
|
|
|
129
109
|
export { useScreenPx } from "./components/stage/useScreenPx";
|
|
130
110
|
|
|
131
111
|
export { ContourEditor, nearestContourSegment, type ContourEditorProps } from "./components/ContourEditor";
|
|
132
|
-
export {
|
|
112
|
+
export { arcLengths, deformContour, eraseArc, normalAtArc, pointAtArc, projectToArc, subPath } from "./components/contourArc";
|
|
113
|
+
export { RectRoiEditor, type RectRoiEditorHandle, type RectRoiEditorProps } from "./components/RectRoiEditor";
|
|
133
114
|
export {
|
|
134
115
|
PolylineSet,
|
|
135
116
|
type PolylineSelectMode,
|
|
117
|
+
type PolylineSetHandle,
|
|
136
118
|
type PolylineSetItem,
|
|
137
119
|
type PolylineSetProps,
|
|
138
120
|
} from "./components/PolylineSet";
|
|
@@ -147,11 +129,7 @@ export {
|
|
|
147
129
|
type PolylineId,
|
|
148
130
|
type PolylineIndex,
|
|
149
131
|
} from "./components/polylineIndex";
|
|
150
|
-
export {
|
|
151
|
-
PointSet,
|
|
152
|
-
type PointSetItem,
|
|
153
|
-
type PointSetProps,
|
|
154
|
-
} from "./components/PointSet";
|
|
132
|
+
export { PointSet, type PointSetItem, type PointSetProps } from "./components/PointSet";
|
|
155
133
|
export {
|
|
156
134
|
buildPointIndex,
|
|
157
135
|
buildPointIndexFrom,
|
|
@@ -164,11 +142,7 @@ export {
|
|
|
164
142
|
type PointIndexOptions,
|
|
165
143
|
type PointItem,
|
|
166
144
|
} from "./components/pointIndex";
|
|
167
|
-
export {
|
|
168
|
-
AreaSet,
|
|
169
|
-
type AreaSetItem,
|
|
170
|
-
type AreaSetProps,
|
|
171
|
-
} from "./components/AreaSet";
|
|
145
|
+
export { AreaSet, type AreaSetItem, type AreaSetProps } from "./components/AreaSet";
|
|
172
146
|
export {
|
|
173
147
|
areaCentre,
|
|
174
148
|
areaPath,
|
|
@@ -181,11 +155,7 @@ export {
|
|
|
181
155
|
type AreaId,
|
|
182
156
|
type AreaIndex,
|
|
183
157
|
} from "./components/areaIndex";
|
|
184
|
-
export {
|
|
185
|
-
EllipseSet,
|
|
186
|
-
type EllipseSetItem,
|
|
187
|
-
type EllipseSetProps,
|
|
188
|
-
} from "./components/EllipseSet";
|
|
158
|
+
export { EllipseSet, type EllipseSetItem, type EllipseSetProps } from "./components/EllipseSet";
|
|
189
159
|
export {
|
|
190
160
|
buildEllipseIndex,
|
|
191
161
|
ellipseBounds,
|
|
@@ -198,12 +168,7 @@ export {
|
|
|
198
168
|
type EllipseId,
|
|
199
169
|
type EllipseIndex,
|
|
200
170
|
} from "./components/ellipseIndex";
|
|
201
|
-
export {
|
|
202
|
-
GridLayer,
|
|
203
|
-
type GridEdgeStyle,
|
|
204
|
-
type GridLayerProps,
|
|
205
|
-
type GridNode,
|
|
206
|
-
} from "./components/GridLayer";
|
|
171
|
+
export { GridLayer, type GridEdgeStyle, type GridLayerProps, type GridNode } from "./components/GridLayer";
|
|
207
172
|
export { latticeEdges, type LatticeAxis, type LatticeEdge, type LatticeNode } from "./components/gridEdges";
|
|
208
173
|
export {
|
|
209
174
|
HeatmapLayer,
|
|
@@ -226,22 +191,25 @@ export {
|
|
|
226
191
|
shapeFromCorner,
|
|
227
192
|
shapeHandleCursor,
|
|
228
193
|
shapeHandlePoint,
|
|
194
|
+
snapAngle,
|
|
229
195
|
toShapeFrame,
|
|
230
196
|
type RotatedShape,
|
|
231
197
|
} from "./components/shapeEdit";
|
|
232
|
-
export {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
type
|
|
243
|
-
type
|
|
244
|
-
} from "./components/
|
|
198
|
+
export { DatumEditor, type DatumEditorProps } from "./components/DatumEditor";
|
|
199
|
+
export {
|
|
200
|
+
DATUM_ARM_PX,
|
|
201
|
+
DATUM_HANDLE_PX,
|
|
202
|
+
DATUM_RING_PX,
|
|
203
|
+
datumAxes,
|
|
204
|
+
datumPress,
|
|
205
|
+
moveDatum,
|
|
206
|
+
rotateDatum,
|
|
207
|
+
type Datum,
|
|
208
|
+
type DatumPressOptions,
|
|
209
|
+
type MoveDatumOptions,
|
|
210
|
+
} from "./components/datumEdit";
|
|
211
|
+
export { DraftShape, MarqueeRect, type DraftShapeProps, type DraftShapeSpec, type MarqueeRectProps } from "./components/DraftShape";
|
|
212
|
+
export { translatePoints, useShapeDrag, type ShapeDragHandlers, type ShapeDragOptions } from "./components/useShapeDrag";
|
|
245
213
|
export { MARKER_SHAPES, circlePath, type BuiltinMarkerKind, type MarkerShape } from "./components/markerShapes";
|
|
246
214
|
export {
|
|
247
215
|
ROI_HANDLES,
|
|
@@ -265,6 +233,7 @@ export {
|
|
|
265
233
|
dimensionGeometry,
|
|
266
234
|
polygonPath,
|
|
267
235
|
rotatePoint,
|
|
236
|
+
segmentsPath,
|
|
268
237
|
strokeWidthFor,
|
|
269
238
|
type DimensionGeometry,
|
|
270
239
|
type Point,
|