import type { View } from "./geometry"; import { PhotoLoader, type LoaderEnv } from "./loader"; import type { Pyramid } from "./types"; export type StripEnv = Omit; export interface StripSize { width: number; closed: number; open: number; dpr: number; } interface Geometry { width: number; closed: number; open: number; dpr: number; coverWidth: number; coverHeight: number; x: number; y: number; band: number; } const settleMs = 260; export class Strip { readonly loader: PhotoLoader; private canvas: HTMLCanvasElement | null = null; private g: CanvasRenderingContext2D | null = null; private geometry: Geometry | null = null; private expanded = false; private canvasOpen = false; private near = false; private settleTimer = 0; private frame = 0; constructor( readonly element: HTMLElement, readonly cover: Pyramid, private readonly focusY: number, poolUrl: string, env: StripEnv, private readonly onStatus: (strip: Strip) => void, ) { this.loader = new PhotoLoader(cover, poolUrl, { ...env, requestDraw: () => this.requestDraw(), onStatus: () => this.onStatus(this), }); } get isOpen(): boolean { return this.expanded; } get isNear(): boolean { return this.near; } layout(size: StripSize): void { const cover = this.cover; const scale = Math.max(size.width / cover.width, size.open / cover.height); const coverWidth = cover.width * scale; const coverHeight = cover.height * scale; const y = (size.open - coverHeight) / 2; const centre = y + this.focusY * coverHeight; const band = Math.max(0, Math.min(size.open - size.closed, centre - size.closed / 2)); const previous = this.geometry; this.geometry = { width: size.width, closed: size.closed, open: size.open, dpr: size.dpr, coverWidth, coverHeight, x: (size.width - coverWidth) / 2, y, band, }; if (this.canvas) { this.sizeCanvas(this.canvasOpen); this.drawNow(); } if ( this.near && previous && (previous.width !== size.width || previous.closed !== size.closed || previous.open !== size.open || previous.dpr !== size.dpr) ) { this.startSession(); } } attach(): void { if (this.canvas) { return; } const canvas = document.createElement("canvas"); canvas.className = "strip-canvas"; canvas.setAttribute("aria-hidden", "true"); const g = canvas.getContext("2d", { alpha: false }); if (!g) { return; } this.canvas = canvas; this.g = g; this.element.prepend(canvas); this.sizeCanvas(this.expanded); this.drawNow(); } detach(): void { if (!this.canvas) { return; } cancelAnimationFrame(this.frame); this.frame = 0; this.canvas.width = 0; this.canvas.height = 0; this.canvas.remove(); this.canvas = null; this.g = null; } setNear(near: boolean): void { if (near === this.near) { return; } this.near = near; if (near) { this.startSession(); } else { this.loader.stop(); } } setOpen(open: boolean): void { if (open === this.expanded) { return; } this.expanded = open; this.element.classList.toggle("is-open", open); clearTimeout(this.settleTimer); const geometry = this.geometry; const canvas = this.canvas; if (open) { if (canvas && this.canvasOpen) { canvas.style.transition = ""; canvas.style.transform = "translate3d(0,0,0)"; } else if (canvas && geometry) { this.sizeCanvas(true); canvas.style.transition = "none"; canvas.style.transform = "translate3d(0," + -geometry.band + "px,0)"; this.drawNow(); requestAnimationFrame(() => { requestAnimationFrame(() => { if (this.expanded && this.canvas === canvas) { canvas.style.transition = ""; canvas.style.transform = "translate3d(0,0,0)"; } }); }); } } else if (canvas && geometry) { canvas.style.transition = ""; canvas.style.transform = "translate3d(0," + -geometry.band + "px,0)"; this.settleTimer = window.setTimeout(() => { if (!this.expanded && this.canvas === canvas) { this.sizeCanvas(false); this.drawNow(); } }, settleMs); } if (this.near) { this.startSession(); } } private sizeCanvas(open: boolean): void { const canvas = this.canvas; const geometry = this.geometry; if (!canvas || !geometry) { return; } this.canvasOpen = open; const height = open ? geometry.open : geometry.closed; const width = Math.max(1, Math.round(geometry.width * geometry.dpr)); const deviceHeight = Math.max(1, Math.round(height * geometry.dpr)); if (canvas.width !== width || canvas.height !== deviceHeight) { canvas.width = width; canvas.height = deviceHeight; } canvas.style.height = height + "px"; if (!open) { canvas.style.transition = "none"; canvas.style.transform = "translate3d(0,0,0)"; } } view(): View | null { const geometry = this.geometry; if (!geometry) { return null; } const top = this.expanded ? 0 : geometry.band; const height = this.expanded ? geometry.open : geometry.closed; return { displayWidth: geometry.coverWidth, u0: Math.max(0, -geometry.x / geometry.coverWidth), u1: Math.min(1, (geometry.width - geometry.x) / geometry.coverWidth), v0: Math.max(0, (top - geometry.y) / geometry.coverHeight), v1: Math.min(1, (top + height - geometry.y) / geometry.coverHeight), }; } startSession(): void { const view = this.view(); if (!view || !this.geometry) { return; } this.loader.start(view, this.geometry.dpr, () => undefined); } requestDraw(): void { if (this.frame === 0 && this.canvas) { this.frame = requestAnimationFrame((now) => { this.frame = 0; if (this.draw(now)) { this.requestDraw(); } }); } } private drawNow(): void { if (this.draw(performance.now())) { this.requestDraw(); } } private draw(now: number): boolean { const g = this.g; const canvas = this.canvas; const geometry = this.geometry; if (!g || !canvas || !geometry) { return false; } g.setTransform(1, 0, 0, 1, 0, 0); g.globalAlpha = 1; g.fillStyle = "#111114"; g.fillRect(0, 0, canvas.width, canvas.height); const shift = this.canvasOpen ? 0 : -geometry.band; return this.loader.draw({ g, originX: geometry.x * geometry.dpr, originY: (geometry.y + shift) * geometry.dpr, width: geometry.coverWidth * geometry.dpr, height: geometry.coverHeight * geometry.dpr, canvasWidth: canvas.width, canvasHeight: canvas.height, displayWidth: geometry.coverWidth, dpr: geometry.dpr, now, }); } }