1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
<header><h1><canvas>: The Graphics Canvas element</h1></header><div class="section-content">
<p>Use the <code><canvas></code> with either the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API">canvas scripting API</a> or the <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API">WebGL API</a> to draw graphics and animations.</p> <figure class="table-container"><div class="_table"><table class="properties"> <tbody> <tr> <th scope="row"><a href="../content_categories">Content categories</a></th> <td> <a href="../content_categories#flow_content">Flow content</a>, <a href="../content_categories#phrasing_content">phrasing content</a>, <a href="../content_categories#embedded_content">embedded content</a>, palpable content. </td> </tr> <tr> <th scope="row">Permitted content</th> <td> Transparent but with no <a href="../content_categories#interactive_content">interactive content</a> descendants except for <a href="a"><code><a></code></a> elements, <a href="button"><code><button></code></a> elements, <a href="input"><code><input></code></a> elements whose <a href="input#type"><code>type</code></a> attribute is <code>checkbox</code>, <code>radio</code>, or <code>button</code>. </td> </tr> <tr> <th scope="row">Tag omission</th> <td>None, both the starting and ending tag are mandatory.</td> </tr> <tr> <th scope="row">Permitted parents</th> <td> Any element that accepts <a href="../content_categories#phrasing_content">phrasing content</a>. </td> </tr> <tr> <th scope="row">Implicit ARIA role</th> <td><a href="https://www.w3.org/TR/html-aria/#dfn-no-corresponding-role" target="_blank">No corresponding role</a></td> </tr> <tr> <th scope="row">Permitted ARIA roles</th> <td>Any</td> </tr> <tr> <th scope="row">DOM interface</th> <td><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement"><code>HTMLCanvasElement</code></a></td> </tr> </tbody> </table></div></figure>
</div>
<h2 id="attributes">Attributes</h2>
<div class="section-content">
<p>This element's attributes include the <a href="../global_attributes">global attributes</a>.</p> <dl> <dt id="height"><a href="#height"><code>height</code></a></dt> <dd> <p>The height of the coordinate space in CSS pixels. Defaults to 150.</p> </dd> <dt id="moz-opaque">
<a href="#moz-opaque"><code>moz-opaque</code></a> <abbr class="icon icon-nonstandard" title="Non-standard. Check cross-browser support before using."> <span class="visually-hidden">Non-standard</span> </abbr> <abbr class="icon icon-deprecated" title="Deprecated. Not for use in new websites."> <span class="visually-hidden">Deprecated</span> </abbr>
</dt> <dd> <p>Lets the canvas know whether 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 <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext"><code>canvas.getContext('2d', { alpha: false })</code></a> instead.</p> </dd> <dt id="width"><a href="#width"><code>width</code></a></dt> <dd> <p>The width of the coordinate space in CSS pixels. Defaults to 300.</p> </dd> </dl>
</div>
<h2 id="usage_notes">Usage notes</h2>
<h3 id="alternative_content">Alternative content</h3>
<div class="section-content"><p>You should provide alternate content inside the <code><canvas></code> block. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled.</p></div>
<h3 id="closing_canvas_tag">Closing <code></canvas></code> tag</h3>
<div class="section-content"><p>Unlike the <a href="img"><code><img></code></a> element, the <a href="canvas" aria-current="page"><code><canvas></code></a> element <strong>requires</strong> the closing tag (<code></canvas></code>).</p></div>
<h3 id="sizing_the_canvas_using_css_versus_html">Sizing the canvas using CSS versus HTML</h3>
<div class="section-content">
<p>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.</p> <p>It is better to specify your canvas dimensions by setting the <code>width</code> and <code>height</code> attributes directly on the <code><canvas></code> elements, either directly in the HTML or by using JavaScript.</p>
</div>
<h3 id="maximum_canvas_size">Maximum canvas size</h3>
<div class="section-content">
<p>The maximum size of a <code><canvas></code> 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. <a href="https://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element" target="_blank">Stack Overflow</a>):</p> <figure class="table-container"><div class="_table"><table> <thead> <tr> <th>Browser</th> <th>Maximum height</th> <th>Maximum width</th> <th>Maximum area</th> </tr> </thead> <tbody> <tr> <td>Chrome</td> <td>32,767 pixels</td> <td>32,767 pixels</td> <td>268,435,456 pixels (i.e., 16,384 x 16,384)</td> </tr> <tr> <td>Firefox</td> <td>32,767 pixels</td> <td>32,767 pixels</td> <td>472,907,776 pixels (i.e., 22,528 x 20,992)</td> </tr> <tr> <td>Safari</td> <td>32,767 pixels</td> <td>32,767 pixels</td> <td>268,435,456 pixels (i.e., 16,384 x 16,384)</td> </tr> <tr> <td>IE</td> <td>8,192 pixels</td> <td>8,192 pixels</td> <td>?</td> </tr> </tbody> </table></div></figure> <div class="notecard note" id="sect1"> <p><strong>Note:</strong> Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.</p> </div>
</div>
<h3 id="using_an_offscreen_canvas">Using an offscreen canvas</h3>
<div class="section-content"><p> A canvas can be rendered using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas"><code>OffscreenCanvas</code></a> API where the document and canvas are decoupled. The benefit is that a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">worker thread</a> can handle canvas rendering and the main thread of your web application is not blocked by canvas operations. By parallelizing work, other UI elements of your web application will remain responsive even if you are running complex graphics on an offscreen canvas. For more information, see the <a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas"><code>OffscreenCanvas</code></a> API documentation. </p></div>
<h2 id="examples">Examples</h2>
<h3 id="html">HTML</h3>
<div class="section-content">
<p>This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to read or render the canvas.</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="hfa+vk8wuoyLX4dWeRBAp5cp86fF68Zrkrowp14bfkQ=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>canvas</span> <span class="token attr-name">width</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>120<span class="token punctuation">"</span></span> <span class="token attr-name">height</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>120<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
An alternative text describing what your canvas displays.
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>canvas</span><span class="token punctuation">></span></span>
</pre>
</div>
</div>
<h3 id="javascript">JavaScript</h3>
<div class="section-content">
<p>Then in the JavaScript code, call <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext"><code>HTMLCanvasElement.getContext()</code></a> to get a drawing context and start drawing onto the canvas:</p> <div class="code-example">
<p class="example-header"><span class="language-name">js</span></p>
<pre data-signature="xE4OlKEN2zzp00MGD/z59mQBMoPMoR02uz/fKLi67as=" data-language="js"><span class="token keyword">const</span> canvas <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">"canvas"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> ctx <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">"2d"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">"green"</span><span class="token punctuation">;</span>
<span class="token comment">// Add a rectangle at (10, 10) with size 100x100 pixels</span>
ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</pre>
</div>
</div>
<h3 id="result">Result</h3>
<div class="section-content"><div class="code-example" id="sect2">
<iframe class="sample-code-frame" title="Examples sample" id="frame_examples" width="600" height="150" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/canvas/runner.html?id=examples" loading="lazy"></iframe>
</div></div>
<h2 id="accessibility_concerns">Accessibility concerns</h2>
<h3 id="alternative_content_2">Alternative content</h3>
<div class="section-content">
<p>The <code><canvas></code> 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.</p> <ul> <li><a href="https://www.w3.org/WAI/PF/HTML/wiki/Canvas_Accessibility_Use_Cases" target="_blank">Canvas accessibility use cases</a></li> <li><a href="https://www.w3.org/html/wg/wiki/AddedElementCanvas" target="_blank">Canvas element accessibility issues</a></li> <li><a href="https://www.tpgi.com/html5-canvas-accessibility-in-firefox-13/" target="_blank">HTML Canvas Accessibility in Firefox 13 – by Steve Faulkner</a></li> <li><a href="https://html.spec.whatwg.org/multipage/scripting.html#best-practices" target="_blank">Best practices for interactive canvas elements</a></li> </ul>
</div>
<h2 id="specifications">Specifications</h2>
<div class="_table"><table class="standard-table">
<thead><tr><th scope="col">Specification</th></tr></thead>
<tbody><tr><td><a href="https://html.spec.whatwg.org/multipage/canvas.html#the-canvas-element">HTML Standard <br><small># the-canvas-element</small></a></td></tr></tbody>
</table></div>
<h2 id="browser_compatibility">Browser compatibility</h2>
<div class="_table"><table>
<thead>
<tr id="bct-browser-type">
<th></th>
<th colspan="6">Desktop</th>
<th colspan="6">Mobile</th>
</tr>
<tr id="bct-browsers">
<th></th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
<th>WebView Android</th>
<th>Chrome Android</th>
<th>Firefox for Android</th>
<th>Opera Android</th>
<th>Safari on IOS</th>
<th>Samsung Internet</th>
</tr>
</thead>
<tbody>
<tr>
<th><code>canvas</code></th>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">12</td>
<td class="bc-supports-yes"><details><summary>1.5</summary>["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "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."]</details></td>
<td class="bc-supports-yes">9</td>
<td class="bc-supports-yes">9</td>
<td class="bc-supports-yes"><details><summary>2</summary>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. Before version 2, Safari will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.</details></td>
<td class="bc-supports-yes">37</td>
<td class="bc-supports-yes">18</td>
<td class="bc-supports-yes"><details><summary>4</summary>["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "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."]</details></td>
<td class="bc-supports-yes">10.1</td>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">1.0</td>
</tr>
<tr>
<th><code>height</code></th>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">12</td>
<td class="bc-supports-yes"><details><summary>1.5</summary>["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "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."]</details></td>
<td class="bc-supports-yes">9</td>
<td class="bc-supports-yes">9</td>
<td class="bc-supports-yes"><details><summary>2</summary>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. Before version 2, Safari will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.</details></td>
<td class="bc-supports-yes">37</td>
<td class="bc-supports-yes">18</td>
<td class="bc-supports-yes"><details><summary>4</summary>["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "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."]</details></td>
<td class="bc-supports-yes">10.1</td>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">1.0</td>
</tr>
<tr>
<th><code>moz-opaque</code></th>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-yes">3.5</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-yes">4</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
<td class="bc-supports-no">No</td>
</tr>
<tr>
<th><code>width</code></th>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">12</td>
<td class="bc-supports-yes"><details><summary>1.5</summary>["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "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."]</details></td>
<td class="bc-supports-yes">9</td>
<td class="bc-supports-yes">9</td>
<td class="bc-supports-yes"><details><summary>2</summary>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. Before version 2, Safari will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it.</details></td>
<td class="bc-supports-yes">37</td>
<td class="bc-supports-yes">18</td>
<td class="bc-supports-yes"><details><summary>4</summary>["Before Firefox 5, the canvas width and height were signed integers instead of unsigned integers.", "Before Firefox 6, a <canvas> element with a zero width or height would be rendered as if it had default dimensions.", "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."]</details></td>
<td class="bc-supports-yes">10.1</td>
<td class="bc-supports-yes">1</td>
<td class="bc-supports-yes">1.0</td>
</tr>
</tbody>
</table></div>
<h2 id="see_also">See also</h2>
<div class="section-content"><ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API">Canvas API</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial">Canvas tutorial</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/Demos#canvas">Canvas-related demos</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas">OffscreenCanvas</a></li> <li>
<a href="https://simon.html5.org/dump/html5-canvas-cheat-sheet.html" target="_blank">Canvas cheat sheet</a> (2009)</li> <li>
<a href="https://websitesetup.org/wp-content/uploads/2015/11/Infopgraphic-CanvasCheatSheet-Final2.pdf" target="_blank">Canvas cheat sheet</a> (pdf) (2015)</li> <li>
<a href="https://www.coding-dude.com/wp/wp-content/uploads/2020/09/HTML5-canvas-cheat-sheet.pdf" target="_blank">Canvas cheat sheet</a> (pdf) (2020)</li> <li>
<a href="https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/HTML-canvas-guide/Introduction/Introduction.html" target="_blank">Canvas introduction by Apple</a> (2013)</li> </ul></div><div class="_attribution">
<p class="_attribution-p">
© 2005–2023 MDN contributors.<br>Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.<br>
<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas" class="_attribution-link">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas</a>
</p>
</div>
|