excalidraw
Expert guidance for Excalidraw, the open-source virtual whiteboard library for creating hand-drawn style diagrams and sketches. Helps developers embed Excalidraw in React applications, build custom integrations, and leverage the API for programmatic diagram creation.
What this skill does
# Excalidraw — Hand-Drawn Whiteboard SDK
## Overview
Excalidraw, the open-source virtual whiteboard library for creating hand-drawn style diagrams and sketches. Helps developers embed Excalidraw in React applications, build custom integrations, and leverage the API for programmatic diagram creation.
## Instructions
### Basic Embedding
Add an Excalidraw whiteboard to any React application:
```tsx
// src/components/Whiteboard.tsx — Embed Excalidraw in a React app
import { Excalidraw } from "@excalidraw/excalidraw";
import { ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
import { AppState } from "@excalidraw/excalidraw/types/types";
import { useState, useCallback } from "react";
export function Whiteboard() {
const [excalidrawAPI, setExcalidrawAPI] = useState<any>(null);
const handleChange = useCallback(
(elements: readonly ExcalidrawElement[], state: AppState) => {
// Called on every change — debounce before persisting
console.log(`${elements.length} elements on canvas`);
},
[]
);
return (
<div style={{ height: "100vh", width: "100%" }}>
<Excalidraw
ref={(api) => setExcalidrawAPI(api)}
onChange={handleChange}
initialData={{
appState: {
viewBackgroundColor: "#fafafa",
currentItemFontFamily: 1, // 1 = Virgil (hand-drawn), 2 = Helvetica, 3 = Cascadia
},
}}
UIOptions={{
canvasActions: {
loadScene: true,
export: { saveFileToDisk: true },
toggleTheme: true,
},
}}
/>
</div>
);
}
```
### Programmatic Scene Creation
Generate diagrams from code or data:
```typescript
// src/diagrams/architecture.ts — Generate architecture diagrams programmatically
import { ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types";
interface ServiceNode {
name: string;
type: "api" | "database" | "queue" | "cache" | "frontend";
x: number;
y: number;
}
function createServiceBox(service: ServiceNode): ExcalidrawElement {
const colors: Record<string, string> = {
api: "#a5d8ff", // Blue for APIs
database: "#b2f2bb", // Green for databases
queue: "#ffec99", // Yellow for queues
cache: "#ffc9c9", // Red for caches
frontend: "#d0bfff", // Purple for frontends
};
return {
type: "rectangle",
id: `service-${service.name}`,
x: service.x,
y: service.y,
width: 200,
height: 80,
strokeColor: "#1e1e1e",
backgroundColor: colors[service.type] || "#e9ecef",
fillStyle: "solid",
strokeWidth: 2,
roughness: 1, // 0 = smooth, 1 = hand-drawn, 2 = very rough
roundness: { type: 3, value: 8 },
isDeleted: false,
boundElements: null,
locked: false,
opacity: 100,
angle: 0,
seed: Math.floor(Math.random() * 100000), // Random seed for hand-drawn variation
version: 1,
versionNonce: Math.floor(Math.random() * 100000),
groupIds: [],
frameId: null,
link: null,
updated: Date.now(),
} as any;
}
function createArrow(
fromId: string, toId: string,
startX: number, startY: number,
endX: number, endY: number,
label?: string
): ExcalidrawElement {
return {
type: "arrow",
id: `arrow-${fromId}-${toId}`,
x: startX,
y: startY,
width: endX - startX,
height: endY - startY,
strokeColor: "#1e1e1e",
strokeWidth: 2,
roughness: 1,
points: [[0, 0], [endX - startX, endY - startY]],
startBinding: { elementId: fromId, focus: 0, gap: 4 },
endBinding: { elementId: toId, focus: 0, gap: 4 },
startArrowhead: null,
endArrowhead: "arrow",
isDeleted: false,
opacity: 100,
angle: 0,
seed: Math.floor(Math.random() * 100000),
version: 1,
versionNonce: Math.floor(Math.random() * 100000),
groupIds: [],
boundElements: null,
locked: false,
frameId: null,
link: null,
updated: Date.now(),
} as any;
}
// Generate a full architecture diagram
export function generateArchitectureDiagram(services: ServiceNode[], connections: [string, string][]) {
const elements: ExcalidrawElement[] = [];
// Create service boxes
for (const service of services) {
elements.push(createServiceBox(service));
// Add label text
elements.push({
type: "text",
id: `label-${service.name}`,
x: service.x + 20,
y: service.y + 25,
width: 160,
height: 30,
text: `${service.name}\n(${service.type})`,
fontSize: 16,
fontFamily: 1, // Virgil hand-drawn font
textAlign: "center",
verticalAlign: "middle",
strokeColor: "#1e1e1e",
isDeleted: false,
opacity: 100,
angle: 0,
seed: Math.floor(Math.random() * 100000),
version: 1,
versionNonce: Math.floor(Math.random() * 100000),
groupIds: [],
boundElements: null,
locked: false,
frameId: null,
link: null,
updated: Date.now(),
containerId: `service-${service.name}`,
} as any);
}
// Create arrows for connections
for (const [from, to] of connections) {
const fromService = services.find(s => s.name === from)!;
const toService = services.find(s => s.name === to)!;
elements.push(createArrow(
`service-${from}`, `service-${to}`,
fromService.x + 200, fromService.y + 40,
toService.x, toService.y + 40,
));
}
return elements;
}
```
### Export and Import
Save diagrams as files, images, or shareable links:
```typescript
// src/utils/export.ts — Export Excalidraw scenes in various formats
import { exportToBlob, exportToSvg, serializeAsJSON } from "@excalidraw/excalidraw";
async function exportAsPNG(excalidrawAPI: any) {
const elements = excalidrawAPI.getSceneElements();
const appState = excalidrawAPI.getAppState();
const blob = await exportToBlob({
elements,
appState: { ...appState, exportWithDarkMode: false },
files: excalidrawAPI.getFiles(),
getDimensions: () => ({ width: 1920, height: 1080, scale: 2 }), // 2x for retina
});
// Download the image
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "diagram.png";
link.click();
URL.revokeObjectURL(url);
}
async function exportAsSVG(excalidrawAPI: any) {
const elements = excalidrawAPI.getSceneElements();
const svg = await exportToSvg({
elements,
appState: { exportWithDarkMode: false },
files: excalidrawAPI.getFiles(),
});
return svg.outerHTML; // Returns SVG as string
}
function saveAsExcalidrawFile(excalidrawAPI: any) {
const elements = excalidrawAPI.getSceneElements();
const appState = excalidrawAPI.getAppState();
const json = serializeAsJSON(elements, appState, excalidrawAPI.getFiles(), "local");
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "diagram.excalidraw";
link.click();
}
// Load from .excalidraw file
async function loadFromFile(excalidrawAPI: any, file: File) {
const text = await file.text();
const data = JSON.parse(text);
excalidrawAPI.updateScene({
elements: data.elements,
appState: data.appState,
});
}
```
### Collaboration with Excalidraw+
Set up real-time collaboration:
```tsx
// src/components/CollaborativeBoard.tsx — Real-time whiteboard with collaboration
import { Excalidraw, LiveCollaborationTrigger } from "@excalidraw/excalidraw";
export function CollaborativeBoard({ roomId }: { roomId: string }) {
return (
<div style={{ height: "100vh" }}>
<Excalidraw
isCollaborating={true}
// Custom collaboration backend using WebSocket
onCollabButtonClick={() => {
// Open share dialog or copy room link
const link = `${window.location.origin}/board/${roomId}`;
navigator.clipboard.writeText(link);
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.