Web/HTML/Element/canvas

From Get docs


Use the HTML <canvas> element with either the canvas scripting API or the WebGL API to draw graphics and animations.

Content categories Flow content, phrasing content, embedded content, palpable content.
Permitted content Transparent but with no interactive content descendants except for <a> elements, <button> elements, <input> elements whose type attribute is checkbox, radio, or button.
Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that accepts phrasing content.
Implicit ARIA role No corresponding role
Permitted ARIA roles Any
DOM interface HTMLCanvasElement

Attributes

This element's attributes include the global attributes.

height
The height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque ' '
Lets the canvas know whether or not translucency will be a factor. If the canvas knows there's no translucency, painting performance can be optimized. This is only supported by Mozilla-based browsers; use the standardized canvas.getContext('2d', { alpha: false }) instead.
width
The width of the coordinate space in CSS pixels. Defaults to 300.

Usage notes

Alternative content

You may (and should) provide alternate content inside the <canvas> block. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled. Providing a useful fallback text or sub DOM helps to make the the canvas more accessible.

Required </canvas> tag

Unlike the <img> element, the <canvas> element requires the closing tag (</canvas>).

Sizing the canvas using CSS versus HTML

The displayed size of the canvas can be changed using CSS, but if you do this the image is scaled during rendering to fit the styled size, which can make the final graphics rendering end up being distorted.

It is better to specify your canvas dimensions by setting the width and height attributes directly on the <canvas> elements, either directly in the HTML or by using JavaScript.

Maximum canvas size

The maximum size of a <canvas> element is very large, but the exact size depends on the browser. The following is some data we've collected from various tests and other sources (e.g. Stack Overflow):

Browser Maximum height Maximum width Maximum area
Chrome 32,767 pixels 32,767 pixels 268,435,456 pixels (i.e., 16,384 x 16,384)
Firefox 32,767 pixels 32,767 pixels 472,907,776 pixels (i.e., 22,528 x 20,992)
Safari 32,767 pixels 32,767 pixels 268,435,456 pixels (i.e., 16,384 x 16,384)
IE 8,192 pixels 8,192 pixels ?

Note: Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.


Examples

HTML

This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to render the canvas, or if can't read a canvas.

<canvas width="300" height="300"> 
  An alternative text describing what your canvas displays. 
</canvas>

JavaScript

Then in the JavaScript code, call HTMLCanvasElement.getContext() to get a drawing context and start drawing onto the canvas:

const canvas = document.querySelector('canvas'); 
const ctx = canvas.getContext('2d'); 
ctx.fillStyle = 'green'; 
ctx.fillRect(10, 10, 100, 100);

Result

Accessibility concerns

Alternative content

The <canvas> element on its own is just a bitmap and does not provide information about any drawn objects. Canvas content is not exposed to accessibility tools as semantic HTML is. In general, you should avoid using canvas in an accessible website or app. The following guides can help to make it more accessible.

Specifications

Specification Status Comment
HTML Living StandardThe definition of '<canvas>' in that specification. Living Standard
HTML5The definition of '<canvas>' in that specification. Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
canvas Chrome

Full support 1

Edge

Full support 12

Firefox Full support 1.5

Notes'

Full support 1.5

Notes'

Notes' Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers. Notes' Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions. Notes' Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

IE

Full support 9

Opera

Full support 9

Safari Full support 2

Notes'

Full support 2

Notes'

Notes' Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Versions of Safari prior to version 2 will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android Full support 4

Notes'

Full support 4

Notes'

Notes' Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers. Notes' Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions. Notes' Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

height Chrome

Full support 1

Edge

Full support 12

Firefox Full support 1.5

Notes'

Full support 1.5

Notes'

Notes' Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers. Notes' Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions. Notes' Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

IE

Full support 9

Opera

Full support 9

Safari Full support 2

Notes'

Full support 2

Notes'

Notes' Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Versions of Safari prior to version 2 will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android Full support 4

Notes'

Full support 4

Notes'

Notes' Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers. Notes' Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions. Notes' Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

moz-opaque

Non-standard'

Chrome

No support No

Edge

No support No

Firefox

Full support 3.5

IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

Full support 4

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

width Chrome

Full support 1

Edge

Full support 12

Firefox Full support 1.5

Notes'

Full support 1.5

Notes'

Notes' Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers. Notes' Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions. Notes' Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

IE

Full support 9

Opera

Full support 9

Safari Full support 2

Notes'

Full support 2

Notes'

Notes' Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Versions of Safari prior to version 2 will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android Full support 4

Notes'

Full support 4

Notes'

Notes' Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers. Notes' Prior to Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions. Notes' Before Firefox 12, if JavaScript is disabled, the <canvas> element was being rendered instead of showing the fallback content as per the specification. Since then, the fallback content is rendered instead.

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
See implementation notes.'
See implementation notes.


See also