Summary
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 {{HTMLElement("a")}} elements, {{HTMLElement("button")}} elements, {{HTMLElement("input")}} elements whose {{htmlattrxref("type", "input")}} attribute is
checkbox
,radio
, orbutton
. - Tag omission {{no_tag_omission}}
- Permitted parent elements Any element that accepts phrasing_content.
- DOM interface {{domxref("HTMLCanvasElement")}}
Attributes
This element includes the global attributes.
- {{htmlattrdef("height")}}
- The height of the coordinate space in CSS pixels. Defaults to 150.
- {{htmlattrdef("moz-opaque")}} {{non-standard_inline}}
- 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.
- {{htmlattrdef("width")}}
- The width of the coordinate space in CSS pixels. Defaults to 300.
Description
Required </canvas>
tag
Unlike the {{HTMLElement("img")}} element, the {{HTMLElement("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
<canvas id="canvas" width="300" height="300"> Sorry, your browser doesn't support the <canvas> element. </canvas>
If you use canvas and doesn’t need to be transparent, 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 does only work in Mozilla-based rendering engines.
<canvas id="mycanvas" moz-opaque></canvas>
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', 'the-canvas-element.html#the-canvas-element', '<canvas>')}} | {{Spec2('HTML WHATWG')}} | |
{{SpecName('HTML5 W3C', 'the-canvas-element.html#the-canvas-element', '<canvas>')}} | {{Spec2('HTML5 W3C')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | {{CompatGeckoDesktop("1.8")}} [1][2][3] | 9.0 | 9.0 | 2.0 [4] |
moz-opaque |
{{CompatNo}} | {{CompatGeckoDesktop("1.9.1")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatGeckoMobile("1.8")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | 1.0 |
moz-opaque |
{{CompatGeckoMobile("1.9.1")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Browser-specific notes
Firefox (Gecko)
[1] Before Gecko 5.0 {{geckoRelease("5.0")}}, the canvas width and height were signed integers instead of unsigned integers.
[2] Prior to Gecko 6.0 {{geckoRelease("6.0")}}, a {{HTMLElement("canvas")}} element with a zero width or height would be rendered as if it had default dimensions.
[3] Before Gecko 12.0 {{geckoRelease("12.0")}}, 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] 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.