Canvas & SVG Design
Create professional-grade visual compositions programmatically using the HTML Canvas and SVG APIs.
Technical Options
1. HTML Canvas (Raster-based)
Best for complex rendering, particle systems, pixel manipulation, and performance-heavy visual effects.
Core Setup:
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 1200;
canvas.height = 630; // OG Image standard
Common Patterns:
- Layers: Draw multiple elements sequentially to create depth.
- Gradients:
ctx.createLinearGradient() or ctx.createRadialGradient().
- Masking:
ctx.globalCompositeOperation = 'source-atop'.
- Text:
ctx.font = 'bold 48px Inter', ctx.fillText().
2. SVG (Vector-based)
Best for sharp graphics, logos, icons, and illustrations that need to scale without quality loss.
Core Setup:
const svg = `
<svg width="1200" height="630" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#1a1a1a" />
<!-- elements go here -->
</svg>
`;
Common Patterns:
- Paths:
<path d="..." /> for complex shapes.
- Groups:
<g> for organization and transformation nesting.
- Filters:
<defs><filter id="blur">...</filter></defs> for effects.
Design Principles for Programmatic Design
1. Grid & Layout
- Use mathematical ratios (Golden Ratio, Rule of Thirds).
- Define margins and gutters as percentage of width/height.
- Ensure visual balance across the frame.
2. Color Systems
- Define a base color palette (see
brand-guidelines).
- Use HSL for easier mathematical manipulation (hue shifting, lightness variation).
- Implement programmatic contrast checking.
3. Typography
- Prioritize legibility.
- Define a clear hierarchy: Heading, Subheading, Meta, Body.
- Use programmatic spacing (kerning, line-height) consistent with the design scale.
Workflow
Step 1: Composition Planning
- Sketch the layout mathematically.
- Define the coordinate system (e.g., 0 to 100 on both axes).
- Identify key visual elements and their relationships.
Step 2: Implementation
- Implement the background and foundational shapes.
- Add gradients, textures, or secondary patterns.
- Layer in typography and primary visual assets.
- Apply final effects (noise, blur, drop shadows).
Step 3: Export
- Provide the self-contained HTML/JS or SVG code.
- Provide a preview command or a way to download the resulting PNG/SVG.
Example: OG Image Generator Template
// Mathematical layout for an OG Image
const w = 1200;
const h = 630;
const padding = w * 0.05;
// Draw Background
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, w, h);
// Draw Accent Gradient
const grad = ctx.createLinearGradient(0, 0, w, h);
grad.addColorStop(0, '#3b82f6');
grad.addColorStop(1, '#8b5cf6');
ctx.fillStyle = grad;
ctx.beginPath();
ctx.roundRect(padding, padding, w - padding*2, h - padding*2, 20);
ctx.fill();
// Draw Title
ctx.fillStyle = '#fff';
ctx.font = 'bold 80px Inter';
ctx.fillText('Programmatic Design', padding * 2, h/2);
1---2name: canvas-design3description: Creates visual designs using HTML Canvas or SVG APIs for banners, social media assets, posters, or UI components. Use when the user requests programmatic graphic design, visual composition, or custom image generation without using a photo editor. Do NOT use for photo realistic image generation or general web UI layouts unless it specifically requires canvas/SVG.4license: MIT5---6# Canvas & SVG Design78Create professional-grade visual compositions programmatically using the HTML Canvas and SVG APIs.910---1112## Technical Options1314### 1. HTML Canvas (Raster-based)15Best for complex rendering, particle systems, pixel manipulation, and performance-heavy visual effects.1617**Core Setup:**18```javascript19const canvas = document.createElement('canvas');20const ctx = canvas.getContext('2d');21canvas.width = 1200;22canvas.height = 630; // OG Image standard23```2425**Common Patterns:**26- **Layers**: Draw multiple elements sequentially to create depth.27- **Gradients**: `ctx.createLinearGradient()` or `ctx.createRadialGradient()`.28- **Masking**: `ctx.globalCompositeOperation = 'source-atop'`.29- **Text**: `ctx.font = 'bold 48px Inter'`, `ctx.fillText()`.3031### 2. SVG (Vector-based)32Best for sharp graphics, logos, icons, and illustrations that need to scale without quality loss.3334**Core Setup:**35```javascript36const svg = `37<svg width="1200" height="630" xmlns="http://www.w3.org/2000/svg">38 <rect width="100%" height="100%" fill="#1a1a1a" />39 <!-- elements go here -->40</svg>41`;42```4344**Common Patterns:**45- **Paths**: `<path d="..." />` for complex shapes.46- **Groups**: `<g>` for organization and transformation nesting.47- **Filters**: `<defs><filter id="blur">...</filter></defs>` for effects.4849---5051## Design Principles for Programmatic Design5253### 1. Grid & Layout54- Use mathematical ratios (Golden Ratio, Rule of Thirds).55- Define margins and gutters as percentage of width/height.56- Ensure visual balance across the frame.5758### 2. Color Systems59- Define a base color palette (see `brand-guidelines`).60- Use HSL for easier mathematical manipulation (hue shifting, lightness variation).61- Implement programmatic contrast checking.6263### 3. Typography64- Prioritize legibility.65- Define a clear hierarchy: Heading, Subheading, Meta, Body.66- Use programmatic spacing (kerning, line-height) consistent with the design scale.6768---6970## Workflow7172### Step 1: Composition Planning73- Sketch the layout mathematically.74- Define the coordinate system (e.g., 0 to 100 on both axes).75- Identify key visual elements and their relationships.7677### Step 2: Implementation78- Implement the background and foundational shapes.79- Add gradients, textures, or secondary patterns.80- Layer in typography and primary visual assets.81- Apply final effects (noise, blur, drop shadows).8283### Step 3: Export84- Provide the self-contained HTML/JS or SVG code.85- Provide a preview command or a way to download the resulting PNG/SVG.8687---8889## Example: OG Image Generator Template9091```javascript92// Mathematical layout for an OG Image93const w = 1200;94const h = 630;95const padding = w * 0.05;9697// Draw Background98ctx.fillStyle = '#111';99ctx.fillRect(0, 0, w, h);100101// Draw Accent Gradient102const grad = ctx.createLinearGradient(0, 0, w, h);103grad.addColorStop(0, '#3b82f6');104grad.addColorStop(1, '#8b5cf6');105ctx.fillStyle = grad;106ctx.beginPath();107ctx.roundRect(padding, padding, w - padding*2, h - padding*2, 20);108ctx.fill();109110// Draw Title111ctx.fillStyle = '#fff';112ctx.font = 'bold 80px Inter';113ctx.fillText('Programmatic Design', padding * 2, h/2);114```