Try it above or visit canvas-with-c1.vercel.app
What You’ll Learn
- Integrating tldraw infinite canvas with C1
- Creating custom shape utils for AI-generated content
- Building context-aware systems that understand selected shapes
- Real-time streaming content into canvas cards
- Implementing keyboard shortcuts for quick access
- Adding image search to enrich visual content
Architecture Overview
AI canvas apps combine visual collaboration tools with generative UI:Setup
Prerequisites
- Node.js 18+
- Thesys API key from console.thesys.dev
- (Optional) Google Custom Search API key and CSE ID for image search
Create Next.js Project
npm
- TypeScript: Yes
- ESLint: Yes
- Tailwind CSS: Yes
- App Router: Yes
- Customize default import alias: No
Install Dependencies
npm
npm
Environment Variables
Create a.env.local file:
Step 1: Set Up tldraw Canvas
Start by creating the infinite canvas workspace with tldraw:app/page.tsx
persistenceKey saves canvas state to localStorage so users don’t lose their work on refresh.
Step 2: Create C1 Component Shape
Define a custom tldraw shape that wraps C1 components:shapes/C1ComponentShape.tsx
shapeUtils/C1ComponentShapeUtil.tsx
Step 3: Extract Context from Selected Shapes
When users select existing cards, extract their content to provide context to the AI:utils/shapeContext.ts
Step 4: Create the API Endpoint
Build the backend endpoint that generates C1 responses for canvas cards:app/api/ask/route.ts
The system prompt is critical for canvas apps. Emphasize short, visually rich cards rather than long text blocks.
Step 5: Shape Creation and Management
Create a manager to handle the lifecycle of canvas shapes:utils/c1ShapeManager.ts
- Extracting context from selected cards
- Positioning the new card in the viewport
- Creating the shape
- Streaming C1 content as it generates
- Marking completion
Step 6: Add Image Search Tool (Optional)
Enhance cards with relevant images using tool calling. This step is optional. Create the image search tool:app/api/ask/tools.ts
app/api/ask/route.ts to use the tool:
Step 7: Keyboard Shortcuts
Add keyboard shortcut support. Update yourapp/page.tsx to include overrides:
app/page.tsx
$k syntax in tldraw means Cmd+K on Mac, Ctrl+K on Windows/Linux.
Step 8: Create Prompt Input Component
Create the UI component that users interact with:app/components/PromptInput.tsx
app/events/index.ts
Step 9: Register Shape Utils and Run
Create the shape utils registry:app/shapeUtils/index.ts
npm
- Press Cmd/Ctrl + K to open the prompt input
- Type “Create a product launch plan” and press Enter
- Watch as an AI-generated card appears on the canvas
- Select the card and press Cmd/Ctrl + K again
- Ask “What are the risks?” - the AI will see the first card’s context
- Experiment with multiple cards and selections
If you skipped Step 6 (image search), the canvas will work perfectly without it - you just won’t get automatic image embedding.
Key Concepts
How does context awareness work?
How does context awareness work?
When you select existing cards on the canvas and create a new one, the
extractC1ShapeContext function reads the C1 responses from selected shapes and sends them as context to the API. The LLM sees this context and generates responses that reference or build upon the selected cards.Why use custom shapes instead of overlays?
Why use custom shapes instead of overlays?
tldraw’s shape system provides built-in features: selection, resizing, repositioning, undo/redo, persistence, and export. Using custom shapes means your AI cards integrate seamlessly with tldraw’s native editing capabilities.
How do I make cards auto-resize to content?
How do I make cards auto-resize to content?
Currently, cards have fixed dimensions. For dynamic sizing, calculate content height in the shape component and call
editor.updateShape() with new height. Monitor the C1Component’s rendered size and update the shape’s h prop accordingly.Can I add multiple users to the same canvas?
Can I add multiple users to the same canvas?
Yes! tldraw has built-in collaboration support. Use their sync server or implement your own using the
store prop. Each user’s shapes, selections, and camera positions sync in real-time.Testing Your Canvas
Try these workflows to test context awareness:- Single card: “Create a product roadmap for Q1”
- Context-aware: Create “Tesla stock analysis” card, select it, then ask “What’s the outlook?”
- Multi-select: Create “Revenue data” and “Cost data” cards, select both, ask “Create a comparison chart”
- Follow-up: After any card, select it and ask “Expand on this with more details”
Going to Production
Before deploying:- Add authentication to prevent unauthorized API usage
- Implement rate limiting on the
/api/askendpoint - Set up canvas sharing if you want users to share their canvases
- Add export functionality - tldraw can export to PNG/SVG
- Consider collaboration - tldraw supports multiplayer mode
Full Example & Source Code
Try Live Demo
Experience the full infinite canvas. Try creating cards, selecting them for context, and building visual layouts with AI assistance.Try it now →
View Source Code
Complete implementation with tldraw integration, custom shape utils, context extraction, and streaming updates.Star on GitHub →