Skip to content

gitflarePublicPartly agent-ready50

cf-durable-whiteboard

Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.

Keep board zoom and pan stable across reloads6eb4fbd
JavaScript68 lines4.3 KB
import { msg } from "./protocol.js";import { toWorld, zoomAt, panBy, topHit, translate } from "./viewport.js";export function createPointerHandler(cv, api) {  const { st } = api;  const ptrs = new Map();  let drag = null, lastCur = 0, pinch = null;  const pos = (e) => { const r = cv.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; };  const world = (e) => { const p = pos(e); return toWorld(st.view, p.x, p.y); };  const shapes = () => [...st.shapes.values()];  function editNote(s) {    const ta = document.createElement("textarea"); ta.className = "note-edit"; ta.value = s.text || ""; ta.setAttribute("aria-label", "Sticky note text"); ta.maxLength = 500;    const b = { w: s.w || 160, h: s.h || 120 };    Object.assign(ta.style, { left: s.x * st.view.k + st.view.x + "px", top: s.y * st.view.k + st.view.y + "px", width: b.w * st.view.k + "px", height: b.h * st.view.k + "px", background: s.colour });    cv.parentElement.append(ta); ta.focus();    let done = false;    const fin = () => { if (done) return; done = true; const t = ta.value; ta.remove(); api.update({ ...s, text: t }); };    ta.onblur = fin; ta.onkeydown = (e) => { if (e.key === "Escape") ta.blur(); };  }  cv.addEventListener("dblclick", (e) => { const w = world(e), h = topHit(shapes(), w.x, w.y, 4); if (h && h.kind === "note") editNote(h); });  cv.addEventListener("pointerdown", (e) => {    cv.setPointerCapture(e.pointerId); ptrs.set(e.pointerId, pos(e));    if (ptrs.size === 2) { drag = null; st.preview = null; const [a, b] = [...ptrs.values()]; pinch = { d: Math.hypot(a.x - b.x, a.y - b.y), mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2 }; return; }    if (ptrs.size > 2) return;    const p = pos(e), w = world(e);    if (st.tool === "pan" || st.space || e.button === 1) { drag = { t: "pan", x: p.x, y: p.y }; return; }    const common = { id: api.shapeId(), colour: st.colour, size: st.size };    if (st.tool === "pen") { st.preview = { ...common, kind: "pen", points: [w.x, w.y] }; drag = { t: "pen" }; }    else if (["rect", "ellipse", "arrow"].includes(st.tool)) { st.preview = { ...common, kind: st.tool, x: w.x, y: w.y, w: 0, h: 0, size: Math.min(st.size, 5) }; drag = { t: "shape", ox: w.x, oy: w.y }; }    else if (st.tool === "note") {      const s = { id: common.id, kind: "note", x: w.x - 80, y: w.y - 60, w: 160, h: 120, text: "", colour: "#ffd166" };      api.add(s); editNote(s);    } else if (st.tool === "eraser") { drag = { t: "erase" }; erase(w); }    else if (st.tool === "select") {      const h = topHit(shapes(), w.x, w.y, 6 / st.view.k); st.sel = h || null;      drag = h ? { t: "move", orig: h, ox: w.x, oy: w.y } : null; api.render();    }  });  function erase(w) { const h = topHit(shapes(), w.x, w.y, 6 / st.view.k); if (h) api.del(h.id); }  cv.addEventListener("pointermove", (e) => {    const p = pos(e), w = world(e), now = performance.now();    if (now - lastCur > 50) { lastCur = now; api.send(msg.cursor(w.x, w.y)); }    if (!ptrs.has(e.pointerId)) return;    const prev = ptrs.get(e.pointerId); ptrs.set(e.pointerId, p);    if (pinch && ptrs.size === 2) {      const [a, b] = [...ptrs.values()], d = Math.hypot(a.x - b.x, a.y - b.y), mx = (a.x + b.x) / 2, my = (a.y + b.y) / 2;      st.view = panBy(zoomAt(st.view, mx, my, d / pinch.d), mx - pinch.mx, my - pinch.my); pinch = { d, mx, my }; api.render(); return;    }    if (!drag) return;    if (drag.t === "pan") { st.view = panBy(st.view, p.x - prev.x, p.y - prev.y); }    else if (drag.t === "pen" && st.preview.points.length < 1990) st.preview.points.push(w.x, w.y);    else if (drag.t === "shape") Object.assign(st.preview, { w: w.x - drag.ox, h: w.y - drag.oy });    else if (drag.t === "erase") erase(w);    else if (drag.t === "move") { api.commit({ type: "update", shape: translate(drag.orig, w.x - drag.ox, w.y - drag.oy) }); }    api.render();  });  function end(e) {    if (!ptrs.delete(e.pointerId)) return;    if (ptrs.size < 2) pinch = null;    const d = drag; drag = null; if (!d) return;    const pv = st.preview; st.preview = null;    if (d.t === "pen" && pv) api.add(pv);    else if (d.t === "shape" && pv && Math.hypot(pv.w, pv.h) > 4) api.add(pv);    else if (d.t === "move") { const cur = st.shapes.get(d.orig.id); if (cur) api.update(cur); }    api.render();  }  cv.addEventListener("pointerup", end); cv.addEventListener("pointercancel", end);}