Revision 29539 of <canvas>

  • Revision slug: HTML/Element/canvas
  • Revision title: canvas
  • Revision id: 29539
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

{{ HTMLVersionHeader("5") }}

{{ gecko_minversion_header("1.8") }}

The Canvas (<canvas>)  HTML 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.

For more articles on canvas, see the canvas topic page.

Usage context

Permitted content Transparent, either phrasing content or flow content.
Tag omission None, both the start tag and end tag must be included
Permitted parent elements Any element that accepts phrasing content or any element that accepts flow content
Normative document HTML 5, section 4.8.10

Attributes

Like all other HTML elements, this element has the global attributes.

{{ htmlattrdef("width") }}
 The width of the coordinate space in CSS pixels. Defaults to 300.
{{ htmlattrdef("height") }}
The height of the coordinate space in CSS pixels. Defaults to 150.

{{ Note("The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size.") }}

DOM interface

Examples

<canvas id="canvas" width="300" height="300">
  Sorry, your browser doesn't support the &lt;canvas&gt; element.
</canvas>

Browser compatibility

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support

{{ CompatGeckoDesktop("1.8") }}

Before Gecko 5.0 {{ geckoRelease("5.0") }}, the canvas width and height were signed integers instead of unsigned integers.

1.0 9.0 9.0 1.0
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support

{{ CompatGeckoMobile("1.8") }}

Before Gecko 5.0 {{ geckoRelease("5.0") }}, the canvas width and height were signed integers instead of unsigned integers.

{{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} 1.0

Browser-specific notes

Firefox (Gecko)

{{ gecko_callout_heading("6.0") }}

Prior to Gecko 6.0 {{ geckoRelease("6.0") }}, the {{ HTMLElement("canvas") }} element's width and height would incorrectly be set to 300px if set to 0px. This has been fixed, since 0px is a valid size.

See also

{{ languages( { "de": "de/HTML/Element/canvas", "en": "en/HTML/Element/canvas", "es": "es/HTML/Elemento/canvas", "ja": "ja/HTML/Element/canvas", "ko": "ko/HTML/Element/canvas", "nl": "nl/HTML/HTML_Tags/canvas", "pl": "pl/HTML/Element/canvas", "ru": "Ru/HTML/Element/canvas", "zh-cn": "cn/HTML/Element/canvas", "fr": "fr/HTML/Element/canvas" } ) }}

Revision Source

<p>{{ HTMLVersionHeader("5") }}</p>
<p>{{ gecko_minversion_header("1.8") }}</p>
<p>The <em>Canvas</em> (&lt;canvas&gt;)  HTML element  can be used to draw graphics via scripting (usually <a href="../../../../en/JavaScript" rel="internal">JavaScript</a>). For example, it can be used to draw graphs, make photo compositions or even perform animations.</p>
<p>For more articles on canvas, see the <a href="/en/HTML/Canvas" title="en/HTML/Canvas">canvas topic page</a>.</p>
<h2>Usage context</h2>
<table class="standard-table"> <tbody> <tr> <td>Permitted content</td> <td>Transparent, either <a href="/en/HTML/Content_categories#phrasing_content" title="en/HTML/Content categories#phrasing content">phrasing content</a> or <a href="/en/HTML/Content_categories#flow_content" title="en/HTML/Content categories#flow content">flow content</a>.</td> </tr> <tr> <td>Tag omission</td> <td>None, both the start tag and end tag must be included</td> </tr> <tr> <td>Permitted parent elements</td> <td>Any element that accepts <a href="/en/HTML/Content_categories#phrasing_content" title="en/HTML/Content categories#phrasing content">phrasing content</a> or any element that accepts <a href="/en/HTML/Content_categories#flow_content" title="en/HTML/Content categories#flow content">flow content</a>. </td> </tr> <tr> <td>Normative document</td> <td><a class=" external" href="http://www.w3.org/TR/html5/the-canvas-element.html#the-canvas-element" title="http://www.w3.org/TR/html5/the-canvas-element.html#the-canvas-element">HTML 5, section 4.8.10</a></td> </tr> </tbody>
</table>
<h2>Attributes</h2>
<p>Like all other HTML elements, this element has the <a href="/en/HTML/Global_attributes" title="en/HTML/Global attributes">global attributes</a>.</p>
<dl> <dt>{{ htmlattrdef("width") }}</dt> <dd> The width of the coordinate space in CSS pixels. Defaults to 300.</dd> <dt>{{ htmlattrdef("height") }}</dt> <dd>The height of the coordinate space in CSS pixels. Defaults to 150.</dd>
</dl>
<p>{{ Note("The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size.") }}</p>
<h2>DOM interface</h2>
<ul> <li><a href="/en/DOM/HTMLCanvasElement" title="en/DOM/HTMLCanvasElement">HTMLCanvasElement</a></li>
</ul>
<h2>Examples</h2>
<pre class="brush: html">&lt;<span class="start-tag">canvas</span><span class="attribute-name"> id</span>=<span class="attribute-value">"canvas" </span><span class="attribute-name">width</span>=<span class="attribute-value">"300" </span><span class="attribute-name">height</span>=<span class="attribute-value">"300"</span>&gt;
  Sorry, your browser doesn't support the &amp;<span class="entity">lt;</span>canvas&amp;<span class="entity">gt;</span> element.
&lt;/<span class="end-tag">canvas</span>&gt;
</pre>
<h2>Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox (Gecko)</th> <th>Chrome</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td> <p>{{ CompatGeckoDesktop("1.8") }}</p> <p>Before Gecko 5.0 {{ geckoRelease("5.0") }}, the canvas width and height were signed integers instead of unsigned integers.</p> </td> <td>1.0</td> <td>9.0</td> <td>9.0</td> <td>1.0</td> </tr> </tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox Mobile (Gecko)</th> <th>Android</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td> <p>{{ CompatGeckoMobile("1.8") }}</p> <p>Before Gecko 5.0 {{ geckoRelease("5.0") }}, the canvas width and height were signed integers instead of unsigned integers.</p> </td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>1.0</td> </tr> </tbody>
</table>
</div>
<h3>Browser-specific notes</h3>
<h4>Firefox (Gecko)</h4>
<div class="geckoVersionNote">
<p>{{ gecko_callout_heading("6.0") }}</p>
<p>Prior to Gecko 6.0 {{ geckoRelease("6.0") }}, the {{ HTMLElement("canvas") }} element's width and height would incorrectly be set to 300px if set to 0px. This has been fixed, since 0px is a valid size.</p>
</div>
<h2>See also</h2>
<ul> <li><a href="/en/HTML/Canvas" title="https://developer.mozilla.org/en/HTML/Canvas">Canvas topics</a></li>
</ul>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><span style=" font-family:'Ubuntu'; font-size:9pt; color:#000000;">{{ languages( { "de": "de/HTML/Element/canvas", "en": "en/HTML/Element/canvas", "es": "es/HTML/Elemento/canvas", "ja": "ja/HTML/Element/canvas", "ko": "ko/HTML/Element/canvas", "nl": "nl/HTML/HTML_Tags/canvas", "pl": "pl/HTML/Element/canvas", "ru": "Ru/HTML/Element/canvas", "zh-cn": "cn/HTML/Element/canvas", "fr": "fr/HTML/Element/canvas" } ) }}</span></p>
Revert to this revision