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
JavaScript75 lines3.4 KB
export const MIN_ZOOM = 0.1, MAX_ZOOM = 4;export const clamp = (v, a, b) => Math.min(b, Math.max(a, v));export const makeView = () => ({ x: 0, y: 0, k: 1 });export const toWorld = (v, sx, sy) => ({ x: (sx - v.x) / v.k, y: (sy - v.y) / v.k });export const toScreen = (v, wx, wy) => ({ x: wx * v.k + v.x, y: wy * v.k + v.y });export function zoomAt(v, sx, sy, factor) {  const k = clamp(v.k * factor, MIN_ZOOM, MAX_ZOOM);  const w = toWorld(v, sx, sy);  return { k, x: sx - w.x * k, y: sy - w.y * k };}export const panBy = (v, dx, dy) => ({ ...v, x: v.x + dx, y: v.y + dy });export function bounds(s) {  if (s.kind === "pen") {    const p = s.points; let a = Infinity, b = Infinity, c = -Infinity, d = -Infinity;    for (let i = 0; i < p.length; i += 2) { a = Math.min(a, p[i]); c = Math.max(c, p[i]); b = Math.min(b, p[i + 1]); d = Math.max(d, p[i + 1]); }    const r = (s.size || 3) / 2;    return { x: a - r, y: b - r, w: c - a + 2 * r, h: d - b + 2 * r };  }  if (s.kind === "note") return { x: s.x, y: s.y, w: s.w || 160, h: s.h || 120 };  const x = Math.min(s.x, s.x + s.w), y = Math.min(s.y, s.y + s.h);  return { x, y, w: Math.abs(s.w), h: Math.abs(s.h) };}export function allBounds(shapes) {  let r = null;  for (const s of shapes) {    const b = bounds(s);    if (!r) { r = { ...b }; continue; }    const x = Math.min(r.x, b.x), y = Math.min(r.y, b.y);    r = { x, y, w: Math.max(r.x + r.w, b.x + b.w) - x, h: Math.max(r.y + r.h, b.y + b.h) - y };  }  return r;}export function fitView(b, width, height, pad = 48) {  if (!b) return makeView();  const k = clamp(Math.min((width - 2 * pad) / Math.max(b.w, 1), (height - 2 * pad) / Math.max(b.h, 1)), MIN_ZOOM, MAX_ZOOM);  return { k, x: (width - b.w * k) / 2 - b.x * k, y: (height - b.h * k) / 2 - b.y * k };}function segDist(px, py, ax, ay, bx, by) {  const dx = bx - ax, dy = by - ay, l = dx * dx + dy * dy;  const t = l ? clamp(((px - ax) * dx + (py - ay) * dy) / l, 0, 1) : 0;  return Math.hypot(px - ax - t * dx, py - ay - t * dy);}export function hitTest(s, x, y, tol = 6) {  if (s.kind === "pen") {    const p = s.points;    if (p.length === 2) return Math.hypot(x - p[0], y - p[1]) <= tol + (s.size || 3);    for (let i = 0; i + 3 < p.length; i += 2) if (segDist(x, y, p[i], p[i + 1], p[i + 2], p[i + 3]) <= tol + (s.size || 3) / 2) return true;    return false;  }  if (s.kind === "arrow") return segDist(x, y, s.x, s.y, s.x + s.w, s.y + s.h) <= tol;  const b = bounds(s);  return x >= b.x - tol && x <= b.x + b.w + tol && y >= b.y - tol && y <= b.y + b.h + tol;}export function topHit(shapes, x, y, tol) {  let best = null;  for (const s of shapes) if (hitTest(s, x, y, tol) && (!best || (s.z ?? 0) >= (best.z ?? 0))) best = s;  return best;}export function translate(s, dx, dy) {  if (s.kind === "pen") return { ...s, points: s.points.map((v, i) => v + (i % 2 ? dy : dx)) };  return { ...s, x: s.x + dx, y: s.y + dy };}export const viewKey = (id) => "wb:view:" + id;export const centeredView = (w, h) => ({ x: w / 2, y: h / 2, k: 1 });export function loadView(storage, id, w, h) {  try {    const v = JSON.parse(storage.getItem(viewKey(id)));    if (v && [v.x, v.y, v.k].every(Number.isFinite)) return { x: v.x, y: v.y, k: clamp(v.k, MIN_ZOOM, MAX_ZOOM) };  } catch {}  return centeredView(w, h);}export function saveView(storage, id, v) {  try { storage.setItem(viewKey(id), JSON.stringify({ x: v.x, y: v.y, k: v.k })); } catch {}}