gitflarePublicPartly agent-ready40
sketch-rush
Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
dbe1e99Oct 23, 2026
Add the Apache-2.0 licence
dbe1e99export const BOARD_W = 1000, BOARD_H = 750;let seq = 0; /** Pointer input. opts: enabled(), onStrokeStart(id), onBatch(id, flat), onStrokeEnd(id), onMove(x,y,on) *//** Ink taper: pull the first/last points of a stroke slightly inward (drawer-side, normal points). */export function taper(pts, speed) { const n = pts.length; if (n < 8) return pts; const k = Math.min(0.5, 0.15 + speed * 0.1); pts[0] = Math.round(pts[0] + (pts[2] - pts[0]) * k); pts[1] = Math.round(pts[1] + (pts[3] - pts[1]) * k); return pts;} export function bindInput(canvas, opts) { canvas.style.touchAction = "none"; let active = -1, id = 0, buf = [], timer = 0, lx = -1, ly = -1, lt = 0; let rect = canvas.getBoundingClientRect(); const refresh = () => { rect = canvas.getBoundingClientRect(); }; if (typeof ResizeObserver !== "undefined") new ResizeObserver(refresh).observe(canvas); const pos = (e) => { const s = Math.min(rect.width / BOARD_W, rect.height / BOARD_H) || 1; const x = Math.round((e.clientX - rect.left - (rect.width - BOARD_W * s) / 2) / s); const y = Math.round((e.clientY - rect.top - (rect.height - BOARD_H * s) / 2) / s); return [Math.max(0, Math.min(BOARD_W, x)), Math.max(0, Math.min(BOARD_H, y))]; }; const add = (e) => { const [x, y] = pos(e); if (x === lx && y === ly) return; const t = e.timeStamp || 0; if (opts.onSpeed && lx >= 0) opts.onSpeed(Math.hypot(x - lx, y - ly) / Math.max(1, t - lt)); lt = t; lx = x; ly = y; buf.push(x, y); if (opts.onMove) opts.onMove(x, y, true); }; let first = true; const flush = () => { if (first && buf.length >= 8) { taper(buf, 0.5); first = false; } while (buf.length) opts.onBatch(id, buf.splice(0, 120)); // <= 60 points per message }; const end = (e) => { if (e.pointerId !== active) return; active = -1; clearInterval(timer); flush(); if (opts.onSpeed) opts.onSpeed(0); if (opts.onStrokeEnd) opts.onStrokeEnd(id); }; canvas.addEventListener("pointerdown", (e) => { if ((e.button > 0 && e.pointerType === "mouse") || !opts.enabled()) return; if (active !== -1) end({ pointerId: active }); e.preventDefault(); refresh(); active = e.pointerId; try { canvas.setPointerCapture(active); } catch {} id = ((Date.now() & 0xffffff) * 16 + (++seq & 15)) | 0; lx = ly = -1; buf = []; first = true; if (opts.onStrokeStart) opts.onStrokeStart(id); add(e); if (buf.length === 2) buf.push(buf[0], buf[1]); timer = setInterval(flush, 50); }); canvas.addEventListener("pointermove", (e) => { if (e.pointerId === active) { const evs = e.getCoalescedEvents ? e.getCoalescedEvents() : null; if (evs && evs.length) for (let i = 0; i < evs.length; i++) add(evs[i]); else add(e); } else if (opts.onMove && e.pointerType === "mouse") { const [x, y] = pos(e); opts.onMove(x, y, opts.enabled()); } }); canvas.addEventListener("pointerup", end); canvas.addEventListener("pointercancel", end); canvas.addEventListener("pointerleave", (e) => { if (opts.onMove && e.pointerType === "mouse") opts.onMove(0, 0, false); }); return { flush };}