@vitavision/stage2d 0.12.0 → 0.13.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 +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/components/AreaSet.tsx +4 -4
- package/src/components/ContourEditor.tsx +286 -37
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +1 -2
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MeasureOverlay.tsx +320 -89
- package/src/components/PointSet.tsx +4 -4
- package/src/components/PolylineSet.tsx +81 -17
- package/src/components/RectRoiEditor.tsx +188 -84
- package/src/components/ShapeEditor.tsx +14 -4
- package/src/components/areaBatching.ts +1 -1
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +2 -3
- package/src/components/markerShapes.ts +1 -2
- package/src/components/measureGeometry.ts +34 -1
- package/src/components/measureHit.ts +371 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +2 -3
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +228 -0
- package/src/components/stage/ImageLayer.tsx +56 -15
- package/src/components/stage/ImageStage.tsx +29 -8
- package/src/components/stage/StageSurface.tsx +1 -4
- package/src/components/stage/StageToolbar.tsx +26 -8
- 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 +11 -0
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +32 -2
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two registered images of one size, compared in place: a checkerboard of the two, a wipe
|
|
3
|
+
* between them, or their difference.
|
|
4
|
+
*
|
|
5
|
+
* A stage layer like `ImageLayer`: both images are laid out at the stage's own pixel size, so
|
|
6
|
+
* they stay registered with each other and with every overlay at any zoom. All three modes are
|
|
7
|
+
* CSS on the second image (a mask, a clip, a blend), never a canvas, so nothing is read back or
|
|
8
|
+
* redrawn as the view moves.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { useState, type KeyboardEvent, type PointerEvent } from "react";
|
|
12
|
+
|
|
13
|
+
import { cn } from "@vitavision/ui";
|
|
14
|
+
import { visibleRect } from "../labelLod";
|
|
15
|
+
import { overlayRole } from "../overlayRole";
|
|
16
|
+
import { clampSplit, compareStyles, knobPosition, splitForKey, splitText, type CompareMode, type CompareOrientation } from "./compareStyle";
|
|
17
|
+
import { useStage } from "./ImageStage";
|
|
18
|
+
import { useStageDrag } from "./StageSurface";
|
|
19
|
+
import { useScreenPx } from "./useScreenPx";
|
|
20
|
+
import { PIXEL_CENTRE } from "./view";
|
|
21
|
+
|
|
22
|
+
/** Props of `CompareLayer`. */
|
|
23
|
+
export interface CompareLayerProps {
|
|
24
|
+
/** Image A's URL: the reference. It is the stage's size. */
|
|
25
|
+
a: string;
|
|
26
|
+
/** Image B's URL: compared with A. The same size as A. */
|
|
27
|
+
b: string;
|
|
28
|
+
/**
|
|
29
|
+
* How they are compared.
|
|
30
|
+
* - `checker`: alternate squares of A and B, `cell` image pixels across, A at the top-left.
|
|
31
|
+
* - `wipe`: A before the divider and B after it; drag the divider, or focus it and use the keys.
|
|
32
|
+
* - `difference`: the per-channel difference of the two, brightened by `gain`. Identical pixels
|
|
33
|
+
* are black. It is computed on the sRGB-encoded values the browser composites, not on linear
|
|
34
|
+
* light, so treat it as a picture of where the images differ, not as a measurement.
|
|
35
|
+
*/
|
|
36
|
+
mode: CompareMode;
|
|
37
|
+
/** The wipe's divider, as the fraction of the image before it (0 to 1), when the app controls it. */
|
|
38
|
+
split?: number | undefined;
|
|
39
|
+
/** The divider's starting place when the layer controls it. Defaults to 0.5. */
|
|
40
|
+
defaultSplit?: number | undefined;
|
|
41
|
+
/** Called as the divider is dragged or moved by a key, with the new fraction. */
|
|
42
|
+
onSplitChange?: ((split: number) => void) | undefined;
|
|
43
|
+
/** `vertical` (the default) puts A left of the divider; `horizontal` puts A above it. */
|
|
44
|
+
orientation?: CompareOrientation | undefined;
|
|
45
|
+
/** The checkerboard's square, in image pixels. Defaults to 32. */
|
|
46
|
+
cell?: number | undefined;
|
|
47
|
+
/** How much the difference is brightened: 1 shows it as it is, 4 makes a small one visible. Defaults to 1. */
|
|
48
|
+
gain?: number | undefined;
|
|
49
|
+
/** What the comparison shows, for assistive technology. */
|
|
50
|
+
alt: string;
|
|
51
|
+
/** The two images' names, for the divider's value and its tags. Defaults to `["A", "B"]`. */
|
|
52
|
+
labels?: readonly [string, string] | undefined;
|
|
53
|
+
/** The zoom (CSS pixels per image pixel) from which pixels are drawn as blocks. Defaults to 4. */
|
|
54
|
+
pixelatedAbove?: number | undefined;
|
|
55
|
+
/** Merged onto the layer's wrapper with `cn`. */
|
|
56
|
+
className?: string | undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The divider's line, its knob's radius and the band that grabs it, in screen pixels. */
|
|
60
|
+
const LINE_PX = 1.5;
|
|
61
|
+
const KNOB_PX = 8;
|
|
62
|
+
const GRAB_PX = 12;
|
|
63
|
+
/** The tags' size and their distance from the knob's centre, in screen pixels. */
|
|
64
|
+
const TAG_PX = 11;
|
|
65
|
+
const TAG_GAP_PX = 14;
|
|
66
|
+
|
|
67
|
+
const HALO = overlayRole("halo");
|
|
68
|
+
const DIVIDER = overlayRole("label");
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Two same-size images compared inside an `ImageStage`: as a checkerboard, a wipe or their
|
|
72
|
+
* difference (`mode`).
|
|
73
|
+
*
|
|
74
|
+
* - **Wipe.** The divider is a line one and a half screen pixels wide with a knob kept in the
|
|
75
|
+
* middle of the part of it on screen. Drag the line or the knob; the press is claimed, so the
|
|
76
|
+
* stage does not pan from it.
|
|
77
|
+
* - **Keyboard.** The knob is a slider (0 to 100, the share of A): arrow keys move it 1 % (10 %
|
|
78
|
+
* with Shift), Page Up and Page Down 10 %, Home and End to either edge. The keys do not reach
|
|
79
|
+
* the stage, so they do not pan it.
|
|
80
|
+
* - **Pixels.** Past `pixelatedAbove` both images are drawn as blocks, as `ImageLayer` does.
|
|
81
|
+
*
|
|
82
|
+
* The images are one picture to assistive technology (`role="img"`, named by `alt`); the knob is
|
|
83
|
+
* outside it, so it stays reachable. The wrapper carries `data-mode`, and `data-pixelated` while
|
|
84
|
+
* pixelated; the knob carries `data-dragging` during a drag.
|
|
85
|
+
*/
|
|
86
|
+
export function CompareLayer({
|
|
87
|
+
a,
|
|
88
|
+
b,
|
|
89
|
+
mode,
|
|
90
|
+
split,
|
|
91
|
+
defaultSplit = 0.5,
|
|
92
|
+
onSplitChange,
|
|
93
|
+
orientation = "vertical",
|
|
94
|
+
cell = 32,
|
|
95
|
+
gain = 1,
|
|
96
|
+
alt,
|
|
97
|
+
labels = ["A", "B"],
|
|
98
|
+
pixelatedAbove = 4,
|
|
99
|
+
className,
|
|
100
|
+
}: CompareLayerProps) {
|
|
101
|
+
const stage = useStage();
|
|
102
|
+
const px = useScreenPx();
|
|
103
|
+
const startDrag = useStageDrag();
|
|
104
|
+
const [ownSplit, setOwnSplit] = useState(() => clampSplit(defaultSplit));
|
|
105
|
+
const [dragging, setDragging] = useState(false);
|
|
106
|
+
const current = clampSplit(split ?? ownSplit);
|
|
107
|
+
|
|
108
|
+
const change = (next: number) => {
|
|
109
|
+
const clamped = clampSplit(next);
|
|
110
|
+
setOwnSplit(clamped);
|
|
111
|
+
onSplitChange?.(clamped);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const styles = compareStyles(mode, { split: current, orientation, cell, gain });
|
|
115
|
+
const pixelated = stage.view.scale >= pixelatedAbove;
|
|
116
|
+
const image = { width: stage.image.width, height: stage.image.height };
|
|
117
|
+
const imgClass = "pointer-events-none absolute inset-0 h-full w-full select-none";
|
|
118
|
+
const rendering = pixelated ? ("pixelated" as const) : undefined;
|
|
119
|
+
|
|
120
|
+
// The divider is drawn in the layer's own CSS pixels (0 at the image's left edge, not at its
|
|
121
|
+
// first pixel's centre), the frame `clip-path` and `visibleRect` work in.
|
|
122
|
+
const vertical = orientation === "vertical";
|
|
123
|
+
const length = vertical ? image.height : image.width;
|
|
124
|
+
const extent = vertical ? image.width : image.height;
|
|
125
|
+
const at = current * extent;
|
|
126
|
+
const visible = visibleRect(stage.view, stage.box);
|
|
127
|
+
const along = knobPosition(visible === null ? null : vertical ? [visible.y, visible.y + visible.height] : [visible.x, visible.x + visible.width], length);
|
|
128
|
+
const knob = vertical ? { x: at, y: along } : { x: along, y: at };
|
|
129
|
+
const line = vertical ? { x1: at, y1: 0, x2: at, y2: image.height } : { x1: 0, y1: at, x2: image.width, y2: at };
|
|
130
|
+
|
|
131
|
+
const grab = (event: PointerEvent<SVGElement>) => {
|
|
132
|
+
if (stage.panMode) return;
|
|
133
|
+
if (event.pointerType !== "touch" && event.button !== 0) return;
|
|
134
|
+
setDragging(true);
|
|
135
|
+
const follow = (p: { x: number; y: number }) => change(((vertical ? p.x : p.y) + PIXEL_CENTRE) / extent);
|
|
136
|
+
startDrag(event, {
|
|
137
|
+
claimsTouch: true,
|
|
138
|
+
onMove: follow,
|
|
139
|
+
onEnd: () => setDragging(false),
|
|
140
|
+
onCancel: () => setDragging(false),
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const onKeyDown = (event: KeyboardEvent<SVGElement>) => {
|
|
145
|
+
const next = splitForKey(current, event.key, event.shiftKey, orientation);
|
|
146
|
+
if (next === null) return;
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
change(next);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<div className={cn("pointer-events-none absolute inset-0", className)} data-mode={mode} data-pixelated={pixelated ? "" : undefined}>
|
|
154
|
+
<div role="img" aria-label={alt} className="absolute inset-0" style={styles.wrapper}>
|
|
155
|
+
<img src={a} alt="" draggable={false} data-image="a" className={imgClass} style={{ imageRendering: rendering }} />
|
|
156
|
+
<img src={b} alt="" draggable={false} data-image="b" className={imgClass} style={{ ...styles.b, imageRendering: rendering }} />
|
|
157
|
+
</div>
|
|
158
|
+
{mode === "wipe" && (
|
|
159
|
+
<svg viewBox={`0 0 ${image.width} ${image.height}`} className="absolute inset-0 h-full w-full overflow-visible" data-divider="">
|
|
160
|
+
<line {...line} stroke={HALO} strokeWidth={px(LINE_PX + 2)} />
|
|
161
|
+
<line {...line} stroke={DIVIDER} strokeWidth={px(LINE_PX)} />
|
|
162
|
+
<line
|
|
163
|
+
{...line}
|
|
164
|
+
data-divider-band=""
|
|
165
|
+
stroke="transparent"
|
|
166
|
+
strokeWidth={px(GRAB_PX)}
|
|
167
|
+
className="pointer-events-auto"
|
|
168
|
+
style={{ pointerEvents: "stroke", cursor: vertical ? "ew-resize" : "ns-resize" }}
|
|
169
|
+
aria-hidden
|
|
170
|
+
onPointerDown={grab}
|
|
171
|
+
/>
|
|
172
|
+
<circle
|
|
173
|
+
cx={knob.x}
|
|
174
|
+
cy={knob.y}
|
|
175
|
+
r={px(KNOB_PX)}
|
|
176
|
+
fill={DIVIDER}
|
|
177
|
+
stroke={HALO}
|
|
178
|
+
strokeWidth={px(1.5)}
|
|
179
|
+
role="slider"
|
|
180
|
+
tabIndex={0}
|
|
181
|
+
aria-label={`Split between ${labels[0]} and ${labels[1]}`}
|
|
182
|
+
aria-orientation={vertical ? "horizontal" : "vertical"}
|
|
183
|
+
aria-valuemin={0}
|
|
184
|
+
aria-valuemax={100}
|
|
185
|
+
aria-valuenow={Math.round(current * 100)}
|
|
186
|
+
aria-valuetext={splitText(current, labels[0])}
|
|
187
|
+
data-dragging={dragging ? "" : undefined}
|
|
188
|
+
className="pointer-events-auto outline-none focus-visible:stroke-signal"
|
|
189
|
+
style={{ cursor: vertical ? "ew-resize" : "ns-resize" }}
|
|
190
|
+
onPointerDown={grab}
|
|
191
|
+
onKeyDown={onKeyDown}
|
|
192
|
+
/>
|
|
193
|
+
{/* Which image is on which side, beside the knob. */}
|
|
194
|
+
<g
|
|
195
|
+
aria-hidden
|
|
196
|
+
fontSize={px(TAG_PX)}
|
|
197
|
+
className="font-mono select-none"
|
|
198
|
+
fill={DIVIDER}
|
|
199
|
+
stroke={HALO}
|
|
200
|
+
strokeWidth={px(3)}
|
|
201
|
+
strokeLinejoin="round"
|
|
202
|
+
paintOrder="stroke"
|
|
203
|
+
>
|
|
204
|
+
{vertical ? (
|
|
205
|
+
<>
|
|
206
|
+
<text data-tag="a" x={knob.x - px(TAG_GAP_PX)} y={knob.y} textAnchor="end" dominantBaseline="central">
|
|
207
|
+
{labels[0]}
|
|
208
|
+
</text>
|
|
209
|
+
<text data-tag="b" x={knob.x + px(TAG_GAP_PX)} y={knob.y} textAnchor="start" dominantBaseline="central">
|
|
210
|
+
{labels[1]}
|
|
211
|
+
</text>
|
|
212
|
+
</>
|
|
213
|
+
) : (
|
|
214
|
+
<>
|
|
215
|
+
<text data-tag="a" x={knob.x} y={knob.y - px(TAG_GAP_PX)} textAnchor="middle" dominantBaseline="text-after-edge">
|
|
216
|
+
{labels[0]}
|
|
217
|
+
</text>
|
|
218
|
+
<text data-tag="b" x={knob.x} y={knob.y + px(TAG_GAP_PX)} textAnchor="middle" dominantBaseline="text-before-edge">
|
|
219
|
+
{labels[1]}
|
|
220
|
+
</text>
|
|
221
|
+
</>
|
|
222
|
+
)}
|
|
223
|
+
</g>
|
|
224
|
+
</svg>
|
|
225
|
+
)}
|
|
226
|
+
</div>
|
|
227
|
+
);
|
|
228
|
+
}
|
|
@@ -10,13 +10,14 @@
|
|
|
10
10
|
* until it would be magnified — until the screen shows more pixels than it has. Past that
|
|
11
11
|
* point the full image is requested, and it stays once loaded, so zooming back out does
|
|
12
12
|
* not swap back. The preview stays underneath while the full image loads, so the frame
|
|
13
|
-
* never goes blank.
|
|
13
|
+
* never goes blank. When the full image is costly to produce, the app can leave its URL
|
|
14
|
+
* out and supply it when `onFullNeeded` says the same rule has tripped.
|
|
14
15
|
* - **Pixelated past a zoom.** Above about 4× the browser's smoothing paints detail the
|
|
15
16
|
* sensor never recorded; on a metrology bench the sensor's own samples are what is worth
|
|
16
17
|
* looking at.
|
|
17
18
|
*/
|
|
18
19
|
|
|
19
|
-
import { useState } from "react";
|
|
20
|
+
import { useEffect, useRef, useState } from "react";
|
|
20
21
|
|
|
21
22
|
import { cn } from "@vitavision/ui";
|
|
22
23
|
import { useStage } from "./ImageStage";
|
|
@@ -29,16 +30,19 @@ export interface ImageTier {
|
|
|
29
30
|
width: number;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
/**
|
|
33
|
-
export interface
|
|
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;
|
|
33
|
+
/** What every `ImageLayer` takes, whichever tiers it is given. */
|
|
34
|
+
export interface ImageLayerBaseProps {
|
|
38
35
|
/** The image's description, for assistive technology. `""` marks it decorative. */
|
|
39
36
|
alt: string;
|
|
40
37
|
/** The zoom (CSS pixels per image pixel) from which pixels are drawn as blocks. Defaults to 4. */
|
|
41
38
|
pixelatedAbove?: number | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Called once per `preview.src` when the full image is first wanted: the stage would magnify
|
|
41
|
+
* the preview. An app whose full image is costly to make or fetch leaves `src` out and
|
|
42
|
+
* passes it from here; the full image is shown as soon as `src` arrives. Not called without
|
|
43
|
+
* a `preview`.
|
|
44
|
+
*/
|
|
45
|
+
onFullNeeded?: (() => void) | undefined;
|
|
42
46
|
/** Called when the full image has loaded. */
|
|
43
47
|
onLoad?: (() => void) | undefined;
|
|
44
48
|
/** Called when the full image fails to load. */
|
|
@@ -47,19 +51,44 @@ export interface ImageLayerProps {
|
|
|
47
51
|
className?: string | undefined;
|
|
48
52
|
}
|
|
49
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Props of `ImageLayer`: the full image's `src`, a `preview` tier, or both. With a `preview`,
|
|
56
|
+
* `src` may be left out until `onFullNeeded` asks for it.
|
|
57
|
+
*/
|
|
58
|
+
export type ImageLayerProps = ImageLayerBaseProps &
|
|
59
|
+
(
|
|
60
|
+
| {
|
|
61
|
+
/** The full-resolution image's URL. */
|
|
62
|
+
src: string;
|
|
63
|
+
/** A smaller tier to show first; without one, `src` is shown from the start. */
|
|
64
|
+
preview?: ImageTier | undefined;
|
|
65
|
+
}
|
|
66
|
+
| {
|
|
67
|
+
/** The full-resolution image's URL, once known. Until then the preview stands in. */
|
|
68
|
+
src?: string | undefined;
|
|
69
|
+
/** A smaller tier to show first. */
|
|
70
|
+
preview: ImageTier;
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
|
|
50
74
|
/**
|
|
51
75
|
* The photograph at its natural size inside an `ImageStage`, with an optional preview tier
|
|
52
76
|
* and pixelated rendering past `pixelatedAbove`. It takes no pointer events, so presses reach
|
|
53
77
|
* the stage and the layers above.
|
|
54
78
|
*
|
|
79
|
+
* With a `preview`, the full image is wanted once the stage would magnify the preview, and
|
|
80
|
+
* `onFullNeeded` says so; `src` can be supplied then rather than up front.
|
|
81
|
+
*
|
|
55
82
|
* Each image carries `data-tier` (`preview` or `full`); the full image carries `data-loaded`
|
|
56
|
-
* once it has loaded,
|
|
83
|
+
* once it has loaded, the preview carries `data-wants-full` from the moment the full image is
|
|
84
|
+
* wanted until it has loaded, and both carry `data-pixelated` while pixelated.
|
|
57
85
|
*/
|
|
58
86
|
export function ImageLayer({
|
|
59
87
|
src,
|
|
60
88
|
preview,
|
|
61
89
|
alt,
|
|
62
90
|
pixelatedAbove = 4,
|
|
91
|
+
onFullNeeded,
|
|
63
92
|
onLoad,
|
|
64
93
|
onError,
|
|
65
94
|
className,
|
|
@@ -70,14 +99,25 @@ export function ImageLayer({
|
|
|
70
99
|
// per image pixel are `preview.width / image.width`. Not before the viewport is measured:
|
|
71
100
|
// the placeholder view then is 1:1, which would fetch the full image on every open.
|
|
72
101
|
const measured = stage.box.width > 0;
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
102
|
+
const previewSrc = preview?.src ?? null;
|
|
103
|
+
const magnified = preview !== undefined && measured && scale * stage.image.width > preview.width;
|
|
104
|
+
// Wanted once per preview, and kept: zooming back out should not drop the pixels already
|
|
105
|
+
// fetched.
|
|
106
|
+
const [wantedFor, setWantedFor] = useState<string | null>(magnified ? previewSrc : null);
|
|
107
|
+
if (magnified && wantedFor !== previewSrc) setWantedFor(previewSrc);
|
|
108
|
+
const wanted = previewSrc === null || wantedFor === previewSrc;
|
|
77
109
|
const [loaded, setLoaded] = useState<string | null>(null);
|
|
78
110
|
|
|
79
|
-
|
|
80
|
-
const
|
|
111
|
+
// Told once per preview: the guard holds through a new `onFullNeeded` and a re-run effect.
|
|
112
|
+
const notifiedRef = useRef<string | null>(null);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (previewSrc === null || wantedFor !== previewSrc || notifiedRef.current === previewSrc) return;
|
|
115
|
+
notifiedRef.current = previewSrc;
|
|
116
|
+
onFullNeeded?.();
|
|
117
|
+
}, [previewSrc, wantedFor, onFullNeeded]);
|
|
118
|
+
|
|
119
|
+
const showFull = wanted && src !== undefined;
|
|
120
|
+
const fullReady = src !== undefined && loaded === src;
|
|
81
121
|
const pixelated = scale >= pixelatedAbove;
|
|
82
122
|
const shared = cn("pointer-events-none absolute inset-0 h-full w-full select-none", className);
|
|
83
123
|
const rendering = pixelated ? ("pixelated" as const) : undefined;
|
|
@@ -91,6 +131,7 @@ export function ImageLayer({
|
|
|
91
131
|
aria-hidden={showFull ? true : undefined}
|
|
92
132
|
draggable={false}
|
|
93
133
|
data-tier="preview"
|
|
134
|
+
data-wants-full={wanted ? "" : undefined}
|
|
94
135
|
data-pixelated={pixelated ? "" : undefined}
|
|
95
136
|
className={shared}
|
|
96
137
|
style={{ imageRendering: rendering }}
|
|
@@ -46,7 +46,7 @@ import type {
|
|
|
46
46
|
|
|
47
47
|
import { cn } from "@vitavision/ui";
|
|
48
48
|
import type { Point } from "../measureGeometry";
|
|
49
|
-
import { centroid, hitRadiusPx, isTap, panButtonCodes, pinchView, type StageMouseButton } from "./gesture";
|
|
49
|
+
import { CLICK_SLOP, centroid, hitRadiusPx, isTap, panButtonCodes, pinchView, type StageMouseButton } from "./gesture";
|
|
50
50
|
import { StageHitContext, type StagePointerEvent } from "./hitContext";
|
|
51
51
|
import { createHitRegistry } from "./hitTest";
|
|
52
52
|
import {
|
|
@@ -81,8 +81,6 @@ const WHEEL_SENSITIVITY = 0.0015;
|
|
|
81
81
|
* has settled, both are as sharp as with no hint. Cleared, the layer re-rasterises.
|
|
82
82
|
*/
|
|
83
83
|
const MOVING_SETTLE_MS = 150;
|
|
84
|
-
/** How far a press may travel and still count as a click rather than a pan. */
|
|
85
|
-
const CLICK_SLOP = 3;
|
|
86
84
|
/** The view before the viewport has been measured — nothing is on screen yet. */
|
|
87
85
|
const UNMEASURED_VIEW: StageView = { scale: 1, tx: 0, ty: 0 };
|
|
88
86
|
|
|
@@ -137,7 +135,10 @@ export interface StageContext {
|
|
|
137
135
|
toViewport: (image: Point) => Point;
|
|
138
136
|
/** The image-pixel length that covers `css` screen pixels — for screen-constant geometry. */
|
|
139
137
|
imageLength: (css: number) => number;
|
|
140
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Put a rect (image coordinates) on screen with a margin: `pad` is a fraction of the rect's
|
|
140
|
+
* own width and height added on each side (default 0.15).
|
|
141
|
+
*/
|
|
141
142
|
frame: (rect: Rect, pad?: number) => void;
|
|
142
143
|
/** Fit the whole image. */
|
|
143
144
|
fit: () => void;
|
|
@@ -162,7 +163,10 @@ export interface StageContext {
|
|
|
162
163
|
* measured. Layers inside the stage use `useStage` instead.
|
|
163
164
|
*/
|
|
164
165
|
export interface StageHandle {
|
|
165
|
-
/**
|
|
166
|
+
/**
|
|
167
|
+
* Put a rect (image coordinates) on screen with a margin: `pad` is a fraction of the rect's
|
|
168
|
+
* own width and height added on each side (default 0.15).
|
|
169
|
+
*/
|
|
166
170
|
frame: (rect: Rect, pad?: number) => void;
|
|
167
171
|
/** Fit the whole image. */
|
|
168
172
|
fit: () => void;
|
|
@@ -220,7 +224,8 @@ export interface ImageStageProps {
|
|
|
220
224
|
panButton?: StageMouseButton | readonly StageMouseButton[] | undefined;
|
|
221
225
|
/**
|
|
222
226
|
* Whether a double-click toggles fit and the previous view. On by default; turn it off in
|
|
223
|
-
* an app whose tools use double-click, such as closing a polygon.
|
|
227
|
+
* an app whose tools use double-click, such as closing a polygon. A double-click a layer
|
|
228
|
+
* takes through the hit-test (an editable `ContourEditor` inserting a vertex) never toggles.
|
|
224
229
|
*/
|
|
225
230
|
doubleClickFit?: boolean | undefined;
|
|
226
231
|
/**
|
|
@@ -246,6 +251,14 @@ export interface ImageStageProps {
|
|
|
246
251
|
clamp?: ClampOptions;
|
|
247
252
|
/** Keyboard zoom/pan shortcuts. On by default; the stage takes focus to receive them. */
|
|
248
253
|
shortcuts?: boolean;
|
|
254
|
+
/**
|
|
255
|
+
* Whether the viewport is a tab stop (`tabIndex={0}`, `role="application"`). Defaults to
|
|
256
|
+
* `shortcuts`. Off, the viewport is a plain `role="group"` that takes no focus, for an app that
|
|
257
|
+
* owns the keyboard on an element around the stage and would otherwise have two nested tab
|
|
258
|
+
* stops. With `shortcuts` on and `focusable` off the keys still work while focus is on an
|
|
259
|
+
* element inside the stage, such as an editor's handle.
|
|
260
|
+
*/
|
|
261
|
+
focusable?: boolean | undefined;
|
|
249
262
|
/**
|
|
250
263
|
* Whether the arrow keys pan. On by default, and worth turning off in an app where the
|
|
251
264
|
* arrows have a better job — stepping through a list of findings, say. The zoom keys
|
|
@@ -283,6 +296,9 @@ export interface ImageStageProps {
|
|
|
283
296
|
* State is exposed on the viewport as `data-fit` (the view is fit), `data-panning` (a drag
|
|
284
297
|
* is in progress) and `data-pan-mode` (the hand tool is on or space is held), each present
|
|
285
298
|
* or absent.
|
|
299
|
+
*
|
|
300
|
+
* The viewport is a `role="application"` tab stop named by `label`. With `focusable` off (the
|
|
301
|
+
* default when `shortcuts` is off) it is a `role="group"` and takes no focus.
|
|
286
302
|
*/
|
|
287
303
|
export function ImageStage({
|
|
288
304
|
image,
|
|
@@ -302,6 +318,7 @@ export function ImageStage({
|
|
|
302
318
|
onBackgroundClick,
|
|
303
319
|
clamp,
|
|
304
320
|
shortcuts = true,
|
|
321
|
+
focusable = shortcuts,
|
|
305
322
|
panKeys = true,
|
|
306
323
|
label = "Image canvas",
|
|
307
324
|
initialView: opening = "auto",
|
|
@@ -772,8 +789,12 @@ export function ImageStage({
|
|
|
772
789
|
* Fit ↔ **the view you were just at**, which is the gesture a reader actually wants: a
|
|
773
790
|
* double-click to see the whole part, another to go back to the corner they were working
|
|
774
791
|
* in. Toggling against 1:1 instead (the predecessor's behaviour) throws that place away.
|
|
792
|
+
*
|
|
793
|
+
* A layer that registered for double-clicks hears one first (inserting a vertex on a
|
|
794
|
+
* contour, say); when it takes it, the view stays where it is.
|
|
775
795
|
*/
|
|
776
796
|
const onDoubleClick = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
797
|
+
if (!panMode && hits.routeDoubleClick(clientToImage({ x: event.clientX, y: event.clientY }), hitRadius(event))) return;
|
|
777
798
|
if (!doubleClickFit || !(box.width > 0)) return;
|
|
778
799
|
if (atFit) {
|
|
779
800
|
const restore = previousRef.current;
|
|
@@ -834,9 +855,9 @@ export function ImageStage({
|
|
|
834
855
|
<StageHitContext value={hits}>
|
|
835
856
|
<div
|
|
836
857
|
ref={attachViewport}
|
|
837
|
-
role="application"
|
|
858
|
+
role={focusable ? "application" : "group"}
|
|
838
859
|
aria-label={label}
|
|
839
|
-
tabIndex={0}
|
|
860
|
+
tabIndex={focusable ? 0 : undefined}
|
|
840
861
|
style={style}
|
|
841
862
|
data-fit={atFit ? "" : undefined}
|
|
842
863
|
data-panning={panning ? "" : undefined}
|
|
@@ -22,7 +22,7 @@ import type { PointerEvent as ReactPointerEvent } from "react";
|
|
|
22
22
|
import { cn } from "@vitavision/ui";
|
|
23
23
|
import type { Point } from "../measureGeometry";
|
|
24
24
|
import { useStage } from "./ImageStage";
|
|
25
|
-
import { hitRadiusPx } from "./gesture";
|
|
25
|
+
import { CLICK_SLOP, hitRadiusPx } from "./gesture";
|
|
26
26
|
import { watchTouch } from "./touchWatch";
|
|
27
27
|
import { clampToImage, imageViewBox, toImage } from "./view";
|
|
28
28
|
|
|
@@ -46,9 +46,6 @@ export interface StageDrag {
|
|
|
46
46
|
claimsTouch?: boolean | undefined;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** How far a press may travel and still be a click, in screen pixels. */
|
|
50
|
-
const CLICK_SLOP = 3;
|
|
51
|
-
|
|
52
49
|
/**
|
|
53
50
|
* Start a drag from a press on any element inside an `ImageStage`.
|
|
54
51
|
*
|
|
@@ -229,8 +229,16 @@ export function StageButton({ label, pressed, hint, shortcut, className, childre
|
|
|
229
229
|
export interface StageLayer {
|
|
230
230
|
/** Its identity, passed back to `onVisibleChange`. */
|
|
231
231
|
id: string;
|
|
232
|
-
/**
|
|
233
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Its name in the menu: text, or richer content such as a name and a count. It names the
|
|
234
|
+
* menu item for assistive technology, so keep its text meaningful.
|
|
235
|
+
*/
|
|
236
|
+
label: ReactNode;
|
|
237
|
+
/**
|
|
238
|
+
* A CSS colour shown as a small dot before the label, matching the layer's colour on the
|
|
239
|
+
* canvas. Decorative (`aria-hidden`), so the label must name the layer on its own.
|
|
240
|
+
*/
|
|
241
|
+
swatch?: string | undefined;
|
|
234
242
|
/** Whether it is shown. */
|
|
235
243
|
visible: boolean;
|
|
236
244
|
/** Shown but not switchable, e.g. a layer that has nothing to draw yet. */
|
|
@@ -245,17 +253,19 @@ export interface StageLayersMenuProps {
|
|
|
245
253
|
layers: readonly StageLayer[];
|
|
246
254
|
/** Called with a layer's id and its new visibility. */
|
|
247
255
|
onVisibleChange: (id: string, visible: boolean) => void;
|
|
248
|
-
/** The trigger's accessible name and
|
|
256
|
+
/** The trigger's accessible name and tooltip. Defaults to "Layers". */
|
|
249
257
|
label?: string | undefined;
|
|
258
|
+
/** The heading over the menu's items. Defaults to `label`. */
|
|
259
|
+
heading?: ReactNode;
|
|
250
260
|
}
|
|
251
261
|
|
|
252
262
|
/**
|
|
253
263
|
* 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.
|
|
264
|
+
* children. Each layer is a `menuitemcheckbox`, with its `swatch` as a dot before its label;
|
|
265
|
+
* toggling one keeps the menu open for the next. The trigger shows as pressed while any layer
|
|
266
|
+
* is hidden, so a canvas missing a layer says so before the menu is opened.
|
|
257
267
|
*/
|
|
258
|
-
export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: StageLayersMenuProps) {
|
|
268
|
+
export function StageLayersMenu({ layers, onVisibleChange, label = "Layers", heading = label }: StageLayersMenuProps) {
|
|
259
269
|
const hidden = layers.some((layer) => !layer.visible && !layer.disabled);
|
|
260
270
|
return (
|
|
261
271
|
<DropdownMenu
|
|
@@ -266,7 +276,7 @@ export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: S
|
|
|
266
276
|
</StageButton>
|
|
267
277
|
}
|
|
268
278
|
>
|
|
269
|
-
<MenuLabel>{
|
|
279
|
+
<MenuLabel>{heading}</MenuLabel>
|
|
270
280
|
{layers.map((layer) => (
|
|
271
281
|
<MenuCheckboxItem
|
|
272
282
|
key={layer.id}
|
|
@@ -275,6 +285,14 @@ export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: S
|
|
|
275
285
|
disabled={layer.disabled}
|
|
276
286
|
shortcut={layer.shortcut}
|
|
277
287
|
>
|
|
288
|
+
{layer.swatch !== undefined && (
|
|
289
|
+
<span
|
|
290
|
+
data-swatch=""
|
|
291
|
+
aria-hidden
|
|
292
|
+
className="mr-2 inline-block size-2 rounded-full align-middle ring-1 ring-line"
|
|
293
|
+
style={{ backgroundColor: layer.swatch }}
|
|
294
|
+
/>
|
|
295
|
+
)}
|
|
278
296
|
{layer.label}
|
|
279
297
|
</MenuCheckboxItem>
|
|
280
298
|
))}
|