@vitavision/stage2d 0.11.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.
Files changed (41) hide show
  1. package/README.md +3 -6
  2. package/dist/index.d.ts +806 -74
  3. package/dist/index.js +6669 -4568
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -2
  6. package/src/components/AreaSet.tsx +87 -56
  7. package/src/components/ContourEditor.tsx +297 -39
  8. package/src/components/DatumEditor.tsx +261 -0
  9. package/src/components/DraftShape.tsx +56 -11
  10. package/src/components/EllipseSet.tsx +3 -3
  11. package/src/components/GridLayer.tsx +1 -2
  12. package/src/components/HeatmapLayer.tsx +1 -1
  13. package/src/components/MaskEditor.tsx +22 -9
  14. package/src/components/MeasureOverlay.tsx +320 -89
  15. package/src/components/PointSet.tsx +18 -10
  16. package/src/components/PolylineSet.tsx +81 -17
  17. package/src/components/RectRoiEditor.tsx +193 -88
  18. package/src/components/ShapeEditor.tsx +14 -4
  19. package/src/components/areaBatching.ts +111 -0
  20. package/src/components/contourArc.ts +268 -0
  21. package/src/components/datumEdit.ts +149 -0
  22. package/src/components/labelLod.ts +2 -3
  23. package/src/components/markerShapes.ts +1 -2
  24. package/src/components/measureGeometry.ts +34 -1
  25. package/src/components/measureHit.ts +371 -0
  26. package/src/components/overlayRole.ts +1 -1
  27. package/src/components/pointIndex.ts +2 -3
  28. package/src/components/polylineIndex.ts +3 -3
  29. package/src/components/shapeEdit.ts +38 -5
  30. package/src/components/stage/CompareLayer.tsx +228 -0
  31. package/src/components/stage/ImageLayer.tsx +56 -15
  32. package/src/components/stage/ImageStage.tsx +231 -128
  33. package/src/components/stage/StageSurface.tsx +44 -18
  34. package/src/components/stage/StageToolbar.tsx +26 -8
  35. package/src/components/stage/compareStyle.ts +155 -0
  36. package/src/components/stage/gesture.ts +5 -0
  37. package/src/components/stage/hitTest.ts +19 -0
  38. package/src/components/stage/useStageHitTest.ts +11 -0
  39. package/src/components/stage/view.ts +33 -6
  40. package/src/components/useShapeDrag.ts +2 -4
  41. package/src/index.ts +34 -2
@@ -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
 
@@ -104,6 +102,21 @@ interface Drag {
104
102
  pointerId: number;
105
103
  }
106
104
 
105
+ /** Why a view was reported through `ImageStage`'s `onView`. */
106
+ export interface StageViewChange {
107
+ /**
108
+ * What moved the view:
109
+ * - `"gesture"`: a wheel zoom, a drag or touch pan, a pinch, a double-click.
110
+ * - `"key"`: a keyboard shortcut or pan key.
111
+ * - `"command"`: a call to the handle or the context (`fit`, `zoomTo`, `frame`, `setView`),
112
+ * which includes the toolbar's buttons.
113
+ * - `"measure"`: the opening view, and a resize re-anchoring the view.
114
+ */
115
+ cause: "gesture" | "key" | "command" | "measure";
116
+ /** The measured viewport, in CSS pixels, at the moment of the change. */
117
+ box: Box;
118
+ }
119
+
107
120
  /** What `useStage` returns: the transform of the enclosing `ImageStage`, and its controls. */
108
121
  export interface StageContext {
109
122
  /** The view in effect — the controlled `view`, or the opening view while that is `null`. */
@@ -122,7 +135,10 @@ export interface StageContext {
122
135
  toViewport: (image: Point) => Point;
123
136
  /** The image-pixel length that covers `css` screen pixels — for screen-constant geometry. */
124
137
  imageLength: (css: number) => number;
125
- /** Put a rect (image coordinates) on screen with a margin. */
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
+ */
126
142
  frame: (rect: Rect, pad?: number) => void;
127
143
  /** Fit the whole image. */
128
144
  fit: () => void;
@@ -147,7 +163,10 @@ export interface StageContext {
147
163
  * measured. Layers inside the stage use `useStage` instead.
148
164
  */
149
165
  export interface StageHandle {
150
- /** Put a rect (image coordinates) on screen with a margin (CSS pixels, default 24). */
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
+ */
151
170
  frame: (rect: Rect, pad?: number) => void;
152
171
  /** Fit the whole image. */
153
172
  fit: () => void;
@@ -179,8 +198,13 @@ export interface ImageStageProps {
179
198
  * view is kept on first measure, only clamped to the legal range.
180
199
  */
181
200
  view: StageView | null;
182
- /** Called with every view change: gestures, keys, toolbar, and viewport resizes. */
183
- onView: (view: StageView) => void;
201
+ /**
202
+ * Called with every view change: gestures, keys, toolbar, and viewport resizes. The second
203
+ * argument says why and carries the measured viewport; a one-argument callback ignores it.
204
+ * A `null` `view` that the stage has already measured is re-opened and reported again, with
205
+ * cause `"measure"`.
206
+ */
207
+ onView: (view: StageView, change: StageViewChange) => void;
184
208
  /** Layers, each `absolute inset-0` and sized by the stage. */
185
209
  children: ReactNode;
186
210
  /** Rendered floating over the bottom-left, outside the transform. */
@@ -200,7 +224,8 @@ export interface ImageStageProps {
200
224
  panButton?: StageMouseButton | readonly StageMouseButton[] | undefined;
201
225
  /**
202
226
  * Whether a double-click toggles fit and the previous view. On by default; turn it off in
203
- * 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.
204
229
  */
205
230
  doubleClickFit?: boolean | undefined;
206
231
  /**
@@ -217,12 +242,23 @@ export interface ImageStageProps {
217
242
  style?: CSSProperties;
218
243
  /** Where the pointer is over the image, in image pixels, or `null` when it is outside. */
219
244
  onHover?: (point: Point | null) => void;
220
- /** A press on the background that was not a pan — how a layer hears "deselect". */
221
- onBackgroundClick?: (event: ReactPointerEvent<HTMLDivElement>) => void;
245
+ /**
246
+ * A press on the background that was not a pan — how a layer hears "deselect". `point` is
247
+ * where it landed, in image coordinates (the pixel-centre convention, unclamped).
248
+ */
249
+ onBackgroundClick?: (event: ReactPointerEvent<HTMLDivElement>, point: Point) => void;
222
250
  /** The legal scale range; see `clampView`. */
223
251
  clamp?: ClampOptions;
224
252
  /** Keyboard zoom/pan shortcuts. On by default; the stage takes focus to receive them. */
225
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;
226
262
  /**
227
263
  * Whether the arrow keys pan. On by default, and worth turning off in an app where the
228
264
  * arrows have a better job — stepping through a list of findings, say. The zoom keys
@@ -260,6 +296,9 @@ export interface ImageStageProps {
260
296
  * State is exposed on the viewport as `data-fit` (the view is fit), `data-panning` (a drag
261
297
  * is in progress) and `data-pan-mode` (the hand tool is on or space is held), each present
262
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.
263
302
  */
264
303
  export function ImageStage({
265
304
  image,
@@ -279,6 +318,7 @@ export function ImageStage({
279
318
  onBackgroundClick,
280
319
  clamp,
281
320
  shortcuts = true,
321
+ focusable = shortcuts,
282
322
  panKeys = true,
283
323
  label = "Image canvas",
284
324
  initialView: opening = "auto",
@@ -301,26 +341,50 @@ export function ImageStage({
301
341
  () => ({ padding: fitOptions?.padding, upscale: fitOptions?.upscale }),
302
342
  [fitOptions?.padding, fitOptions?.upscale],
303
343
  );
304
- const latestRef = useRef({ view, box, image, clamp, onView, opening, fitOpts });
344
+ const effective = useMemo(
345
+ () => view ?? (box.width > 0 ? openingView(opening, box, image, fitOpts) : UNMEASURED_VIEW),
346
+ [view, box, image, opening, fitOpts],
347
+ );
348
+
349
+ const latestRef = useRef({
350
+ view,
351
+ effective: UNMEASURED_VIEW,
352
+ box,
353
+ image,
354
+ clamp,
355
+ onView,
356
+ opening,
357
+ fitOpts,
358
+ });
305
359
  useLayoutEffect(() => {
306
- latestRef.current = { view, box, image, clamp, onView, opening, fitOpts };
360
+ latestRef.current = {
361
+ view,
362
+ effective,
363
+ box,
364
+ image,
365
+ clamp,
366
+ onView,
367
+ opening,
368
+ fitOpts,
369
+ };
307
370
  });
371
+ /** Whether the opening view of the current `null` view has been reported. */
372
+ const openedRef = useRef(false);
308
373
  /** The view to come back to when a double-click leaves fit. */
309
374
  const previousRef = useRef<StageView | null>(null);
310
375
  const dragRef = useRef<Drag | null>(null);
311
376
  /** The touches down now, by pointer id, in client coordinates. */
312
377
  const touchesRef = useRef(new Map<number, Point>());
313
378
  /** The two-finger gesture in progress, from the moment the second finger went down. */
314
- const pinchRef = useRef<{ distance: number; centre: Point; view: StageView } | null>(null);
379
+ const pinchRef = useRef<{
380
+ distance: number;
381
+ centre: Point;
382
+ view: StageView;
383
+ } | null>(null);
315
384
  /** The registry the stage's layers answer hit-tests through. */
316
385
  const [hits] = useState(() => createHitRegistry<StagePointerEvent>());
317
386
  const panButtons = useMemo(() => panButtonCodes(panButton), [panButton]);
318
387
 
319
- const effective = useMemo(
320
- () => view ?? (box.width > 0 ? openingView(opening, box, image, fitOpts) : UNMEASURED_VIEW),
321
- [view, box, image, opening, fitOpts],
322
- );
323
-
324
388
  // `will-change: transform` (and `data-moving`) on the transformed box while the view moves, set
325
389
  // on the element itself so a pan costs no React render; see `MOVING_SETTLE_MS`.
326
390
  useEffect(() => {
@@ -335,11 +399,34 @@ export function ImageStage({
335
399
  return () => clearTimeout(timer);
336
400
  }, [effective.scale, effective.tx, effective.ty]);
337
401
 
338
- const commit = useCallback((next: StageView) => {
339
- const { box: b, image: i, clamp: c, onView: report } = latestRef.current;
340
- report(b.width > 0 ? clampView(next, b, i, c) : next);
402
+ /** Report a view with its cause and the viewport it was made for (the measured one by default). */
403
+ const emit = useCallback((next: StageView, cause: StageViewChange["cause"], box?: Box) => {
404
+ const { box: b, onView: report } = latestRef.current;
405
+ report(next, { cause, box: box ?? b });
341
406
  }, []);
342
407
 
408
+ const commit = useCallback(
409
+ (next: StageView, cause: StageViewChange["cause"] = "command") => {
410
+ const { box: b, image: i, clamp: c } = latestRef.current;
411
+ emit(b.width > 0 ? clampView(next, b, i, c) : next, cause);
412
+ },
413
+ [emit],
414
+ );
415
+
416
+ // A `null` view the stage has already measured: `measure` only fires when the size changes,
417
+ // so without this the opening view was drawn but never reported, and the pane stayed on a
418
+ // view the app did not hold. Once per null period; a non-null view re-arms it.
419
+ useEffect(() => {
420
+ if (view !== null) {
421
+ openedRef.current = false;
422
+ return;
423
+ }
424
+ if (box.width > 0 && !openedRef.current) {
425
+ openedRef.current = true;
426
+ emit(openingView(opening, box, image, fitOpts), "measure");
427
+ }
428
+ }, [view, box, image, opening, fitOpts, emit]);
429
+
343
430
  /*
344
431
  * The viewport, measured and observed from the moment it mounts (a callback ref with a
345
432
  * cleanup, so the subscription lives exactly as long as the element).
@@ -353,54 +440,63 @@ export function ImageStage({
353
440
  * as **passive**, which makes `preventDefault` in a synthetic handler a no-op — the page
354
441
  * would scroll behind the canvas while you zoom, which reads as the zoom being broken.
355
442
  */
356
- const attachViewport = useCallback((element: HTMLDivElement | null) => {
357
- viewportRef.current = element;
358
- if (!element) return;
359
-
360
- const measure = ({ width, height }: Box) => {
361
- if (!(width > 0) || !(height > 0)) return;
362
- const current = measuredRef.current;
363
- if (current.width === width && current.height === height) return;
364
- const next = { width, height };
365
- measuredRef.current = next;
366
- setBox(next);
367
- const { view: v, image: i, clamp: c, onView: report, opening: o, fitOpts: f } = latestRef.current;
368
- if (v === null) report(openingView(o, next, i, f));
369
- // The first measurement: there is no previous viewport to re-anchor against, so the
370
- // caller's view is kept — only made legal for this one.
371
- else if (!(current.width > 0)) report(clampView(v, next, i, c));
372
- else report(preserveCenter(v, current, next, i, c, f));
373
- };
443
+ const attachViewport = useCallback(
444
+ (element: HTMLDivElement | null) => {
445
+ viewportRef.current = element;
446
+ if (!element) return;
447
+
448
+ const measure = ({ width, height }: Box) => {
449
+ if (!(width > 0) || !(height > 0)) return;
450
+ const current = measuredRef.current;
451
+ if (current.width === width && current.height === height) return;
452
+ const next = { width, height };
453
+ measuredRef.current = next;
454
+ setBox(next);
455
+ const { view: v, image: i, clamp: c, opening: o, fitOpts: f } = latestRef.current;
456
+ if (v === null) {
457
+ openedRef.current = true;
458
+ emit(openingView(o, next, i, f), "measure", next);
459
+ }
460
+ // The first measurement: there is no previous viewport to re-anchor against, so the
461
+ // caller's view is kept — only made legal for this one.
462
+ else if (!(current.width > 0)) emit(clampView(v, next, i, c), "measure", next);
463
+ else emit(preserveCenter(v, current, next, i, c, f), "measure", next);
464
+ };
374
465
 
375
- // Synchronously, so the first paint already has the right view — and as the *content*
376
- // box, which is what the observer reports; measuring the border box here re-anchored
377
- // the view by the border's width as soon as the observer's first report arrived.
378
- measure(contentSize(element));
379
- const observer = new ResizeObserver((entries) => {
380
- const entry = entries[0];
381
- if (entry) measure(entry.contentRect);
382
- });
383
- observer.observe(element);
384
-
385
- const onWheel = (event: WheelEvent) => {
386
- event.preventDefault();
387
- const { view: v, box: b, image: i, clamp: c, onView: report } = latestRef.current;
388
- if (v === null || !(b.width > 0)) return;
389
- const origin = viewportOrigin(element);
390
- const [min, max] = scaleRange(b, i, c);
391
- const factor = Math.exp(-event.deltaY * WHEEL_SENSITIVITY);
392
- const scale = Math.min(max, Math.max(min, v.scale * factor));
393
- const anchor = { x: event.clientX - origin.x, y: event.clientY - origin.y };
394
- report(clampView(zoomAbout(v, scale, anchor), b, i, c));
395
- };
396
- element.addEventListener("wheel", onWheel, { passive: false });
466
+ // Synchronously, so the first paint already has the right view — and as the *content*
467
+ // box, which is what the observer reports; measuring the border box here re-anchored
468
+ // the view by the border's width as soon as the observer's first report arrived.
469
+ measure(contentSize(element));
470
+ const observer = new ResizeObserver((entries) => {
471
+ const entry = entries[0];
472
+ if (entry) measure(entry.contentRect);
473
+ });
474
+ observer.observe(element);
475
+
476
+ const onWheel = (event: WheelEvent) => {
477
+ event.preventDefault();
478
+ const { effective: v, box: b, image: i, clamp: c } = latestRef.current;
479
+ if (!(b.width > 0)) return;
480
+ const origin = viewportOrigin(element);
481
+ const [min, max] = scaleRange(b, i, c);
482
+ const factor = Math.exp(-event.deltaY * WHEEL_SENSITIVITY);
483
+ const scale = Math.min(max, Math.max(min, v.scale * factor));
484
+ const anchor = {
485
+ x: event.clientX - origin.x,
486
+ y: event.clientY - origin.y,
487
+ };
488
+ emit(clampView(zoomAbout(v, scale, anchor), b, i, c), "gesture");
489
+ };
490
+ element.addEventListener("wheel", onWheel, { passive: false });
397
491
 
398
- return () => {
399
- observer.disconnect();
400
- element.removeEventListener("wheel", onWheel);
401
- viewportRef.current = null;
402
- };
403
- }, []);
492
+ return () => {
493
+ observer.disconnect();
494
+ element.removeEventListener("wheel", onWheel);
495
+ viewportRef.current = null;
496
+ };
497
+ },
498
+ [emit],
499
+ );
404
500
 
405
501
  /* Space is a *modifier*, not a command: held, it turns any press into a pan, which is the
406
502
  * one gesture that has to work no matter which layer is under the cursor. */
@@ -425,45 +521,56 @@ export function ImageStage({
425
521
 
426
522
  const clientToImage = useCallback((p: Point): Point => {
427
523
  const element = viewportRef.current;
428
- const { view: v } = latestRef.current;
429
- if (!element || v === null) return { x: 0, y: 0 };
524
+ const { view: held, effective: v, box: b } = latestRef.current;
525
+ // Unmeasured and uncontrolled: there is no view to convert with yet.
526
+ if (!element || (held === null && !(b.width > 0))) return { x: 0, y: 0 };
430
527
  const origin = viewportOrigin(element);
431
528
  return toImage(v, { x: p.x - origin.x, y: p.y - origin.y });
432
529
  }, []);
433
530
 
434
531
  const imageToClient = useCallback((p: Point): Point => {
435
532
  const element = viewportRef.current;
436
- const { view: v } = latestRef.current;
437
- if (!element || v === null) return { x: 0, y: 0 };
533
+ const { view: held, effective: v, box: b } = latestRef.current;
534
+ // Unmeasured and uncontrolled: there is no view to convert with yet.
535
+ if (!element || (held === null && !(b.width > 0))) return { x: 0, y: 0 };
438
536
  const origin = viewportOrigin(element);
439
537
  const local = toScreen(v, p);
440
538
  return { x: local.x + origin.x, y: local.y + origin.y };
441
539
  }, []);
442
540
 
443
- const fit = useCallback(() => {
444
- const { box: b, image: i, view: v, onView: report, fitOpts: f } = latestRef.current;
445
- if (!(b.width > 0)) return;
446
- if (v !== null && !isFit(v, b, i, f)) previousRef.current = v;
447
- report(fitView(b, i, f));
448
- }, []);
449
-
450
- const zoomTo = useCallback((scale: number, anchor?: Point) => {
451
- const { view: v, box: b, image: i, clamp: c, onView: report } = latestRef.current;
452
- const element = viewportRef.current;
453
- if (v === null || !(b.width > 0) || !element) return;
454
- const origin = viewportOrigin(element);
455
- const local = anchor
456
- ? { x: anchor.x - origin.x, y: anchor.y - origin.y }
457
- : { x: b.width / 2, y: b.height / 2 };
458
- const [min, max] = scaleRange(b, i, c);
459
- report(clampView(zoomAbout(v, Math.min(max, Math.max(min, scale)), local), b, i, c));
460
- }, []);
541
+ const fitBy = useCallback(
542
+ (cause: StageViewChange["cause"]) => {
543
+ const { box: b, image: i, view: v, fitOpts: f } = latestRef.current;
544
+ if (!(b.width > 0)) return;
545
+ if (v !== null && !isFit(v, b, i, f)) previousRef.current = v;
546
+ emit(fitView(b, i, f), cause);
547
+ },
548
+ [emit],
549
+ );
550
+ const fit = useCallback(() => fitBy("command"), [fitBy]);
461
551
 
462
- const frame = useCallback((rect: Rect, pad?: number) => {
463
- const { box: b, image: i, onView: report } = latestRef.current;
464
- if (!(b.width > 0)) return;
465
- report(frameRect(b, i, rect, pad));
466
- }, []);
552
+ const zoomBy = useCallback(
553
+ (cause: StageViewChange["cause"], scale: number, anchor?: Point) => {
554
+ const { effective: v, box: b, image: i, clamp: c } = latestRef.current;
555
+ const element = viewportRef.current;
556
+ if (!(b.width > 0) || !element) return;
557
+ const origin = viewportOrigin(element);
558
+ const local = anchor ? { x: anchor.x - origin.x, y: anchor.y - origin.y } : { x: b.width / 2, y: b.height / 2 };
559
+ const [min, max] = scaleRange(b, i, c);
560
+ emit(clampView(zoomAbout(v, Math.min(max, Math.max(min, scale)), local), b, i, c), cause);
561
+ },
562
+ [emit],
563
+ );
564
+ const zoomTo = useCallback((scale: number, anchor?: Point) => zoomBy("command", scale, anchor), [zoomBy]);
565
+
566
+ const frame = useCallback(
567
+ (rect: Rect, pad?: number) => {
568
+ const { box: b, image: i } = latestRef.current;
569
+ if (!(b.width > 0)) return;
570
+ emit(frameRect(b, i, rect, pad), "command");
571
+ },
572
+ [emit],
573
+ );
467
574
 
468
575
  useImperativeHandle(ref, () => ({ frame, fit, zoomTo }), [frame, fit, zoomTo]);
469
576
 
@@ -486,25 +593,11 @@ export function ImageStage({
486
593
  panning,
487
594
  panMode,
488
595
  }),
489
- [
490
- effective,
491
- commit,
492
- image,
493
- box,
494
- clientToImage,
495
- imageToClient,
496
- frame,
497
- fit,
498
- zoomTo,
499
- atFit,
500
- panning,
501
- panMode,
502
- ],
596
+ [effective, commit, image, box, clientToImage, imageToClient, frame, fit, zoomTo, atFit, panning, panMode],
503
597
  );
504
598
 
505
599
  /** The pointer's tolerance for hover and presses in image pixels: a fingertip is fatter. */
506
- const hitRadius = (event: { pointerType: string }) =>
507
- imageLengthFor(effective, hitRadiusPx(event.pointerType));
600
+ const hitRadius = (event: { pointerType: string }) => imageLengthFor(effective, hitRadiusPx(event.pointerType));
508
601
 
509
602
  /** Offer a press to the items under it, topmost first; whether one claimed it. */
510
603
  const routePress = (event: ReactPointerEvent<HTMLDivElement>) =>
@@ -568,7 +661,7 @@ export function ImageStage({
568
661
  // During a pan the pointer is holding the image, not pointing at a pixel.
569
662
  onHover?.(null);
570
663
  hits.clearHover();
571
- commit({ scale: effective.scale, tx: origin.tx + dx, ty: origin.ty + dy });
664
+ commit({ scale: effective.scale, tx: origin.tx + dx, ty: origin.ty + dy }, "gesture");
572
665
  };
573
666
 
574
667
  const endDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
@@ -576,7 +669,9 @@ export function ImageStage({
576
669
  if (origin?.touch) return; // a touch ends through `touchEnd`
577
670
  dragRef.current = null;
578
671
  setPanning(false);
579
- if (origin && !origin.moved && event.button === 0) onBackgroundClick?.(event);
672
+ if (origin && !origin.moved && event.button === 0) {
673
+ onBackgroundClick?.(event, clientToImage({ x: event.clientX, y: event.clientY }));
674
+ }
580
675
  };
581
676
 
582
677
  /*
@@ -644,6 +739,7 @@ export function ImageStage({
644
739
  { x: mid.x - origin.x, y: mid.y - origin.y },
645
740
  scaleRange(box, image, clamp),
646
741
  ),
742
+ "gesture",
647
743
  );
648
744
  return;
649
745
  }
@@ -656,7 +752,7 @@ export function ImageStage({
656
752
  if (!drag.pan) return;
657
753
  setPanning(true);
658
754
  onHover?.(null);
659
- commit({ scale: effective.scale, tx: drag.tx + dx, ty: drag.ty + dy });
755
+ commit({ scale: effective.scale, tx: drag.tx + dx, ty: drag.ty + dy }, "gesture");
660
756
  };
661
757
 
662
758
  const touchEnd = (event: ReactPointerEvent<HTMLDivElement>, cancelled: boolean) => {
@@ -686,27 +782,31 @@ export function ImageStage({
686
782
  dragRef.current = null;
687
783
  setPanning(false);
688
784
  if (!drag || cancelled || drag.moved || !isTap(0, event.timeStamp - drag.at)) return;
689
- if (!routePress(event)) onBackgroundClick?.(event);
785
+ if (!routePress(event)) onBackgroundClick?.(event, clientToImage({ x: event.clientX, y: event.clientY }));
690
786
  };
691
787
 
692
788
  /*
693
789
  * Fit ↔ **the view you were just at**, which is the gesture a reader actually wants: a
694
790
  * double-click to see the whole part, another to go back to the corner they were working
695
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.
696
795
  */
697
796
  const onDoubleClick = (event: ReactPointerEvent<HTMLDivElement>) => {
797
+ if (!panMode && hits.routeDoubleClick(clientToImage({ x: event.clientX, y: event.clientY }), hitRadius(event))) return;
698
798
  if (!doubleClickFit || !(box.width > 0)) return;
699
799
  if (atFit) {
700
800
  const restore = previousRef.current;
701
801
  if (restore) {
702
- onView(clampView(restore, box, image, clamp));
802
+ emit(clampView(restore, box, image, clamp), "gesture");
703
803
  } else {
704
- zoomTo(1, { x: event.clientX, y: event.clientY });
804
+ zoomBy("gesture", 1, { x: event.clientX, y: event.clientY });
705
805
  }
706
806
  return;
707
807
  }
708
808
  previousRef.current = effective;
709
- onView(fitView(box, image, fitOpts));
809
+ emit(fitView(box, image, fitOpts), "gesture");
710
810
  };
711
811
 
712
812
  const onKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
@@ -716,33 +816,33 @@ export function ImageStage({
716
816
  switch (event.key) {
717
817
  case "+":
718
818
  case "=":
719
- zoomTo(Math.min(max, effective.scale * 1.5));
819
+ zoomBy("key", Math.min(max, effective.scale * 1.5));
720
820
  break;
721
821
  case "-":
722
822
  case "_":
723
- zoomTo(Math.max(min, effective.scale / 1.5));
823
+ zoomBy("key", Math.max(min, effective.scale / 1.5));
724
824
  break;
725
825
  case "0":
726
- fit();
826
+ fitBy("key");
727
827
  break;
728
828
  case "1":
729
- zoomTo(1);
829
+ zoomBy("key", 1);
730
830
  break;
731
831
  case "ArrowLeft":
732
832
  if (!panKeys) return;
733
- commit({ ...effective, tx: effective.tx + nudge });
833
+ commit({ ...effective, tx: effective.tx + nudge }, "key");
734
834
  break;
735
835
  case "ArrowRight":
736
836
  if (!panKeys) return;
737
- commit({ ...effective, tx: effective.tx - nudge });
837
+ commit({ ...effective, tx: effective.tx - nudge }, "key");
738
838
  break;
739
839
  case "ArrowUp":
740
840
  if (!panKeys) return;
741
- commit({ ...effective, ty: effective.ty + nudge });
841
+ commit({ ...effective, ty: effective.ty + nudge }, "key");
742
842
  break;
743
843
  case "ArrowDown":
744
844
  if (!panKeys) return;
745
- commit({ ...effective, ty: effective.ty - nudge });
845
+ commit({ ...effective, ty: effective.ty - nudge }, "key");
746
846
  break;
747
847
  default:
748
848
  return;
@@ -755,9 +855,9 @@ export function ImageStage({
755
855
  <StageHitContext value={hits}>
756
856
  <div
757
857
  ref={attachViewport}
758
- role="application"
858
+ role={focusable ? "application" : "group"}
759
859
  aria-label={label}
760
- tabIndex={0}
860
+ tabIndex={focusable ? 0 : undefined}
761
861
  style={style}
762
862
  data-fit={atFit ? "" : undefined}
763
863
  data-panning={panning ? "" : undefined}
@@ -836,7 +936,10 @@ function hoverPoint(
836
936
  ): Point | null {
837
937
  if (!element) return null;
838
938
  const origin = viewportOrigin(element);
839
- const p = toImage(view, { x: event.clientX - origin.x, y: event.clientY - origin.y });
939
+ const p = toImage(view, {
940
+ x: event.clientX - origin.x,
941
+ y: event.clientY - origin.y,
942
+ });
840
943
  return insideImage(p, image) ? p : null;
841
944
  }
842
945