Our volunteers haven't translated this article into Hrvatski yet. Join us and help get the job done!
The HTML <canvas>
Element can be used to draw graphics via scripting (usually JavaScript). For example, it can be used to draw graphs, make photo compositions or even perform animations. 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.
For more articles on canvas, see the canvas topic page.
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 parent elements | Any element that accepts phrasing_content. |
DOM interface | HTMLCanvasElement |
Attributes
This element includes 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.
width
- The width of the coordinate space in CSS pixels. Defaults to 300.
Description
Required </canvas>
tag
Unlike the <img>
element, the <canvas>
element requires the closing tag (</canvas>
).
Sizing the 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.
Examples
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>
If your canvas does not use transparency set the moz-opaque
attribute on the canvas tag. This information can be used internally to optimize rendering. However, this attribute has not been standardized and only works in Mozilla-based rendering engines.
<canvas id="mycanvas" moz-opaque></canvas>
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of '<canvas>' in that specification. |
Living Standard | |
HTML5 The definition of '<canvas>' in that specification. |
Recommendation | Initial definition |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | 1.5 (1.8)[1] 6.0 (6.0)[2] 12.0 (12.0)[3] |
9.0 | 9.0[4] | 2.0[5] |
moz-opaque |
Not supported | 3.5 (1.9.1) | Not supported | Not supported | Not supported |
Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 1.5 (1.8)[1] 6.0 (6.0)[2] 12.0 (12.0)[3] |
? | ? | ? | 1.0 |
moz-opaque |
1.0 (1.9.1) | Not supported | Not supported | Not supported | Not supported |
[1] Before Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the canvas width and height were signed integers instead of unsigned integers.
[2] Prior to Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), a <canvas>
element with a zero width or height would be rendered as if it had default dimensions.
[3] Before Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9), if JavaScript is disabled, the <canvas>
element was being rendered instead of showing the fallback content as per the specification. Now the fallback content is rendered instead.
[4] See the changelog for Opera 9.0.
[5] 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. Those versions of Safari (prior to version 2.0) will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it. Fortunately, users of these versions of Safari are rare nowadays.
See also
- MDN canvas portal
- Canvas tutorial
- Canvas cheat sheet
- Canvas-related demos
- Canvas introduction by Apple