Triamorph Systems

Full-Stack · 13 min read · Sep 03, 2024

Real-Time Collaborative Canvas: Architecting Figma-Grade Multi-User Software with WebSockets & Yjs

Learn how to engineer real-time multiplayer whiteboards and design tools with Yjs, WebSocket relays, R-tree spatial indexing, and 60 FPS HTML5 Canvas rendering.

By Hammad Haider

Core Engineering Takeaways

  • CRDTs (Conflict-Free Replicated Data Types) enable concurrent, offline-resilient document edits that mathematically converge to the same state across all peers.
  • Separate ephemeral presence state (live user cursors, viewport bounding boxes) from persistent document state to prevent database bloat.
  • Use R-tree spatial indexes (rbush) to render only shapes visible in the user’s immediate viewport, keeping 60 FPS performance across 50,000+ objects.

Building multi-user collaborative tools like Figma or Miro requires solving distributed concurrency, spatial rendering, and latency compensation simultaneously. Naive client-server architectures fail when two users move the same element simultaneously or when a temporary network hiccup overwrites off-screen shapes. This technical guide explains how to build production multi-user canvas applications using Yjs CRDTs and WebSockets.

1. Why CRDTs Beat Operational Transformation (OT)

Decentralized convergence without centralized coordination locks

Operational Transformation (OT) requires a single authoritative server to order and transform operations sequentially. Under network disconnection or high packet latency, OT stalls. CRDTs ensure that any operation can be applied locally immediately and synced out-of-order, always resolving to deterministic convergence.

2. Modeling Canvas Elements with Y.Doc and Y.Map

Synchronizing geometric nodes across WebSocket channels

Here is the core setup connecting a local HTML5 canvas state to a Yjs shared map and WebSocket provider.

canvas/multiplayer_sync.ts (typescript)
import * as Y from 'yjs';
import { WebsocketProvider } from 'y-websocket';

export interface CanvasShape {
  id: string;
  x: number;
  y: number;
  width: number;
  height: number;
  color: string;
}

export class CollaborativeCanvasEngine {
  private doc: Y.Doc;
  private shapesMap: Y.Map<CanvasShape>;
  private provider: WebsocketProvider;

  constructor(boardId: string, wsUrl: string) {
    this.doc = new Y.Doc();
    this.shapesMap = this.doc.getMap<CanvasShape>('shapes');
    this.provider = new WebsocketProvider(wsUrl, boardId, this.doc);

    this.shapesMap.observe((event) => {
      event.changes.keys.forEach((change, key) => {
        if (change.action === 'add' || change.action === 'update') {
          const updatedShape = this.shapesMap.get(key);
          console.log(`Shape ${key} synchronized:`, updatedShape);
        }
      });
    });
  }

  public updateShapePosition(id: string, x: number, y: number): void {
    const existing = this.shapesMap.get(id);
    if (existing) {
      this.shapesMap.set(id, { ...existing, x, y });
    }
  }
}

3. Spatial Viewport Culling & 60 FPS Rendering

Using R-trees to draw only visible canvas objects

In large canvases containing 10,000+ geometric paths, executing canvas draw calls for off-screen items drops frame rates below 20 FPS. By inserting all shapes into an R-tree index, the engine queries bounding boxes intersecting the active camera viewport in sub-millisecond time.

4. Ephemeral Presence & Live Multiplayer Cursors

Throttled UDP/WebSocket cursor coordinates with smooth interpolation

Transmitting cursor mouse coordinates at 60 Hz floods network sockets. We sample mouse positions at 20-30 Hz, broadcasting over Yjs awareness protocols, and use quadratic Catmull-Rom spline interpolation on client displays for fluid 60 FPS cursor motion.

Tags: real time collaborative software architectureyjs crdt canvaswebsocket multi-user syncingfigma like canvas developmentCollaborative Apps

Related Engineering Practice & Case Studies

Engineering Practice Blueprint

Custom Web Development & SaaS Platforms →

Next.js & React web platforms engineered for sub-1.5s load times and 98+ Lighthouse scores.

View Practice Blueprint & Capabilities →
Production Client System

Portfolix Developer Portfolio Builder →

Modern developer portfolio builder platform delivering sub-5 minute deployments with 98+ Lighthouse scores and 100% clean code export.

Read Deployed Case Study (Sub-5 minute edge deployment with 100% clean code export) →

Related Engineering Articles & Guides

Explore all technical guides in our Articles & Guides Index → or discuss your architecture with our engineers via Technical Discovery →