Skip to content

<canvas> - Graphics Canvas Element

Embedded Content HTML5

The <canvas> element provides a drawable region in HTML where you can render graphics, animations, game graphics, data visualizations, photo manipulation, and real-time video processing using JavaScript. Unlike SVG which is vector-based, canvas uses a bitmap (raster) approach with immediate-mode rendering.

Result
<canvas id="myCanvas" width="600" height="400">
Fallback content for browsers that don't support canvas
</canvas>
AttributeValueDescription
widthNumberCanvas width in pixels (default: 300)
heightNumberCanvas height in pixels (default: 150)

The <canvas> element supports all global attributes.

Before drawing, you must obtain a rendering context:

Result
Result
Result
Result
Result
Result

Canvas supports various transformations:

Result
Result
Result

Understanding when to use canvas versus SVG:

Use Canvas For

  • High-performance games
  • Pixel manipulation
  • Many objects (1000+)
  • Real-time data visualization
  • Photo editing
  • Particle effects

Use SVG For

  • Scalable graphics
  • Interactive UI elements
  • Accessibility requirements
  • SEO-friendly graphics
  • Print-quality output
  • Document illustrations

Canvas creates significant accessibility challenges because it renders to a bitmap:

Result
  1. Provide text alternatives: Use ARIA labels or fallback content
  2. Add keyboard navigation: For interactive canvases
  3. Announce dynamic changes: Use ARIA live regions
  4. Consider alternative formats: Provide data tables for charts
  5. Test with screen readers: Ensure meaningful experience
  1. Batch draw calls: Minimize state changes
  2. Use layers: Multiple canvases for static/dynamic content
  3. Optimize clearing: Clear only changed regions
  4. Use offscreen canvas: Pre-render complex elements
  5. Avoid unnecessary redraws: Track dirty regions
  6. Reduce canvas size: Use CSS scaling when appropriate
Result

When you draw cross-origin images, the canvas becomes “tainted”:

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const img = new Image();
// This will taint the canvas if image is from another origin
img.src = 'https://example.com/image.jpg';
img.onload = () => {
ctx.drawImage(img, 0, 0);
// This will throw a security error
// ctx.getImageData(0, 0, 100, 100); // Error!
// canvas.toDataURL(); // Error!
};

To avoid tainting, configure CORS properly:

const img = new Image();
img.crossOrigin = 'anonymous';
img.src = 'https://example.com/image.jpg';

Excellent Support

The <canvas> element is supported in all modern browsers:

  • Chrome: 4+ (2010)
  • Firefox: 2+ (2006)
  • Safari: 3.1+ (2008)
  • Edge: All versions
  • Opera: 9+ (2006)
  • Mobile: Full support

Fallback: Provide alternative content inside the <canvas> tags for older browsers.

  • <svg> - Vector graphics alternative
  • <img> - Static images
  • WebGL - 3D graphics context for canvas
  • OffscreenCanvas - Canvas in Web Workers
  • ImageBitmap - Optimized image rendering
  1. Set dimensions correctly: Use width/height attributes, not CSS
  2. Always provide fallback: Add meaningful content inside tags
  3. Clear before redrawing: Prevent visual artifacts
  4. Save/restore context state: When using transformations
  5. Handle high DPI displays: Scale for retina screens
  6. Optimize for performance: Use layers and batch operations
  7. Make it accessible: Add ARIA labels and text alternatives
  8. Test across devices: Performance varies significantly

Learn More: