Give canvas pans momentum

Releasing a pan now lets the view glide on in the drag's direction,
slowing under exponential drag (time constant GLIDE_TAU = 325 ms, so it
coasts about 325 px per 1 px/ms of release speed). Only the position
moves; the zoom is untouched. A drag that stops before lifting doesn't
glide. Touching the canvas, wheel, zoom or fit stops a glide.

Applies to finger, mouse (middle/space/background drag) and pen pans.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015cuSqnxerdvXU934U9DddL
This commit is contained in:
agent 2026-09-30 19:38:57 +00:00
parent 8a3d2224a7
commit 9b13dee725
5 changed files with 129 additions and 14 deletions

View file

@ -1,6 +1,7 @@
// Pan/zoom of the canvas view. World units are PDF points.
import type { Rect } from '$lib/model/layout';
import { GLIDE_MAX_SPEED, GLIDE_MIN_SPEED, glideStep } from '$lib/ink/fling';
export const MIN_SCALE = 0.04;
export const MAX_SCALE = 8;
@ -13,6 +14,8 @@ class Viewport {
/** Size of the stage element. */
width = $state(0);
height = $state(0);
/** Pending animation frame of a momentum glide, 0 when still. */
private glideFrame = 0;
toWorld(sx: number, sy: number) {
return { x: (sx - this.x) / this.scale, y: (sy - this.y) / this.scale };
@ -28,6 +31,7 @@ class Viewport {
}
zoomAt(factor: number, sx = this.width / 2, sy = this.height / 2) {
this.stopGlide();
const next = Math.min(MAX_SCALE, Math.max(MIN_SCALE, this.scale * factor));
const w = this.toWorld(sx, sy);
this.scale = next;
@ -36,12 +40,44 @@ class Viewport {
}
panBy(dx: number, dy: number) {
this.stopGlide();
this.x += dx;
this.y += dy;
}
/**
* Keep panning after a drag is released, at velocity (vx, vy) px/ms, slowing to
* a stop. Only the position moves; the zoom stays as it is. Any other view change
* (pan, zoom, fit) stops it.
*/
glide(vx: number, vy: number) {
this.stopGlide();
const speed = Math.hypot(vx, vy);
if (speed < GLIDE_MIN_SPEED) return;
if (speed > GLIDE_MAX_SPEED) {
vx *= GLIDE_MAX_SPEED / speed;
vy *= GLIDE_MAX_SPEED / speed;
}
let last = performance.now();
const frame = (now: number) => {
const s = glideStep(vx, vy, Math.max(0, now - last));
last = now;
this.x += s.dx;
this.y += s.dy;
({ vx, vy } = s);
this.glideFrame = Math.hypot(vx, vy) < GLIDE_MIN_SPEED ? 0 : requestAnimationFrame(frame);
};
this.glideFrame = requestAnimationFrame(frame);
}
stopGlide() {
if (this.glideFrame) cancelAnimationFrame(this.glideFrame);
this.glideFrame = 0;
}
/** Fit a world rectangle into the stage with some padding. */
fit(r: Rect, pad = 24) {
this.stopGlide();
if (!this.width || !this.height) return;
const s = Math.min((this.width - 2 * pad) / r.width, (this.height - 2 * pad) / r.height);
this.scale = Math.min(MAX_SCALE, Math.max(MIN_SCALE, s));
@ -51,6 +87,7 @@ class Viewport {
/** Pan so the rectangle is centred, keeping the zoom. */
center(r: Rect) {
this.stopGlide();
this.x = this.width / 2 - (r.x + r.width / 2) * this.scale;
this.y = this.height / 2 - (r.y + r.height / 2) * this.scale;
}