@vitavision/stage2d 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -1
- package/dist/index.d.ts +538 -14
- package/dist/index.js +1276 -50
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/MeasureOverlay.tsx +128 -32
- package/src/components/PolylineSet.tsx +308 -0
- package/src/components/RectRoiEditor.tsx +292 -0
- package/src/components/overlayRole.ts +53 -0
- package/src/components/polylineIndex.ts +288 -0
- package/src/components/roiEdit.ts +154 -0
- package/src/components/stage/ImageLayer.tsx +118 -0
- package/src/components/stage/ImageStage.tsx +49 -10
- package/src/components/stage/StageSurface.tsx +177 -0
- package/src/components/stage/StageToolbar.tsx +105 -6
- package/src/components/stage/useScreenPx.ts +16 -0
- package/src/components/stage/view.ts +7 -2
- package/src/index.ts +55 -0
- package/src/styles.css +27 -3
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of editing an axis-aligned region: eight handles and an interior.
|
|
3
|
+
*
|
|
4
|
+
* Kept pure, so the cases that bite are tested without a pointer:
|
|
5
|
+
* - a drag that turns the box inside out through itself;
|
|
6
|
+
* - a handle pulled past the image edge;
|
|
7
|
+
* - a box collapsed to nothing.
|
|
8
|
+
*
|
|
9
|
+
* Everything is in the stage's image coordinates; `bounds` is the area a region must stay in.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { Rect } from "./stage/view";
|
|
13
|
+
import type { Point } from "./measureGeometry";
|
|
14
|
+
|
|
15
|
+
/** A region's handle, by compass direction. */
|
|
16
|
+
export type RoiHandle = "nw" | "n" | "ne" | "e" | "se" | "s" | "sw" | "w";
|
|
17
|
+
|
|
18
|
+
/** The eight handles, clockwise from the top-left corner. */
|
|
19
|
+
export const ROI_HANDLES: readonly RoiHandle[] = ["nw", "n", "ne", "e", "se", "s", "sw", "w"];
|
|
20
|
+
|
|
21
|
+
/** The CSS resize cursor for each handle, so the affordance is legible before the press. */
|
|
22
|
+
export const ROI_HANDLE_CURSOR: Readonly<Record<RoiHandle, string>> = {
|
|
23
|
+
nw: "nwse-resize",
|
|
24
|
+
n: "ns-resize",
|
|
25
|
+
ne: "nesw-resize",
|
|
26
|
+
e: "ew-resize",
|
|
27
|
+
se: "nwse-resize",
|
|
28
|
+
s: "ns-resize",
|
|
29
|
+
sw: "nesw-resize",
|
|
30
|
+
w: "ew-resize",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Where a handle sits.
|
|
35
|
+
*
|
|
36
|
+
* @param rect - The region.
|
|
37
|
+
* @param handle - The handle.
|
|
38
|
+
* @returns The handle's centre.
|
|
39
|
+
*/
|
|
40
|
+
export function roiHandlePoint(rect: Rect, handle: RoiHandle): Point {
|
|
41
|
+
const { x, y, width: w, height: h } = rect;
|
|
42
|
+
const cx = x + w / 2;
|
|
43
|
+
const cy = y + h / 2;
|
|
44
|
+
const px = handle.includes("w") ? x : handle.includes("e") ? x + w : cx;
|
|
45
|
+
const py = handle.includes("n") ? y : handle.includes("s") ? y + h : cy;
|
|
46
|
+
return { x: px, y: py };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The box spanned by two corners, with positive width and height whichever way it was
|
|
51
|
+
* dragged.
|
|
52
|
+
*
|
|
53
|
+
* @param a - One corner.
|
|
54
|
+
* @param b - The opposite corner.
|
|
55
|
+
* @returns The normalised box.
|
|
56
|
+
*/
|
|
57
|
+
export function rectFromCorners(a: Point, b: Point): Rect {
|
|
58
|
+
return {
|
|
59
|
+
x: Math.min(a.x, b.x),
|
|
60
|
+
y: Math.min(a.y, b.y),
|
|
61
|
+
width: Math.abs(b.x - a.x),
|
|
62
|
+
height: Math.abs(b.y - a.y),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A box held inside `bounds`, and no smaller than `minSize` on either side, unless `bounds`
|
|
68
|
+
* itself is smaller.
|
|
69
|
+
*
|
|
70
|
+
* @param rect - The box.
|
|
71
|
+
* @param bounds - The area it must stay in.
|
|
72
|
+
* @param minSize - The smallest side.
|
|
73
|
+
* @returns The clamped box.
|
|
74
|
+
*/
|
|
75
|
+
export function clampRect(rect: Rect, bounds: Rect, minSize: number): Rect {
|
|
76
|
+
const width = Math.min(Math.max(rect.width, minSize), bounds.width);
|
|
77
|
+
const height = Math.min(Math.max(rect.height, minSize), bounds.height);
|
|
78
|
+
return {
|
|
79
|
+
x: clamp(rect.x, bounds.x, bounds.x + bounds.width - width),
|
|
80
|
+
y: clamp(rect.y, bounds.y, bounds.y + bounds.height - height),
|
|
81
|
+
width,
|
|
82
|
+
height,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Drag a handle to `to`.
|
|
88
|
+
*
|
|
89
|
+
* The dragged edge follows the pointer and the opposite edge stays put, which is what makes
|
|
90
|
+
* a handle feel attached to the corner it is drawn on. Dragging an edge through its opposite
|
|
91
|
+
* flips the box instead of clamping it to zero: a clamp leaves the pointer moving and the box
|
|
92
|
+
* not, which reads as the drag having died.
|
|
93
|
+
*
|
|
94
|
+
* @param rect - The box before the drag.
|
|
95
|
+
* @param handle - The handle being dragged.
|
|
96
|
+
* @param to - The pointer, in image coordinates.
|
|
97
|
+
* @param bounds - The area the box must stay in.
|
|
98
|
+
* @param minSize - The smallest side.
|
|
99
|
+
* @returns The resized box.
|
|
100
|
+
*/
|
|
101
|
+
export function resizeRect(rect: Rect, handle: RoiHandle, to: Point, bounds: Rect, minSize: number): Rect {
|
|
102
|
+
let left = rect.x;
|
|
103
|
+
let top = rect.y;
|
|
104
|
+
let right = rect.x + rect.width;
|
|
105
|
+
let bottom = rect.y + rect.height;
|
|
106
|
+
const px = clamp(to.x, bounds.x, bounds.x + bounds.width);
|
|
107
|
+
const py = clamp(to.y, bounds.y, bounds.y + bounds.height);
|
|
108
|
+
if (handle.includes("w")) left = px;
|
|
109
|
+
if (handle.includes("e")) right = px;
|
|
110
|
+
if (handle.includes("n")) top = py;
|
|
111
|
+
if (handle.includes("s")) bottom = py;
|
|
112
|
+
return clampRect(rectFromCorners({ x: left, y: top }, { x: right, y: bottom }), bounds, minSize);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Move the whole box, held inside `bounds` without changing its size.
|
|
117
|
+
*
|
|
118
|
+
* @param rect - The box.
|
|
119
|
+
* @param dx - Horizontal shift.
|
|
120
|
+
* @param dy - Vertical shift.
|
|
121
|
+
* @param bounds - The area it must stay in.
|
|
122
|
+
* @returns The moved box.
|
|
123
|
+
*/
|
|
124
|
+
export function moveRect(rect: Rect, dx: number, dy: number, bounds: Rect): Rect {
|
|
125
|
+
return {
|
|
126
|
+
x: clamp(rect.x + dx, bounds.x, Math.max(bounds.x, bounds.x + bounds.width - rect.width)),
|
|
127
|
+
y: clamp(rect.y + dy, bounds.y, Math.max(bounds.y, bounds.y + bounds.height - rect.height)),
|
|
128
|
+
width: rect.width,
|
|
129
|
+
height: rect.height,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Whether two boxes are the same, to within `epsilon`. Useful for telling whether a region
|
|
135
|
+
* still matches the one a result was computed for.
|
|
136
|
+
*
|
|
137
|
+
* @param a - A box, or `null`.
|
|
138
|
+
* @param b - A box, or `null`.
|
|
139
|
+
* @param epsilon - The tolerance. Defaults to 1e-6.
|
|
140
|
+
* @returns `true` for two equal boxes, or two `null`s.
|
|
141
|
+
*/
|
|
142
|
+
export function sameRect(a: Rect | null, b: Rect | null, epsilon = 1e-6): boolean {
|
|
143
|
+
if (a === null || b === null) return a === b;
|
|
144
|
+
return (
|
|
145
|
+
Math.abs(a.x - b.x) < epsilon &&
|
|
146
|
+
Math.abs(a.y - b.y) < epsilon &&
|
|
147
|
+
Math.abs(a.width - b.width) < epsilon &&
|
|
148
|
+
Math.abs(a.height - b.height) < epsilon
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function clamp(value: number, low: number, high: number): number {
|
|
153
|
+
return Math.min(high, Math.max(low, value));
|
|
154
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The photograph, as a stage layer.
|
|
3
|
+
*
|
|
4
|
+
* The stage is laid out at the image's own pixel size, so the photograph is drawn at its
|
|
5
|
+
* natural size, with no `object-fit` letterbox for an overlay to disagree with.
|
|
6
|
+
*
|
|
7
|
+
* Two things every viewer of a large frame ends up doing by hand, done once here:
|
|
8
|
+
*
|
|
9
|
+
* - **A preview tier, then the full image.** A downscaled preview loads fast and is enough
|
|
10
|
+
* until it would be magnified — until the screen shows more pixels than it has. Past that
|
|
11
|
+
* point the full image is requested, and it stays once loaded, so zooming back out does
|
|
12
|
+
* not swap back. The preview stays underneath while the full image loads, so the frame
|
|
13
|
+
* never goes blank.
|
|
14
|
+
* - **Pixelated past a zoom.** Above about 4× the browser's smoothing paints detail the
|
|
15
|
+
* sensor never recorded; on a metrology bench the sensor's own samples are what is worth
|
|
16
|
+
* looking at.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { useState } from "react";
|
|
20
|
+
|
|
21
|
+
import { cn } from "@vitavision/ui";
|
|
22
|
+
import { useStage } from "./ImageStage";
|
|
23
|
+
|
|
24
|
+
/** A downscaled tier of the image, shown until the stage would magnify it. */
|
|
25
|
+
export interface ImageTier {
|
|
26
|
+
/** Its URL. */
|
|
27
|
+
src: string;
|
|
28
|
+
/** Its width in pixels. The full image's width is the stage's own. */
|
|
29
|
+
width: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Props of `ImageLayer`. */
|
|
33
|
+
export interface ImageLayerProps {
|
|
34
|
+
/** The full-resolution image's URL. */
|
|
35
|
+
src: string;
|
|
36
|
+
/** A smaller tier to show first; without one, `src` is shown from the start. */
|
|
37
|
+
preview?: ImageTier | undefined;
|
|
38
|
+
/** The image's description, for assistive technology. `""` marks it decorative. */
|
|
39
|
+
alt: string;
|
|
40
|
+
/** The zoom (CSS pixels per image pixel) from which pixels are drawn as blocks. Defaults to 4. */
|
|
41
|
+
pixelatedAbove?: number | undefined;
|
|
42
|
+
/** Called when the full image has loaded. */
|
|
43
|
+
onLoad?: (() => void) | undefined;
|
|
44
|
+
/** Called when the full image fails to load. */
|
|
45
|
+
onError?: (() => void) | undefined;
|
|
46
|
+
/** Merged onto both images with `cn`. */
|
|
47
|
+
className?: string | undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The photograph at its natural size inside an `ImageStage`, with an optional preview tier
|
|
52
|
+
* and pixelated rendering past `pixelatedAbove`. It takes no pointer events, so presses reach
|
|
53
|
+
* the stage and the layers above.
|
|
54
|
+
*
|
|
55
|
+
* Each image carries `data-tier` (`preview` or `full`); the full image carries `data-loaded`
|
|
56
|
+
* once it has loaded, and both carry `data-pixelated` while pixelated.
|
|
57
|
+
*/
|
|
58
|
+
export function ImageLayer({
|
|
59
|
+
src,
|
|
60
|
+
preview,
|
|
61
|
+
alt,
|
|
62
|
+
pixelatedAbove = 4,
|
|
63
|
+
onLoad,
|
|
64
|
+
onError,
|
|
65
|
+
className,
|
|
66
|
+
}: ImageLayerProps) {
|
|
67
|
+
const stage = useStage();
|
|
68
|
+
const scale = stage.view.scale;
|
|
69
|
+
// The full image is wanted once the preview would be magnified on screen: its own pixels
|
|
70
|
+
// per image pixel are `preview.width / image.width`. Not before the viewport is measured:
|
|
71
|
+
// the placeholder view then is 1:1, which would fetch the full image on every open.
|
|
72
|
+
const measured = stage.box.width > 0;
|
|
73
|
+
const needsFull = preview === undefined || (measured && scale * stage.image.width > preview.width);
|
|
74
|
+
// Requested once, kept: zooming back out should not drop the pixels already fetched.
|
|
75
|
+
const [requested, setRequested] = useState<string | null>(needsFull ? src : null);
|
|
76
|
+
if (needsFull && requested !== src) setRequested(src);
|
|
77
|
+
const [loaded, setLoaded] = useState<string | null>(null);
|
|
78
|
+
|
|
79
|
+
const showFull = requested === src;
|
|
80
|
+
const fullReady = loaded === src;
|
|
81
|
+
const pixelated = scale >= pixelatedAbove;
|
|
82
|
+
const shared = cn("pointer-events-none absolute inset-0 h-full w-full select-none", className);
|
|
83
|
+
const rendering = pixelated ? ("pixelated" as const) : undefined;
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<>
|
|
87
|
+
{preview && !fullReady && (
|
|
88
|
+
<img
|
|
89
|
+
src={preview.src}
|
|
90
|
+
alt={showFull ? "" : alt}
|
|
91
|
+
aria-hidden={showFull ? true : undefined}
|
|
92
|
+
draggable={false}
|
|
93
|
+
data-tier="preview"
|
|
94
|
+
data-pixelated={pixelated ? "" : undefined}
|
|
95
|
+
className={shared}
|
|
96
|
+
style={{ imageRendering: rendering }}
|
|
97
|
+
/>
|
|
98
|
+
)}
|
|
99
|
+
{showFull && (
|
|
100
|
+
<img
|
|
101
|
+
src={src}
|
|
102
|
+
alt={alt}
|
|
103
|
+
draggable={false}
|
|
104
|
+
data-tier="full"
|
|
105
|
+
data-loaded={fullReady ? "" : undefined}
|
|
106
|
+
data-pixelated={pixelated ? "" : undefined}
|
|
107
|
+
onLoad={() => {
|
|
108
|
+
setLoaded(src);
|
|
109
|
+
onLoad?.();
|
|
110
|
+
}}
|
|
111
|
+
onError={() => onError?.()}
|
|
112
|
+
className={cn(shared, preview && !fullReady && "opacity-0")}
|
|
113
|
+
style={{ imageRendering: rendering }}
|
|
114
|
+
/>
|
|
115
|
+
)}
|
|
116
|
+
</>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
use,
|
|
31
31
|
useCallback,
|
|
32
32
|
useEffect,
|
|
33
|
+
useImperativeHandle,
|
|
33
34
|
useLayoutEffect,
|
|
34
35
|
useMemo,
|
|
35
36
|
useRef,
|
|
@@ -40,6 +41,7 @@ import type {
|
|
|
40
41
|
KeyboardEvent as ReactKeyboardEvent,
|
|
41
42
|
PointerEvent as ReactPointerEvent,
|
|
42
43
|
ReactNode,
|
|
44
|
+
Ref,
|
|
43
45
|
} from "react";
|
|
44
46
|
|
|
45
47
|
import { cn } from "@vitavision/ui";
|
|
@@ -106,6 +108,20 @@ export interface StageContext {
|
|
|
106
108
|
panMode: boolean;
|
|
107
109
|
}
|
|
108
110
|
|
|
111
|
+
/**
|
|
112
|
+
* The stage's controls for code outside it — an inspector's "frame this contour", a
|
|
113
|
+
* "fit on open" — through `ImageStage`'s `ref`. Each is a no-op until the viewport has been
|
|
114
|
+
* measured. Layers inside the stage use `useStage` instead.
|
|
115
|
+
*/
|
|
116
|
+
export interface StageHandle {
|
|
117
|
+
/** Put a rect (image coordinates) on screen with a margin (CSS pixels, default 24). */
|
|
118
|
+
frame: (rect: Rect, pad?: number) => void;
|
|
119
|
+
/** Fit the whole image. */
|
|
120
|
+
fit: () => void;
|
|
121
|
+
/** Set a scale about the viewport centre, or about `anchor` in client coordinates. */
|
|
122
|
+
zoomTo: (scale: number, anchor?: Point) => void;
|
|
123
|
+
}
|
|
124
|
+
|
|
109
125
|
const ImageStageContext = createContext<StageContext | null>(null);
|
|
110
126
|
|
|
111
127
|
/**
|
|
@@ -162,6 +178,14 @@ export interface ImageStageProps {
|
|
|
162
178
|
panKeys?: boolean;
|
|
163
179
|
/** The viewport's accessible name. Defaults to "Image canvas". */
|
|
164
180
|
label?: string;
|
|
181
|
+
/**
|
|
182
|
+
* How a `null` view opens: `"auto"` (the default) at 1:1 when the image fits the viewport
|
|
183
|
+
* and fit otherwise; `"fit"` always fit, for a viewer that should show the whole frame
|
|
184
|
+
* whatever its size.
|
|
185
|
+
*/
|
|
186
|
+
initialView?: "auto" | "fit" | undefined;
|
|
187
|
+
/** The stage's controls for code outside it; see `StageHandle`. */
|
|
188
|
+
ref?: Ref<StageHandle> | undefined;
|
|
165
189
|
}
|
|
166
190
|
|
|
167
191
|
/**
|
|
@@ -190,6 +214,8 @@ export function ImageStage({
|
|
|
190
214
|
shortcuts = true,
|
|
191
215
|
panKeys = true,
|
|
192
216
|
label = "Image canvas",
|
|
217
|
+
initialView: opening = "auto",
|
|
218
|
+
ref,
|
|
193
219
|
}: ImageStageProps) {
|
|
194
220
|
const viewportRef = useRef<HTMLDivElement | null>(null);
|
|
195
221
|
const [box, setBox] = useState<Box>({ width: 0, height: 0 });
|
|
@@ -202,17 +228,17 @@ export function ImageStage({
|
|
|
202
228
|
|
|
203
229
|
// What the handlers attached once (and the stable callbacks below) read: the props and
|
|
204
230
|
// box of the last commit, so none of them closes over stale values.
|
|
205
|
-
const latestRef = useRef({ view, box, image, clamp, onView });
|
|
231
|
+
const latestRef = useRef({ view, box, image, clamp, onView, opening });
|
|
206
232
|
useLayoutEffect(() => {
|
|
207
|
-
latestRef.current = { view, box, image, clamp, onView };
|
|
233
|
+
latestRef.current = { view, box, image, clamp, onView, opening };
|
|
208
234
|
});
|
|
209
235
|
/** The view to come back to when a double-click leaves fit. */
|
|
210
236
|
const previousRef = useRef<StageView | null>(null);
|
|
211
237
|
const dragRef = useRef<{ x: number; y: number; tx: number; ty: number; moved: boolean } | null>(null);
|
|
212
238
|
|
|
213
239
|
const effective = useMemo(
|
|
214
|
-
() => view ?? (box.width > 0 ?
|
|
215
|
-
[view, box, image],
|
|
240
|
+
() => view ?? (box.width > 0 ? openingView(opening, box, image) : UNMEASURED_VIEW),
|
|
241
|
+
[view, box, image, opening],
|
|
216
242
|
);
|
|
217
243
|
|
|
218
244
|
const commit = useCallback((next: StageView) => {
|
|
@@ -244,8 +270,8 @@ export function ImageStage({
|
|
|
244
270
|
const next = { width, height };
|
|
245
271
|
measuredRef.current = next;
|
|
246
272
|
setBox(next);
|
|
247
|
-
const { view: v, image: i, clamp: c, onView: report } = latestRef.current;
|
|
248
|
-
if (v === null) report(
|
|
273
|
+
const { view: v, image: i, clamp: c, onView: report, opening: o } = latestRef.current;
|
|
274
|
+
if (v === null) report(openingView(o, next, i));
|
|
249
275
|
// The first measurement: there is no previous viewport to re-anchor against, so the
|
|
250
276
|
// caller's view is kept — only made legal for this one.
|
|
251
277
|
else if (!(current.width > 0)) report(clampView(v, next, i, c));
|
|
@@ -345,6 +371,8 @@ export function ImageStage({
|
|
|
345
371
|
report(frameRect(b, i, rect, pad));
|
|
346
372
|
}, []);
|
|
347
373
|
|
|
374
|
+
useImperativeHandle(ref, () => ({ frame, fit, zoomTo }), [frame, fit, zoomTo]);
|
|
375
|
+
|
|
348
376
|
const atFit = box.width > 0 && isFit(effective, box, image);
|
|
349
377
|
|
|
350
378
|
const context = useMemo<StageContext>(
|
|
@@ -496,7 +524,9 @@ export function ImageStage({
|
|
|
496
524
|
data-panning={panning ? "" : undefined}
|
|
497
525
|
data-pan-mode={panMode ? "" : undefined}
|
|
498
526
|
className={cn(
|
|
499
|
-
|
|
527
|
+
// `@container/stage`: the toolbar and readout collapse by the canvas's own width,
|
|
528
|
+
// not the window's — a canvas beside a wide inspector is narrow on a wide screen.
|
|
529
|
+
"@container/stage relative h-full w-full overflow-hidden rounded-control border border-line bg-canvas select-none",
|
|
500
530
|
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-signal",
|
|
501
531
|
panning ? "cursor-grabbing" : panMode ? "cursor-grab" : "cursor-default",
|
|
502
532
|
className,
|
|
@@ -533,9 +563,13 @@ export function ImageStage({
|
|
|
533
563
|
an ordinary window on a two-column workbench. `justify-between` keeps them apart
|
|
534
564
|
and lets the readout be the one that gives up room. */}
|
|
535
565
|
{(toolbar || readout) && (
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
566
|
+
// Wraps rather than clips: on a canvas too narrow for both, the readout moves onto
|
|
567
|
+
// its own line above the toolbar instead of being truncated to nothing.
|
|
568
|
+
<div className="pointer-events-none absolute inset-x-2 bottom-2 z-10 flex flex-wrap-reverse items-end justify-between gap-2">
|
|
569
|
+
<div className="pointer-events-auto max-w-full shrink-0">{toolbar}</div>
|
|
570
|
+
<div data-readout-slot="" className="max-w-full min-w-0 truncate">
|
|
571
|
+
{readout}
|
|
572
|
+
</div>
|
|
539
573
|
</div>
|
|
540
574
|
)}
|
|
541
575
|
</div>
|
|
@@ -543,6 +577,11 @@ export function ImageStage({
|
|
|
543
577
|
);
|
|
544
578
|
}
|
|
545
579
|
|
|
580
|
+
/** The view a `null` view opens at, by the `initialView` policy. */
|
|
581
|
+
function openingView(opening: "auto" | "fit", box: Box, image: Box): StageView {
|
|
582
|
+
return opening === "fit" ? fitView(box, image) : initialView(box, image);
|
|
583
|
+
}
|
|
584
|
+
|
|
546
585
|
function hoverPoint(
|
|
547
586
|
event: { clientX: number; clientY: number },
|
|
548
587
|
element: HTMLDivElement | null,
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stage's tool model: one place that decides what a press on the image means.
|
|
3
|
+
*
|
|
4
|
+
* Several layers usually want a full-frame target: one to draw a region, one to sweep a
|
|
5
|
+
* selection, one to place a point. Only the topmost element under the pointer receives a press,
|
|
6
|
+
* so two full-frame targets are one that always wins and one that never fires. Apps that grew
|
|
7
|
+
* such layers one by one found this out the hard way.
|
|
8
|
+
*
|
|
9
|
+
* The rule here is one surface per stage, under the layers' own small targets (a handle, a
|
|
10
|
+
* stroke). It asks the app what a press means, in priority order, and a press the app declines
|
|
11
|
+
* bubbles to the stage, which pans.
|
|
12
|
+
*
|
|
13
|
+
* Drags listen on `window`, not on the pressed element. `setPointerCapture` routes every later
|
|
14
|
+
* event to that one element, so a drag begun on a handle sent its moves to the handle, never
|
|
15
|
+
* to the surface that held the move logic. Listening at the window also lets a drag survive
|
|
16
|
+
* the pointer leaving the canvas, which is what dragging a corner outward does.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
20
|
+
import type { PointerEvent as ReactPointerEvent } from "react";
|
|
21
|
+
|
|
22
|
+
import type { Point } from "../measureGeometry";
|
|
23
|
+
import { useStage } from "./ImageStage";
|
|
24
|
+
import { imageViewBox } from "./view";
|
|
25
|
+
|
|
26
|
+
/** What a drag does as the pointer moves and when it is released, in image coordinates. */
|
|
27
|
+
export interface StageDrag {
|
|
28
|
+
/** Each move of the pointer while the button is held. */
|
|
29
|
+
onMove?: ((point: Point, event: PointerEvent) => void) | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Release. `moved` is whether the pointer travelled more than a few screen pixels, which
|
|
32
|
+
* tells a click from a drag.
|
|
33
|
+
*/
|
|
34
|
+
onEnd?: ((point: Point, event: PointerEvent, moved: boolean) => void) | undefined;
|
|
35
|
+
/** The drag was interrupted (`pointercancel`) or the component unmounted mid-drag. */
|
|
36
|
+
onCancel?: (() => void) | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** How far a press may travel and still be a click, in screen pixels. */
|
|
40
|
+
const CLICK_SLOP = 3;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Start a drag from a press on any element inside an `ImageStage`.
|
|
44
|
+
*
|
|
45
|
+
* The press is claimed: propagation stops, so the stage does not pan. Later moves and the
|
|
46
|
+
* release are read from `window` and converted to image coordinates. Call it from a
|
|
47
|
+
* `pointerdown` handler.
|
|
48
|
+
*
|
|
49
|
+
* @returns `start(event, drag)`.
|
|
50
|
+
*/
|
|
51
|
+
export function useStageDrag(): (event: ReactPointerEvent<Element>, drag: StageDrag) => void {
|
|
52
|
+
// `toImage` is stable for the stage's lifetime and reads the live view, so a drag converts
|
|
53
|
+
// with the view in effect at each move.
|
|
54
|
+
const { toImage } = useStage();
|
|
55
|
+
const cleanupRef = useRef<(() => void) | null>(null);
|
|
56
|
+
|
|
57
|
+
// A drag still in flight when the component unmounts is cancelled, not leaked.
|
|
58
|
+
useEffect(() => () => cleanupRef.current?.(), []);
|
|
59
|
+
|
|
60
|
+
return useCallback((event: ReactPointerEvent<Element>, drag: StageDrag) => {
|
|
61
|
+
event.stopPropagation();
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
cleanupRef.current?.();
|
|
64
|
+
const from = { x: event.clientX, y: event.clientY };
|
|
65
|
+
let moved = false;
|
|
66
|
+
const at = (e: PointerEvent) => toImage({ x: e.clientX, y: e.clientY });
|
|
67
|
+
|
|
68
|
+
const move = (e: PointerEvent) => {
|
|
69
|
+
if (!moved && Math.hypot(e.clientX - from.x, e.clientY - from.y) > CLICK_SLOP) moved = true;
|
|
70
|
+
drag.onMove?.(at(e), e);
|
|
71
|
+
};
|
|
72
|
+
const up = (e: PointerEvent) => {
|
|
73
|
+
stop();
|
|
74
|
+
drag.onEnd?.(at(e), e, moved);
|
|
75
|
+
};
|
|
76
|
+
const cancel = () => {
|
|
77
|
+
stop();
|
|
78
|
+
drag.onCancel?.();
|
|
79
|
+
};
|
|
80
|
+
const stop = () => {
|
|
81
|
+
window.removeEventListener("pointermove", move);
|
|
82
|
+
window.removeEventListener("pointerup", up);
|
|
83
|
+
window.removeEventListener("pointercancel", cancel);
|
|
84
|
+
cleanupRef.current = null;
|
|
85
|
+
};
|
|
86
|
+
window.addEventListener("pointermove", move);
|
|
87
|
+
window.addEventListener("pointerup", up);
|
|
88
|
+
window.addEventListener("pointercancel", cancel);
|
|
89
|
+
cleanupRef.current = cancel;
|
|
90
|
+
}, [toImage]);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A press on the surface, as the app's `onPress` sees it. */
|
|
94
|
+
export interface StagePress {
|
|
95
|
+
/** Where, in image coordinates. */
|
|
96
|
+
point: Point;
|
|
97
|
+
/** Shift was held. */
|
|
98
|
+
shiftKey: boolean;
|
|
99
|
+
/** Alt / Option was held. */
|
|
100
|
+
altKey: boolean;
|
|
101
|
+
/** ⌘ (macOS) or Ctrl was held: the usual "add to / toggle in the selection" modifier. */
|
|
102
|
+
metaKey: boolean;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Props of `StageSurface`. */
|
|
106
|
+
export interface StageSurfaceProps {
|
|
107
|
+
/**
|
|
108
|
+
* What a press means. Return a `StageDrag` to claim it (a click is a drag that never
|
|
109
|
+
* moved), or nothing to decline it: a declined press reaches the stage, which pans.
|
|
110
|
+
*/
|
|
111
|
+
onPress: (press: StagePress) => StageDrag | null | undefined | void;
|
|
112
|
+
/** The cursor over the image while no drag is in flight, e.g. `crosshair` for a draw tool. */
|
|
113
|
+
cursor?: string | undefined;
|
|
114
|
+
/** Where the pointer is over the image while no drag is in flight, for hover feedback. */
|
|
115
|
+
onHover?: ((point: Point) => void) | undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The stage's one full-frame press target. Put it inside an `ImageStage`, below the layers
|
|
120
|
+
* that have their own small targets (handles, strokes). Each press is offered to `onPress`
|
|
121
|
+
* unless the hand tool or a held space bar make it a pan; a declined press pans.
|
|
122
|
+
*
|
|
123
|
+
* The surface draws nothing. It carries `data-dragging` while a drag it started is in
|
|
124
|
+
* flight.
|
|
125
|
+
*/
|
|
126
|
+
export function StageSurface({ onPress, cursor, onHover }: StageSurfaceProps) {
|
|
127
|
+
const stage = useStage();
|
|
128
|
+
const start = useStageDrag();
|
|
129
|
+
const [dragging, setDragging] = useState(false);
|
|
130
|
+
|
|
131
|
+
const onPointerDown = (event: ReactPointerEvent<SVGRectElement>) => {
|
|
132
|
+
if (event.button !== 0 || stage.panMode) return;
|
|
133
|
+
const drag = onPress({
|
|
134
|
+
point: stage.toImage({ x: event.clientX, y: event.clientY }),
|
|
135
|
+
shiftKey: event.shiftKey,
|
|
136
|
+
altKey: event.altKey,
|
|
137
|
+
metaKey: event.metaKey || event.ctrlKey,
|
|
138
|
+
});
|
|
139
|
+
if (!drag) return; // Declined: the stage pans.
|
|
140
|
+
setDragging(true);
|
|
141
|
+
start(event, {
|
|
142
|
+
onMove: drag.onMove,
|
|
143
|
+
onEnd: (point, e, moved) => {
|
|
144
|
+
setDragging(false);
|
|
145
|
+
drag.onEnd?.(point, e, moved);
|
|
146
|
+
},
|
|
147
|
+
onCancel: () => {
|
|
148
|
+
setDragging(false);
|
|
149
|
+
drag.onCancel?.();
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<svg
|
|
156
|
+
viewBox={imageViewBox(stage.image)}
|
|
157
|
+
className="pointer-events-none absolute inset-0 h-full w-full"
|
|
158
|
+
data-dragging={dragging ? "" : undefined}
|
|
159
|
+
aria-hidden
|
|
160
|
+
>
|
|
161
|
+
<rect
|
|
162
|
+
data-stage-surface=""
|
|
163
|
+
x={-0.5}
|
|
164
|
+
y={-0.5}
|
|
165
|
+
width={stage.image.width}
|
|
166
|
+
height={stage.image.height}
|
|
167
|
+
fill="transparent"
|
|
168
|
+
className="pointer-events-auto"
|
|
169
|
+
style={{ cursor: stage.panMode ? undefined : cursor }}
|
|
170
|
+
onPointerDown={onPointerDown}
|
|
171
|
+
onPointerMove={(event) => {
|
|
172
|
+
if (!dragging) onHover?.(stage.toImage({ x: event.clientX, y: event.clientY }));
|
|
173
|
+
}}
|
|
174
|
+
/>
|
|
175
|
+
</svg>
|
|
176
|
+
);
|
|
177
|
+
}
|