feat(editor): two-finger pan, touch hint, loupe, touch-sized handles

useFraming gains twoPointerPan (the editor's pinch also pans by the
midpoint and scales from the spread since the pinch began; the
catalogue passes nothing). A futile one-finger drag shows the R-1
hint unless two fingers already worked in this editor visit (work
store flag). A finger drag of a handle shows the loupe (technique A:
the drawing copied once, data-anno-id stripped, outline on top).
editor-touch.spec covers 1 024, 1 180 and 1 280 px with CDP touch.
measure-drag.ts is the hand-run smoothness measure (MEASURE_DRAG=1).
equineShape moved from editor-zones.spec to support/editor.ts, no
assertion changed.
This commit is contained in:
bchanot committed 2026-11-18 10:54:46 +01:00
1 parent 3a7715486b
commit 85231a2680
18 files changed
+1338 -53

No files matched your search

+70 -10
View File
@@ -30,6 +30,17 @@ export interface FramingOptions {
* caller tells a drag from a click), replacing the 4 px threshold and `canPan`.
*/
panning?: () => boolean
/**
* The editor's pinch: two fingers also pan by their midpoint's movement, and the scale
* follows the fingers' spread since the pinch began (a clamp at the fit never ratchets).
*/
twoPointerPan?: boolean
}
/** Where a pinch began: its spread and the scale then. */
interface PinchStart {
spread: number
scale: number
}
function boxOf(
@@ -48,6 +59,42 @@ function distance(a: Point, b: Point): number {
return Math.hypot(a.x - b.x, a.y - b.y)
}
const midpoint = (a: Point, b: Point, box: { left: number; top: number }) => ({
x: (a.x + b.x) / 2 - box.left,
y: (a.y + b.y) / 2 - box.top,
})
/** The pinch's base, taken whenever exactly two pointers are down. */
function pinchStart(
pointers: Map<number, Point>,
scale: number,
): PinchStart | null {
if (pointers.size !== 2) return null
const [a, b] = [...pointers.values()]
return { spread: Math.max(distance(a, b), 1), scale }
}
/**
* One finger of a two-finger pan moved from `before` to `now`: the drawing scales around
* the old midpoint to the spread's scale, then follows the midpoint (half the move).
*/
function pinchPan(
framing: Framing,
start: PinchStart,
fingers: { before: Point; now: Point; other: Point },
box: Box & { left: number; top: number },
): Framing {
const { before, now, other } = fingers
const target = (start.scale * distance(now, other)) / start.spread
const zoomed = zoomBy(
framing,
target / framing.scale,
box,
midpoint(before, other, box),
)
return panBy(zoomed, (now.x - before.x) / 2, (now.y - before.y) / 2, box)
}
/** One-pointer pan: the caller's gate when it gives one, else past the threshold and allowed. */
function pansNow(
gate: (() => boolean) | undefined,
@@ -81,6 +128,9 @@ export function useFraming(
canPan.current = options.canPan
const panning = useRef(options.panning)
panning.current = options.panning
const twoPointerPan = useRef(options.twoPointerPan ?? false)
twoPointerPan.current = options.twoPointerPan ?? false
const pinch = useRef<PinchStart | null>(null)
useEffect(() => setFraming(FIT), [resetKey])
useEffect(() => {
@@ -128,8 +178,25 @@ export function useFraming(
dragged.current = false
panAllowed.current = canPan.current?.(event) ?? true
}
pinch.current = pinchStart(pointers.current, scale.current)
}, [])
const pinchMove = useCallback(
(fingers: { before: Point; now: Point; other: Point }) => {
const box = boxOf(viewport.current)
const start = pinch.current
if (twoPointerPan.current && start) {
setFraming((f) => pinchPan(f, start, fingers, box))
return
}
const { now, other } = fingers
const factor =
distance(now, other) / Math.max(distance(fingers.before, other), 1)
zoom(factor, midpoint(now, other, box))
},
[viewport, zoom],
)
const onPointerMove = useCallback(
(event: PointerEvent) => {
const before = pointers.current.get(event.pointerId)
@@ -142,15 +209,7 @@ export function useFraming(
moved.current += distance(before, now)
if (moved.current > DRAG_THRESHOLD) dragged.current = true
if (others.length === 1) {
const other = others[0][1]
const box = boxOf(viewport.current)
const at = {
x: (now.x + other.x) / 2 - box.left,
y: (now.y + other.y) / 2 - box.top,
}
const factor =
distance(now, other) / Math.max(distance(before, other), 1)
zoom(factor, at)
pinchMove({ before, now, other: others[0][1] })
} else if (
others.length === 0 &&
pansNow(panning.current, dragged, panAllowed)
@@ -159,11 +218,12 @@ export function useFraming(
pan(now.x - before.x, now.y - before.y)
}
},
[viewport, zoom, pan],
[pinchMove, pan],
)
const onPointerUp = useCallback((event: PointerEvent) => {
pointers.current.delete(event.pointerId)
pinch.current = pinchStart(pointers.current, scale.current)
}, [])
const onKeyDown = useCallback(