@vitavision/stage2d 0.6.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/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +99 -0
- package/dist/index.d.ts +712 -0
- package/dist/index.js +1559 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
- package/src/api/mapValues.ts +127 -0
- package/src/components/MeasureOverlay.tsx +353 -0
- package/src/components/ZoomPanCanvas.tsx +314 -0
- package/src/components/measureGeometry.ts +234 -0
- package/src/components/stage/ImageStage.tsx +594 -0
- package/src/components/stage/StageToolbar.tsx +254 -0
- package/src/components/stage/view.ts +319 -0
- package/src/index.ts +97 -0
- package/src/styles.css +7 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The viewer's own controls, floating over the image rather than filed in a side panel.
|
|
3
|
+
*
|
|
4
|
+
* Zoom, fit and 1:1 are properties of *the picture*, not of the task beside it, and a
|
|
5
|
+
* workbench that puts them in the inspector spends inspector width on them on every screen
|
|
6
|
+
* while leaving the canvas with no visible affordance at all — which is indistinguishable
|
|
7
|
+
* from a canvas that cannot zoom.
|
|
8
|
+
*
|
|
9
|
+
* The bar is deliberately small and quiet: `bg-overlay/85` over a blur, one row of icon
|
|
10
|
+
* buttons, no labels except the percentage. It is chrome over the subject, so it recedes
|
|
11
|
+
* until aimed at.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { Maximize2, Minus, Plus, Scan } from "lucide-react";
|
|
15
|
+
import { useEffect, useRef, useState } from "react";
|
|
16
|
+
import type { ComponentPropsWithRef, ReactNode } from "react";
|
|
17
|
+
|
|
18
|
+
import { cn, focusRing } from "@vitavision/ui";
|
|
19
|
+
import { useStage } from "./ImageStage";
|
|
20
|
+
import { MAX_SCALE, formatScale, scaleRange, steppedScale } from "./view";
|
|
21
|
+
|
|
22
|
+
/** The presets the percentage menu offers, beside "Fit". */
|
|
23
|
+
const PRESETS = [0.25, 0.5, 1, 2, 4, 8];
|
|
24
|
+
|
|
25
|
+
/** Props of `StageToolbar`. */
|
|
26
|
+
export interface StageToolbarProps {
|
|
27
|
+
/** App-specific groups — a tool selector, a layers popover — after a divider. */
|
|
28
|
+
children?: ReactNode;
|
|
29
|
+
/** Merged onto the bar with `cn`. */
|
|
30
|
+
className?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The stage's zoom controls — zoom out, a percentage menu (fit and presets), zoom in, fit,
|
|
35
|
+
* and 100% — plus any app groups passed as children. Pass it as `ImageStage`'s `toolbar`;
|
|
36
|
+
* it reads the stage through `useStage`.
|
|
37
|
+
*
|
|
38
|
+
* The bar carries `data-fit` while the view is fit; the percentage button carries
|
|
39
|
+
* `data-state="open"` or `"closed"` for its menu.
|
|
40
|
+
*/
|
|
41
|
+
export function StageToolbar({ children, className }: StageToolbarProps) {
|
|
42
|
+
const stage = useStage();
|
|
43
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
44
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
45
|
+
const [min, max] = scaleRange(stage.box, stage.image);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!menuOpen) return;
|
|
49
|
+
const close = (event: MouseEvent) => {
|
|
50
|
+
if (!menuRef.current?.contains(event.target as Node)) setMenuOpen(false);
|
|
51
|
+
};
|
|
52
|
+
window.addEventListener("pointerdown", close);
|
|
53
|
+
return () => window.removeEventListener("pointerdown", close);
|
|
54
|
+
}, [menuOpen]);
|
|
55
|
+
|
|
56
|
+
const step = (direction: 1 | -1) => stage.zoomTo(steppedScale(stage.view.scale, direction, min, max));
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div
|
|
60
|
+
data-fit={stage.isFit ? "" : undefined}
|
|
61
|
+
className={cn(
|
|
62
|
+
"flex items-center gap-0.5 rounded-panel border border-line bg-overlay/85 p-1 shadow-lg backdrop-blur",
|
|
63
|
+
className,
|
|
64
|
+
)}
|
|
65
|
+
// Every press here is a control, never the start of a pan.
|
|
66
|
+
onPointerDown={(event) => event.stopPropagation()}
|
|
67
|
+
onDoubleClick={(event) => event.stopPropagation()}
|
|
68
|
+
>
|
|
69
|
+
<StageButton label="Zoom out" onClick={() => step(-1)} disabled={stage.view.scale <= min + 1e-9}>
|
|
70
|
+
<Minus className="size-4" aria-hidden />
|
|
71
|
+
</StageButton>
|
|
72
|
+
|
|
73
|
+
<div className="relative" ref={menuRef}>
|
|
74
|
+
<button
|
|
75
|
+
type="button"
|
|
76
|
+
aria-haspopup="menu"
|
|
77
|
+
aria-expanded={menuOpen}
|
|
78
|
+
data-state={menuOpen ? "open" : "closed"}
|
|
79
|
+
onClick={() => setMenuOpen((open) => !open)}
|
|
80
|
+
className={cn(
|
|
81
|
+
"h-7 min-w-14 rounded-control px-2 font-mono text-xs text-fg tabular-nums hover:bg-raised",
|
|
82
|
+
focusRing,
|
|
83
|
+
)}
|
|
84
|
+
>
|
|
85
|
+
{stage.isFit ? "Fit" : formatScale(stage.view.scale)}
|
|
86
|
+
</button>
|
|
87
|
+
{menuOpen && (
|
|
88
|
+
<div
|
|
89
|
+
role="menu"
|
|
90
|
+
className="absolute bottom-9 left-0 z-20 min-w-28 rounded-panel border border-line bg-overlay p-1 shadow-lg"
|
|
91
|
+
>
|
|
92
|
+
<MenuItem
|
|
93
|
+
onClick={() => {
|
|
94
|
+
stage.fit();
|
|
95
|
+
setMenuOpen(false);
|
|
96
|
+
}}
|
|
97
|
+
active={stage.isFit}
|
|
98
|
+
>
|
|
99
|
+
Fit
|
|
100
|
+
</MenuItem>
|
|
101
|
+
{PRESETS.filter((scale) => scale >= min && scale <= max).map((scale) => (
|
|
102
|
+
<MenuItem
|
|
103
|
+
key={scale}
|
|
104
|
+
onClick={() => {
|
|
105
|
+
stage.zoomTo(scale);
|
|
106
|
+
setMenuOpen(false);
|
|
107
|
+
}}
|
|
108
|
+
active={!stage.isFit && Math.abs(stage.view.scale - scale) < 1e-6}
|
|
109
|
+
>
|
|
110
|
+
{formatScale(scale)}
|
|
111
|
+
</MenuItem>
|
|
112
|
+
))}
|
|
113
|
+
</div>
|
|
114
|
+
)}
|
|
115
|
+
</div>
|
|
116
|
+
|
|
117
|
+
<StageButton
|
|
118
|
+
label="Zoom in"
|
|
119
|
+
onClick={() => step(1)}
|
|
120
|
+
disabled={stage.view.scale >= Math.min(max, MAX_SCALE) - 1e-9}
|
|
121
|
+
>
|
|
122
|
+
<Plus className="size-4" aria-hidden />
|
|
123
|
+
</StageButton>
|
|
124
|
+
|
|
125
|
+
<StageToolbarDivider />
|
|
126
|
+
|
|
127
|
+
<StageButton label="Fit to window" onClick={stage.fit} pressed={stage.isFit}>
|
|
128
|
+
<Maximize2 className="size-4" aria-hidden />
|
|
129
|
+
</StageButton>
|
|
130
|
+
<StageButton
|
|
131
|
+
label="Actual size (100%)"
|
|
132
|
+
onClick={() => stage.zoomTo(1)}
|
|
133
|
+
pressed={!stage.isFit && Math.abs(stage.view.scale - 1) < 1e-6}
|
|
134
|
+
>
|
|
135
|
+
<Scan className="size-4" aria-hidden />
|
|
136
|
+
</StageButton>
|
|
137
|
+
|
|
138
|
+
{children && (
|
|
139
|
+
<>
|
|
140
|
+
<StageToolbarDivider />
|
|
141
|
+
{children}
|
|
142
|
+
</>
|
|
143
|
+
)}
|
|
144
|
+
</div>
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Props of `StageButton`: a `<button>`'s, less the ones it sets itself. */
|
|
149
|
+
export interface StageButtonProps
|
|
150
|
+
extends Omit<ComponentPropsWithRef<"button">, "type" | "title" | "aria-label" | "aria-pressed" | "children"> {
|
|
151
|
+
/** The accessible name, also shown as the tooltip. */
|
|
152
|
+
label: string;
|
|
153
|
+
/** A toggle's state; omit for a plain action button. */
|
|
154
|
+
pressed?: boolean;
|
|
155
|
+
/** The icon (mark it `aria-hidden`). */
|
|
156
|
+
children: ReactNode;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The bar's own button shape, exported so app groups match it exactly. An icon button
|
|
161
|
+
* named by `label`; a toggle when `pressed` is given, exposed as `aria-pressed` and
|
|
162
|
+
* `data-state="on"` or `"off"`. Other button props (and `ref`) are passed through.
|
|
163
|
+
*/
|
|
164
|
+
export function StageButton({ label, pressed, className, children, ...props }: StageButtonProps) {
|
|
165
|
+
return (
|
|
166
|
+
<button
|
|
167
|
+
{...props}
|
|
168
|
+
type="button"
|
|
169
|
+
title={label}
|
|
170
|
+
aria-label={label}
|
|
171
|
+
aria-pressed={pressed}
|
|
172
|
+
data-state={pressed === undefined ? undefined : pressed ? "on" : "off"}
|
|
173
|
+
className={cn(
|
|
174
|
+
"grid size-7 place-items-center rounded-control text-fg-muted transition-colors",
|
|
175
|
+
"hover:bg-raised hover:text-fg disabled:pointer-events-none disabled:opacity-40",
|
|
176
|
+
pressed && "bg-signal/15 text-signal",
|
|
177
|
+
focusRing,
|
|
178
|
+
className,
|
|
179
|
+
)}
|
|
180
|
+
>
|
|
181
|
+
{children}
|
|
182
|
+
</button>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Props of `StageToolbarDivider`. */
|
|
187
|
+
export interface StageToolbarDividerProps {
|
|
188
|
+
/** Merged onto the divider with `cn`. */
|
|
189
|
+
className?: string;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** A thin vertical rule between groups of `StageButton`s. Decorative (`aria-hidden`). */
|
|
193
|
+
export function StageToolbarDivider({ className }: StageToolbarDividerProps) {
|
|
194
|
+
return <span className={cn("mx-0.5 h-5 w-px bg-line", className)} aria-hidden />;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function MenuItem({
|
|
198
|
+
onClick,
|
|
199
|
+
active,
|
|
200
|
+
children,
|
|
201
|
+
}: {
|
|
202
|
+
onClick: () => void;
|
|
203
|
+
active?: boolean;
|
|
204
|
+
children: ReactNode;
|
|
205
|
+
}) {
|
|
206
|
+
return (
|
|
207
|
+
<button
|
|
208
|
+
type="button"
|
|
209
|
+
role="menuitem"
|
|
210
|
+
onClick={onClick}
|
|
211
|
+
className={cn(
|
|
212
|
+
"block w-full rounded-control px-2 py-1 text-left font-mono text-xs tabular-nums hover:bg-raised",
|
|
213
|
+
active ? "text-signal" : "text-fg",
|
|
214
|
+
focusRing,
|
|
215
|
+
)}
|
|
216
|
+
>
|
|
217
|
+
{children}
|
|
218
|
+
</button>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Props of `StageReadout`. */
|
|
223
|
+
export interface StageReadoutProps {
|
|
224
|
+
/** The pointer in image pixels (from `ImageStage`'s `onHover`); the image size shows without it. */
|
|
225
|
+
cursor?: { x: number; y: number } | null;
|
|
226
|
+
/** Appended after a separator — a pixel value, a channel. */
|
|
227
|
+
extra?: ReactNode;
|
|
228
|
+
/** Merged onto the readout with `cn`. */
|
|
229
|
+
className?: string;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The instrument's display line for the canvas: where the cursor is, and how magnified.
|
|
234
|
+
* Pass it as `ImageStage`'s `readout`; it reads the stage through `useStage`.
|
|
235
|
+
*
|
|
236
|
+
* In image pixels, because that is the coordinate every number the backend returns is in —
|
|
237
|
+
* a readout in screen pixels would be a second coordinate system to reconcile by hand.
|
|
238
|
+
*/
|
|
239
|
+
export function StageReadout({ cursor, extra, className }: StageReadoutProps) {
|
|
240
|
+
const stage = useStage();
|
|
241
|
+
return (
|
|
242
|
+
<span
|
|
243
|
+
className={cn(
|
|
244
|
+
"rounded bg-black/60 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums",
|
|
245
|
+
className,
|
|
246
|
+
)}
|
|
247
|
+
>
|
|
248
|
+
{cursor ? `${cursor.x.toFixed(1)}, ${cursor.y.toFixed(1)} px` : `${stage.image.width}×${stage.image.height}`}
|
|
249
|
+
{" · "}
|
|
250
|
+
{stage.isFit ? "fit" : formatScale(stage.view.scale)}
|
|
251
|
+
{extra && <> · {extra}</>}
|
|
252
|
+
</span>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image ↔ screen transform, as pure arithmetic.
|
|
3
|
+
*
|
|
4
|
+
* One transform, in absolute units: the stage element is laid out at exactly the source
|
|
5
|
+
* image's pixel size and carries `translate(tx, ty) scale(scale)` about its top-left corner,
|
|
6
|
+
* so an image point `p` lands at `p · scale + t` and nothing else participates. `scale` is
|
|
7
|
+
* therefore *CSS pixels per image pixel* — `scale === 1` is 100%, and fit is whatever
|
|
8
|
+
* `fitScale` returns for the current viewport, a value rather than a magic constant.
|
|
9
|
+
*
|
|
10
|
+
* The predecessor (`ZoomPanCanvas`) instead composed two scales it never named: a layout
|
|
11
|
+
* scale from the frame's own size, and a CSS transform about the frame's centre. That
|
|
12
|
+
* worked only while the frame's aspect ratio matched the image's, and the one consumer that
|
|
13
|
+
* forgot to enforce that drew its overlays stretched against a letterboxed photograph — the
|
|
14
|
+
* layers drifting apart on window resize, which is precisely the failure this replaces.
|
|
15
|
+
* Laying the stage out at the image's own size removes the first scale entirely, so a child
|
|
16
|
+
* `<svg viewBox="0 0 W H">` is registered with the photograph by construction.
|
|
17
|
+
*
|
|
18
|
+
* Every function here is pure and tested, because this is arithmetic that goes wrong in a
|
|
19
|
+
* way nothing on screen reveals: an overlay that is confidently off by a pan offset looks
|
|
20
|
+
* exactly like an overlay that works.
|
|
21
|
+
*
|
|
22
|
+
* Points are in **viewport-local** CSS pixels — client coordinates with the viewport's
|
|
23
|
+
* `getBoundingClientRect()` origin already subtracted. Keeping the rect out of these
|
|
24
|
+
* signatures is what makes them testable without a DOM.
|
|
25
|
+
*
|
|
26
|
+
* **Image coordinates name pixel centres**, which is the convention every image-processing
|
|
27
|
+
* result uses: `i` means the centre of pixel `i`, so an edge detected exactly between two
|
|
28
|
+
* columns comes back as `i + 0.5`. CSS is the other convention: an `<img>` laid out at its
|
|
29
|
+
* natural size puts pixel `i` across `[i, i + 1)`, so *its* `i` is the pixel's left edge.
|
|
30
|
+
* The half-pixel between them is what `PIXEL_CENTRE` carries, and it is not cosmetic on a
|
|
31
|
+
* metrology bench — at 8x it is four screen pixels of disagreement between an overlay and
|
|
32
|
+
* the edge it claims to mark, and it is invisible at fit, which is where it gets missed.
|
|
33
|
+
*
|
|
34
|
+
* A layer drawn in image coordinates therefore uses `imageViewBox`, not `0 0 W H`.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import type { Point } from "../measureGeometry";
|
|
38
|
+
|
|
39
|
+
/** A `translate(tx, ty) scale(scale)` about the stage's top-left corner. */
|
|
40
|
+
export interface StageView {
|
|
41
|
+
/** CSS pixels per image pixel. `1` is 100%. */
|
|
42
|
+
scale: number;
|
|
43
|
+
/** Horizontal offset of the image's top-left corner from the viewport's, in CSS pixels. */
|
|
44
|
+
tx: number;
|
|
45
|
+
/** Vertical offset of the image's top-left corner from the viewport's, in CSS pixels. */
|
|
46
|
+
ty: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A size: the viewport's in CSS pixels, or the image's in image pixels. */
|
|
50
|
+
export interface Box {
|
|
51
|
+
/** Horizontal extent. */
|
|
52
|
+
width: number;
|
|
53
|
+
/** Vertical extent. */
|
|
54
|
+
height: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** An axis-aligned rectangle in image coordinates, from its top-left corner. */
|
|
58
|
+
export interface Rect {
|
|
59
|
+
/** Left edge. */
|
|
60
|
+
x: number;
|
|
61
|
+
/** Top edge. */
|
|
62
|
+
y: number;
|
|
63
|
+
/** Horizontal extent. */
|
|
64
|
+
width: number;
|
|
65
|
+
/** Vertical extent. */
|
|
66
|
+
height: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The offset between the two pixel conventions: an image coordinate `i` sits at CSS
|
|
71
|
+
* `i + 0.5` inside a stage laid out at the image's natural size.
|
|
72
|
+
*/
|
|
73
|
+
export const PIXEL_CENTRE = 0.5;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The `viewBox` for an SVG layer drawn in image coordinates, over a stage of that image.
|
|
77
|
+
*
|
|
78
|
+
* Shifted by half a pixel so a primitive at image coordinate `i` lands on the *centre* of
|
|
79
|
+
* pixel `i` rather than on its left edge. Its extent is unchanged — the box still covers
|
|
80
|
+
* exactly the image — so nothing about sizing or `preserveAspectRatio` changes with it.
|
|
81
|
+
*/
|
|
82
|
+
export function imageViewBox(image: Box): string {
|
|
83
|
+
return `${-PIXEL_CENTRE} ${-PIXEL_CENTRE} ${image.width} ${image.height}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The most a viewer may magnify: past this the resampler is the subject, not the sensor. */
|
|
87
|
+
export const MAX_SCALE = 32;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* How far below fit zooming out may go.
|
|
91
|
+
*
|
|
92
|
+
* `ZoomPanCanvas` bottomed out *at* fit, on the reasoning that there is nothing further to
|
|
93
|
+
* see. There is: room around the part, which is what you want when dragging an ROI out to
|
|
94
|
+
* the frame edge or checking that a model's points do not run off the image.
|
|
95
|
+
*/
|
|
96
|
+
export const MIN_SCALE_VS_FIT = 0.25;
|
|
97
|
+
|
|
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
|
+
];
|
|
102
|
+
|
|
103
|
+
/** Relative tolerance for "is this the same scale" — a hair under a tenth of a percent. */
|
|
104
|
+
const SCALE_EPSILON = 1e-3;
|
|
105
|
+
/** Absolute tolerance for "is this the same offset", in CSS pixels. */
|
|
106
|
+
const OFFSET_EPSILON = 0.5;
|
|
107
|
+
|
|
108
|
+
/** The scale at which the whole image is just visible inside `box`. */
|
|
109
|
+
export function fitScale(box: Box, image: Box): number {
|
|
110
|
+
if (!(box.width > 0) || !(box.height > 0) || !(image.width > 0) || !(image.height > 0)) return 1;
|
|
111
|
+
return Math.min(box.width / image.width, box.height / image.height);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Fit, centred — the view a viewer opens at and the one `0` returns to. */
|
|
115
|
+
export function fitView(box: Box, image: Box): StageView {
|
|
116
|
+
const scale = fitScale(box, image);
|
|
117
|
+
return centred(scale, box, image);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The whole image visible at 1:1 if it fits, otherwise fit — a sane opening view. */
|
|
121
|
+
export function initialView(box: Box, image: Box): StageView {
|
|
122
|
+
return centred(Math.min(1, fitScale(box, image)), box, image);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function centred(scale: number, box: Box, image: Box): StageView {
|
|
126
|
+
return {
|
|
127
|
+
scale,
|
|
128
|
+
tx: (box.width - image.width * scale) / 2,
|
|
129
|
+
ty: (box.height - image.height * scale) / 2,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Whether `view` is (indistinguishably) the fit view for this box and image. */
|
|
134
|
+
export function isFit(view: StageView, box: Box, image: Box): boolean {
|
|
135
|
+
const fit = fitView(box, image);
|
|
136
|
+
return (
|
|
137
|
+
Math.abs(view.scale - fit.scale) <= fit.scale * SCALE_EPSILON &&
|
|
138
|
+
Math.abs(view.tx - fit.tx) <= OFFSET_EPSILON &&
|
|
139
|
+
Math.abs(view.ty - fit.ty) <= OFFSET_EPSILON
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Where the **centre of** image pixel `p` lands in the viewport. */
|
|
144
|
+
export function toScreen(view: StageView, p: Point): Point {
|
|
145
|
+
return {
|
|
146
|
+
x: (p.x + PIXEL_CENTRE) * view.scale + view.tx,
|
|
147
|
+
y: (p.y + PIXEL_CENTRE) * view.scale + view.ty,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Which image coordinate a viewport position is over, in the same pixel-centre convention:
|
|
153
|
+
* the exact centre of the top-left pixel reads `0, 0`, and its top-left corner `-0.5, -0.5`.
|
|
154
|
+
*
|
|
155
|
+
* Unbounded — callers clamp if they care.
|
|
156
|
+
*/
|
|
157
|
+
export function toImage(view: StageView, p: Point): Point {
|
|
158
|
+
return {
|
|
159
|
+
x: (p.x - view.tx) / view.scale - PIXEL_CENTRE,
|
|
160
|
+
y: (p.y - view.ty) / view.scale - PIXEL_CENTRE,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Whether an image coordinate is inside the image, in the pixel-centre convention. */
|
|
165
|
+
export function insideImage(p: Point, image: Box): boolean {
|
|
166
|
+
return (
|
|
167
|
+
p.x >= -PIXEL_CENTRE &&
|
|
168
|
+
p.y >= -PIXEL_CENTRE &&
|
|
169
|
+
p.x < image.width - PIXEL_CENTRE &&
|
|
170
|
+
p.y < image.height - PIXEL_CENTRE
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** A length in image pixels that covers `css` screen pixels at this view. */
|
|
175
|
+
export function imageLengthFor(view: StageView, css: number): number {
|
|
176
|
+
return view.scale > 0 ? css / view.scale : css;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Change the scale while holding whatever is under `anchor` where it is.
|
|
181
|
+
*
|
|
182
|
+
* The one piece of arithmetic here that is silently wrong when centre-anchored instead:
|
|
183
|
+
* magnifying a defect in a corner then costs a drag to undo every wheel notch.
|
|
184
|
+
*/
|
|
185
|
+
export function zoomAbout(view: StageView, scale: number, anchor: Point): StageView {
|
|
186
|
+
if (!(view.scale > 0) || !(scale > 0)) return view;
|
|
187
|
+
const ratio = scale / view.scale;
|
|
188
|
+
return {
|
|
189
|
+
scale,
|
|
190
|
+
tx: anchor.x - (anchor.x - view.tx) * ratio,
|
|
191
|
+
ty: anchor.y - (anchor.y - view.ty) * ratio,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The scale range `clampView` and `scaleRange` allow. */
|
|
196
|
+
export interface ClampOptions {
|
|
197
|
+
/** Lowest scale, as a multiple of fit. Defaults to `MIN_SCALE_VS_FIT`. */
|
|
198
|
+
minScaleVsFit?: number;
|
|
199
|
+
/** Highest scale, in CSS pixels per image pixel. Defaults to `MAX_SCALE`. */
|
|
200
|
+
maxScale?: number;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The scale range a viewport allows for an image, as `[min, max]`. */
|
|
204
|
+
export function scaleRange(box: Box, image: Box, options: ClampOptions = {}): [number, number] {
|
|
205
|
+
const fit = fitScale(box, image);
|
|
206
|
+
const min = fit * (options.minScaleVsFit ?? MIN_SCALE_VS_FIT);
|
|
207
|
+
const max = Math.max(min, options.maxScale ?? MAX_SCALE);
|
|
208
|
+
return [min, max];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Keep the view legal: scale inside its range, and the image never dragged off screen.
|
|
213
|
+
*
|
|
214
|
+
* An axis where the scaled image is smaller than the viewport is centred rather than left
|
|
215
|
+
* where a drag put it — free-floating pixels in a grey field is a way to be lost with no
|
|
216
|
+
* visible handle to get back. An axis where it is larger is held so the viewport stays
|
|
217
|
+
* covered.
|
|
218
|
+
*/
|
|
219
|
+
export function clampView(
|
|
220
|
+
view: StageView,
|
|
221
|
+
box: Box,
|
|
222
|
+
image: Box,
|
|
223
|
+
options: ClampOptions = {},
|
|
224
|
+
): StageView {
|
|
225
|
+
const [min, max] = scaleRange(box, image, options);
|
|
226
|
+
const scale = clamp(view.scale, min, max);
|
|
227
|
+
return {
|
|
228
|
+
scale,
|
|
229
|
+
tx: clampAxis(view.tx, box.width, image.width * scale),
|
|
230
|
+
ty: clampAxis(view.ty, box.height, image.height * scale),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function clampAxis(t: number, boxLength: number, contentLength: number): number {
|
|
235
|
+
if (contentLength <= boxLength) return (boxLength - contentLength) / 2;
|
|
236
|
+
return clamp(t, boxLength - contentLength, 0);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The view that puts `rect` (image coordinates) on screen with a margin around it.
|
|
241
|
+
*
|
|
242
|
+
* `pad` is a fraction of the rect's own size, so framing a long thin contour and framing a
|
|
243
|
+
* blob both leave a proportionate border rather than a fixed one that swallows the small
|
|
244
|
+
* one.
|
|
245
|
+
*/
|
|
246
|
+
export function frameRect(box: Box, image: Box, rect: Rect, pad = 0.15): StageView {
|
|
247
|
+
const width = Math.max(rect.width, 1e-6) * (1 + 2 * pad);
|
|
248
|
+
const height = Math.max(rect.height, 1e-6) * (1 + 2 * pad);
|
|
249
|
+
const scale = Math.min(box.width / width, box.height / height);
|
|
250
|
+
const centre = { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
251
|
+
return clampView(
|
|
252
|
+
{
|
|
253
|
+
scale,
|
|
254
|
+
tx: box.width / 2 - (centre.x + PIXEL_CENTRE) * scale,
|
|
255
|
+
ty: box.height / 2 - (centre.y + PIXEL_CENTRE) * scale,
|
|
256
|
+
},
|
|
257
|
+
box,
|
|
258
|
+
image,
|
|
259
|
+
{ maxScale: MAX_SCALE },
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The same view, after the viewport changed size.
|
|
265
|
+
*
|
|
266
|
+
* The image point at the old centre stays at the new centre, at the same scale — so a
|
|
267
|
+
* window resize moves the frame around the picture instead of moving the picture. A view
|
|
268
|
+
* that was *fit* re-fits instead, because fit is a relationship to the viewport rather than
|
|
269
|
+
* a particular pair of numbers.
|
|
270
|
+
*/
|
|
271
|
+
export function preserveCenter(
|
|
272
|
+
view: StageView,
|
|
273
|
+
from: Box,
|
|
274
|
+
to: Box,
|
|
275
|
+
image: Box,
|
|
276
|
+
options: ClampOptions = {},
|
|
277
|
+
): StageView {
|
|
278
|
+
if (!(from.width > 0) || !(from.height > 0)) return clampView(fitView(to, image), to, image, options);
|
|
279
|
+
if (isFit(view, from, image)) return fitView(to, image);
|
|
280
|
+
|
|
281
|
+
const centre = toImage(view, { x: from.width / 2, y: from.height / 2 });
|
|
282
|
+
return clampView(
|
|
283
|
+
{
|
|
284
|
+
scale: view.scale,
|
|
285
|
+
tx: to.width / 2 - (centre.x + PIXEL_CENTRE) * view.scale,
|
|
286
|
+
ty: to.height / 2 - (centre.y + PIXEL_CENTRE) * view.scale,
|
|
287
|
+
},
|
|
288
|
+
to,
|
|
289
|
+
image,
|
|
290
|
+
options,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* The next scale up or down the ladder from `scale`, so the readout lands on a round
|
|
296
|
+
* percentage rather than wherever a multiplier happened to leave it.
|
|
297
|
+
*/
|
|
298
|
+
export function steppedScale(scale: number, direction: 1 | -1, min: number, max: number): number {
|
|
299
|
+
const ladder = SCALE_LADDER.filter((s) => s >= min - 1e-9 && s <= max + 1e-9);
|
|
300
|
+
const candidates = ladder.length > 0 ? ladder : [clamp(scale, min, max)];
|
|
301
|
+
if (direction > 0) {
|
|
302
|
+
const next = candidates.find((s) => s > scale * (1 + SCALE_EPSILON));
|
|
303
|
+
return next ?? Math.min(max, Math.max(scale, candidates[candidates.length - 1]!));
|
|
304
|
+
}
|
|
305
|
+
const previous = [...candidates].reverse().find((s) => s < scale * (1 - SCALE_EPSILON));
|
|
306
|
+
return previous ?? Math.max(min, Math.min(scale, candidates[0]!));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** `0.5` → `"50%"`, `1` → `"100%"`, `0.0833` → `"8%"`. */
|
|
310
|
+
export function formatScale(scale: number): string {
|
|
311
|
+
const percent = scale * 100;
|
|
312
|
+
if (percent >= 100) return `${Math.round(percent)}%`;
|
|
313
|
+
if (percent >= 10) return `${Math.round(percent)}%`;
|
|
314
|
+
return `${percent.toFixed(1)}%`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function clamp(value: number, low: number, high: number): number {
|
|
318
|
+
return Math.min(high, Math.max(low, value));
|
|
319
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@vitavision/stage2d` — the image viewer: one view transform over a stack of layers, the measurement
|
|
3
|
+
* overlay drawn into it, and the value planes behind a rendered image.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
ImageStage,
|
|
8
|
+
useStage,
|
|
9
|
+
type ImageStageProps,
|
|
10
|
+
type StageContext,
|
|
11
|
+
} from "./components/stage/ImageStage";
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
StageButton,
|
|
15
|
+
StageReadout,
|
|
16
|
+
StageToolbar,
|
|
17
|
+
StageToolbarDivider,
|
|
18
|
+
type StageButtonProps,
|
|
19
|
+
type StageReadoutProps,
|
|
20
|
+
type StageToolbarDividerProps,
|
|
21
|
+
type StageToolbarProps,
|
|
22
|
+
} from "./components/stage/StageToolbar";
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
MAX_SCALE,
|
|
26
|
+
MIN_SCALE_VS_FIT,
|
|
27
|
+
PIXEL_CENTRE,
|
|
28
|
+
clampView,
|
|
29
|
+
fitScale,
|
|
30
|
+
fitView,
|
|
31
|
+
formatScale,
|
|
32
|
+
frameRect,
|
|
33
|
+
imageLengthFor,
|
|
34
|
+
imageViewBox,
|
|
35
|
+
initialView,
|
|
36
|
+
insideImage,
|
|
37
|
+
isFit,
|
|
38
|
+
preserveCenter,
|
|
39
|
+
scaleRange,
|
|
40
|
+
steppedScale,
|
|
41
|
+
toImage,
|
|
42
|
+
toScreen,
|
|
43
|
+
zoomAbout,
|
|
44
|
+
type Box,
|
|
45
|
+
type ClampOptions,
|
|
46
|
+
type Rect,
|
|
47
|
+
type StageView,
|
|
48
|
+
} from "./components/stage/view";
|
|
49
|
+
|
|
50
|
+
export {
|
|
51
|
+
FULL_TIER_ZOOM,
|
|
52
|
+
MAX_ZOOM,
|
|
53
|
+
MIN_ZOOM,
|
|
54
|
+
RESET_VIEW,
|
|
55
|
+
ZoomPanCanvas,
|
|
56
|
+
contentUnder,
|
|
57
|
+
nativeZoomFor,
|
|
58
|
+
zoomAt,
|
|
59
|
+
type View,
|
|
60
|
+
type ZoomPanCanvasProps,
|
|
61
|
+
} from "./components/ZoomPanCanvas";
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
PlaneFormatError,
|
|
65
|
+
decodePlane,
|
|
66
|
+
fetchPlane,
|
|
67
|
+
fractionOf,
|
|
68
|
+
valueAt,
|
|
69
|
+
valuesAt,
|
|
70
|
+
type ValuePlane,
|
|
71
|
+
} from "./api/mapValues";
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
MeasureOverlay,
|
|
75
|
+
type ArcPrimitive,
|
|
76
|
+
type CaliperPrimitive,
|
|
77
|
+
type CirclePrimitive,
|
|
78
|
+
type DimensionPrimitive,
|
|
79
|
+
type MeasureOverlayProps,
|
|
80
|
+
type MeasurePrimitive,
|
|
81
|
+
type PointPrimitive,
|
|
82
|
+
type SegmentPrimitive,
|
|
83
|
+
} from "./components/MeasureOverlay";
|
|
84
|
+
|
|
85
|
+
export {
|
|
86
|
+
arcPath,
|
|
87
|
+
arrowHeadPoints,
|
|
88
|
+
caliperArrow,
|
|
89
|
+
caliperCorners,
|
|
90
|
+
crossSegments,
|
|
91
|
+
dimensionGeometry,
|
|
92
|
+
polygonPath,
|
|
93
|
+
rotatePoint,
|
|
94
|
+
strokeWidthFor,
|
|
95
|
+
type DimensionGeometry,
|
|
96
|
+
type Point,
|
|
97
|
+
} from "./components/measureGeometry";
|
package/src/styles.css
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Nothing to style: this file exists so Tailwind v4 finds this package's class names.
|
|
3
|
+
* `@source` resolves against the stylesheet that declares it, and this file is exported from
|
|
4
|
+
* `src/` (which ships), so it scans the package's sources. Import it after
|
|
5
|
+
* `@vitavision/ui/styles.css`.
|
|
6
|
+
*/
|
|
7
|
+
@source "./";
|