# 0xjoy07/xcalidraw architecture

> Architecture diagram of the GitHub repository 0xjoy07/xcalidraw, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0xjoy07/xcalidraw
- Repository: https://github.com/0xjoy07/xcalidraw

## Overview

#### Purpose and Entry Points
Xcalidraw is a hand-drawn, persistent canvas application with authenticated sharing and role-based access; it does not provide live multiplayer editing. The React/Vite client starts at `client/src/main.tsx` and routes through `client/src/App.tsx`. The Bun/Express API starts at `server/src/index.js`.

#### Client Application Shell and Access
The client separates public/auth screens, protected canvas/dashboard access, and an in-app documentation area. Authentication state is held in React context, while protected navigation gates canvas resources. Relevant paths: `client/src/auth/AuthContext.tsx`, `client/src/routes/ProtectedRoute.tsx`, `client/src/pages/CanvasDashboardPage.tsx`.

#### Canvas Interaction and Rendering
`Canvas` is the primary interactive surface: it owns pointer/touch-driven drawing, pan/zoom, selection, move/resize, and tool interaction. Rendering and hit testing are separated from the root interaction component, supporting RoughJS-style element drawing and spatial targeting. Core paths: `client/src/canvas/Canvas.tsx`, `client/src/canvas/renderElement.ts`, `client/src/canvas/hitTest.ts`.

#### Canvas State, Coordinate Model, and Persistence
Canvas elements and editor state are centralized in a Zustand store and represented by shared element types. The autosave boundary performs immediate localStorage persistence, then debounced API synchronization, allowing responsive offline-tolerant edits before MongoDB persistence. Core paths: `client/src/store/elementsStore.ts`, `client/src/types/element.ts`, `client/src/hooks/useCanvasAutosave.ts`.

#### API Boundary and Canvas Authorization
Client API modules isolate HTTP calls for authentication and canvas CRUD/sharing from UI and stores. Server canvas routes enforce authenticated access and resolve Owner, Editor, or Viewer permissions before operating on canvas documents and collaborator records. Core paths: `client/src/lib/canvasApi.ts`, `server/src/routes/canvases.js`, `server/src/utils/resolveAccess.js`.

#### Identity, Sessions, and Recovery
The server implements email/password and Google/GitHub OAuth through Passport. JWT access tokens are used client-side in memory, while refresh tokens use HTTP-only cookies and can be persisted/revoked. Password-reset tokens and transactional email complete account recovery. Core paths: `server/src/routes/auth.js`, `server/src/config/passport.js`, `server/src/models/RefreshToken.js`.

#### Data and External Infrastructure
MongoDB, accessed through Mongoose, is the durable system of record for users, canvases, collaboration grants, refresh tokens, and reset-token state. Nodemailer sends password recovery and collaborator-invite messages. Required external provisioning includes MongoDB, Google OAuth, GitHub OAuth, and an SMTP/Gmail App Password. Core paths: `server/src/config/db.js`, `server/src/models/Canvas.js`, `server/src/lib/mailer.js`.

#### Runtime and Deployment Boundaries
The frontend is a TypeScript React application built and served by Vite, with Vercel deployment configuration. The backend is JavaScript running on Bun with Express; its environment controls database, client-origin, OAuth, token, and mail configuration. Core paths: `client/vite.config.ts`, `client/vercel.json`, `server/package.json`.

Main entry points: [`server/src/routes/auth.js`](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/routes/auth.js) (Auth & recovery routes), [`server/src/routes/canvases.js`](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/routes/canvases.js) (Canvas routes), [`client/src/App.tsx`](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/App.tsx) (App routing shell), [`client/src/canvas/Canvas.tsx`](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/canvas/Canvas.tsx) (Interactive canvas)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React client"]
  node_client_entry["Client entry<br/>React bootstrap<br/>[main.tsx]"]
  node_app_shell["App routing shell<br/>React routes<br/>[App.tsx]"]
  node_auth_context["Auth state<br/>React context<br/>[AuthContext.tsx]"]
  node_protected_routes["Protected navigation<br/>route guard<br/>[ProtectedRoute.tsx]"]
  node_dashboard["Canvas dashboard<br/>page"]
  node_canvas["Interactive canvas<br/>editor surface<br/>[Canvas.tsx]"]
  node_rendering["Rendering &amp; targeting<br/>canvas rendering<br/>[renderElement.ts]"]
  node_element_store["Editor state<br/>Zustand store<br/>[elementsStore.ts]"]
  node_element_types["Element model<br/>shared types<br/>[element.ts]"]
  node_autosave["Autosave boundary<br/>persistence hook"]
  node_canvas_api["Canvas API client<br/>HTTP client<br/>[canvasApi.ts]"]
  node_deployment["Vite &amp; Vercel delivery<br/>frontend deployment<br/>[vercel.json]"]
end

subgraph group_server["Bun / Express API"]
  node_api_entry["API entry<br/>Express server<br/>[index.js]"]
  node_canvas_routes["Canvas routes<br/>Express routes<br/>[canvases.js]"]
  node_access_resolution["Role resolution<br/>authorization<br/>[resolveAccess.js]"]
  node_auth_routes["Auth &amp; recovery routes<br/>Express routes<br/>[auth.js]"]
  node_passport["OAuth strategies<br/>Passport config<br/>[passport.js]"]
  node_refresh_tokens["Refresh token records<br/>Mongoose model<br/>[RefreshToken.js]"]
  node_bun_runtime["Bun runtime<br/>backend runtime<br/>[package.json]"]
end

subgraph group_infra["Data &amp; external services"]
  node_database[("MongoDB connection<br/>Mongoose data access<br/>[db.js]")]
  node_canvas_model["Canvas documents<br/>Mongoose model<br/>[Canvas.js]"]
  node_mailer["Transactional mail<br/>Nodemailer<br/>[mailer.js]"]
  node_oauth_providers{{"Google &amp; GitHub OAuth<br/>identity providers"}}
  node_smtp{{"SMTP / Gmail<br/>email provider"}}
end

node_client_entry -->|"mounts"| node_app_shell
node_app_shell -->|"uses"| node_auth_context
node_app_shell -->|"routes through"| node_protected_routes
node_protected_routes -->|"permits"| node_dashboard
node_dashboard -->|"hosts"| node_canvas
node_canvas -->|"draws and targets through"| node_rendering
node_canvas -->|"reads and updates"| node_element_store
node_element_store -->|"uses"| node_element_types
node_autosave -->|"observes changes"| node_element_store
node_autosave -->|"debounced sync"| node_canvas_api
node_canvas_api -->|"HTTPS canvas requests"| node_canvas_routes
node_auth_context -->|"authentication requests"| node_auth_routes
node_api_entry -->|"mounts"| node_canvas_routes
node_api_entry -->|"mounts"| node_auth_routes
node_bun_runtime -->|"runs"| node_api_entry
node_canvas_routes -->|"authorizes with"| node_access_resolution
node_canvas_routes -->|"reads and writes"| node_canvas_model
node_canvas_model -->|"persists in"| node_database
node_auth_routes -->|"delegates OAuth to"| node_passport
node_passport -->|"authenticates against"| node_oauth_providers
node_auth_routes -->|"issues and revokes"| node_refresh_tokens
node_refresh_tokens -->|"persists in"| node_database
node_auth_routes -->|"sends recovery mail"| node_mailer
node_mailer -->|"delivers via"| node_smtp
node_deployment -->|"builds and serves"| node_client_entry

click node_client_entry "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/main.tsx"
click node_app_shell "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/App.tsx"
click node_auth_context "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/auth/AuthContext.tsx"
click node_protected_routes "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/routes/ProtectedRoute.tsx"
click node_dashboard "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/pages/CanvasDashboardPage.tsx"
click node_canvas "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/canvas/Canvas.tsx"
click node_rendering "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/canvas/renderElement.ts"
click node_element_store "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/store/elementsStore.ts"
click node_element_types "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/types/element.ts"
click node_autosave "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/hooks/useCanvasAutosave.ts"
click node_canvas_api "https://github.com/0xjoy07/xcalidraw/blob/main/client/src/lib/canvasApi.ts"
click node_api_entry "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/index.js"
click node_canvas_routes "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/routes/canvases.js"
click node_access_resolution "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/utils/resolveAccess.js"
click node_auth_routes "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/routes/auth.js"
click node_passport "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/config/passport.js"
click node_refresh_tokens "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/models/RefreshToken.js"
click node_database "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/config/db.js"
click node_canvas_model "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/models/Canvas.js"
click node_mailer "https://github.com/0xjoy07/xcalidraw/blob/main/server/src/lib/mailer.js"
click node_deployment "https://github.com/0xjoy07/xcalidraw/blob/main/client/vercel.json"
click node_bun_runtime "https://github.com/0xjoy07/xcalidraw/blob/main/server/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_entry,node_app_shell,node_auth_context,node_protected_routes,node_dashboard,node_canvas,node_rendering,node_element_store,node_element_types,node_autosave,node_canvas_api,node_deployment toneBlue
class node_api_entry,node_canvas_routes,node_access_resolution,node_auth_routes,node_passport,node_refresh_tokens,node_bun_runtime toneAmber
class node_database,node_canvas_model,node_mailer,node_oauth_providers,node_smtp toneMint
```

## Components

### React client

Vite single-page application.

- [Client entry](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/main.tsx): React bootstrap · `client/src/main.tsx`
- [App routing shell](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/App.tsx): React routes · Separates public, protected, and documentation areas. · `client/src/App.tsx`
- [Auth state](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/auth/AuthContext.tsx): React context · Holds client authentication state and tokens. · `client/src/auth/AuthContext.tsx`
- [Protected navigation](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/routes/ProtectedRoute.tsx): route guard · Gates dashboard and canvas resources. · `client/src/routes/ProtectedRoute.tsx`
- [Canvas dashboard](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/pages/CanvasDashboardPage.tsx): page · `client/src/pages/CanvasDashboardPage.tsx`
- [Interactive canvas](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/canvas/Canvas.tsx): editor surface · Handles drawing, gestures, selection, transforms, and tools. · `client/src/canvas/Canvas.tsx`
- [Rendering & targeting](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/canvas/renderElement.ts): canvas rendering · Draws elements and performs hit testing. · `client/src/canvas/renderElement.ts`
- [Editor state](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/store/elementsStore.ts): Zustand store · Centralizes elements and editor state. · `client/src/store/elementsStore.ts`
- [Element model](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/types/element.ts): shared types · `client/src/types/element.ts`
- [Autosave boundary](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/hooks/useCanvasAutosave.ts): persistence hook · Writes locally immediately and syncs API changes after debounce. · `client/src/hooks/useCanvasAutosave.ts`
- [Canvas API client](https://github.com/0xjoy07/xcalidraw/blob/main/client/src/lib/canvasApi.ts): HTTP client · Isolates canvas CRUD and sharing requests. · `client/src/lib/canvasApi.ts`
- [Vite & Vercel delivery](https://github.com/0xjoy07/xcalidraw/blob/main/client/vercel.json): frontend deployment · Builds the client and configures Vercel hosting. · `client/vercel.json`

### Bun / Express API

Authenticated canvas and identity services.

- [API entry](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/index.js): Express server · `server/src/index.js`
- [Canvas routes](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/routes/canvases.js): Express routes · Handles canvas CRUD and collaborator sharing. · `server/src/routes/canvases.js`
- [Role resolution](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/utils/resolveAccess.js): authorization · Resolves Owner, Editor, or Viewer access. · `server/src/utils/resolveAccess.js`
- [Auth & recovery routes](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/routes/auth.js): Express routes · Provides password login, OAuth, refresh, and recovery flows. · `server/src/routes/auth.js`
- [OAuth strategies](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/config/passport.js): Passport config · Supports Google and GitHub identity providers. · `server/src/config/passport.js`
- [Refresh token records](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/models/RefreshToken.js): Mongoose model · Supports persisted and revocable HTTP-only-cookie sessions. · `server/src/models/RefreshToken.js`
- [Bun runtime](https://github.com/0xjoy07/xcalidraw/blob/main/server/package.json): backend runtime · Runs the Express API. · `server/package.json`

### Data & external services

Durable storage and third-party providers.

- [MongoDB connection](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/config/db.js): Mongoose data access · Connects the API to the durable system of record. · `server/src/config/db.js`
- [Canvas documents](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/models/Canvas.js): Mongoose model · Stores canvases and collaboration state. · `server/src/models/Canvas.js`
- [Transactional mail](https://github.com/0xjoy07/xcalidraw/blob/main/server/src/lib/mailer.js): Nodemailer · Sends recovery and collaborator-invite email. · `server/src/lib/mailer.js`
- **Google & GitHub OAuth**: identity providers
- **SMTP / Gmail**: email provider

## Connections

- Client entry → App routing shell: mounts
- App routing shell → Auth state: uses
- App routing shell → Protected navigation: routes through
- Protected navigation → Canvas dashboard: permits
- Canvas dashboard → Interactive canvas: hosts
- Interactive canvas → Rendering & targeting: draws and targets through
- Interactive canvas → Editor state: reads and updates
- Editor state → Element model: uses
- Autosave boundary → Editor state: observes changes
- Autosave boundary → Canvas API client: debounced sync
- Canvas API client → Canvas routes: HTTPS canvas requests
- Auth state → Auth & recovery routes: authentication requests
- API entry → Canvas routes: mounts
- API entry → Auth & recovery routes: mounts
- Bun runtime → API entry: runs
- Canvas routes → Role resolution: authorizes with
- Canvas routes → Canvas documents: reads and writes
- Canvas documents → MongoDB connection: persists in
- Auth & recovery routes → OAuth strategies: delegates OAuth to
- OAuth strategies → Google & GitHub OAuth: authenticates against
- Auth & recovery routes → Refresh token records: issues and revokes
- Refresh token records → MongoDB connection: persists in
- Auth & recovery routes → Transactional mail: sends recovery mail
- Transactional mail → SMTP / Gmail: delivers via
- Vite & Vercel delivery → Client entry: builds and serves
