Viewer zoom and sharper tiles

Mouse wheel, trackpad and two-finger pinch zoom in the album viewer,
up to one image pixel per device pixel; drag pans while zoomed and
only the visible tiles of the needed level are loaded.

Default WebP quality raised to top 90, mid 88, low 88, with
smart_subsample for WebP tiles and the seed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
lucioandClaude Opus 5.5 committed 2026-11-04 20:56:12 -08:00
1 parent ae898a2787
commit aaf7cafdf9
9 files changed
+302 -18

No files matched your search

+284 -7
View File
@@ -42,6 +42,10 @@ const flickSpeed = 0.35;
const rubberBand = 0.35;
const captionZone = 0.2;
const idleMs = 2500;
const zoomSettleMs = 120;
const wheelLine = 16;
const wheelRate = 0.002;
const pinchWheelRate = 0.01;
function cubicBezier(x1: number, y1: number, x2: number, y2: number): (t: number) => number {
const cx = 3 * x1;
@@ -133,6 +137,24 @@ export class Viewer {
private pinned = false;
private resizeTimer = 0;
private indexRequested = false;
private ox = 0;
private oy = 0;
private secondId: number | null = null;
private ax = 0;
private ay = 0;
private bx = 0;
private by = 0;
private pinching = false;
private stale = false;
private zoom = 1;
private zoomMax = 1;
private zoomPhoto = -1;
private zx = 0;
private zy = 0;
private slackX = 0;
private slackY = 0;
private zoomTimer = 0;
private gestureZoom = 1;
constructor(
private readonly stage: HTMLElement,
@@ -235,6 +257,8 @@ export class Viewer {
const rect = this.stage.getBoundingClientRect();
this.vw = Math.max(1, rect.width);
this.vh = Math.max(1, rect.height);
this.ox = rect.left;
this.oy = rect.top;
this.dpr = Math.min(window.devicePixelRatio || 1, this.settings.maxDpr);
const cw = Math.max(1, Math.round(this.vw * this.dpr));
const ch = Math.max(1, Math.round(this.vh * this.dpr));
@@ -322,6 +346,13 @@ export class Viewer {
this.updateCaption();
}
});
stage.addEventListener("wheel", (e) => this.onWheel(e), { passive: false });
stage.addEventListener("gesturestart", (e) => {
e.preventDefault();
this.gestureZoom = this.zoom;
});
stage.addEventListener("gesturechange", (e) => this.onGesture(e as Event & { scale?: number; clientX?: number; clientY?: number }));
stage.addEventListener("gestureend", (e) => e.preventDefault());
stage.addEventListener("dragstart", (e) => e.preventDefault());
stage.addEventListener("selectstart", (e) => e.preventDefault());
if (this.settings.blockContextMenu) {
@@ -332,6 +363,9 @@ export class Viewer {
const target = this.stopForHash();
if (this.stops[target].photo !== this.stops[this.current].photo) {
this.jump(target);
} else if (this.clearZoom()) {
this.startSession();
this.updateCaption();
}
});
new ResizeObserver(() => this.onResize()).observe(stage);
@@ -349,6 +383,7 @@ export class Viewer {
if (before === this.vw + "x" + this.vh + "@" + this.dpr) {
return;
}
this.clearZoom();
this.tween = null;
this.camera = this.stops[this.current].camera;
this.requestDraw();
@@ -379,7 +414,11 @@ export class Viewer {
this.go(this.stops.length - 2);
break;
case "Escape":
location.assign("/");
if (this.zoomPhoto >= 0) {
this.go(this.current);
} else {
location.assign("/");
}
break;
case "i":
case "I":
@@ -393,7 +432,24 @@ export class Viewer {
}
private onPointerDown(e: PointerEvent): void {
if (this.pointerId !== null || (e.pointerType === "mouse" && e.button !== 0)) {
if (e.pointerType === "mouse" && e.button !== 0) {
return;
}
if (this.pointerId !== null) {
if (
this.secondId === null &&
e.pointerType !== "mouse" &&
this.pointerType !== "mouse" &&
!this.stale &&
(this.zoomPhoto >= 0 || (!this.dragging && this.enterZoom()))
) {
this.secondId = e.pointerId;
this.bx = e.clientX;
this.by = e.clientY;
this.pinching = true;
this.dragging = true;
this.stage.setPointerCapture(e.pointerId);
}
return;
}
if ((e.target as Element).closest("a, button")) {
@@ -402,8 +458,11 @@ export class Viewer {
this.pointerId = e.pointerId;
this.pointerType = e.pointerType;
this.dragging = false;
this.stale = false;
this.downX = e.clientX;
this.downY = e.clientY;
this.ax = e.clientX;
this.ay = e.clientY;
this.downT = e.timeStamp;
this.tween = null;
this.downCamera = this.camera;
@@ -422,7 +481,11 @@ export class Viewer {
}
return;
}
if (e.pointerId !== this.pointerId) {
if (this.pinching && (e.pointerId === this.pointerId || e.pointerId === this.secondId)) {
this.pinchMove(e.pointerId, e.clientX, e.clientY);
return;
}
if (e.pointerId !== this.pointerId || this.stale) {
return;
}
const dx = e.clientX - this.downX;
@@ -433,6 +496,16 @@ export class Viewer {
this.updateCaption();
}
if (!this.dragging) {
this.ax = e.clientX;
this.ay = e.clientY;
return;
}
if (this.zoomPhoto >= 0) {
this.zx += e.clientX - this.ax;
this.zy += e.clientY - this.ay;
this.ax = e.clientX;
this.ay = e.clientY;
this.settleZoom();
return;
}
this.samples.push({ t: e.timeStamp, x: e.clientX });
@@ -444,6 +517,10 @@ export class Viewer {
}
private onPointerUp(e: PointerEvent, cancelled: boolean): void {
if (this.pinching && (e.pointerId === this.pointerId || e.pointerId === this.secondId)) {
this.endPinch(e.pointerId);
return;
}
if (e.pointerId !== this.pointerId) {
return;
}
@@ -454,12 +531,16 @@ export class Viewer {
const wasDragging = this.dragging;
this.dragging = false;
this.stage.classList.remove("is-dragging");
if (this.stale) {
this.stale = false;
return;
}
const moved = Math.hypot(e.clientX - this.downX, e.clientY - this.downY);
if (!cancelled && !wasDragging && moved < tapDistance && e.timeStamp - this.downT < tapMs) {
this.onTap(e);
return;
}
if (!wasDragging) {
if (!wasDragging || this.zoomPhoto >= 0) {
return;
}
let velocity = 0;
@@ -491,7 +572,7 @@ export class Viewer {
private onTap(e: PointerEvent): void {
const stop = this.stops[this.current];
if (stop.photo < 0) {
if (stop.photo < 0 || this.zoomPhoto >= 0) {
return;
}
const rect = this.photoRect(stop.photo);
@@ -501,6 +582,178 @@ export class Viewer {
}
}
private onWheel(e: WheelEvent): void {
e.preventDefault();
if (this.pinching || (this.dragging && this.zoomPhoto < 0)) {
return;
}
let delta = e.deltaY;
if (e.deltaMode === 1) {
delta *= wheelLine;
} else if (e.deltaMode === 2) {
delta *= this.vh;
}
if (delta === 0) {
return;
}
const step = Math.max(-1, Math.min(1, delta * (e.ctrlKey ? pinchWheelRate : wheelRate)));
const x = e.clientX - this.ox;
const y = e.clientY - this.oy;
this.zoomTo(this.zoom * Math.exp(-step), x, y, x, y);
}
private onGesture(e: Event & { scale?: number; clientX?: number; clientY?: number }): void {
e.preventDefault();
if (this.pointerId !== null || !e.scale) {
return;
}
const x = (e.clientX ?? this.ox + this.vw / 2) - this.ox;
const y = (e.clientY ?? this.oy + this.vh / 2) - this.oy;
this.zoomTo(this.gestureZoom * e.scale, x, y, x, y);
}
private pinchMove(id: number, x: number, y: number): void {
const px = (this.ax + this.bx) / 2 - this.ox;
const py = (this.ay + this.by) / 2 - this.oy;
const before = Math.hypot(this.ax - this.bx, this.ay - this.by);
if (id === this.pointerId) {
this.ax = x;
this.ay = y;
} else {
this.bx = x;
this.by = y;
}
const after = Math.hypot(this.ax - this.bx, this.ay - this.by);
const target = before > 1 && after > 1 ? (this.zoom * after) / before : this.zoom;
this.zoomTo(target, px, py, (this.ax + this.bx) / 2 - this.ox, (this.ay + this.by) / 2 - this.oy);
}
private endPinch(id: number): void {
if (this.stage.hasPointerCapture(id)) {
this.stage.releasePointerCapture(id);
}
if (id === this.pointerId) {
this.pointerId = this.secondId;
this.ax = this.bx;
this.ay = this.by;
}
this.secondId = null;
this.pinching = false;
this.dragging = true;
if (this.zoomPhoto >= 0) {
this.settleZoom();
}
}
private enterZoom(): boolean {
const stop = this.stops[this.current];
if (stop.photo < 0 || this.suspended) {
return false;
}
const layout = this.layouts[stop.photo];
const max = this.manifest.photos[stop.photo].width / (window.devicePixelRatio || 1) / layout.width;
if (max <= 1.001) {
return false;
}
this.tween = null;
this.camera = stop.camera;
this.zoomPhoto = stop.photo;
this.zoomMax = max;
this.zoom = 1;
this.zx = layout.left - (this.camera - this.vw / 2);
this.zy = layout.top;
this.slackX = this.zx - this.clampAxis(this.zx, layout.width, this.vw, 0);
this.slackY = this.zy - this.clampAxis(this.zy, layout.height, this.vh, 0);
this.updateCaption();
return true;
}
private zoomTo(target: number, px: number, py: number, qx: number, qy: number): void {
if (this.zoomPhoto < 0 && (target <= 1 || !this.enterZoom())) {
return;
}
const zoom = Math.max(1, Math.min(this.zoomMax, target));
const f = zoom / this.zoom;
this.zx = qx - (px - this.zx) * f;
this.zy = qy - (py - this.zy) * f;
this.zoom = zoom;
this.settleZoom();
}
private settleZoom(): void {
const layout = this.layouts[this.zoomPhoto];
this.zx = this.clampAxis(this.zx, layout.width * this.zoom, this.vw, this.slackX);
this.zy = this.clampAxis(this.zy, layout.height * this.zoom, this.vh, this.slackY);
this.requestDraw();
if (this.zoom <= 1 && !this.pinching) {
this.camera = layout.left + this.vw / 2 - this.zx;
this.go(this.nearestStop(this.camera));
return;
}
clearTimeout(this.zoomTimer);
this.zoomTimer = window.setTimeout(() => this.startZoomSession(), zoomSettleMs);
}
private clampAxis(position: number, size: number, view: number, slack: number): number {
const centre = (view - size) / 2;
let lo = Math.min(view - size, centre);
let hi = Math.max(0, centre);
const extra = slack * Math.max(0, 2 - this.zoom);
if (extra > 0) {
hi += extra;
} else {
lo += extra;
}
return Math.min(hi, Math.max(lo, position));
}
private clearZoom(): boolean {
if (this.zoomPhoto < 0) {
return false;
}
this.zoomPhoto = -1;
this.zoom = 1;
this.gestureZoom = 1;
clearTimeout(this.zoomTimer);
this.zoomTimer = 0;
if (this.pointerId !== null) {
this.stale = true;
this.pinching = false;
this.secondId = null;
}
this.requestDraw();
return true;
}
private levelDpr(): number {
if (this.zoomPhoto >= 0 && this.zoom >= this.zoomMax * 0.999) {
return Math.max(this.dpr, window.devicePixelRatio || 1);
}
return this.dpr;
}
private startZoomSession(): void {
this.zoomTimer = 0;
if (this.suspended || this.zoomPhoto < 0) {
return;
}
const layout = this.layouts[this.zoomPhoto];
const w = layout.width * this.zoom;
const h = layout.height * this.zoom;
const a = Math.max(0, this.zx);
const b = Math.min(this.vw, this.zx + w);
const c = Math.max(0, this.zy);
const d = Math.min(this.vh, this.zy + h);
if (b <= a || d <= c) {
return;
}
this.loader(this.zoomPhoto).start(
{ displayWidth: w, u0: (a - this.zx) / w, u1: (b - this.zx) / w, v0: (c - this.zy) / h, v1: (d - this.zy) / h },
this.levelDpr(),
() => undefined,
);
}
private rubber(camera: number): number {
const min = this.stops[0].camera;
const max = this.stops[this.stops.length - 1].camera;
@@ -526,6 +779,7 @@ export class Viewer {
private go(index: number): void {
const target = Math.max(0, Math.min(this.stops.length - 1, index));
const previous = this.current;
const unzoomed = this.clearZoom();
const to = this.stops[target].camera;
if (this.reducedMotion.matches) {
this.crossfade(() => {
@@ -549,6 +803,9 @@ export class Viewer {
if (target !== previous) {
this.activate(previous);
} else {
if (unzoomed) {
this.startSession();
}
this.updateCaption();
}
this.requestDraw();
@@ -556,6 +813,7 @@ export class Viewer {
private jump(index: number): void {
const previous = this.current;
this.clearZoom();
this.tween = null;
this.crossfade(() => {
this.current = index;
@@ -661,6 +919,7 @@ export class Viewer {
return;
}
this.suspended = false;
this.clearZoom();
this.canvas.width = 0;
this.measure();
this.camera = this.stops[this.current].camera;
@@ -746,7 +1005,7 @@ export class Viewer {
private updateCaption(): void {
const stop = this.stops[this.current];
const visible = stop.photo >= 0 && !this.dragging && (this.pinned || this.hover);
const visible = stop.photo >= 0 && this.zoomPhoto < 0 && !this.dragging && (this.pinned || this.hover);
if (visible) {
const rect = this.photoRect(stop.photo);
this.caption.place(rect.left, rect.right - rect.left, this.vh - rect.bottom);
@@ -835,7 +1094,25 @@ export class Viewer {
const dpr = this.dpr;
const near = this.stops[this.current].photo;
const from = Math.max(0, (near < 0 ? this.layouts.length - 1 : near) - 2);
const to = Math.min(this.layouts.length - 1, (near < 0 ? this.layouts.length - 1 : near) + 2);
const to = this.zoomPhoto >= 0 ? -1 : Math.min(this.layouts.length - 1, (near < 0 ? this.layouts.length - 1 : near) + 2);
if (this.zoomPhoto >= 0) {
const layout = this.layouts[this.zoomPhoto];
const w = layout.width * this.zoom;
if (this.loader(this.zoomPhoto).draw({
g,
originX: this.zx * dpr,
originY: this.zy * dpr,
width: w * dpr,
height: layout.height * this.zoom * dpr,
canvasWidth: cw,
canvasHeight: ch,
displayWidth: w,
dpr: this.levelDpr(),
now,
})) {
again = true;
}
}
for (let i = from; i <= to; i++) {
const layout = this.layouts[i];
if (layout.left + layout.width < left || layout.left > right) {