2D Graphics

HTML5 Canvas

Create dynamic 2D graphics, animations, and interactive visualizations using the HTML5 Canvas API. Perfect for games, data visualization, and creative applications.

What is the HTML canvas element?

<canvas> is a blank drawing surface that you paint on with JavaScript, pixel by pixel, through the 2D context (getContext("2d")) or WebGL for 3D. It's used for games, charts, image editing and visual effects. Unlike SVG, nothing you draw is an element in the DOM, so it's fast for many objects but not accessible on its own.

<canvas id="c" width="300" height="150">Fallback text</canvas>
<script>
  const ctx = document.getElementById("c").getContext("2d");
  ctx.fillStyle = "#3b82f6";
  ctx.fillRect(20, 20, 100, 60);
  ctx.font = "20px sans-serif";
  ctx.fillText("Hello", 150, 60);
</script>

Canvas Fundamentals

The HTML5 Canvas element provides a drawable region defined in HTML code with height and width attributes. JavaScript code may access the area through drawing APIs, enabling dynamic generation of graphics, game graphics, art, or other visual elements.

Drawing API

Shapes, paths, text, and images

Pixel Control

Direct pixel manipulation

Interactive

Mouse and touch events

Essential Canvas Methods

Drawing Shapes

  • • fillRect() - filled rectangle
  • • strokeRect() - outlined rectangle
  • • arc() - circles and arcs
  • • beginPath() - start new path

Text & Images

  • • fillText() - filled text
  • • strokeText() - outlined text
  • • drawImage() - draw images
  • • measureText() - text metrics

Styling

  • • fillStyle - fill color/pattern
  • • strokeStyle - stroke color
  • • lineWidth - line thickness
  • • createLinearGradient()

Transformations

  • • translate() - move origin
  • • rotate() - rotate canvas
  • • scale() - scale drawing
  • • save() / restore() - state

Complete Canvas Examples

Interactive canvas examples demonstrating shapes, text, gradients, image manipulation, drawing tools, charts, and pixel effects.

Code Editor

<canvas id="c" width="320" height="200">A drawing of shapes and text</canvas>

Preview

Performance & Best Practices

Optimization Tips

  • • Use requestAnimationFrame() for smooth animations
  • • Clear only necessary canvas areas
  • • Batch drawing operations
  • • Use appropriate canvas resolution
  • • Minimize context state changes

Best Practices

  • • Provide fallback content
  • • Handle high DPI displays
  • • Use offscreen canvas for complex drawings
  • • Implement proper event handling
  • • Consider accessibility requirements

Canvas Development Checklist

  • Set appropriate canvas dimensions
  • Handle both mouse and touch events
  • Implement efficient rendering loops
  • Test across different devices
  • Provide accessibility alternatives
  • Optimize for performance

HTML Canvas — step by step

1 / 4
1

Add the canvas element

The canvas element creates a blank drawing surface. Set width and height attributes (not CSS) to define the pixel dimensions of the drawing area.

1 of 4
🔍Canvas vs SVG
Canvas draws pixels directly and is best for complex, high-performance graphics like games or real-time data visualizations. SVG maintains a DOM tree and is better for static or lightly animated graphics that need to stay accessible and scalable.

🎯 Quick Check

What JavaScript method do you call to get started with 2D drawing on a canvas element?

HTML Canvas: summary

Key points

  • Set the size with the width and height attributes. CSS only stretches the bitmap, which blurs it.
  • On high-DPI screens, multiply the canvas size by devicePixelRatio and scale the context for sharp output.
  • Provide fallback content or an accessible description, because screen readers can't see what's drawn.

Frequently asked questions

›Why is my canvas blurry?

Either CSS is resizing it, or the screen is high-DPI. Set the width/height attributes to the CSS size multiplied by window.devicePixelRatio, then call ctx.scale(dpr, dpr).