Skip to content

gitflarePublicPartly agent-ready40

sketch-rush

Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.

Add the Apache-2.0 licencedbe1e99
JavaScript141 lines6.1 KB
import { createParticles } from "./fx.js"; export const BOARD_W = 1000, BOARD_H = 750;export const PALETTE = ["#111111", "#ffffff", "#ff3b3b", "#ff9f1c", "#ffe14d", "#35d96b", "#2f8bff", "#b44dff"];export const WIDTHS = [4, 9, 18];const BG = "#ffffff"; /** Draw points from index `from` (flat array) of one stroke onto ctx in board units. */export function drawSegment(ctx, pts, from, color, size) {  const n = pts.length;  if (n < 2) return;  ctx.strokeStyle = color; ctx.fillStyle = color; ctx.lineWidth = size;  if (from === 0 && n === 2) {    ctx.beginPath(); ctx.arc(pts[0], pts[1], size / 2, 0, 6.2832); ctx.fill(); return;  }  if (n < 4) return;  ctx.beginPath();  let i = from >= 4 ? from - 2 : 0; // start at previous midpoint  if (i === 0) ctx.moveTo(pts[0], pts[1]);  else ctx.moveTo((pts[i - 2] + pts[i]) * 0.5, (pts[i - 1] + pts[i + 1]) * 0.5);  for (; i + 3 < n; i += 2) {    ctx.quadraticCurveTo(pts[i], pts[i + 1], (pts[i] + pts[i + 2]) * 0.5, (pts[i + 1] + pts[i + 3]) * 0.5);  }  ctx.lineTo(pts[n - 2], pts[n - 1]);  ctx.stroke();} export function createRenderer(canvas, net) {  const ctx = canvas.getContext("2d");  const layer = document.createElement("canvas");  const lctx = layer.getContext("2d");  const parts = createParticles();  let dpr = 1, cw = 1, ch = 1, scale = 1, ox = 0, oy = 0;  let dirty = true, lastEpoch = -1, lastVersion = -1, lastCount = 0, lastTotal = 0;  let drawn = [], ids = []; // per-stroke drawn lengths (grown only on reset)  const cursor = { x: -1, y: -1, on: false, size: 9, color: PALETTE[0], erase: false };  const live = { pts: null, color: PALETTE[0], size: 9 };  let last = 0;   const setup = () => {    dpr = Math.min(2, window.devicePixelRatio || 1);    const r = canvas.getBoundingClientRect();    cw = Math.max(1, Math.round(r.width * dpr)); ch = Math.max(1, Math.round(r.height * dpr));    canvas.width = cw; canvas.height = ch;    scale = Math.min(cw / BOARD_W, ch / BOARD_H);    ox = (cw - BOARD_W * scale) / 2; oy = (ch - BOARD_H * scale) / 2;    layer.width = Math.max(1, Math.round(BOARD_W * scale)); layer.height = Math.max(1, Math.round(BOARD_H * scale));    dirty = true;  };  if (typeof ResizeObserver !== "undefined") new ResizeObserver(setup).observe(canvas);  setup();   const colorOf = (s) => (PALETTE[s.color] || PALETTE[0]);  const sizeOf = (s) => WIDTHS[s.size] || WIDTHS[1];   function fullRedraw(b) {    lctx.setTransform(1, 0, 0, 1, 0, 0);    lctx.fillStyle = BG; lctx.fillRect(0, 0, layer.width, layer.height);    lctx.setTransform(scale, 0, 0, scale, 0, 0);    lctx.lineCap = "round"; lctx.lineJoin = "round";    const list = b.strokes;    drawn.length = list.length;    for (let i = 0; i < list.length; i++) {      const s = list[i];      drawSegment(lctx, s.pts, 0, colorOf(s), sizeOf(s));      drawn[i] = s.pts.length; ids[i] = s.id;    }    lastEpoch = b.epoch; lastVersion = b.version; lastCount = list.length;    dirty = false;  }   function syncBoard() {    const b = net.board;    if (!b) return;    if (dirty || b.epoch !== lastEpoch) return fullRedraw(b);    if (b.version === lastVersion) return;    const list = b.strokes;    // Only appends keep a prefix identical; anything else (undo, replaced) redraws.    let ok = list.length >= lastCount;    for (let i = 0; ok && i < lastCount; i++) if (list[i].pts.length < drawn[i] || list[i].id !== ids[i]) ok = false;    if (!ok) return fullRedraw(b);    for (let i = 0; i < list.length; i++) {      const s = list[i], d = i < lastCount ? drawn[i] : 0;      if (s.pts.length > d) {        drawSegment(lctx, s.pts, d, colorOf(s), sizeOf(s));        drawn[i] = s.pts.length; ids[i] = s.id;      }    }    lastCount = list.length; lastVersion = b.version;  }   function frame(t) {    requestAnimationFrame(frame);    const dt = last ? Math.min(0.05, (t - last) / 1000) : 0.016;    last = t;    syncBoard();    ctx.setTransform(1, 0, 0, 1, 0, 0);    ctx.fillStyle = "#f6f0e4"; ctx.fillRect(0, 0, cw, ch);    ctx.save();    ctx.shadowColor = "rgba(40,30,20,0.28)"; ctx.shadowBlur = 14 * dpr; ctx.shadowOffsetY = 4 * dpr;    ctx.fillStyle = BG; ctx.fillRect(ox, oy, BOARD_W * scale, BOARD_H * scale);    ctx.restore();    ctx.drawImage(layer, ox, oy);    ctx.lineWidth = 1.5 * dpr; ctx.strokeStyle = "#1d1a16";    ctx.strokeRect(ox - 0.75 * dpr, oy - 0.75 * dpr, BOARD_W * scale + 1.5 * dpr, BOARD_H * scale + 1.5 * dpr);    ctx.save();    ctx.beginPath(); ctx.rect(ox, oy, BOARD_W * scale, BOARD_H * scale); ctx.clip();    ctx.setTransform(scale, 0, 0, scale, ox, oy);    ctx.lineCap = "round"; ctx.lineJoin = "round";    if (live.pts && live.pts.length >= 2) drawSegment(ctx, live.pts, 0, live.color, live.size);    if (cursor.on) {      ctx.lineWidth = 2; ctx.strokeStyle = cursor.erase ? "#555" : cursor.color;      if (cursor.erase) { ctx.fillStyle = "rgba(255,255,255,0.6)"; }      ctx.beginPath(); ctx.arc(cursor.x, cursor.y, cursor.size / 2 + 1, 0, 6.2832);      if (cursor.erase) ctx.fill();      ctx.stroke();    }    ctx.restore();    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);    parts.update(dt); parts.draw(ctx);  }  requestAnimationFrame(frame);   return {    resize: setup,    /** live local stroke / cursor hooks used by input wiring */    setLive(pts, color, size) { live.pts = pts; live.color = color; live.size = size; },    setCursor(x, y, on, size, color, erase) { cursor.x = x; cursor.y = y; cursor.on = on; cursor.size = size; cursor.color = color; cursor.erase = erase; },    fx(kind, a, b) {      let x = (a === undefined ? cw / dpr / 2 : a), y = (b === undefined ? ch / dpr / 3 : b);      if (kind === "correct" && a === undefined) {        const gi = typeof document !== "undefined" && document.querySelector("#guess, .guess, form input");        if (gi && gi.getBoundingClientRect) { const r = gi.getBoundingClientRect(), c = canvas.getBoundingClientRect(); x = r.left + r.width / 2 - c.left; y = r.top - c.top; }      }      if (kind === "correct") parts.burst(x, y, 40, 380);      else if (kind === "reveal") parts.burst(x, y, 24, 260);      else if (kind === "confetti") for (let k = 0; k < 5; k++) parts.burst((k + 0.5) * cw / dpr / 5, -10, 44, 160);    },  };}