mozilla

Revision 308027 of <canvas>

  • Revision slug: HTML/Element/canvas
  • Revision title: canvas
  • Revision id: 308027
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment +HTML:Element_Navigation ...etc.

Revision Content

{{ HTMLVersionHeader(5) }}

Summary

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. 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.

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") }} 1.0 9.0 9.0 2.0
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatGeckoMobile("1.8") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} 1.0

Browser-specific notes

Firefox (Gecko)

  • Before Gecko 5.0 {{ geckoRelease("5.0") }}, the canvas width and height were signed integers instead of unsigned integers.
  • 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.
  • 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.

See also

{{ HTML:Element_Navigation() }}

Revision Source

<p>{{ HTMLVersionHeader(5) }}</p>
<h2 id="Summary">Summary</h2>
<p>The <em>Canvas</em> (<code>&lt;canvas&gt;</code>) HTML element can be used to draw graphics via scripting (usually <a href="/en-US/docs/JavaScript" title="JavaScript">JavaScript</a>). 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 <code>&lt;canvas&gt;</code> block. That content will be rendered both on older browsers that don't support canvas <strong>and</strong> in browsers with JavaScript disabled.</p>
<p>For more articles on canvas, see the <a href="/en-US/docs/HTML/Canvas" title="HTML/Canvas">canvas topic page</a>.</p>
<h2 id="Usage_context">Usage context</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td>Permitted content</td>
      <td>Transparent, either <a href="/en-US/docs/HTML/Content_categories#phrasing_content" title="HTML/Content categories#phrasing content">phrasing content</a> or <a href="/en-US/docs/HTML/Content_categories#flow_content" title="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-US/docs/HTML/Content_categories#phrasing_content" title="HTML/Content categories#phrasing content">phrasing content</a> or any element that accepts <a href="/en-US/docs/HTML/Content_categories#flow_content" title="HTML/Content categories#flow content">flow content</a>.</td>
    </tr>
    <tr>
      <td>Normative document</td>
      <td><a 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 id="Attributes">Attributes</h2>
<p>Like all other HTML elements, this element has the <a href="/en-US/docs/HTML/Global_attributes" title="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 id="DOM_interface">DOM interface</h2>
<ul>
  <li><a href="/en-US/docs/DOM/HTMLCanvasElement" title="DOM/HTMLCanvasElement">HTMLCanvasElement</a></li>
</ul>
<h2 id="Examples">Examples</h2>

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

<h2 id="Browser_compatibility">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>{{ CompatGeckoDesktop("1.8") }}</td>
        <td>1.0</td>
        <td>9.0</td>
        <td><a href="http://www.opera.com/docs/changelogs/windows/900/">9.0</a></td>
        <td><a href="http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/HTML-canvas-guide/Introduction/Introduction.html">2.0</a></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>{{ CompatGeckoMobile("1.8") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>1.0</td>
      </tr>
    </tbody>
  </table>
</div>

<h3 id="Browser-specific_notes">Browser-specific notes</h3>
<h4 id="Firefox_(Gecko)">Firefox (Gecko)</h4>
<ul>
  <li>Before Gecko 5.0 {{ geckoRelease("5.0") }}, the canvas width and height were signed integers instead of unsigned integers.</li>
  <li>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.</li>
  <li>Before Gecko 12.0 {{ geckoRelease("12.0") }}, if JavaScript is disabled, the <code>&lt;canvas&gt;</code> element was being rendered instead of showing the fallback content as per the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">specification</a>. Now the fallback content is rendered instead.</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/HTML/Canvas" title="https://developer.mozilla.org/en-US/docs/HTML/Canvas">MDN canvas portal</a></li>
  <li>A <a href="/en-US/docs/Canvas_tutorial" title="Canvas tutorial">canvas tutorial</a></li>
  <li><a href="http://blog.nihilogic.dk/2009/02/html5-canvas-cheat-sheet.html">Canvas cheat sheet</a></li>
  <li><a href="/en-US/demos/tag/tech:canvas" title="/en-US/demos/tag/tech:canvas/">Canvas-related demos</a></li>
</ul>

<div>
  {{ HTML:Element_Navigation() }}</div>
Revert to this revision