@vitavision/stage2d 0.8.1 → 0.9.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 +14 -0
- package/dist/index.d.ts +381 -3
- package/dist/index.js +1090 -61
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/PointSet.tsx +391 -0
- package/src/components/PolylineSet.tsx +39 -0
- package/src/components/markerShapes.ts +119 -0
- package/src/components/pointIndex.ts +320 -0
- package/src/components/stage/ImageStage.tsx +264 -59
- package/src/components/stage/gesture.ts +81 -0
- package/src/components/stage/hitContext.ts +16 -0
- package/src/components/stage/hitTest.ts +181 -0
- package/src/components/stage/useStageHitTest.ts +127 -0
- package/src/index.ts +35 -0
|
@@ -46,6 +46,9 @@ import type {
|
|
|
46
46
|
|
|
47
47
|
import { cn } from "@vitavision/ui";
|
|
48
48
|
import type { Point } from "../measureGeometry";
|
|
49
|
+
import { centroid, isTap, panButtonCodes, pinchView, type StageMouseButton } from "./gesture";
|
|
50
|
+
import { StageHitContext, type StagePointerEvent } from "./hitContext";
|
|
51
|
+
import { createHitRegistry } from "./hitTest";
|
|
49
52
|
import {
|
|
50
53
|
clampView,
|
|
51
54
|
fitView,
|
|
@@ -68,9 +71,30 @@ import {
|
|
|
68
71
|
const WHEEL_SENSITIVITY = 0.0015;
|
|
69
72
|
/** How far a press may travel and still count as a click rather than a pan. */
|
|
70
73
|
const CLICK_SLOP = 3;
|
|
74
|
+
/** The pointer's tolerance for hover and presses, in screen pixels: a mouse, and a fingertip. */
|
|
75
|
+
const POINTER_RADIUS = 6;
|
|
76
|
+
const TOUCH_RADIUS = 12;
|
|
71
77
|
/** The view before the viewport has been measured — nothing is on screen yet. */
|
|
72
78
|
const UNMEASURED_VIEW: StageView = { scale: 1, tx: 0, ty: 0 };
|
|
73
79
|
|
|
80
|
+
/** A press that may become a pan, a click or a tap. */
|
|
81
|
+
interface Drag {
|
|
82
|
+
x: number;
|
|
83
|
+
y: number;
|
|
84
|
+
tx: number;
|
|
85
|
+
ty: number;
|
|
86
|
+
/** Strayed past the click slop at some point, so it is not a click or a tap. */
|
|
87
|
+
moved: boolean;
|
|
88
|
+
/** Whether movement pans the view. */
|
|
89
|
+
pan: boolean;
|
|
90
|
+
/** A touch, which reports a tap rather than a click. */
|
|
91
|
+
touch: boolean;
|
|
92
|
+
/** `timeStamp` of the press. */
|
|
93
|
+
at: number;
|
|
94
|
+
/** The touch's pointer id, or `-1`. */
|
|
95
|
+
pointerId: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
74
98
|
/** What `useStage` returns: the transform of the enclosing `ImageStage`, and its controls. */
|
|
75
99
|
export interface StageContext {
|
|
76
100
|
/** The view in effect — the controlled `view`, or the opening view while that is `null`. */
|
|
@@ -158,6 +182,26 @@ export interface ImageStageProps {
|
|
|
158
182
|
banner?: ReactNode;
|
|
159
183
|
/** Force panning regardless of where the press lands (a "hand tool"). */
|
|
160
184
|
panTool?: boolean;
|
|
185
|
+
/**
|
|
186
|
+
* The mouse buttons that pan on drag, over background no layer claimed. Defaults to
|
|
187
|
+
* `["left", "middle"]`. An app whose left button belongs to the active drawing tool passes
|
|
188
|
+
* `"right"`; the context menu is then suppressed on the stage, since the button's job is
|
|
189
|
+
* to drag. The hand tool and a held space bar still pan with the left button.
|
|
190
|
+
*/
|
|
191
|
+
panButton?: StageMouseButton | readonly StageMouseButton[] | undefined;
|
|
192
|
+
/**
|
|
193
|
+
* Whether a double-click toggles fit and the previous view. On by default; turn it off in
|
|
194
|
+
* an app whose tools use double-click, such as closing a polygon.
|
|
195
|
+
*/
|
|
196
|
+
doubleClickFit?: boolean | undefined;
|
|
197
|
+
/**
|
|
198
|
+
* What one finger does on background no layer claimed: `"one-finger"` (the default) pans,
|
|
199
|
+
* as the mouse does; `"two-finger"` leaves it to the app's tools and pans only with two.
|
|
200
|
+
* Two fingers always pinch-zoom about their midpoint and pan with it. A tap (a touch that
|
|
201
|
+
* stays put and is short) is a press: a layer's `onItemPress` hears it, or else
|
|
202
|
+
* `onBackgroundClick`.
|
|
203
|
+
*/
|
|
204
|
+
touchPan?: "one-finger" | "two-finger" | undefined;
|
|
161
205
|
/** Merged onto the viewport with `cn`. */
|
|
162
206
|
className?: string;
|
|
163
207
|
/** Inline style for the viewport. */
|
|
@@ -206,6 +250,9 @@ export function ImageStage({
|
|
|
206
250
|
readout,
|
|
207
251
|
banner,
|
|
208
252
|
panTool = false,
|
|
253
|
+
panButton,
|
|
254
|
+
doubleClickFit = true,
|
|
255
|
+
touchPan = "one-finger",
|
|
209
256
|
className,
|
|
210
257
|
style,
|
|
211
258
|
onHover,
|
|
@@ -234,7 +281,14 @@ export function ImageStage({
|
|
|
234
281
|
});
|
|
235
282
|
/** The view to come back to when a double-click leaves fit. */
|
|
236
283
|
const previousRef = useRef<StageView | null>(null);
|
|
237
|
-
const dragRef = useRef<
|
|
284
|
+
const dragRef = useRef<Drag | null>(null);
|
|
285
|
+
/** The touches down now, by pointer id, in client coordinates. */
|
|
286
|
+
const touchesRef = useRef(new Map<number, Point>());
|
|
287
|
+
/** The two-finger gesture in progress, from the moment the second finger went down. */
|
|
288
|
+
const pinchRef = useRef<{ distance: number; centre: Point; view: StageView } | null>(null);
|
|
289
|
+
/** The registry the stage's layers answer hit-tests through. */
|
|
290
|
+
const [hits] = useState(() => createHitRegistry<StagePointerEvent>());
|
|
291
|
+
const panButtons = useMemo(() => panButtonCodes(panButton), [panButton]);
|
|
238
292
|
|
|
239
293
|
const effective = useMemo(
|
|
240
294
|
() => view ?? (box.width > 0 ? openingView(opening, box, image) : UNMEASURED_VIEW),
|
|
@@ -408,15 +462,35 @@ export function ImageStage({
|
|
|
408
462
|
],
|
|
409
463
|
);
|
|
410
464
|
|
|
465
|
+
/** The pointer's tolerance for hover and presses in image pixels: a fingertip is fatter. */
|
|
466
|
+
const hitRadius = (event: { pointerType: string }) =>
|
|
467
|
+
imageLengthFor(effective, event.pointerType === "touch" ? TOUCH_RADIUS : POINTER_RADIUS);
|
|
468
|
+
|
|
469
|
+
/** Offer a press to the items under it, topmost first; whether one claimed it. */
|
|
470
|
+
const routePress = (event: ReactPointerEvent<HTMLDivElement>) =>
|
|
471
|
+
hits.routePress(clientToImage({ x: event.clientX, y: event.clientY }), hitRadius(event), event);
|
|
472
|
+
|
|
411
473
|
/*
|
|
412
474
|
* Panning is the *default* reading of a press, and a layer opts out of it by handling the
|
|
413
475
|
* event and stopping propagation — so the stage needs to know nothing about ROI handles or
|
|
414
476
|
* clickable contours, and a layer needs no permission to exist. The predecessor inverted
|
|
415
477
|
* this: it captured every `pointerdown` unconditionally, which is why its consumers had to
|
|
416
478
|
* mount interactive layers outside the transform, where they stopped moving with the image.
|
|
479
|
+
*
|
|
480
|
+
* Layers that cannot own an element — a marker among thousands — register with the hit
|
|
481
|
+
* registry instead, and the press reaches them here: after every element-level handler had
|
|
482
|
+
* its say, before the stage reads the press as a pan.
|
|
417
483
|
*/
|
|
418
484
|
const onPointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
419
|
-
if (event.
|
|
485
|
+
if (event.pointerType === "touch") {
|
|
486
|
+
touchDown(event);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const primary = event.button === 0;
|
|
490
|
+
if (primary && !panMode && routePress(event)) return;
|
|
491
|
+
// The hand tool and a held space bar pan with the primary button whatever `panButton` says.
|
|
492
|
+
const pan = panButtons.has(event.button) || (primary && panMode);
|
|
493
|
+
if (!pan && !primary) return;
|
|
420
494
|
event.preventDefault();
|
|
421
495
|
dragRef.current = {
|
|
422
496
|
x: event.clientX,
|
|
@@ -424,15 +498,27 @@ export function ImageStage({
|
|
|
424
498
|
tx: effective.tx,
|
|
425
499
|
ty: effective.ty,
|
|
426
500
|
moved: false,
|
|
501
|
+
pan,
|
|
502
|
+
touch: false,
|
|
503
|
+
at: event.timeStamp,
|
|
504
|
+
pointerId: event.pointerId,
|
|
427
505
|
};
|
|
428
|
-
setPanning(true);
|
|
506
|
+
if (pan) setPanning(true);
|
|
429
507
|
event.currentTarget.setPointerCapture(event.pointerId);
|
|
430
508
|
};
|
|
431
509
|
|
|
432
510
|
const onPointerMove = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
511
|
+
if (event.pointerType === "touch") {
|
|
512
|
+
touchMove(event);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
433
515
|
const origin = dragRef.current;
|
|
434
|
-
if (!origin) {
|
|
516
|
+
if (!origin?.pan) {
|
|
435
517
|
onHover?.(hoverPoint(event, viewportRef.current, effective, image));
|
|
518
|
+
hits.routeHover(clientToImage({ x: event.clientX, y: event.clientY }), hitRadius(event));
|
|
519
|
+
if (origin && !origin.moved && Math.hypot(event.clientX - origin.x, event.clientY - origin.y) > CLICK_SLOP) {
|
|
520
|
+
origin.moved = true;
|
|
521
|
+
}
|
|
436
522
|
return;
|
|
437
523
|
}
|
|
438
524
|
const dx = event.clientX - origin.x;
|
|
@@ -441,23 +527,135 @@ export function ImageStage({
|
|
|
441
527
|
origin.moved = true;
|
|
442
528
|
// During a pan the pointer is holding the image, not pointing at a pixel.
|
|
443
529
|
onHover?.(null);
|
|
530
|
+
hits.clearHover();
|
|
444
531
|
commit({ scale: effective.scale, tx: origin.tx + dx, ty: origin.ty + dy });
|
|
445
532
|
};
|
|
446
533
|
|
|
447
534
|
const endDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
448
535
|
const origin = dragRef.current;
|
|
536
|
+
if (origin?.touch) return; // a touch ends through `touchEnd`
|
|
449
537
|
dragRef.current = null;
|
|
450
538
|
setPanning(false);
|
|
451
539
|
if (origin && !origin.moved && event.button === 0) onBackgroundClick?.(event);
|
|
452
540
|
};
|
|
453
541
|
|
|
542
|
+
/*
|
|
543
|
+
* Touch. The first finger is a pan candidate (if `touchPan` says so) and a tap candidate;
|
|
544
|
+
* the second turns the gesture into a pinch about the fingers' midpoint, which also
|
|
545
|
+
* carries the view with it. A tap is a press and is reported on release, because on
|
|
546
|
+
* `pointerdown` a finger might still be the first of two.
|
|
547
|
+
*/
|
|
548
|
+
const touchDown = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
549
|
+
const touches = touchesRef.current;
|
|
550
|
+
if (touches.size >= 2) return; // a third finger has no job
|
|
551
|
+
event.preventDefault();
|
|
552
|
+
touches.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
553
|
+
try {
|
|
554
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
555
|
+
} catch {
|
|
556
|
+
// The touch ended or was taken by the browser before this handler ran. The gesture
|
|
557
|
+
// still works from the events the viewport receives; capture only keeps a drag that
|
|
558
|
+
// leaves the viewport.
|
|
559
|
+
}
|
|
560
|
+
if (touches.size === 1) {
|
|
561
|
+
dragRef.current = {
|
|
562
|
+
x: event.clientX,
|
|
563
|
+
y: event.clientY,
|
|
564
|
+
tx: effective.tx,
|
|
565
|
+
ty: effective.ty,
|
|
566
|
+
moved: false,
|
|
567
|
+
pan: touchPan === "one-finger" || panMode,
|
|
568
|
+
touch: true,
|
|
569
|
+
at: event.timeStamp,
|
|
570
|
+
pointerId: event.pointerId,
|
|
571
|
+
};
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const [a, b] = [...touches.values()] as [Point, Point];
|
|
575
|
+
const origin = viewportOrigin(event.currentTarget);
|
|
576
|
+
const mid = centroid(a, b);
|
|
577
|
+
pinchRef.current = {
|
|
578
|
+
distance: Math.hypot(a.x - b.x, a.y - b.y),
|
|
579
|
+
centre: { x: mid.x - origin.x, y: mid.y - origin.y },
|
|
580
|
+
view: effective,
|
|
581
|
+
};
|
|
582
|
+
if (dragRef.current) dragRef.current.moved = true; // two fingers are never a tap
|
|
583
|
+
setPanning(true);
|
|
584
|
+
onHover?.(null);
|
|
585
|
+
hits.clearHover();
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
const touchMove = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
589
|
+
const touches = touchesRef.current;
|
|
590
|
+
if (!touches.has(event.pointerId)) return;
|
|
591
|
+
touches.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
592
|
+
const pinch = pinchRef.current;
|
|
593
|
+
if (pinch && touches.size === 2) {
|
|
594
|
+
if (!(box.width > 0)) return;
|
|
595
|
+
const [a, b] = [...touches.values()] as [Point, Point];
|
|
596
|
+
const origin = viewportOrigin(event.currentTarget);
|
|
597
|
+
const mid = centroid(a, b);
|
|
598
|
+
commit(
|
|
599
|
+
pinchView(
|
|
600
|
+
pinch.view,
|
|
601
|
+
pinch.distance,
|
|
602
|
+
pinch.centre,
|
|
603
|
+
Math.hypot(a.x - b.x, a.y - b.y),
|
|
604
|
+
{ x: mid.x - origin.x, y: mid.y - origin.y },
|
|
605
|
+
scaleRange(box, image, clamp),
|
|
606
|
+
),
|
|
607
|
+
);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
const drag = dragRef.current;
|
|
611
|
+
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
612
|
+
const dx = event.clientX - drag.x;
|
|
613
|
+
const dy = event.clientY - drag.y;
|
|
614
|
+
if (!drag.moved && Math.hypot(dx, dy) <= CLICK_SLOP) return;
|
|
615
|
+
drag.moved = true;
|
|
616
|
+
if (!drag.pan) return;
|
|
617
|
+
setPanning(true);
|
|
618
|
+
onHover?.(null);
|
|
619
|
+
commit({ scale: effective.scale, tx: drag.tx + dx, ty: drag.ty + dy });
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
const touchEnd = (event: ReactPointerEvent<HTMLDivElement>, cancelled: boolean) => {
|
|
623
|
+
const touches = touchesRef.current;
|
|
624
|
+
if (!touches.delete(event.pointerId)) return; // already ended (the capture is lost after the release)
|
|
625
|
+
if (pinchRef.current) {
|
|
626
|
+
pinchRef.current = null;
|
|
627
|
+
const rest = [...touches.entries()][0];
|
|
628
|
+
if (rest) {
|
|
629
|
+
// One finger is still down: it carries on as a pan from where it is, never as a tap.
|
|
630
|
+
dragRef.current = {
|
|
631
|
+
x: rest[1].x,
|
|
632
|
+
y: rest[1].y,
|
|
633
|
+
tx: effective.tx,
|
|
634
|
+
ty: effective.ty,
|
|
635
|
+
moved: true,
|
|
636
|
+
pan: touchPan === "one-finger" || panMode,
|
|
637
|
+
touch: true,
|
|
638
|
+
at: event.timeStamp,
|
|
639
|
+
pointerId: rest[0],
|
|
640
|
+
};
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (touches.size > 0) return;
|
|
645
|
+
const drag = dragRef.current;
|
|
646
|
+
dragRef.current = null;
|
|
647
|
+
setPanning(false);
|
|
648
|
+
if (!drag || cancelled || drag.moved || !isTap(0, event.timeStamp - drag.at)) return;
|
|
649
|
+
if (!routePress(event)) onBackgroundClick?.(event);
|
|
650
|
+
};
|
|
651
|
+
|
|
454
652
|
/*
|
|
455
653
|
* Fit ↔ **the view you were just at**, which is the gesture a reader actually wants: a
|
|
456
654
|
* double-click to see the whole part, another to go back to the corner they were working
|
|
457
655
|
* in. Toggling against 1:1 instead (the predecessor's behaviour) throws that place away.
|
|
458
656
|
*/
|
|
459
657
|
const onDoubleClick = (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
460
|
-
if (!(box.width > 0)) return;
|
|
658
|
+
if (!doubleClickFit || !(box.width > 0)) return;
|
|
461
659
|
if (atFit) {
|
|
462
660
|
const restore = previousRef.current;
|
|
463
661
|
if (restore) {
|
|
@@ -514,65 +712,72 @@ export function ImageStage({
|
|
|
514
712
|
|
|
515
713
|
return (
|
|
516
714
|
<ImageStageContext value={context}>
|
|
517
|
-
<
|
|
518
|
-
ref={attachViewport}
|
|
519
|
-
role="application"
|
|
520
|
-
aria-label={label}
|
|
521
|
-
tabIndex={0}
|
|
522
|
-
style={style}
|
|
523
|
-
data-fit={atFit ? "" : undefined}
|
|
524
|
-
data-panning={panning ? "" : undefined}
|
|
525
|
-
data-pan-mode={panMode ? "" : undefined}
|
|
526
|
-
className={cn(
|
|
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",
|
|
530
|
-
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-signal",
|
|
531
|
-
panning ? "cursor-grabbing" : panMode ? "cursor-grab" : "cursor-default",
|
|
532
|
-
className,
|
|
533
|
-
)}
|
|
534
|
-
onPointerDown={onPointerDown}
|
|
535
|
-
onPointerMove={onPointerMove}
|
|
536
|
-
onPointerUp={endDrag}
|
|
537
|
-
// A drag interrupted by a lost capture — a context menu, a browser gesture, the
|
|
538
|
-
// pointer leaving the window — used to leave the drag set, so the next hover panned
|
|
539
|
-
// the image with no button held down.
|
|
540
|
-
onPointerCancel={endDrag}
|
|
541
|
-
onLostPointerCapture={endDrag}
|
|
542
|
-
onPointerLeave={() => onHover?.(null)}
|
|
543
|
-
onDoubleClick={onDoubleClick}
|
|
544
|
-
onKeyDown={onKeyDown}
|
|
545
|
-
>
|
|
546
|
-
{/* One transformed box holding every layer, laid out at the image's own pixel size. */}
|
|
715
|
+
<StageHitContext value={hits}>
|
|
547
716
|
<div
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
717
|
+
ref={attachViewport}
|
|
718
|
+
role="application"
|
|
719
|
+
aria-label={label}
|
|
720
|
+
tabIndex={0}
|
|
721
|
+
style={style}
|
|
722
|
+
data-fit={atFit ? "" : undefined}
|
|
723
|
+
data-panning={panning ? "" : undefined}
|
|
724
|
+
data-pan-mode={panMode ? "" : undefined}
|
|
725
|
+
className={cn(
|
|
726
|
+
// `@container/stage`: the toolbar and readout collapse by the canvas's own width,
|
|
727
|
+
// not the window's — a canvas beside a wide inspector is narrow on a wide screen.
|
|
728
|
+
"@container/stage relative h-full w-full touch-none overflow-hidden rounded-control border border-line bg-canvas select-none",
|
|
729
|
+
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-signal",
|
|
730
|
+
panning ? "cursor-grabbing" : panMode ? "cursor-grab" : "cursor-default",
|
|
731
|
+
className,
|
|
732
|
+
)}
|
|
733
|
+
onPointerDown={onPointerDown}
|
|
734
|
+
onPointerMove={onPointerMove}
|
|
735
|
+
onPointerUp={(event) => (event.pointerType === "touch" ? touchEnd(event, false) : endDrag(event))}
|
|
736
|
+
// A drag interrupted by a lost capture — a context menu, a browser gesture, the
|
|
737
|
+
// pointer leaving the window — used to leave the drag set, so the next hover panned
|
|
738
|
+
// the image with no button held down.
|
|
739
|
+
onPointerCancel={(event) => (event.pointerType === "touch" ? touchEnd(event, true) : endDrag(event))}
|
|
740
|
+
onLostPointerCapture={(event) => (event.pointerType === "touch" ? touchEnd(event, true) : endDrag(event))}
|
|
741
|
+
onPointerLeave={() => {
|
|
742
|
+
onHover?.(null);
|
|
743
|
+
hits.clearHover();
|
|
554
744
|
}}
|
|
745
|
+
onDoubleClick={onDoubleClick}
|
|
746
|
+
// The button's job is to drag; a menu opening at the end of every pan would undo it.
|
|
747
|
+
onContextMenu={panButtons.has(2) ? (event) => event.preventDefault() : undefined}
|
|
748
|
+
onKeyDown={onKeyDown}
|
|
555
749
|
>
|
|
556
|
-
{
|
|
557
|
-
|
|
750
|
+
{/* One transformed box holding every layer, laid out at the image's own pixel size. */}
|
|
751
|
+
<div
|
|
752
|
+
data-stage
|
|
753
|
+
className="absolute top-0 left-0 origin-top-left"
|
|
754
|
+
style={{
|
|
755
|
+
width: image.width,
|
|
756
|
+
height: image.height,
|
|
757
|
+
transform: `translate(${effective.tx}px, ${effective.ty}px) scale(${effective.scale})`,
|
|
758
|
+
}}
|
|
759
|
+
>
|
|
760
|
+
{children}
|
|
761
|
+
</div>
|
|
558
762
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
763
|
+
{banner && <div className="pointer-events-none absolute top-2 left-2 z-10">{banner}</div>}
|
|
764
|
+
|
|
765
|
+
{/* One row, not two corners: as separate absolute boxes the toolbar and the readout
|
|
766
|
+
overlapped as soon as the canvas was narrower than their combined width, which is
|
|
767
|
+
an ordinary window on a two-column workbench. `justify-between` keeps them apart
|
|
768
|
+
and lets the readout be the one that gives up room. */}
|
|
769
|
+
{(toolbar || readout) && (
|
|
770
|
+
// Wraps rather than clips: on a canvas too narrow for both, the readout moves onto
|
|
771
|
+
// its own line above the toolbar instead of being truncated to nothing.
|
|
772
|
+
<div className="pointer-events-none absolute inset-x-2 bottom-2 z-10 flex flex-wrap-reverse items-end justify-between gap-2">
|
|
773
|
+
<div className="pointer-events-auto max-w-full shrink-0">{toolbar}</div>
|
|
774
|
+
<div data-readout-slot="" className="max-w-full min-w-0 truncate">
|
|
775
|
+
{readout}
|
|
776
|
+
</div>
|
|
572
777
|
</div>
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
</
|
|
778
|
+
)}
|
|
779
|
+
</div>
|
|
780
|
+
</StageHitContext>
|
|
576
781
|
</ImageStageContext>
|
|
577
782
|
);
|
|
578
783
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stage's pointer gestures as pure arithmetic: which mouse buttons pan, what a
|
|
3
|
+
* two-finger pinch does to the view, and what counts as a tap.
|
|
4
|
+
*
|
|
5
|
+
* Points are viewport-local CSS pixels, as in `view.ts`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Point } from "../measureGeometry";
|
|
9
|
+
import { zoomAbout, type StageView } from "./view";
|
|
10
|
+
|
|
11
|
+
/** A mouse button that can pan the stage. */
|
|
12
|
+
export type StageMouseButton = "left" | "middle" | "right";
|
|
13
|
+
|
|
14
|
+
/** How far a touch may travel and still be a tap, in CSS pixels. */
|
|
15
|
+
export const TAP_SLOP = 3;
|
|
16
|
+
/** The longest a touch may last and still be a tap, in milliseconds. */
|
|
17
|
+
export const TAP_MS = 500;
|
|
18
|
+
|
|
19
|
+
const BUTTON_CODE: Record<StageMouseButton, number> = { left: 0, middle: 1, right: 2 };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The `PointerEvent.button` codes a `panButton` prop names.
|
|
23
|
+
*
|
|
24
|
+
* @param panButton - One button or a list; omitted means left and middle, the stage's
|
|
25
|
+
* long-standing default.
|
|
26
|
+
* @returns The codes: 0 left, 1 middle, 2 right.
|
|
27
|
+
*/
|
|
28
|
+
export function panButtonCodes(panButton: StageMouseButton | readonly StageMouseButton[] | undefined): ReadonlySet<number> {
|
|
29
|
+
if (panButton === undefined) return new Set([0, 1]);
|
|
30
|
+
const list = typeof panButton === "string" ? [panButton] : panButton;
|
|
31
|
+
return new Set(list.map((button) => BUTTON_CODE[button]));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The midpoint of two points.
|
|
36
|
+
*
|
|
37
|
+
* @param a - One point.
|
|
38
|
+
* @param b - The other.
|
|
39
|
+
* @returns Their centroid.
|
|
40
|
+
*/
|
|
41
|
+
export function centroid(a: Point, b: Point): Point {
|
|
42
|
+
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The view a pinch has reached: scaled by how much the fingers spread, and carried by how
|
|
47
|
+
* far their midpoint moved. The image point under the fingers' first midpoint stays under
|
|
48
|
+
* their current one, so the gesture reads as holding the picture with two fingers.
|
|
49
|
+
*
|
|
50
|
+
* @param start - The view when the second finger went down.
|
|
51
|
+
* @param startDistance - The fingers' distance then.
|
|
52
|
+
* @param startCentre - Their midpoint then.
|
|
53
|
+
* @param distance - Their distance now.
|
|
54
|
+
* @param centre - Their midpoint now.
|
|
55
|
+
* @param range - The `[min, max]` scale to stay within.
|
|
56
|
+
* @returns The view; not yet clamped to the legal pan range.
|
|
57
|
+
*/
|
|
58
|
+
export function pinchView(
|
|
59
|
+
start: StageView,
|
|
60
|
+
startDistance: number,
|
|
61
|
+
startCentre: Point,
|
|
62
|
+
distance: number,
|
|
63
|
+
centre: Point,
|
|
64
|
+
range: readonly [number, number],
|
|
65
|
+
): StageView {
|
|
66
|
+
const ratio = startDistance > 0 ? distance / startDistance : 1;
|
|
67
|
+
const scale = Math.min(range[1], Math.max(range[0], start.scale * ratio));
|
|
68
|
+
const zoomed = zoomAbout(start, scale, startCentre);
|
|
69
|
+
return { scale: zoomed.scale, tx: zoomed.tx + centre.x - startCentre.x, ty: zoomed.ty + centre.y - startCentre.y };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Whether a touch that has ended was a tap.
|
|
74
|
+
*
|
|
75
|
+
* @param travel - The farthest it strayed from where it began, in CSS pixels.
|
|
76
|
+
* @param elapsed - How long it lasted, in milliseconds.
|
|
77
|
+
* @returns `true` for a short touch that stayed put.
|
|
78
|
+
*/
|
|
79
|
+
export function isTap(travel: number, elapsed: number): boolean {
|
|
80
|
+
return travel <= TAP_SLOP && elapsed <= TAP_MS;
|
|
81
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The context an `ImageStage` provides its layers' hit registry through. Its own module so
|
|
3
|
+
* that `ImageStage` (which provides it) and `useStageHitTest` (which reads it, and reads
|
|
4
|
+
* `useStage` from `ImageStage`) do not import each other.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createContext } from "react";
|
|
8
|
+
import type { PointerEvent as ReactPointerEvent } from "react";
|
|
9
|
+
|
|
10
|
+
import type { HitRegistry } from "./hitTest";
|
|
11
|
+
|
|
12
|
+
/** The press event a layer's `press` handler receives. */
|
|
13
|
+
export type StagePointerEvent = ReactPointerEvent<Element>;
|
|
14
|
+
|
|
15
|
+
/** The enclosing stage's registry, or `null` outside a stage. */
|
|
16
|
+
export const StageHitContext = createContext<HitRegistry<StagePointerEvent> | null>(null);
|