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
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
// 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;
|
|
|
|
class Viewport {
|
|
/** Screen position of the world origin, in CSS px. */
|
|
x = $state(0);
|
|
y = $state(0);
|
|
scale = $state(1);
|
|
/** 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 };
|
|
}
|
|
|
|
toScreen(wx: number, wy: number) {
|
|
return { x: wx * this.scale + this.x, y: wy * this.scale + this.y };
|
|
}
|
|
|
|
/** The visible world rectangle. */
|
|
get world(): Rect {
|
|
return { x: -this.x / this.scale, y: -this.y / this.scale, width: this.width / this.scale, height: this.height / this.scale };
|
|
}
|
|
|
|
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;
|
|
this.x = sx - w.x * next;
|
|
this.y = sy - w.y * next;
|
|
}
|
|
|
|
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));
|
|
this.x = this.width / 2 - (r.x + r.width / 2) * this.scale;
|
|
this.y = this.height / 2 - (r.y + r.height / 2) * this.scale;
|
|
}
|
|
|
|
/** 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;
|
|
}
|
|
}
|
|
|
|
export const viewport = new Viewport();
|