gitflarePublicPartly agent-ready50
cf-durable-whiteboard
Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
6eb4fbdOct 20, 2026
Keep board zoom and pan stable across reloads
6eb4fbdimport { bounds, allBounds } from "./viewport.js";export function drawShape(c, s, ink = "#e8edf7") { c.strokeStyle = c.fillStyle = s.colour || ink; c.lineWidth = s.size || 3; c.lineCap = c.lineJoin = "round"; if (s.kind === "pen") { const p = s.points; c.beginPath(); c.moveTo(p[0], p[1]); if (p.length === 2) c.lineTo(p[0] + 0.01, p[1]); for (let i = 2; i < p.length; i += 2) c.lineTo(p[i], p[i + 1]); c.stroke(); } else if (s.kind === "rect") { const b = bounds(s); c.strokeRect(b.x, b.y, b.w, b.h); } else if (s.kind === "ellipse") { const b = bounds(s); c.beginPath(); c.ellipse(b.x + b.w / 2, b.y + b.h / 2, b.w / 2, b.h / 2, 0, 0, 7); c.stroke(); } else if (s.kind === "arrow") { const x2 = s.x + s.w, y2 = s.y + s.h, a = Math.atan2(s.h, s.w), L = 10 + (s.size || 3) * 2; c.beginPath(); c.moveTo(s.x, s.y); c.lineTo(x2, y2); c.moveTo(x2 - L * Math.cos(a - 0.45), y2 - L * Math.sin(a - 0.45)); c.lineTo(x2, y2); c.lineTo(x2 - L * Math.cos(a + 0.45), y2 - L * Math.sin(a + 0.45)); c.stroke(); } else if (s.kind === "note") drawNote(c, s);}function drawNote(c, s) { const b = bounds(s); c.fillStyle = s.colour || "#ffd166"; c.fillRect(b.x, b.y, b.w, b.h); c.fillStyle = "#12161f"; c.font = "16px system-ui, sans-serif"; c.textBaseline = "top"; let y = b.y + 10; for (const para of String(s.text || "").split("\n")) { let line = ""; for (const w of para.split(" ")) { const t = line ? line + " " + w : w; if (c.measureText(t).width > b.w - 20 && line) { c.fillText(line, b.x + 10, y); y += 20; line = w; } else line = t; } c.fillText(line, b.x + 10, y); y += 20; if (y > b.y + b.h) break; }}export function drawBoard(c, shapes, view, w, h, sel) { c.setTransform(devicePixelRatio, 0, 0, devicePixelRatio, 0, 0); c.clearRect(0, 0, w, h); c.setTransform(devicePixelRatio * view.k, 0, 0, devicePixelRatio * view.k, devicePixelRatio * view.x, devicePixelRatio * view.y); const list = [...shapes].sort((a, b) => (a.z ?? 0) - (b.z ?? 0)); for (const s of list) drawShape(c, s); if (sel) { const b = bounds(sel); c.save(); c.strokeStyle = "#3df0ff"; c.lineWidth = 1.5 / view.k; c.setLineDash([6 / view.k, 4 / view.k]); c.strokeRect(b.x - 4, b.y - 4, b.w + 8, b.h + 8); c.restore(); }}export function exportPng(shapes, mode, view, w, h, dark) { const list = [...shapes].sort((a, b) => (a.z ?? 0) - (b.z ?? 0)); let v = view, W = w, H = h; const bb = allBounds(list); if (mode === "all" && bb) { const pad = 32; W = Math.ceil(bb.w + 2 * pad); H = Math.ceil(bb.h + 2 * pad); v = { k: 1, x: pad - bb.x, y: pad - bb.y }; } const cv = document.createElement("canvas"); cv.width = W; cv.height = H; const c = cv.getContext("2d"); c.fillStyle = dark ? "#0f1420" : "#f6f4ee"; c.fillRect(0, 0, W, H); c.translate(v.x, v.y); c.scale(v.k, v.k); for (const s of list) drawShape(c, s, dark ? "#e8edf7" : "#1b2233"); return new Promise((res) => cv.toBlob(res, "image/png"));}