HTML5 Canvas Introduction

Beginner
6 min read
Updated: Nov 2026
2D drawing API

What Is HTML5 Canvas?

The HTML5 <canvas> element is a bitmap drawing surface. JavaScript paints pixels with the Canvas 2D API (or WebGL), so you can draw shapes, charts, animations, and games in the browser.

A pixel grid

Once you draw, the page stores colored pixels. Each shape is not its own HTML element.

Drawn with JavaScript

An empty <canvas> shows nothing. You call getContext('2d'), then methods such as fillRect.

Redraw to move

To animate, clear the surface and paint the next frame. requestAnimationFrame is the usual loop.

Remember
Pipeline:
  <canvas> → getContext('2d') → draw methods → pixels on screen

Raster:   pixel grid (not DOM shapes)
Animate:  clear + redraw (often with requestAnimationFrame)
Size:     set width/height attributes (drawing resolution)

Unlike SVG, canvas does not keep each shape as a DOM node. Once you draw, you have colored pixels. To move something, you usually clear and redraw.

Set the size in HTML. Put width and height on the element itself. Sizing the canvas with CSS alone stretches the bitmap and can blur the drawing.

Why Use Canvas?

Games, dashboards, and creative tools often need to redraw many pixels every frame without creating thousands of DOM elements.

Fast redraws

Paint a bitmap each frame. The page does not rebuild a tree of elements.

Pixel control

Read and write pixels with getImageData when you build filters or photo tools.

Games and particles

Move many objects without giving each one its own element.

Charts and editors

Draw custom plots, brushes, and dashboards that markup alone cannot describe.

How Canvas Drawing Works

Every sketch follows the same short loop.

Markup
Add a canvas and set its pixel size. <canvas width="400" height="200"> is a 400 by 200 drawing.
Context
canvas.getContext('2d') returns the object that exposes the drawing methods.
Draw
Call methods such as fillRect, stroke, and fillText to paint pixels.
Animate
Optional. Clear the surface and redraw on each frame with requestAnimationFrame.

Your First Canvas Drawing

Always set width and height attributes. CSS size alone stretches the bitmap and can blur the image.

HTML
<canvas id="myCanvas" width="400" height="200"></canvas>
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "red";
ctx.fillRect(10, 10, 100, 100);
Try It Yourself

How it works

  1. Find the canvas

    getElementById returns the element from the page.

  2. Get the 2D context

    getContext('2d') is the object that exposes the drawing methods.

  3. Paint

    fillStyle sets the color. fillRect(x, y, w, h) fills a rectangle.

Next, practice strokes with Canvas Draw Lines.

Canvas vs SVG vs PNG

Both Canvas and SVG draw graphics in the browser. Pick the one that matches the job.

TopicSVGCanvasPNG / JPEG
What it isVector markupRaster pixels drawn with JavaScriptRaster image files
Page structureEach shape is in the DOMNo element per shapeOne bitmap
ScalingStays sharpCan blur if stretchedPixelates when enlarged
Best forIcons and diagramsGames and animationPhotographs

When to choose which

Choose Canvas

You redraw many objects every frame, edit pixels or photos, or build a 2D game or particle system.

Choose SVG or a raster image

Use SVG for crisp icons, diagrams, CSS styling, and clickable shapes. Use PNG or JPEG for photographs.

Many sites use both: SVG for UI icons, Canvas for interactive plots or games. See the SVG introduction for the vector side.

Core 2D API Starters

These are the drawing methods you will meet first in this tutorial.

  1. 01 Lines moveTo, lineTo Build a path, then stroke it.
  2. 02 Rectangles fillRect Fill, outline, and clear rectangular regions.
  3. 03 Arcs arc Draw circles and parts of a circle.
  4. 04 Paths beginPath Combine lines into a closed shape, then fill or stroke it.
  5. 05 Quadratic curves quadraticCurveTo Bend a line with one control point.
  6. 06 Bezier curves bezierCurveTo Draw a smoother curve with two control points.
  7. 07 Text fillText Paint words onto the bitmap.

Where Is Canvas Used?

These are the places a scripted bitmap shows up in real products.

2D games

Many sprites are redrawn each frame.

Charts and dashboards

Custom plots stay light because they do not build a heavy DOM tree.

Drawing and photo tools

Brushes, filters, and pixel edits live on the bitmap.

Animations and effects

Particles and transitions run inside requestAnimationFrame.

Generative art

Procedural shapes and patterns are painted in code.

Simulations

Physics and interactive visualizations update by redrawing the frame.

Keep This in Mind

  • Canvas is a bitmap surface you paint with JavaScript.
  • Pipeline: element, then getContext('2d'), then draw methods.
  • Start with fillRect, then lines, paths, and arcs.
  • Canvas or SVG: use Canvas for pixels and games, and SVG for scalable shapes in the page.

Frequently Asked Questions

Canvas is an HTML element that provides a bitmap drawing surface. JavaScript uses the Canvas 2D API (or WebGL) to render shapes, charts, animations, and games in the browser.
Yes. The canvas element alone is an empty box. You need JavaScript to call getContext('2d') and drawing methods like fillRect or stroke.
Neither is always better. Canvas is raster-based and strong for animations, games, and pixel work. SVG is vector-based and better for scalable icons and diagrams you can style in the DOM.
Charts, visualizations, photo tools, drawing apps, particle effects, 2D games, and interactive simulations. Libraries like Chart.js and Phaser build on canvas or WebGL.
Yes. Set explicit width and height attributes on the element. Sizing only with CSS stretches the bitmap and can blur drawings.
Practice lines, rectangles, arcs, and paths. Then learn text, state (save/restore), colors, and transforms using the Canvas topics in the sidebar.

Did you know?

Add a <canvas width="400" height="200"></canvas> element, then call canvas.getContext('2d') in JavaScript to draw shapes, text, and animations pixel by pixel.

Next: Core Drawing

Browse lines, rectangles, arcs, curves, and paths — then open each guide.

Core Drawing →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful