The HTML <canvas> element is a powerful tool for drawing graphics with JavaScript. It provides a drawable region on your page where you can create 2D shapes, render text, draw images, and build animations.
Canvas is widely used for games, data visualizations, photo editors, and interactive web applications. This tutorial walks through setup, drawing basics, styling, transformations, and a complete working example.
What You’ll Learn
01
Setup
canvas + ctx.
02
Shapes
Rect & arc.
03
Colors
fill & stroke.
04
Text
fillText.
05
Transform
rotate, scale.
06
Animate
rAF loop.
Fundamentals
What Is HTML Canvas?
The <canvas> element is part of HTML5. It renders a bitmap (grid of pixels) that you paint with JavaScript. Unlike static images, you can redraw the canvas every frame for animation or update charts when data changes.
Canvas is ideal when you need pixel-level control or thousands of moving objects. For logos and icons that scale cleanly, consider HTML SVG instead.
💡
Beginner Tip
Nothing appears on a canvas until you run JavaScript. The element only creates an empty drawing surface—getContext('2d') gives you the paintbrush.
Foundation
Setting Up the Canvas
Define the canvas in HTML with width and height attributes (the internal pixel size):
html
<canvas id="myCanvas" width="400" height="300">
Your browser does not support canvas.
</canvas>
Text between <canvas> tags is fallback content for very old browsers. See also the HTML canvas tag reference for all attributes.
Grid
Canvas Coordinates
The canvas uses a coordinate system where the top-left corner is (0, 0). X increases to the right; Y increases downward. When you draw at (50, 50), you place content 50 pixels from the left and 50 pixels from the top.
All drawing methods use these pixel coordinates unless you apply transformations (translate, scale, rotate) to the context.
Drawing
Drawing Shapes
Canvas provides built-in methods for common shapes. Here are the two you will use most often:
Rectangles
fillRect(x, y, width, height) draws a filled rectangle. strokeRect draws only the outline.
lightblue rectangle
"Hello Canvas" text
tomato circle
How It Works
Combines techniques from this tutorial. The original reference used an external image URL—this version uses shapes only so it works offline in Try It.
Pro Tips
Best Practices
✅ Do
Use save() and restore() around transforms
Set width/height attributes for pixel dimensions
Clear only the region that changed when optimizing animation
Provide fallback text inside <canvas>
Use requestAnimationFrame for smooth animation
❌ Don’t
Stretch canvas with CSS without adjusting internal resolution
Forget beginPath() between unrelated shapes
Draw images before onload fires
Rely on canvas alone for accessible data charts
Redraw the entire scene when a tiny part changed (at scale, optimize)
Applications
🚀 Common Use Cases
Games — sprites, collision, and frame loops.
Charts — bar, line, and pie visualizations (Chart.js uses canvas).
Image editing — filters and pixel manipulation.
Signatures — capture mouse/touch strokes.
Particle effects — fireworks, snow, confetti.
Video processing — draw video frames to canvas.
Compatibility
Universal Browser Support
Canvas 2D context is supported in all modern browsers. IE9+ had basic support; current Chrome, Firefox, Safari, and Edge fully support the API used in this tutorial.
✓ Baseline · Since HTML
Canvas 2D API
Canvas 2D context is supported in all modern browsers. IE9+ had basic support; current Chrome, Firefox, Safari, and Edge fully support the API used in this tutorial.
98%Modern browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Canvas 2D APIExcellent
Bottom line: Safe for production. Feature-detect with !!document.createElement('canvas').getContext if you need to support very old environments.
Wrap Up
Conclusion
HTML Canvas is a versatile tool for rendering dynamic graphics in web applications. With methods for shapes, text, images, and transformations, it enables rich interactive experiences. Master the 2D context API to build games, charts, and visual tools.
Next, compare bitmap canvas drawing with vector graphics in HTML SVG, or dive into the canvas tag reference for every attribute.
Canvas is an HTML element that provides a bitmap surface for drawing 2D graphics with JavaScript. You use methods like fillRect, arc, and fillText on a rendering context obtained via canvas.getContext('2d').
Canvas is pixel-based—you draw with JavaScript and cannot select individual shapes in the DOM. SVG is vector markup with DOM elements you can style and click. Use canvas for games and heavy animation; use SVG for icons and scalable illustrations.
Add <canvas id="myCanvas" width="400" height="300"></canvas>, then in JavaScript: var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d');. Call ctx.fillRect() or other drawing methods on ctx.
A blank canvas often means JavaScript ran before the element existed or getContext failed. Blurry drawings on retina screens happen when CSS width/height differs from canvas width/height attributes—set both or scale with devicePixelRatio.
ctx.save() pushes the current drawing state (styles, transforms) onto a stack. ctx.restore() pops it back. Use them around rotate/scale blocks so later drawings are not affected.
Canvas content is not exposed to screen readers by default. Provide fallback text inside <canvas> tags, duplicate important data in HTML, or use aria-label on the canvas. For interactive charts, consider SVG or supplement with a data table.
Did you know?
Canvas was introduced with HTML5 around 2004–2010 and quickly became the standard for in-browser games and charts. WebGL adds a webgl or webgl2 context for hardware-accelerated 3D on the same <canvas> element.