Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Scripting

The <canvas> element

Reference for the HTML <canvas> element: a bitmap you draw on with JavaScript, the width and height attributes, fallback content, accessibility and technical details.

The <canvas> element creates a bitmap drawing surface. On its own it is just a transparent rectangle; you paint on it with JavaScript, using a rendering context such as the 2D API (getContext("2d")) or WebGL for 3D graphics. Charts, games, image filters, signature pads and generative art are common uses.

Because a canvas is only pixels, it has no built-in meaning for assistive technology. Content placed between the tags is fallback content: it is shown when the canvas can't be rendered and is the place to describe what the drawing shows.

HTML
<canvas id="chart" width="400" height="200">
  Bar chart: visitors per day, highest on Saturday (1,240).
</canvas>

Try it

Drawing a small chart

The fallback sentence inside <canvas> gives the same facts as the picture. Edit the numbers in data and press Run to redraw the chart.

Attributes

Attribute Values Description
width A non-negative integer Width of the bitmap, in canvas pixels. Defaults to 300.
height A non-negative integer Height of the bitmap. Defaults to 150.

It also accepts the global attributes.

Usage notes

  • Attribute size vs CSS size. width and height set the number of pixels in the bitmap. CSS width and height only stretch the finished image, so setting a size in CSS alone makes the drawing blurry or distorted. Keep the two in proportion.
  • Sharp on high-density screens. Multiply the bitmap size by window.devicePixelRatio, keep the CSS size the same, and scale the context (ctx.scale(ratio, ratio)) so lines stay crisp.
  • Changing size clears it. Setting canvas.width or canvas.height, even to the same value, wipes the drawing and resets the context state.
  • Contexts. getContext() accepts "2d", "webgl", "webgl2", "bitmaprenderer" and, in browsers with WebGPU, "webgpu". A canvas can only have one kind of context.
  • Exporting. canvas.toDataURL() and canvas.toBlob() turn the drawing into an image file. They throw a security error if the canvas has been "tainted" by drawing an image from another origin without CORS.
  • Accessibility. Describe the drawing in the fallback content or with role="img" and aria-label. For interactive canvases, keep any controls as real HTML elements outside or inside the fallback, not just painted pixels.
  • Consider SVG. For diagrams with few shapes, text that should be selectable, or graphics that must scale cleanly, <svg> is often the better choice.
  • Workers. canvas.transferControlToOffscreen() lets a Web Worker draw on the canvas without blocking the page.

Technical summary

Content categories Flow content, phrasing content, embedded content, palpable content
Permitted content Transparent, but with no interactive content descendants except <a> elements, <img> elements with usemap, <button> elements, <input> elements whose type is checkbox, radio or button, and <select> elements with multiple or a size greater than 1
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts phrasing content
Implicit ARIA role No corresponding role
DOM interface HTMLCanvasElement
Default display inline

Browser support

The element and its 2D context are supported in every browser. WebGPU contexts are newer and not yet available everywhere.