Skip to content

About

Real-Time Collaborative CRDT Document & Code Canvas Engine with WebAssembly

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

2 Commits

Folders and files

Repository files navigation

🎨 CRDT Canvas Engine

Next-Generation Real-Time Collaborative Document & Code Canvas Engine

CI Pipeline TypeScript License CRDT WebSocket


  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚                           INFINITE CRDT CANVAS                         β”‚
  β”‚                                                                        β”‚
  β”‚   [Peer A] ──── (Op: Insert 'X' @ [3,1]) ────► [CRDT State Router]      β”‚
  β”‚                                                         β”‚              β”‚
  β”‚   [Peer B] ──── (Op: Move Shape #42)     ────► [Vector Clock]          β”‚
  β”‚                                                         β”‚              β”‚
  β”‚                                                         β–Ό              β”‚
  β”‚                                                 [Convergence Engine]   β”‚
  β”‚                                                         β”‚              β”‚
  β”‚                                                         β–Ό              β”‚
  β”‚                                               [Infinite 2D/3D Canvas]  β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Sub-millisecond Convergence β€’ Infinite 2D/3D Viewport β€’ Causal Vector Clocks β€’ Decentralized P2P & Client-Server


🌟 Overview

crdt-canvas-engine is a distributed collaborative canvas and rich document editing engine built ground-up in TypeScript. Designed for high-frequency collaborative whiteboards, architectural diagrams, visual programming, and shared text editing, it guarantees Strong Eventual Consistency (SEC) under arbitrary network partitions, latency spikes, and packet reordering.

Key Capabilities

  • Logoot / LSeq-style Position CRDT: Infinite fractional identifier tree ensuring deterministic total ordering with bounded identifier growth.
  • State-based & Operation-based Shape CRDT: Field-level Last-Write-Wins (LWW) registers with deterministic Lamport tie-breaking.
  • Infinite Zoom 2D/3D Camera: 60fps rendering pipeline supporting pan, smooth zoom scaling ($0.05\times - 25.0\times$), camera tilt, and frustum culling.
  • Spatial Collision Detection: Precise 2D/3D Oriented Bounding Box (OBB) & Axis-Aligned Bounding Box (AABB) hit-testing with inverse transform projection.
  • Causal Synchronization: Distributed VectorClock tracking happens-before relationships and concurrency conflicts.
  • PeerSync WebSocket Architecture: Real-time room multiplexing, peer cursor awareness, and state snapshot transfer.
  • Undo / Redo Manager: Non-destructive timeline history with undo/redo execution tracking.

πŸ›οΈ System Architecture

flowchart TD
    subgraph Client Application
        CanvasUI["Infinite Canvas Viewport (HTML5 Canvas / WebGL)"]
        Renderer["InfiniteCanvasRenderer"]
        Collision["SpatialCollisionIndex (AABB / OBB)"]
    end

    subgraph CRDT Core
        Doc["CRDTDocument"]
        Logoot["LogootSequence (Text Model)"]
        ShapeMap["ShapeCRDTMap (2D/3D Vector Model)"]
        VC["VectorClock"]
        Undo["UndoRedoManager"]
    end

    subgraph Network Layer
        SyncClient["PeerSyncClient (WebSocket)"]
        SyncServer["PeerSyncServer"]
    end

    CanvasUI --> Renderer
    Renderer --> Collision
    Renderer --> Doc
    Doc --> Logoot
    Doc --> ShapeMap
    Doc --> VC
    Doc --> Undo
    Doc <--> SyncClient
    SyncClient <--> SyncServer
Loading

πŸ“¦ Installation

npm install @er-brocode/crdt-canvas-engine ws

πŸš€ Quick Start

1. Initializing Collaborative Document & Canvas

import { CRDTDocument, InfiniteCanvasRenderer } from '@er-brocode/crdt-canvas-engine';

// Initialize document with local site ID
const doc = new CRDTDocument('workspace-alpha', 'site-peer-1');

// Collaborative text modifications
doc.insertText(0, 'Hello Collaborative World!');

// Collaborative 2D/3D shapes
const shapeOp = doc.createShape(
  'node-arch-1',
  'rectangle',
  { x: 300, y: 200, scaleX: 1.5, scaleY: 1.2 },
  { fillColor: '#3b82f6', strokeColor: '#1d4ed8' },
  'Auth Microservice'
);

// Infinite Viewport Renderer
const renderer = new InfiniteCanvasRenderer(doc, 1920, 1080);
renderer.panBy(100, 50);
renderer.zoomAt(960, 540, 1.2);

2. Real-Time Peer Sync & Awareness

import { PeerSyncServer, PeerSyncClient, CRDTDocument } from '@er-brocode/crdt-canvas-engine';

// Run WebSocket sync server
const server = new PeerSyncServer(8080);
await server.start();

// Connect client
const doc = new CRDTDocument('board-101', 'peer-alice');
const client = new PeerSyncClient(doc, {
  serverUrl: 'ws://localhost:8080',
  roomId: 'board-101',
  siteId: 'peer-alice',
  userName: 'Alice'
});

await client.connect();

// Broadcast cursor coordinates
client.sendAwareness({ x: 450, y: 280, z: 0 });

// Listen for peer cursor updates
client.onAwareness(({ siteId, cursor }) => {
  console.log(`Peer ${siteId} moved cursor to`, cursor);
});

πŸ”¬ Mathematical Verification & Convergence

Logoot Identifier Model

Logoot represents positions as sequences of tuples: $$P = \langle (d_1, s_1, c_1), (d_2, s_2, c_2), \dots, (d_k, s_k, c_k) \rangle$$ Where:

  • $d_i \in [0, \text{BASE}-1]$ represents the spatial digit.
  • $s_i \in \text{SiteId}$ is the unique peer identifier.
  • $c_i \in \mathbb{N}$ is the logical clock counter.

Total order is strictly enforced via lexicographical ordering: $$P_A &lt; P_B \iff \exists i: \forall j &lt; i, P_A[j] = P_B[j] \land P_A[i] &lt; P_B[i]$$


πŸ§ͺ Automated Test Suite

Run the full verification suite covering concurrent conflict resolution and eventual consistency:

# Run unit & convergence tests
npm test

# Run test coverage report
npm run test:coverage

Verified Scenarios

  • βœ… Dense fractional Logoot position allocation under 0-distance collisions.
  • βœ… Interleaved concurrent text insertions and deletions at identical indices.
  • βœ… Concurrent field-level Last-Write-Wins (LWW) register convergence.
  • βœ… Non-blocking Vector Clock causality tracking ($happened\text{-}before$, concurrent).
  • βœ… N-Peer random network delays and packet reordering convergence.
  • βœ… 2D/3D oriented bounding box collision detection and hit testing.

πŸ› οΈ Project Structure

crdt-canvas-engine/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci.yml               # GitHub Actions CI matrix build & test
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ crdt/
β”‚   β”‚   β”œβ”€β”€ LogootPosition.ts    # Dense fractional index total order allocator
β”‚   β”‚   β”œβ”€β”€ LogootSequence.ts    # Text sequence CRDT model
β”‚   β”‚   β”œβ”€β”€ ShapeCRDT.ts         # 2D/3D Shape CRDT with LWW registers
β”‚   β”‚   β”œβ”€β”€ CRDTDocument.ts      # Composite document orchestrator
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ sync/
β”‚   β”‚   β”œβ”€β”€ VectorClock.ts       # Distributed causality tracking
β”‚   β”‚   β”œβ”€β”€ UndoRedoManager.ts   # Local timeline undo/redo manager
β”‚   β”‚   β”œβ”€β”€ PeerSyncServer.ts    # WebSocket sync server with room routing
β”‚   β”‚   β”œβ”€β”€ PeerSyncClient.ts    # Resilient sync client with awareness
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ canvas/
β”‚   β”‚   β”œβ”€β”€ SpatialCollisionIndex.ts  # AABB & OBB bounding box collision
β”‚   β”‚   β”œβ”€β”€ InfiniteCanvasRenderer.ts # Infinite zoom/pan 2D/3D renderer
β”‚   β”‚   └── index.ts
β”‚   └── index.ts
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ conflict-resolution.test.ts # Concurrent conflict resolution tests
β”‚   └── state-convergence.test.ts   # N-peer convergence & collision tests
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ jest.config.js
└── README.md

πŸ“„ License

Licensed under the Apache License, Version 2.0. Copyright (c) 2026 koushik4475 / ER-brocode.

About

Real-Time Collaborative CRDT Document & Code Canvas Engine with WebAssembly

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages