How would you build a collaborative whiteboard in React?
Design a real-time collaborative whiteboard using canvas, WebSockets/WebRTC, CRDT syncing, undo/redo, and multi-user presence.
advancedUi patternsreactwhiteboardcanvascollaborationcrdt
Published: 11/23/2025
Updated: 11/23/2025
Advertisement
đź§© Scenario
You're building a Google Jamboard/Miro-style collaborative whiteboard that supports:
- Freehand drawing
- Shapes (rectangles, arrows, text)
- Real-time remote collaboration
- Cursor presence for all users
- Undo/redo stacks
- Zooming and panning
- Conflict-free syncing
đź§ Answer (Design Overview)
Core Components
-
Canvas Rendering Layer
- Use
<canvas>for freehand strokes - For shapes, either draw via canvas or overlay SVG for interactive nodes
- Use
-
Drawing Model
- Every stroke = list of points
{ x, y, pressure } - Shapes = objects with type + geometry
- Keep operations immutable
- Every stroke = list of points
-
Collaboration Layer
- Use WebSockets for real-time updates
- Use CRDT (e.g., Yjs, Automerge) for multi-user conflict-free syncing
- Broadcast:
{ type: 'stroke.append', strokeId, point; }
-
Cursor Presence
- Every client broadcasts cursor movements ~10–20 fps
- Render remote cursors at interpolated positions
-
Undo/Redo
- Store operations, not canvas bitmaps
- Undo = revert last operation
-
Performance
- Use
requestAnimationFrameto batch draws - Avoid re-rendering entire React tree — canvas is imperative
- Use
Tradeoffs:
- CRDTs handle conflict merging elegantly but add bundle size.
- Simple WebSocket broadcasting may lead to desync if not carefully managed.
🎯 Live Demo
🔍 Real-World Tips
- Use Yjs + WebRTC provider for peer-to-peer syncing.
- Implement brush smoothing (Chaikin algorithm).
- Use spatial indexing (R-tree) for selecting shapes.
- Use layers for organizing drawing elements.
- Batch updates with
requestAnimationFrame.
Quick Practice
- Add shape creation (rectangles, circles).
- Add a selection box with drag handles.
- Add collaboration cursors.
- Add undo/redo based on operation stack.
Summary
- Use Canvas for rendering and WebSockets/CRDTs for syncing.
- Treat interactions as operations, not pixels.
- Handle multi-user presence and cursor sharing.
- Keep rendering imperative for performance.
Frequently Asked Questions
Should I use Canvas or SVG for drawing?
Canvas is better for freehand drawing and performance; SVG is better for shape editing and DOM-interactive elements.
How do you handle multi-user conflicts?
Use CRDTs or OT to merge operations without overwriting each other.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement