Canvas design

From oyi77/1ai-skills · View interactive version

Canvas design is an AI agent skill from the GitHub repository oyi77/1ai-skills. Use when design visual art and graphics using HTML5 Canvas, p5.js, or SVG. Create generative art, data visualizations, diagrams, and interactive graphics. Use when designing visual art and graphics using html5 canvas, p5.js, or.

How to use this skill

Skill file

Canvas Design

When to Use

Trigger phrases:

When NOT to Use

Overview

Create professional visual content using programmatic design tools. Supports HTML5 Canvas, p5.js for generative art, SVG for scalable graphics, and Canvas API for data visualization.

Canvas design spans three approaches: pixel-level rendering (HTML Canvas API, node-canvas) where every pixel is explicitly drawn; declarative vectors (SVG) where shapes are defined as DOM elements that remain selectable and scalable; and creative coding (p5.js, Processing) which provides a friendly API for generative art and interactive sketches.

Python stack: Pillow (PIL) for server-side image generation, compositing, and batch processing. Works in CI/CD pipelines, web backends, and data workflow automation.

Browser stack: HTML5 Canvas 2D API for raster graphics, SVG for vector graphics, and p5.js for generative art and creative coding. Zero-install — runs in every browser.

Node.js stack: node-canvas provides a Cairo-backed Canvas API for server-side rendering, enabling automated social media image generation, chart rendering, and thumbnail creation.

Key capabilities: generative art with algorithmic patterns, data-driven infographics and charts, responsive diagrams that scale across devices, real-time interactive visualizations, and automated asset generation for content pipelines.

Workflow

  1. Define visual spec — Dimensions, style, color palette, content
  2. Choose tool — p5.js (generative), SVG (scalable), Canvas (pixel)
  3. Create composition — Layout, shapes, text, images
  4. Apply style — Colors, gradients, shadows, typography
  5. Export — PNG, SVG, or interactive HTML

Tool Selection Guidance

Design Principles

Anti-Rationalization Table

| Rationalization | Reality |

|---|---|

| "I will use a template" | Programmatic design is reproducible, data-driven, and unique |

| "Design tools are for designers" | Canvas/SVG code is just code — engineers can create visual content |

| "Stock images work" | Custom graphics communicate your specific message better |

| "I'll screenshot a chart library" | Custom canvas code matches exact brand requirements, not just generic library defaults |

| "Canvas is too slow for complex graphics" | Spatial indexing, off-screen canvases, and WebGL handle millions of elements efficiently |

| "SVG is better for everything" | Canvas is faster for real-time rendering, pixel manipulation, and dense scatter plots |

Code Example (p5.js)

function setup() {
  createCanvas(800, 600);
  background(20);
  for (let i = 0; i < 200; i++) {
    let x = random(width);
    let y = random(height);
    let size = random(2, 8);
    fill(100, 200, 255, random(50, 200));
    noStroke();
    ellipse(x, y, size);
  }
  fill(255);
  textSize(32);
  textAlign(CENTER);
  text('Data Visualization', width/2, 50);
}

Code Example (Python PIL)

from PIL import Image, ImageDraw, ImageFont
import random

# Create a dark canvas
img = Image.new('RGBA', (800, 600), (17, 17, 34, 255))
draw = ImageDraw.Draw(img)

# Generative particle field
for _ in range(200):
    x = random.randint(0, 800)
    y = random.randint(0, 600)
    r = random.uniform(2, 8)
    alpha = random.randint(50, 200)
    draw.ellipse([x-r, y-r, x+r, y+r], fill=(100, 200, 255, alpha))

# Title text
try:
    font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 32)
except (OSError, AttributeError):
    font = ImageFont.load_default()
draw.text((400, 50), "Data Visualization", fill=(255, 255, 255), font=font, anchor="mm")

img.save('canvas_output.png')

Code Example (HTML5 Canvas)

const canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
const ctx = canvas.getContext('2d');

// Background
ctx.fillStyle = '#111122';
ctx.fillRect(0, 0, 800, 600);

// Generative particle field
for (let i = 0; i < 200; i++) {
    const x = Math.random() * 800;
    const y = Math.random() * 600;
    const r = Math.random() * 6 + 2;
    ctx.beginPath();
    ctx.arc(x, y, r, 0, Math.PI * 2);
    ctx.fillStyle = `rgba(100, 200, 255, ${Math.random() * 0.4 + 0.1})`;
    ctx.fill();
}

// Title text
ctx.fillStyle = '#ffffff';
ctx.font = '32px sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('Data Visualization', 400, 50);

Setup & Configuration

Python environment

pip install Pillow

PIL/Pillow supports RGBA images, TrueType fonts, shape drawing, and image compositing. For vector output, use svgwrite or cairosvg.

Node.js environment (node-canvas)

npm install canvas

Requires Cairo (system library): apt install libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev

Browser (no install needed)

HTML5 Canvas and SVG are built into every modern browser. The Canvas API gives pixel-level control; SVG gives declarative vector graphics. No external dependencies required.

p5.js

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.min.js"></script>

Common Issues & Troubleshooting

| Problem | Solution |

|---|---|

| Canvas renders blurry on Retina/HiDPI screens | Scale the backing store by devicePixelRatio: set canvas.width = cssWidth * dpr; canvas.height = cssHeight * dpr; then scale the context with ctx.scale(dpr, dpr). Keep CSS size at intended display dimensions. |

| Performance drops with thousands of elements | Use off-screen canvases for static layers; batch draw calls under a single beginPath(); consider WebGL (canvas.getContext('webgl')) for particle systems over 10K elements. |

| Lines and text look jagged | Align 1px lines to the pixel grid with ctx.translate(x + 0.5, y + 0.5); use ctx.imageSmoothingEnabled = false for pixel-art; set text-rendering: geometricPrecision for SVG text. |

| Canvas sizing mismatch (CSS vs. element) | The <canvas> has two sizes: the drawing buffer (width/height attributes) and CSS display size. Always set both to prevent stretching. Use getBoundingClientRect() to map mouse coordinates. |

| Memory leaks from continuous animation | Call clearRect(0, 0, w, h) before each frame; reuse Path2D objects instead of creating new ones every frame. In p5.js, use noLoop() when animation isn't needed. |

| SVG performance with dense elements | Beyond ~10K DOM nodes, use Canvas instead of SVG. For hybrid data viz: render scatter/network on Canvas, overlay axes and labels via SVG or HTML. |

| Cross-origin images taint the canvas | Set img.crossOrigin = "anonymous" and ensure the server sends Access-Control-Allow-Origin. Without this, toDataURL() / toBlob() throw a security error. |

Monetization

Process

  1. Prepare — Gather requirements, verify prerequisites, set up environment
  2. Execute — Run canvas design workflow with configured parameters
  3. Verify — Validate output meets requirements, document results

Detailed Canvas Process

  1. Coordinate system planning — Map visual elements to canvas coordinates. In HTML Canvas, (0,0) is top-left, Y increases downward. In SVG, the viewBox defines the coordinate space. Plan for responsive scaling from the start.
  2. Retina/DPI handling — Scale the canvas backing store by window.devicePixelRatio: set canvas.width = cssWidth * dpr; canvas.height = cssHeight * dpr; and scale the context with ctx.scale(dpr, dpr). Keep CSS size at intended display dimensions.
  3. Composition layers — Use off-screen canvases for complex scenes: draw static backgrounds once onto a separate canvas, then composite dynamic elements on top each frame. This avoids redrawing unchanged geometry.
  4. Export pipeline — canvas.toBlob() for PNG/JPEG, canvas.toDataURL() for inline images, or SVG serialization for vector output. For print, calculate canvas resolution at 300 DPI equivalent.

Verification

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.