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

@ -21,7 +21,7 @@
import { canInsert, freeSlots, freeSlotsOf, neighbor, type Dir, type Slot } from '$lib/model/tree';
import { A4, PAGE_PRESETS, newId, type PageImage, type Stroke } from '$lib/model/types';
import { PalmRejector, touchIntent } from '$lib/ink/palm';
import { flingDir, pushSample, type Sample } from '$lib/ink/fling';
import { flingDir, pushSample, releaseVelocity, type Sample } from '$lib/ink/fling';
import { StrokeRecorder, hitStroke } from '$lib/ink/stroke';
import {
IDENTITY,
@ -607,6 +607,8 @@
return;
}
}
// Touching the canvas catches a gliding view.
viewport.stopGlide();
if (plus && e.button === 0) {
e.preventDefault();
@ -828,9 +830,12 @@
selPreview = null;
if (e.type === 'pointerup' && t && (t.k !== 1 || t.tx || t.ty)) commitTransform(t);
} else if (g.kind === 'pan') {
if (e.type === 'pointerup' && g.moved && g.pointerType === 'touch') {
if (e.type === 'pointerup' && g.moved) {
pushSample(g.trail, { ...local(e), t: e.timeStamp });
offerAfterFling(g.trail);
// The view coasts on in the direction of the drag (a drag that stopped before lifting has ~no velocity).
const v = releaseVelocity(g.trail);
if (v) viewport.glide(v.vx, v.vy);
if (g.pointerType === 'touch') offerAfterFling(g.trail);
}
} else if (g.kind === 'add') {
if (e.type === 'pointerup' && (g.pointerType !== 'touch' || palm.evaluate(e, 'navigate').accept)) addPage(g.slot.anchor, g.slot.dir);