Canvas API

The Canvas API provides a drawable area that JavaScript can use to render shapes, text, images, and animations.

What you will learn

Minimal drawing

<canvas id="scene" width="240" height="120">A rectangle drawing appears here.</canvas>
const canvas = document.querySelector('#scene'); const context = canvas.getContext('2d'); context.fillStyle = '#2463eb'; context.fillRect(20, 20, 100, 60);

The origin (0, 0) is at the top-left. The first two arguments of fillRect are the x and y position; the next two are width and height.

Important boundaries

Suggested order

  1. Understand the canvas element.
  2. Learn getContext('2d'), coordinates, and rectangles.
  3. Add colors, paths, text, and images one step at a time.
  4. Test keyboard access and provide an equivalent description.