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
JavaScript56 lines3.1 KB
import { PALETTE, WIDTHS } from "./render.js";export { PALETTE, WIDTHS }; /** createToolbar(el, onChange) -> { state, destroy }. onChange(state) or onChange({action:'undo'|'clear'}) */export function createToolbar(el, onChange, options) {  const opts = options || {};  const minPx = (opts.touchSize | 0) || 44;  const state = { color: 0, size: 1, eraser: false, get colorIdx() { return this.eraser ? 1 : this.color; } };  el.textContent = ""; el.classList.add("toolbar");  const mk = (cls, label, fn, title) => {    const b = document.createElement("button");    b.type = "button"; b.className = cls; b.textContent = label || ""; b.title = title; b.setAttribute("aria-label", title);    b.style.minWidth = b.style.minHeight = minPx + "px";    if (cls === "swatch") { b.style.borderRadius = "50%"; b.style.width = b.style.height = Math.max(minPx, 40) + "px"; }    b.addEventListener("click", fn); el.appendChild(b); return b;  };  const sw = [], sz = [];  let er = null;  const refresh = () => {    const mark = (b, on) => {      b.classList.toggle("active", on); b.setAttribute("aria-pressed", on ? "true" : "false");      b.style.transform = on ? "translateY(-3px) scale(1.08)" : "";      b.style.boxShadow = on ? "0 0 0 3px #fff, 0 0 0 6px #ff6b00" : "";      b.style.borderColor = on ? "#ff6b00" : "#222";    };    sw.forEach((b, i) => mark(b, !state.eraser && state.color === i));    sz.forEach((b, i) => mark(b, state.size === i));    if (er) mark(er, state.eraser);  };  const change = () => { refresh(); onChange(state); };  PALETTE.forEach((c, i) => { const b = mk("swatch", "", () => { state.color = i; state.eraser = false; change(); }, "Colour " + (i + 1)); b.style.background = c; b.style.border = "3px solid #222"; sw.push(b); });  WIDTHS.forEach((w, i) => {    const b = mk("size", "", () => { state.size = i; change(); }, "Size " + (i + 1));    const d = document.createElement("i");    d.style.cssText = "display:block;margin:auto;border-radius:50%;background:currentColor;width:" + (6 + i * 6) + "px;height:" + (6 + i * 6) + "px";    b.appendChild(d); sz.push(b);  });  er = mk("tool eraser", "🧽", () => { state.eraser = !state.eraser; change(); }, "Eraser (E)");  mk("tool undo", "↶", () => onChange({ action: "undo" }), "Undo (Ctrl+Z)");  mk("tool clear", "🗑", () => onChange({ action: "clear" }), "Clear");  const onKey = (e) => {    const t = e.target && e.target.tagName;    if (t === "INPUT" || t === "TEXTAREA") return;    const k = e.key;    if ((e.ctrlKey || e.metaKey) && (k === "z" || k === "Z")) { e.preventDefault(); onChange({ action: "undo" }); }    else if (e.ctrlKey || e.metaKey || e.altKey) return;    else if (k >= "1" && k <= "8") { state.color = +k - 1; state.eraser = false; change(); }    else if (k === "[") { state.size = Math.max(0, state.size - 1); change(); }    else if (k === "]") { state.size = Math.min(2, state.size + 1); change(); }    else if (k === "e" || k === "E") { state.eraser = !state.eraser; change(); }  };  document.addEventListener("keydown", onKey);  refresh();  return { state, destroy() { document.removeEventListener("keydown", onKey); el.textContent = ""; } };}