Use the <canvas>
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. |
Permitted ARIA roles | Any |
DOM interface | HTMLCanvasElement |
This element's attributes include the global attributes.
height
moz-opaque
canvas.getContext('2d', { alpha: false })
instead.width
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.
</canvas>
tagUnlike the <img>
element, the <canvas>
element requires the closing tag (</canvas>
).
The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size. If your renderings seem distorted, try specifying your width
and height
attributes explicitly in the <canvas>
attributes, and not using CSS.
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. Providing a useful fallback text or sub DOM helps to make the the canvas more accessible.
<canvas id="canvas" width="300" height="300"> An alternative text describing what your canvas displays. </canvas>
Then in the JavaScript code, call HTMLCanvasElement.getContext()
to get a drawing context and start drawing onto the canvas:
<script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.fillStyle = 'green'; ctx.fillRect(10, 10, 100, 100); </script>
If your canvas does not use transparency, you can tell the browser that your canvas is opaque, this will be used internally to optimize rendering. To do this, set alpha
to false
when getting the drawing context:
<script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d', { alpha: false }); </script>
Before the alpha
option was standardized, you could use the moz-opaque
attribute on the canvas tag. However, this only works in Mozilla-based rendering engines and should be avoided; check bug 878155 to track when this attribute will be removed.
<canvas id="myCanvas" moz-opaque></canvas>
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 like 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.
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of '<canvas>' in that specification. | Living Standard | |
HTML5 The definition of '<canvas>' in that specification. | Recommendation | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1.5
|
9 | 9 | 2
|
height |
1 | Yes | 1.5
|
9 | 9 | 2
|
moz-opaque
|
No | No | 3.5 | No | No | No |
width |
1 | Yes | 1.5
|
9 | 9 | 2
|
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | ? | Yes | 4
|
No | 1 | ? |
height |
? | ? | Yes | 4
|
No | 1 | ? |
moz-opaque
|
No | No | No | 4 | No | No | No |
width |
? | ? | Yes | 4
|
No | 1 | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas