A real-time collaborative canvas application where multiple users can create and manipulate rectangles simultaneously on a shared canvas, with live cursor tracking and presence awareness.
CollabCanvas is a collaborative design tool that enables multiple users to work together in real-time on a shared canvas. Users can create, move, resize, and delete rectangles while seeing each other's cursors and presence. The system prevents race conditions through an ownership mechanism where objects must be selected and claimed before modification.
Status: โ COMPLETE - All project requirements met and performance targets achieved.
The application uses a dual-channel real-time architecture with direct database operations for ownership management.
CollabCanvas follows a real-time collaborative architecture with clear separation of concerns:
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Client A โ โ Client B โ โ Client C โ
โ โ โ โ โ โ
โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ
โ โ Konva โ โ โ โ Konva โ โ โ โ Konva โ โ
โ โ Canvas โ โ โ โ Canvas โ โ โ โ Canvas โ โ
โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ
โ โ React โ โ โ โ React โ โ โ โ React โ โ
โ โ State โ โ โ โ State โ โ โ โ State โ โ
โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโ
โ Supabase โ
โ โ
โ โโโโโโโโโโโโโโโ โ
โ โ Realtime โ โ
โ โ Channels โ โ
โ โโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโ โ
โ โ PostgreSQL โ โ
โ โ Database โ โ
โ โโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโ
-
Canvas Rendering: react-konva
- React integration with Konva.js for component-based architecture
- React components wrapping Konva elements
- Canvas components use react-konva components
-
Real-time Sync: Dual Channel Architecture
- Broadcast Channel: Object CRUD operations (create, update, delete, duplicate)
- Database Channel: Ownership changes only (prevents duplicate processing)
- Pattern: Optimistic UI with server validation
-
Ownership System: Claim-Confirm Pattern
1. User clicks object โ Check owner === "all" 2. Show yellow border (pending state) 3. Send claim request to server 4. Server validates โ Responds success/failure 5. Update UI: Yellow โ User color (success) or Owner color (failure) 6. On release โ Set owner: "all" and broadcast -
State Management: Local + Sync
- Local State: Immediate UI updates for smooth interactions
- Sync State: Server state for conflict resolution
- Pattern: Optimistic updates with rollback capability
- Real-time Collaboration - Multiple users editing simultaneously
- Live Cursors - See where other users are working with names and colors
- Presence Awareness - Modal showing online users with avatars
- Object Ownership - Prevents editing conflicts with atomic claim/release system
- Smooth Performance - 60 FPS maintained during all interactions with batching system
- User Authentication - Secure login with display names
- State Persistence - Canvas state saved across sessions
- Color Picker - Full color selection for rectangles
- Multi-select - Select and manipulate multiple objects
- Keyboard Shortcuts - Delete, duplicate, and navigation shortcuts
- Ellipse Support - Create and manipulate ellipse shapes
- Automatic Reconnection - Handles network disconnections gracefully
- Optimized Updates - Batched updates for smooth multi-user collaboration
- Frontend: Next.js 15 (App Router), TailwindCSS, shadcn/ui
- Canvas: react-konva (React wrapper for Konva.js)
- Backend: Supabase (Auth + Realtime + Database)
- State Management: React hooks (no external state library)
- Runtime: Bun
- Deployment: Vercel
- next - React framework with App Router
- react - JavaScript library for building component-based applications
- react-konva - React wrapper for Konva.js 2D canvas library
- konva - 2D canvas library (dependency of react-konva)
- @supabase/supabase-js - Supabase client for authentication, database, and real-time features
- tailwindcss - CSS framework for styling
- @radix-ui/react-* - UI primitives (via shadcn/ui components)
- @ai-sdk/openai - AI SDK for OpenAI integration
- ai - Vercel AI SDK for text generation
- typescript - Type checking and development
- @types/react - React type definitions
- @types/konva - Konva type definitions
- supabase - CLI for database management (run via
bunx supabase)
- react-konva + konva: Canvas rendering, shape manipulation, real-time graphics
- @supabase/supabase-js: User authentication, database persistence, real-time synchronization
- @ai-sdk/openai + ai: Natural language processing for AI commands
- tailwindcss + @radix-ui: Modern UI components and styling
- next: Server-side rendering, API routes, deployment optimization
- Bun installed
- Supabase CLI installed
-
Clone the repository
git clone https://github.com/lousydropout/collab-canvas cd collab-canvas -
Install dependencies
bun install
-
Set up Supabase
bunx supabase start bunx supabase migration up
-
Configure environment variables Create
.env.localwith your Supabase credentials:NEXT_PUBLIC_SUPABASE_URL=your_supabase_url NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=your_supabase_publishable_key
Note: Get these values from
bunx supabase status -
Start the development server
bun dev
-
Open your browser Navigate to http://localhost:3000
- Register/Login - Create an account or sign in
- Create Shapes - Click and drag on the canvas to create rectangles or ellipses
- Move Objects - Click and drag shapes to move them
- Resize Objects - Use the corner handles to resize
- Multi-select - Hold Shift and click multiple objects
- Delete Objects - Select objects and press Delete key
- Duplicate Objects - Select objects and press Ctrl+D
- Change Colors - Use the color picker in the toolbar
- View Online Users - Click the connection status indicator
- Switch Tools - Use toolbar buttons to switch between rectangle and ellipse tools
- Open multiple browser windows/tabs to the canvas page
- Login with different accounts (or same account in different tabs)
- Create shapes in one window and see them appear in others
- Move objects and watch real-time synchronization
- See live cursors of other users as they work
- Test ownership conflicts - try to edit objects owned by others
- Test presence consistency - verify user counts match across all windows
- Test batching performance - monitor console for batched update messages
For detailed testing instructions, see:
- REALTIME_TEST_GUIDE.md - Real-time collaboration testing
- BATCHING_TEST_GUIDE.md - Performance optimization testing
- PRESENCE_FIX_VERIFICATION.md - Presence system testing
collab-canvas/
โโโ app/ # Next.js App Router
โ โโโ (auth)/ # Authentication routes
โ โโโ canvas/ # Main canvas page
โ โโโ layout.tsx # Root layout
โโโ components/ # React components
โ โโโ canvas/ # Canvas-specific components
โ โโโ layout/ # Layout components
โ โโโ auth/ # Authentication components
โ โโโ ui/ # shadcn/ui components
โโโ hooks/ # Custom React hooks
โโโ lib/ # Utility libraries
โ โโโ supabase/ # Supabase client configuration
โโโ types/ # TypeScript type definitions
โโโ supabase/ # Database schema and migrations
โโโ memory-bank/ # Project documentation
# Start development server
bun dev
# Start Supabase locally
bunx supabase start
# Run database migrations
bunx supabase migration up
# Check Supabase status
bunx supabase status
# Reset database
bunx supabase db reset
# Stop Supabase
bunx supabase stopCollabCanvas includes AI-powered natural language commands that let you create and modify objects using simple text instructions.
-
Generate Objects
- "Create rectangle"
- "Add triangle at top-right"
- "Make rectangle that's taller than it is long"
- "Add a 200x15 rectangle"
-
Generate Multiple Objects
- "Add 3 blue circles of different sizes"
- "16 rectangles on a 4x4 grid"
- "34 rectangles at random positions"
-
Modify Selected Objects
- "Turn blue"
- "Make 2x as large"
- "Move to bottom-left"
- โ 60 FPS - Maintained during all interactions with batching system
- โ <100ms - Object sync latency across users
- โ <50ms - Cursor position sync latency
- โ Multiple objects - Smooth performance with many objects
- โ Multiple users - Smooth collaboration with concurrent users
- โ Optimized Updates - Batched updates reduce React re-renders
- โ Presence Consistency - Fixed duplicate user counting issues
Note: Performance is better on the production site due to React rendering things twice while in development mode (due to Strict Mode).
The application is deployed on Vercel and publicly accessible at https://collab-canvas-gamma.vercel.app/.
- Product Requirements Document - Detailed project specifications
- Real-time Testing Guide - How to test collaboration features
- Performance Testing - Performance benchmarks and testing
- Batching Test Guide - Performance optimization testing
- Presence Fix Verification - Presence system testing
- Memory Bank - Project documentation and context
- Tasks - Development progress and completed features
- โ Canvas with pan/zoom
- โ Rectangle creation & movement
- โ Real-time 2+ user sync
- โ Multiplayer cursors with labels
- โ Online presence list (modal)
- โ Auth with display names
- โ Public deployment
- โ Ownership system prevents conflicts
- โ State persists
- โ 60 FPS under load
- โ <100ms object sync
- โ <50ms cursor sync
- โ Handles multiple objects smoothly
- โ Supports multiple concurrent users smoothly
- Dual Channel System - Broadcast channels for CRUD operations + database subscriptions for ownership
- Optimistic UI - Immediate feedback with server validation
- Automatic Reconnection - Handles network disconnections gracefully
- Creator Ownership - Objects are owned by their creators initially
- Atomic Ownership Claims - Database-level validation prevents conflicts
- Batched Updates - 16ms timer-based flushing for smooth multi-user collaboration
- Presence Deduplication - Prevents duplicate user counting across reconnections
- Shape Support - Both rectangles and ellipses with unified transformation system
CollabCanvas implements a claim-based ownership system to prevent editing conflicts in real-time collaboration. Users must claim ownership of objects before they can modify them.
-
Object States:
- Available (
owner: "all"): Any user can claim and edit - Claimed (
owner: userId): Only the claiming user can edit - Pending: User clicked object, awaiting server validation
- Available (
-
Claim Process:
User clicks object โ Check if owner === "all" โ Show yellow border (pending state) โ Send claim request to server โ Server validates atomically โ Success/Failure โ Update UI: Yellow โ User color (success) or Owner color (failure) -
Visual Feedback:
- Yellow Border: Pending claim (waiting for server)
- User Color Border: Successfully claimed (can edit)
- Red Border: Claimed by another user (cannot edit)
- No Border: Available for claiming
-
Release Process:
- Owner releases by clicking empty space
- Owner releases by finishing their edit operation
- Object returns to
owner: "all"state
Prevents Race Conditions: Without ownership, multiple users could simultaneously modify the same object, leading to:
- Lost changes
- Inconsistent state
- Poor user experience
Clear User Feedback: Users always know:
- Which objects they can edit
- Which objects are being edited by others
- When their claim is pending
Server-Side Validation: Database-level atomic operations ensure only one user can claim an object at a time.
Online-Only Requirement: The conflict resolution strategy requires network connectivity because:
- Ownership claims must be validated server-side
- Users cannot claim objects when offline
- This prevents offline users from creating conflicting changes
Trade-off Decision: While this limits offline functionality, it ensures data consistency and prevents the complex merge conflicts that would arise from offline editing.
CollabCanvas uses a single-prompt approach for AI command processing, designed to avoid API key exposure while maintaining client-side canvas control.
User Input โ Server Action โ LLM Processing โ JSON Response โ Client Parsing โ Canvas Operations
Security First: API keys must remain server-side to prevent exposure:
- OpenAI API keys are never sent to the client
- All LLM calls happen in Next.js server actions
- Client receives only structured JSON responses
Client-Side Canvas Control: Canvas objects must be manipulated client-side because:
- react-konva requires direct DOM manipulation
- Real-time synchronization happens client-side
- Canvas state management is local to each client
No Tool Calling: Traditional AI tool calling wasn't feasible because:
- Tools would need to run client-side (security risk)
- Canvas operations require immediate UI updates
- Real-time collaboration needs local state management
-
Server Action (
detectObjectIntent):- Receives natural language command
- Sends structured prompt to OpenAI GPT-4o-mini
- Returns JSON command object
-
Prompt Engineering:
- Detailed instructions for JSON response format
- Context about viewport, selection, and canvas state
- Examples for different command types
- Validation rules for coordinate systems
-
Command Types:
- Create: Single objects, batches, or patterns
- Modify: Position, size, color, or text changes
- Layout: Alignment and distribution operations
-
Client Processing:
- Parses JSON command
- Maps to appropriate canvas operations
- Handles ownership claims
- Executes with optimistic UI updates
User: "Create a blue rectangle"
โ
Server Action: Sends prompt to OpenAI
โ
OpenAI Response: {"command": "create", "objectType": "rectangle", "color": "#0000ff", ...}
โ
Client: Parses JSON, calls createRectangle(), claims ownership
โ
Canvas: Rectangle appears with blue color
- Secure: API keys never exposed to client
- Flexible: Single prompt handles all command types
- Efficient: No complex tool calling infrastructure
- Maintainable: Clear separation between AI processing and canvas operations
- Extensible: Easy to add new command types via prompt engineering
This project is complete and successful. Recent improvements include:
- Ellipse Support - Added ellipse creation and manipulation
- Batching System - Optimized updates for smoother multi-user collaboration
- Presence Fix - Resolved duplicate user counting issues
- Performance Optimization - Enhanced real-time synchronization
Future enhancements could include:
- Additional shape tools (text, polygons)
- Advanced transformations (rotation handles)
- Undo/redo functionality
- Mobile responsive design
- Layer management system
- Export functionality
This project is part of a development challenge demonstrating AI-assisted development with comprehensive documentation and clean architecture.
CollabCanvas has been successfully delivered with all requirements met and performance targets achieved.