Part VI — The Blueprint · Lesson 11.9
HLD – Diagram Tools (Excalidraw)
In one lineA canvas render loop plus an operation log — undo, collaboration, and persistence all come from the log.
Where you've seen itDragging a shape smoothly while someone else edits the same diagram in real time.
Diagram
| Step 1–2 | Scope and scale |
|---|---|
| In scope | draw shapes · select/move/resize · undo · export · collab |
| Out of scope | version history UI · comments · templates · auth |
| Scale | ~5k elements per board · 60fps interaction required · 2–10 concurrent editors |
6b · DEEP DIVE — COLLABORATION AND SPATIAL LOOKUP
collab WebSocket (2.4) · per-element last-write-wins is
adequate here (unlike text — see 11.11)
cursors broadcast at ~20Hz, throttled, never persisted
hit-test naive: check 5,000 shapes per mousemove ✗
spatial index (quadtree/R-tree): check ~10 ✓
rebuild the index on commit, not during dragHow it works
- Canvas with layered surfaces, redrawing only the layer that changed.
- Model elements as plain data (type, x, y, w, h, style, version) — rendering is a pure function of that array.
- Every mutation is an operation appended to a log; undo inverts and re-applies.
- Spatial index for hit testing and viewport culling — draw only what's on screen.
requestAnimationFramerender loop, coalescing pointer events so a fast mouse doesn't trigger extra frames.
Trade-offs
| Choice | Cost |
|---|---|
| Canvas | Fast; you rebuild accessibility and text selection |
| SVG/DOM | Free a11y and hit testing; dies past ~1k nodes |
| Operation log | Undo + sync + persistence; more memory |
| Last-write-wins | Simple; occasional lost edit on the same shape |
Accessibility (8.1): canvas is opaque to screen readers. Maintain a parallel DOM list of elements, keyboard shortcuts for select/move, and text alternatives — this is the honest weakness of canvas apps and worth naming yourself.
Interview angle
"Dragging gets slow with 5,000 shapes. What's wrong?"
- You're probably redrawing the entire canvas per pointer event — split into static and active layers so a drag redraws one shape.
- Hit testing is likely linear per mousemove; add a quadtree and cull to the viewport.
- Coalesce pointer events into one
requestAnimationFrametick; browsers deliver far more of them than you can render.
Recap
- Layered canvases: redraw the smallest surface that changed.
- The operation log gives you undo, collaboration, and persistence from one structure.
- Spatial indexing turns hit testing from O(n) into O(log n).