The <canvas> element
The <canvas> tag creates a blank drawing area that you paint on with JavaScript, pixel by pixel. It powers charts, games, image editors and animations. On its own it shows nothing: you get a drawing context with getContext("2d") and then call methods like fillRect(). Set its size with the width and height attributes.
Last updated: October 3, 2026
Quick facts
| Category | Flow content, phrasing content, embedded content, palpable content |
| Content model | Fallback content, shown only when canvas isn’t supported |
| Tag omission | Neither the start nor the end tag can be omitted |
| Permitted parents | Any element that accepts embedded content |
| DOM interface | HTMLCanvasElement |
Syntax
Live example
Edit the code below and watch the preview update instantly.
Attributes
| Attribute | Description |
|---|---|
| width | The drawing width in pixels (default 300) |
| height | The drawing height in pixels (default 150) |
Common examples
Drawing basic shapes
Get the 2D context, then draw rectangles, circles and text with simple commands.
A simple animation
requestAnimationFrame redraws the canvas smoothly, about 60 times a second.
Saving the drawing as an image
toDataURL() turns the canvas into a PNG that visitors can download.
2D canvas vs WebGL
A canvas offers different drawing “contexts”. The 2D context (getContext("2d")) is the easy one: shapes, lines, text and images with simple commands. It’s perfect for charts, signature pads, simple games and image editing.
WebGL (getContext("webgl2")) uses the graphics card directly for 3D scenes and very large numbers of moving objects. It’s much faster for heavy work but far more complex, so most people use it through a library such as Three.js rather than writing it by hand.
| 2D context | WebGL | |
|---|---|---|
| Difficulty | Beginner-friendly | Advanced |
| Best for | Charts, games, drawing | 3D, heavy graphics |
| Typical tool | Plain JavaScript | Three.js, Babylon.js |
Common mistakes to avoid
- Sizing the canvas with CSS only. CSS stretches the drawing; set the
widthandheightattributes to the real pixel size. - Putting important text only in the canvas. Screen readers and search engines can’t read pixels — keep important text in the HTML.
- No fallback content. Put a short description between the canvas tags for screen readers and old browsers.
- Running animations forever. Stop the animation loop when it isn’t visible to save battery on phones.
Accessibility & SEO tips
- Describe what the canvas shows in its fallback content, or give it
role="img"and anaria-labelfor simple pictures. - For charts, also provide the data as a <table> so everyone, including search engines, can read the numbers.
- Respect
prefers-reduced-motionand pause animations for visitors who have asked for less movement.
Browser support
Supported in all modern browsers, including Chrome, Edge, Firefox, Safari and their mobile versions. It was introduced with HTML5, and only very old browsers such as Internet Explorer 8 needed workarounds. WebGL 2 is also supported in all current browsers.