Something went wrong. Try again.
r/place on the atmosphere, targeting cloudflare's free tier
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125// Renders the canvas with pan (drag) and zoom (wheel); a click without a drag// picks a pixel.
import type { CanvasConfig, Pixel } from "../shared/protocol.ts";
const MIN_ZOOM = 1;const MAX_ZOOM = 40;const DRAG_THRESHOLD_PX = 4;
export class CanvasView { private readonly container: HTMLElement; private readonly canvas = document.createElement("canvas"); private readonly ctx: CanvasRenderingContext2D; private readonly onPick: (x: number, y: number) => void; private image: ImageData | null = null; private rgba = new Uint32Array(0); // Palette indices, kept so a palette change can re-render. private indices = new Uint8Array(0); private colors = new Uint32Array(0); private zoom = 4; private panX = 0; private panY = 0;
constructor(container: HTMLElement, onPick: (x: number, y: number) => void) { this.container = container; this.onPick = onPick; this.ctx = this.canvas.getContext("2d")!; container.append(this.canvas); this.bindInput(); this.applyTransform(); }
setConfig(config: CanvasConfig): void { this.colors = Uint32Array.from(config.palette, toAbgr); if (this.canvas.width !== config.width || this.canvas.height !== config.height) { this.canvas.width = config.width; this.canvas.height = config.height; this.image = this.ctx.createImageData(config.width, config.height); this.rgba = new Uint32Array(this.image.data.buffer); this.indices = new Uint8Array(config.width * config.height); } this.render(); }
setPixels(indices: Uint8Array): void { this.indices.set(indices.subarray(0, this.indices.length)); this.render(); }
applyDiff(pixels: Pixel[]): void { if (!this.image) return; for (const { x, y, color } of pixels) { const i = y * this.canvas.width + x; this.indices[i] = color; this.rgba[i] = this.colors[color] ?? 0; } this.ctx.putImageData(this.image, 0, 0); }
private render(): void { if (!this.image) return; for (let i = 0; i < this.indices.length; i++) this.rgba[i] = this.colors[this.indices[i]] ?? 0; this.ctx.putImageData(this.image, 0, 0); }
private applyTransform(): void { this.canvas.style.transform = `translate(${this.panX}px, ${this.panY}px) scale(${this.zoom})`; }
private bindInput(): void { let drag: { startX: number; startY: number; panX: number; panY: number; moved: boolean } | null = null;
this.container.addEventListener("pointerdown", (e) => { drag = { startX: e.clientX, startY: e.clientY, panX: this.panX, panY: this.panY, moved: false }; this.container.setPointerCapture(e.pointerId); }); this.container.addEventListener("pointermove", (e) => { if (!drag) return; const dx = e.clientX - drag.startX; const dy = e.clientY - drag.startY; drag.moved ||= Math.hypot(dx, dy) > DRAG_THRESHOLD_PX; if (!drag.moved) return; this.panX = drag.panX + dx; this.panY = drag.panY + dy; this.applyTransform(); }); this.container.addEventListener("pointerup", (e) => { if (drag && !drag.moved) this.pick(e); drag = null; }); this.container.addEventListener( "wheel", (e) => { e.preventDefault(); const { mx, my } = this.local(e); const next = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, this.zoom * (e.deltaY < 0 ? 1.2 : 1 / 1.2))); // Keep the point under the cursor fixed. this.panX = mx - ((mx - this.panX) / this.zoom) * next; this.panY = my - ((my - this.panY) / this.zoom) * next; this.zoom = next; this.applyTransform(); }, { passive: false }, ); }
private pick(e: PointerEvent): void { const { mx, my } = this.local(e); const x = Math.floor((mx - this.panX) / this.zoom); const y = Math.floor((my - this.panY) / this.zoom); if (x >= 0 && y >= 0 && x < this.canvas.width && y < this.canvas.height) this.onPick(x, y); }
private local(e: MouseEvent): { mx: number; my: number } { const rect = this.container.getBoundingClientRect(); return { mx: e.clientX - rect.left, my: e.clientY - rect.top }; }}
// ImageData is RGBA bytes; read as little-endian u32 that's 0xAABBGGRR.function toAbgr(hex: string): number { const n = parseInt(hex.slice(1), 16); return ((0xff << 24) | ((n & 0xff) << 16) | (n & 0xff00) | ((n >> 16) & 0xff)) >>> 0;}