The loader now measures throughput from whole tile batches, not just the two probe tiles, and after a session settles inside its deadline it tops up in the background: the needed level if the budget cut it short, then one level above it on capable devices with a fast connection (drawn downscaled), within time and memory caps and never under Save-Data. The chop in the album end card and the page footer is replaced by the LUCIO loader in smoke from lucioIP, falling back to the SVG loader without WebGL. sync-brand.sh now copies lucio-loaders.js and defaults to ../lucioIP. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1205 lines
37 KiB
TypeScript
1205 lines
37 KiB
TypeScript
import { mountSignoffMark, mountWordmark } from "./brand";
|
|
import { CaptionPanel, setLang } from "./caption";
|
|
import type { View } from "./geometry";
|
|
import { guardLink, type GuardOptions } from "./gate";
|
|
import { BusyIndicator, ProgressLine } from "./indicator";
|
|
import { fixedKeys, type AlbumKeys } from "./keys";
|
|
import { PhotoLoader, type LoaderEnv } from "./loader";
|
|
import { Strip } from "./strip";
|
|
import { poolUrl, type Manifest, type Settings, type SiteIndex } from "./types";
|
|
|
|
interface PhotoLayout {
|
|
scale: number;
|
|
width: number;
|
|
height: number;
|
|
section: number;
|
|
slides: number;
|
|
start: number;
|
|
left: number;
|
|
top: number;
|
|
}
|
|
|
|
interface Stop {
|
|
photo: number;
|
|
slide: number;
|
|
camera: number;
|
|
}
|
|
|
|
interface Sample {
|
|
t: number;
|
|
x: number;
|
|
}
|
|
|
|
interface Tween {
|
|
from: number;
|
|
to: number;
|
|
start: number;
|
|
duration: number;
|
|
}
|
|
|
|
const gutter = 24;
|
|
const tapDistance = 10;
|
|
const tapMs = 300;
|
|
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;
|
|
const bx = 3 * (x2 - x1) - cx;
|
|
const ax = 1 - cx - bx;
|
|
const cy = 3 * y1;
|
|
const by = 3 * (y2 - y1) - cy;
|
|
const ay = 1 - cy - by;
|
|
const sampleX = (s: number) => ((ax * s + bx) * s + cx) * s;
|
|
const sampleY = (s: number) => ((ay * s + by) * s + cy) * s;
|
|
const slopeX = (s: number) => (3 * ax * s + 2 * bx) * s + cx;
|
|
return (t: number) => {
|
|
if (t <= 0) {
|
|
return 0;
|
|
}
|
|
if (t >= 1) {
|
|
return 1;
|
|
}
|
|
let s = t;
|
|
for (let i = 0; i < 8; i++) {
|
|
const err = sampleX(s) - t;
|
|
const d = slopeX(s);
|
|
if (Math.abs(err) < 1e-5) {
|
|
return sampleY(s);
|
|
}
|
|
if (Math.abs(d) < 1e-6) {
|
|
break;
|
|
}
|
|
s -= err / d;
|
|
}
|
|
let lo = 0;
|
|
let hi = 1;
|
|
s = t;
|
|
for (let i = 0; i < 24; i++) {
|
|
const x = sampleX(s);
|
|
if (Math.abs(x - t) < 1e-5) {
|
|
break;
|
|
}
|
|
if (x < t) {
|
|
lo = s;
|
|
} else {
|
|
hi = s;
|
|
}
|
|
s = (lo + hi) / 2;
|
|
}
|
|
return sampleY(s);
|
|
};
|
|
}
|
|
|
|
const ease = cubicBezier(0.16, 1, 0.3, 1);
|
|
|
|
export class Viewer {
|
|
private readonly canvas: HTMLCanvasElement;
|
|
private readonly fade: HTMLCanvasElement;
|
|
private readonly g: CanvasRenderingContext2D;
|
|
private readonly caption: CaptionPanel;
|
|
private readonly endCard: HTMLElement;
|
|
private readonly nextLink: HTMLAnchorElement;
|
|
private readonly nextFrame: HTMLElement;
|
|
private readonly indicator: BusyIndicator;
|
|
private readonly progress: ProgressLine;
|
|
private readonly homeMark: HTMLElement | null;
|
|
private readonly signoffMark: HTMLElement;
|
|
private nextStrip: Strip | null = null;
|
|
private nextSlug = "";
|
|
private idleTimer = 0;
|
|
private announceTimer = 0;
|
|
private suspended = false;
|
|
private readonly announcer: HTMLElement;
|
|
private readonly reducedMotion: MediaQueryList;
|
|
private readonly loaders: Array<PhotoLoader | undefined>;
|
|
private layouts: PhotoLayout[] = [];
|
|
private stops: Stop[] = [];
|
|
private endLeft = 0;
|
|
private vw = 0;
|
|
private vh = 0;
|
|
private dpr = 1;
|
|
private camera = 0;
|
|
private current = 0;
|
|
private tween: Tween | null = null;
|
|
private frame = 0;
|
|
private pointerId: number | null = null;
|
|
private pointerType = "";
|
|
private dragging = false;
|
|
private downX = 0;
|
|
private downY = 0;
|
|
private downT = 0;
|
|
private downCamera = 0;
|
|
private samples: Sample[] = [];
|
|
private hover = false;
|
|
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,
|
|
private readonly manifest: Manifest,
|
|
private readonly settings: Settings,
|
|
private readonly env: Omit<LoaderEnv, "requestDraw" | "onStatus">,
|
|
private readonly keys: AlbumKeys,
|
|
guard: GuardOptions | null,
|
|
) {
|
|
this.canvas = document.createElement("canvas");
|
|
this.canvas.className = "stage-canvas";
|
|
this.canvas.setAttribute("role", "img");
|
|
this.announcer = document.createElement("p");
|
|
this.announcer.className = "visually-hidden";
|
|
this.announcer.setAttribute("aria-live", "polite");
|
|
this.announcer.setAttribute("aria-atomic", "true");
|
|
this.fade = document.createElement("canvas");
|
|
this.fade.className = "stage-fade";
|
|
this.fade.setAttribute("aria-hidden", "true");
|
|
this.fade.width = 1;
|
|
this.fade.height = 1;
|
|
const g = this.canvas.getContext("2d", { alpha: false });
|
|
if (!g) {
|
|
throw new Error("canvas 2d unavailable");
|
|
}
|
|
this.g = g;
|
|
this.signoffMark = document.createElement("span");
|
|
this.signoffMark.className = "chop";
|
|
this.signoffMark.setAttribute("aria-hidden", "true");
|
|
this.endCard = this.buildEndCard();
|
|
this.nextLink = this.endCard.querySelector("a.next-album") as HTMLAnchorElement;
|
|
this.nextFrame = this.endCard.querySelector(".next-strip") as HTMLElement;
|
|
stage.prepend(this.canvas, this.fade);
|
|
stage.appendChild(this.endCard);
|
|
stage.appendChild(this.announcer);
|
|
this.caption = new CaptionPanel(stage);
|
|
this.progress = new ProgressLine(document.body);
|
|
this.indicator = new BusyIndicator(document.body);
|
|
this.homeMark = document.querySelector<HTMLElement>(".home-mark");
|
|
if (this.homeMark) {
|
|
mountWordmark(this.homeMark, false);
|
|
}
|
|
this.loaders = new Array(manifest.photos.length);
|
|
this.reducedMotion = matchMedia("(prefers-reduced-motion: reduce)");
|
|
this.measure();
|
|
this.current = this.stopForHash();
|
|
this.camera = this.stops[this.current].camera;
|
|
keys.onKeys((fresh) => this.onKeys(fresh));
|
|
if (guard) {
|
|
guardLink(this.nextLink, () => this.nextSlug, guard);
|
|
}
|
|
this.bind();
|
|
this.activate(-1);
|
|
this.requestDraw();
|
|
this.wake();
|
|
this.loadNextAlbum();
|
|
}
|
|
|
|
private buildEndCard(): HTMLElement {
|
|
const card = document.createElement("section");
|
|
card.className = "end-card";
|
|
card.setAttribute("aria-label", "End of album");
|
|
const next = document.createElement("a");
|
|
next.className = "next-album";
|
|
next.href = "/";
|
|
const frame = document.createElement("span");
|
|
frame.className = "next-strip";
|
|
const text = document.createElement("span");
|
|
text.className = "next-text";
|
|
const eyebrow = document.createElement("span");
|
|
eyebrow.className = "next-eyebrow";
|
|
eyebrow.textContent = "All albums";
|
|
const title = document.createElement("span");
|
|
title.className = "next-title";
|
|
text.append(eyebrow, title);
|
|
frame.appendChild(text);
|
|
next.appendChild(frame);
|
|
const signoff = document.createElement("div");
|
|
signoff.className = "signoff";
|
|
signoff.appendChild(this.signoffMark);
|
|
const signature = document.createElement("p");
|
|
signature.className = "signature";
|
|
signature.textContent = "— Lucio";
|
|
signoff.appendChild(signature);
|
|
card.append(next, signoff);
|
|
return card;
|
|
}
|
|
|
|
private loader(index: number): PhotoLoader {
|
|
let loader = this.loaders[index];
|
|
if (!loader) {
|
|
const photo = this.manifest.photos[index];
|
|
loader = new PhotoLoader(photo, poolUrl(this.manifest.build, this.manifest.slug, photo.hash), {
|
|
...this.env,
|
|
requestDraw: () => this.requestDraw(),
|
|
onStatus: (changed) => {
|
|
this.indicator.update(changed);
|
|
this.progress.update(changed);
|
|
},
|
|
});
|
|
this.loaders[index] = loader;
|
|
}
|
|
return loader;
|
|
}
|
|
|
|
private measure(): void {
|
|
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));
|
|
if (this.canvas.width !== cw || this.canvas.height !== ch) {
|
|
this.canvas.width = cw;
|
|
this.canvas.height = ch;
|
|
}
|
|
this.layouts = [];
|
|
this.stops = [];
|
|
let start = 0;
|
|
this.manifest.photos.forEach((photo, index) => {
|
|
const slides = Math.max(1, photo.slides || 1);
|
|
const scale = Math.min(this.vw / (photo.width / slides), this.vh / photo.height);
|
|
const width = photo.width * scale;
|
|
const height = photo.height * scale;
|
|
const section = width / slides;
|
|
const left = start + gutter / 2 + (this.vw - section) / 2;
|
|
this.layouts.push({ scale, width, height, section, slides, start, left, top: (this.vh - height) / 2 });
|
|
for (let slide = 0; slide < slides; slide++) {
|
|
this.stops.push({ photo: index, slide, camera: start + gutter / 2 + this.vw / 2 + slide * section });
|
|
}
|
|
start += this.vw + (slides - 1) * section + gutter;
|
|
});
|
|
this.endLeft = start + gutter / 2;
|
|
this.stops.push({ photo: -1, slide: 0, camera: this.endLeft + this.vw / 2 });
|
|
this.endCard.style.width = this.vw + "px";
|
|
this.layoutNextStrip();
|
|
}
|
|
|
|
private layoutNextStrip(): void {
|
|
if (!this.nextStrip) {
|
|
return;
|
|
}
|
|
const width = Math.max(1, Math.min(this.vw - 64, 1200));
|
|
const closed = Math.round(Math.min((width * 6) / 32, this.vh * 0.28));
|
|
this.nextFrame.style.width = width + "px";
|
|
this.nextFrame.style.height = closed + "px";
|
|
this.nextStrip.layout({ width, closed, open: Math.round((closed * 9) / 6), dpr: this.dpr });
|
|
}
|
|
|
|
private syncNextStrip(): void {
|
|
const strip = this.nextStrip;
|
|
if (!strip) {
|
|
return;
|
|
}
|
|
const stop = this.stops[this.current];
|
|
const near = stop.photo < 0 || stop.photo === this.manifest.photos.length - 1;
|
|
if (near) {
|
|
strip.attach();
|
|
} else {
|
|
strip.detach();
|
|
}
|
|
strip.setNear(near);
|
|
}
|
|
|
|
private wake(): void {
|
|
document.body.classList.remove("is-idle");
|
|
clearTimeout(this.idleTimer);
|
|
this.idleTimer = window.setTimeout(() => {
|
|
if (this.pointerId === null) {
|
|
document.body.classList.add("is-idle");
|
|
}
|
|
}, idleMs);
|
|
}
|
|
|
|
private stopForHash(): number {
|
|
const match = /^#(\d+)$/.exec(location.hash);
|
|
if (!match) {
|
|
return 0;
|
|
}
|
|
const photo = Math.min(this.manifest.photos.length, Math.max(1, Number(match[1]))) - 1;
|
|
const index = this.stops.findIndex((stop) => stop.photo === photo);
|
|
return index < 0 ? 0 : index;
|
|
}
|
|
|
|
private bind(): void {
|
|
const stage = this.stage;
|
|
stage.addEventListener("pointerdown", (e) => this.onPointerDown(e));
|
|
stage.addEventListener("pointermove", (e) => this.onPointerMove(e));
|
|
stage.addEventListener("pointerup", (e) => this.onPointerUp(e, false));
|
|
stage.addEventListener("pointercancel", (e) => this.onPointerUp(e, true));
|
|
stage.addEventListener("pointerleave", (e) => {
|
|
if (e.pointerType === "mouse" && this.pointerId === null) {
|
|
this.hover = false;
|
|
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) {
|
|
stage.addEventListener("contextmenu", (e) => e.preventDefault());
|
|
}
|
|
window.addEventListener("keydown", (e) => this.onKey(e));
|
|
window.addEventListener("hashchange", () => {
|
|
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);
|
|
this.fade.addEventListener("transitionend", () => {
|
|
if (this.fade.style.opacity === "0") {
|
|
this.fade.width = 1;
|
|
this.fade.height = 1;
|
|
}
|
|
});
|
|
}
|
|
|
|
private onResize(): void {
|
|
const before = this.vw + "x" + this.vh + "@" + this.dpr;
|
|
this.measure();
|
|
if (before === this.vw + "x" + this.vh + "@" + this.dpr) {
|
|
return;
|
|
}
|
|
this.clearZoom();
|
|
this.tween = null;
|
|
this.camera = this.stops[this.current].camera;
|
|
this.requestDraw();
|
|
clearTimeout(this.resizeTimer);
|
|
this.resizeTimer = window.setTimeout(() => this.startSession(), 200);
|
|
}
|
|
|
|
private onKey(e: KeyboardEvent): void {
|
|
if (e.altKey || e.ctrlKey || e.metaKey) {
|
|
return;
|
|
}
|
|
this.wake();
|
|
const target = e.target as HTMLElement | null;
|
|
if (target && (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName))) {
|
|
return;
|
|
}
|
|
switch (e.key) {
|
|
case "ArrowRight":
|
|
this.go(this.current + 1);
|
|
break;
|
|
case "ArrowLeft":
|
|
this.go(this.current - 1);
|
|
break;
|
|
case "Home":
|
|
this.go(0);
|
|
break;
|
|
case "End":
|
|
this.go(this.stops.length - 2);
|
|
break;
|
|
case "Escape":
|
|
if (this.zoomPhoto >= 0) {
|
|
this.go(this.current);
|
|
} else {
|
|
location.assign("/");
|
|
}
|
|
break;
|
|
case "i":
|
|
case "I":
|
|
this.pinned = !this.pinned;
|
|
this.updateCaption();
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
e.preventDefault();
|
|
}
|
|
|
|
private onPointerDown(e: PointerEvent): void {
|
|
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")) {
|
|
return;
|
|
}
|
|
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;
|
|
this.samples = [{ t: e.timeStamp, x: e.clientX }];
|
|
this.stage.setPointerCapture(e.pointerId);
|
|
}
|
|
|
|
private onPointerMove(e: PointerEvent): void {
|
|
if (e.pointerType === "mouse") {
|
|
this.wake();
|
|
}
|
|
if (this.pointerId === null) {
|
|
if (e.pointerType === "mouse") {
|
|
this.hover = this.inCaptionZone(e.clientX, e.clientY);
|
|
this.updateCaption();
|
|
}
|
|
return;
|
|
}
|
|
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;
|
|
const dy = e.clientY - this.downY;
|
|
if (!this.dragging && Math.hypot(dx, dy) >= tapDistance) {
|
|
this.dragging = true;
|
|
this.stage.classList.add("is-dragging");
|
|
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 });
|
|
while (this.samples.length > 2 && e.timeStamp - this.samples[0].t > 100) {
|
|
this.samples.shift();
|
|
}
|
|
this.camera = this.rubber(this.downCamera - dx);
|
|
this.requestDraw();
|
|
}
|
|
|
|
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;
|
|
}
|
|
if (this.stage.hasPointerCapture(e.pointerId)) {
|
|
this.stage.releasePointerCapture(e.pointerId);
|
|
}
|
|
this.pointerId = null;
|
|
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 || this.zoomPhoto >= 0) {
|
|
return;
|
|
}
|
|
let velocity = 0;
|
|
const first = this.samples[0];
|
|
const last = this.samples[this.samples.length - 1];
|
|
if (first && last && last.t > first.t && e.timeStamp - last.t < 100) {
|
|
velocity = (last.x - first.x) / (last.t - first.t);
|
|
}
|
|
let target = this.nearestStop(this.camera);
|
|
if (!cancelled && Math.abs(velocity) > flickSpeed) {
|
|
if (velocity < 0) {
|
|
const next = this.stops.findIndex((stop) => stop.camera > this.camera + 1);
|
|
target = next < 0 ? this.stops.length - 1 : next;
|
|
} else {
|
|
let previous = 0;
|
|
this.stops.forEach((stop, index) => {
|
|
if (stop.camera < this.camera - 1) {
|
|
previous = index;
|
|
}
|
|
});
|
|
target = previous;
|
|
}
|
|
}
|
|
if (this.pointerType === "mouse") {
|
|
this.hover = this.inCaptionZone(e.clientX, e.clientY);
|
|
}
|
|
this.go(target);
|
|
}
|
|
|
|
private onTap(e: PointerEvent): void {
|
|
const stop = this.stops[this.current];
|
|
if (stop.photo < 0 || this.zoomPhoto >= 0) {
|
|
return;
|
|
}
|
|
const rect = this.photoRect(stop.photo);
|
|
if (e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom) {
|
|
this.pinned = !this.pinned;
|
|
this.updateCaption();
|
|
}
|
|
}
|
|
|
|
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;
|
|
if (camera < min) {
|
|
return min - (min - camera) * rubberBand;
|
|
}
|
|
if (camera > max) {
|
|
return max + (camera - max) * rubberBand;
|
|
}
|
|
return camera;
|
|
}
|
|
|
|
private nearestStop(camera: number): number {
|
|
let best = 0;
|
|
this.stops.forEach((stop, index) => {
|
|
if (Math.abs(stop.camera - camera) < Math.abs(this.stops[best].camera - camera)) {
|
|
best = index;
|
|
}
|
|
});
|
|
return best;
|
|
}
|
|
|
|
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(() => {
|
|
this.current = target;
|
|
this.camera = to;
|
|
});
|
|
} else {
|
|
const distance = Math.abs(to - this.camera);
|
|
if (distance > 0.5) {
|
|
this.tween = {
|
|
from: this.camera,
|
|
to,
|
|
start: performance.now(),
|
|
duration: 240 + 200 * Math.min(1, distance / this.vw),
|
|
};
|
|
} else {
|
|
this.camera = to;
|
|
}
|
|
this.current = target;
|
|
}
|
|
if (target !== previous) {
|
|
this.activate(previous);
|
|
} else {
|
|
if (unzoomed) {
|
|
this.startSession();
|
|
}
|
|
this.updateCaption();
|
|
}
|
|
this.requestDraw();
|
|
}
|
|
|
|
private jump(index: number): void {
|
|
const previous = this.current;
|
|
this.clearZoom();
|
|
this.tween = null;
|
|
this.crossfade(() => {
|
|
this.current = index;
|
|
this.camera = this.stops[index].camera;
|
|
});
|
|
this.activate(previous);
|
|
this.requestDraw();
|
|
}
|
|
|
|
private crossfade(change: () => void): void {
|
|
const fade = this.fade;
|
|
if (fade.width !== this.canvas.width || fade.height !== this.canvas.height) {
|
|
fade.width = this.canvas.width;
|
|
fade.height = this.canvas.height;
|
|
}
|
|
const fg = fade.getContext("2d");
|
|
if (fg) {
|
|
fg.drawImage(this.canvas, 0, 0);
|
|
}
|
|
fade.style.transition = "none";
|
|
fade.style.opacity = "1";
|
|
change();
|
|
this.render(performance.now());
|
|
requestAnimationFrame(() => {
|
|
requestAnimationFrame(() => {
|
|
fade.style.transition = "";
|
|
fade.style.opacity = "0";
|
|
});
|
|
});
|
|
}
|
|
|
|
private activate(previousStop: number): void {
|
|
const stop = this.stops[this.current];
|
|
const before = previousStop >= 0 ? this.stops[previousStop] : null;
|
|
if (before && before.photo >= 0 && before.photo !== stop.photo) {
|
|
this.loaders[before.photo]?.stop();
|
|
}
|
|
this.loaders.forEach((loader, index) => {
|
|
if (loader && index !== stop.photo && index !== stop.photo + 1) {
|
|
loader.stopPrefetch();
|
|
}
|
|
});
|
|
if (stop.photo < 0) {
|
|
this.indicator.watch(null);
|
|
this.progress.watch(null);
|
|
}
|
|
this.describe(stop.photo, previousStop >= 0);
|
|
if (stop.photo >= 0) {
|
|
const photo = this.manifest.photos[stop.photo];
|
|
this.caption.set(photo, stop.photo, this.manifest.photos.length, this.manifest.show_exif);
|
|
if ((previousStop >= 0 || location.hash !== "") && location.hash !== "#" + (stop.photo + 1)) {
|
|
history.replaceState(history.state, "", "#" + (stop.photo + 1));
|
|
}
|
|
}
|
|
this.wantKeys();
|
|
if (stop.photo >= 0) {
|
|
this.startSession();
|
|
}
|
|
if (stop.photo < 0 || stop.photo === this.manifest.photos.length - 1) {
|
|
mountSignoffMark(this.signoffMark);
|
|
}
|
|
this.syncNextStrip();
|
|
this.updateCaption();
|
|
}
|
|
|
|
private describe(photoIndex: number, announce: boolean): void {
|
|
const total = this.manifest.photos.length;
|
|
let label: string;
|
|
if (photoIndex < 0) {
|
|
const next = this.nextLink.querySelector(".next-title")?.textContent ?? "";
|
|
label = "End of album." + (next ? " Next album: " + next + "." : "");
|
|
} else {
|
|
const photo = this.manifest.photos[photoIndex];
|
|
const name = photo.alt || photo.title || "";
|
|
label = "Photo " + (photoIndex + 1) + " of " + total + (name ? ": " + name : "");
|
|
}
|
|
this.canvas.setAttribute("aria-label", label);
|
|
clearTimeout(this.announceTimer);
|
|
if (announce) {
|
|
this.announceTimer = window.setTimeout(() => {
|
|
this.announcer.textContent = label;
|
|
}, 500);
|
|
}
|
|
}
|
|
|
|
suspend(hidden: boolean): void {
|
|
if (this.suspended) {
|
|
return;
|
|
}
|
|
this.suspended = true;
|
|
this.tween = null;
|
|
for (const loader of this.loaders) {
|
|
loader?.stop();
|
|
}
|
|
this.nextStrip?.loader.stop();
|
|
this.env.scheduler.cancelAll();
|
|
this.env.cache.trim(hidden ? 0 : this.env.cache.budget / 4);
|
|
if (hidden) {
|
|
this.nextStrip?.detach();
|
|
this.canvas.width = 1;
|
|
this.canvas.height = 1;
|
|
this.fade.width = 1;
|
|
this.fade.height = 1;
|
|
}
|
|
}
|
|
|
|
resume(): void {
|
|
if (!this.suspended) {
|
|
return;
|
|
}
|
|
this.suspended = false;
|
|
this.clearZoom();
|
|
this.canvas.width = 0;
|
|
this.measure();
|
|
this.camera = this.stops[this.current].camera;
|
|
this.syncNextStrip();
|
|
this.startSession();
|
|
this.requestDraw();
|
|
}
|
|
|
|
private wantKeys(): void {
|
|
const photos = this.manifest.photos;
|
|
const stop = this.stops[this.current];
|
|
const at = stop.photo < 0 ? photos.length - 1 : stop.photo;
|
|
const pick = (offsets: number[]): string[] => {
|
|
const out: string[] = [];
|
|
for (const offset of offsets) {
|
|
const i = at + offset;
|
|
if (i >= 0 && i < photos.length) {
|
|
out.push(photos[i].hash);
|
|
}
|
|
}
|
|
return out;
|
|
};
|
|
this.keys.want(pick([0, 1, -1, 2, 3]), pick([4, 5, 6, 7, -2, 8, 9]));
|
|
}
|
|
|
|
private onKeys(fresh: string[]): void {
|
|
if (this.suspended) {
|
|
return;
|
|
}
|
|
const photos = this.manifest.photos;
|
|
const got = (i: number) => i >= 0 && i < photos.length && fresh.includes(photos[i].hash);
|
|
const stop = this.stops[this.current];
|
|
if (this.zoomPhoto >= 0) {
|
|
if (got(this.zoomPhoto) && this.loader(this.zoomPhoto).stalled) {
|
|
this.startZoomSession();
|
|
}
|
|
} else if (stop.photo >= 0 && got(stop.photo) && this.loader(stop.photo).stalled) {
|
|
this.startSession();
|
|
} else if (stop.photo >= 0 && got(stop.photo + 1) && !this.loader(stop.photo).status.active) {
|
|
this.prefetchAround(this.current);
|
|
}
|
|
this.requestDraw();
|
|
}
|
|
|
|
private startSession(): void {
|
|
const stop = this.stops[this.current];
|
|
if (stop.photo < 0) {
|
|
return;
|
|
}
|
|
const index = this.current;
|
|
const loader = this.loader(stop.photo);
|
|
loader.start(
|
|
this.viewAt(stop),
|
|
this.dpr,
|
|
() => {
|
|
if (this.current === index) {
|
|
this.prefetchAround(index);
|
|
}
|
|
},
|
|
true,
|
|
);
|
|
this.indicator.watch(loader);
|
|
this.progress.watch(loader);
|
|
}
|
|
|
|
private prefetchAround(index: number): void {
|
|
const stop = this.stops[index];
|
|
const next = this.stops[index + 1];
|
|
if (!next || next.photo < 0) {
|
|
return;
|
|
}
|
|
if (next.photo === stop.photo) {
|
|
const loader = this.loader(stop.photo);
|
|
loader.prefetchLevel(this.viewAt(next), loader.finalLevel);
|
|
const following = this.stops.find((s) => s.photo === stop.photo + 1);
|
|
if (following) {
|
|
this.loader(following.photo).prefetchProbe(this.viewAt(following), this.dpr);
|
|
}
|
|
return;
|
|
}
|
|
this.loader(next.photo).prefetchProbe(this.viewAt(next), this.dpr);
|
|
}
|
|
|
|
private viewAt(stop: Stop): View {
|
|
const layout = this.layouts[stop.photo];
|
|
const left = stop.camera - this.vw / 2;
|
|
const a = Math.max(left, layout.left);
|
|
const b = Math.min(left + this.vw, layout.left + layout.width);
|
|
const c = Math.max(0, layout.top);
|
|
const d = Math.min(this.vh, layout.top + layout.height);
|
|
return {
|
|
displayWidth: layout.width,
|
|
u0: (a - layout.left) / layout.width,
|
|
u1: (b - layout.left) / layout.width,
|
|
v0: (c - layout.top) / layout.height,
|
|
v1: (d - layout.top) / layout.height,
|
|
};
|
|
}
|
|
|
|
private photoRect(index: number): { left: number; right: number; top: number; bottom: number } {
|
|
const layout = this.layouts[index];
|
|
const x = layout.left - (this.camera - this.vw / 2);
|
|
return {
|
|
left: Math.max(0, x),
|
|
right: Math.min(this.vw, x + layout.width),
|
|
top: Math.max(0, layout.top),
|
|
bottom: Math.min(this.vh, layout.top + layout.height),
|
|
};
|
|
}
|
|
|
|
private inCaptionZone(clientX: number, clientY: number): boolean {
|
|
const stop = this.stops[this.current];
|
|
if (stop.photo < 0) {
|
|
return false;
|
|
}
|
|
const bounds = this.stage.getBoundingClientRect();
|
|
const x = clientX - bounds.left;
|
|
const y = clientY - bounds.top;
|
|
const layout = this.layouts[stop.photo];
|
|
const rect = this.photoRect(stop.photo);
|
|
const zoneTop = layout.top + layout.height * (1 - captionZone);
|
|
return x >= rect.left && x <= rect.right && y >= zoneTop && y <= rect.bottom;
|
|
}
|
|
|
|
private updateCaption(): void {
|
|
const stop = this.stops[this.current];
|
|
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);
|
|
}
|
|
this.caption.show(visible);
|
|
document.body.classList.toggle("has-caption", visible);
|
|
}
|
|
|
|
private loadNextAlbum(): void {
|
|
if (this.indexRequested || !this.settings.indexUrl) {
|
|
return;
|
|
}
|
|
this.indexRequested = true;
|
|
fetch(this.settings.indexUrl, { credentials: "same-origin" })
|
|
.then((response) => (response.ok ? (response.json() as Promise<SiteIndex>) : Promise.reject(new Error("index"))))
|
|
.then((index) => this.showNextAlbum(index))
|
|
.catch(() => {
|
|
this.indexRequested = false;
|
|
});
|
|
}
|
|
|
|
private showNextAlbum(index: SiteIndex): void {
|
|
const albums = index.albums;
|
|
if (albums.length === 0) {
|
|
return;
|
|
}
|
|
const at = albums.findIndex((album) => album.slug === this.manifest.slug);
|
|
const next = albums[(at + 1) % albums.length];
|
|
if (!next || next.slug === this.manifest.slug) {
|
|
return;
|
|
}
|
|
this.nextLink.href = "/a/" + encodeURIComponent(next.slug) + "/";
|
|
this.nextSlug = next.slug;
|
|
const eyebrow = this.nextLink.querySelector(".next-eyebrow");
|
|
const title = this.nextLink.querySelector(".next-title");
|
|
if (eyebrow) {
|
|
eyebrow.textContent = "Next album" + (next.date ? " · " + next.date : "");
|
|
}
|
|
if (title) {
|
|
title.textContent = next.title;
|
|
setLang(title as HTMLElement, next.title);
|
|
}
|
|
if (next.cover) {
|
|
this.nextStrip = new Strip(
|
|
this.nextFrame,
|
|
next.cover,
|
|
next.collapsed_focus_y ?? 0.5,
|
|
poolUrl(index.build, next.slug, next.cover.hash),
|
|
{ ...this.env, keys: fixedKeys(index.gate.cover), tiles: index.tiles },
|
|
() => undefined,
|
|
);
|
|
this.nextLink.classList.add("has-strip");
|
|
this.layoutNextStrip();
|
|
this.syncNextStrip();
|
|
}
|
|
}
|
|
|
|
private requestDraw(): void {
|
|
if (this.frame === 0) {
|
|
this.frame = requestAnimationFrame((now) => {
|
|
this.frame = 0;
|
|
this.render(now);
|
|
});
|
|
}
|
|
}
|
|
|
|
private render(now: number): void {
|
|
let again = false;
|
|
if (this.tween) {
|
|
const t = Math.min(1, (now - this.tween.start) / this.tween.duration);
|
|
this.camera = this.tween.from + (this.tween.to - this.tween.from) * ease(t);
|
|
if (t >= 1) {
|
|
this.tween = null;
|
|
this.updateCaption();
|
|
} else {
|
|
again = true;
|
|
}
|
|
}
|
|
const g = this.g;
|
|
const cw = this.canvas.width;
|
|
const ch = this.canvas.height;
|
|
g.setTransform(1, 0, 0, 1, 0, 0);
|
|
g.globalAlpha = 1;
|
|
g.fillStyle = "#111114";
|
|
g.fillRect(0, 0, cw, ch);
|
|
const left = this.camera - this.vw / 2;
|
|
const right = left + this.vw;
|
|
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 = 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) {
|
|
continue;
|
|
}
|
|
if (this.loader(i).draw({
|
|
g,
|
|
originX: (layout.left - left) * dpr,
|
|
originY: layout.top * dpr,
|
|
width: layout.width * dpr,
|
|
height: layout.height * dpr,
|
|
canvasWidth: cw,
|
|
canvasHeight: ch,
|
|
displayWidth: layout.width,
|
|
dpr,
|
|
now,
|
|
})) {
|
|
again = true;
|
|
}
|
|
}
|
|
this.endCard.style.transform = "translate3d(" + Math.round(this.endLeft - left) + "px,0,0)";
|
|
if (this.caption.element.classList.contains("is-visible") && this.tween) {
|
|
this.updateCaption();
|
|
}
|
|
if (again) {
|
|
this.requestDraw();
|
|
}
|
|
}
|
|
}
|