Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 822607 of <canvas>

  • Revision slug: Web/HTML/Element/canvas
  • Revision title: <canvas>
  • Revision id: 822607
  • Created:
  • Creator: andyearnshaw
  • Is current revision? No
  • Comment Revert to revision of 2015-01-13 04:17:16 by fscholz: "The current revision from June 14th is broken."

Revision Content

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, or button.
  • 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 &lt;canvas&gt; 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.

See also

{{HTMLRef}}

Revision Source

<h2 id="Summary">Summary</h2>
<p>The <strong>HTML <code>&lt;canvas&gt;</code> Element</strong> can be used to draw graphics via scripting (usually <a href="/en-US/docs/Web/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/Web/API/Canvas_API">canvas topic page</a>.</p>
<ul class="htmlelt">
 <li><dfn><a href="/en-US/docs/HTML/Content_categories" title="HTML/Content_categories">Content categories</a></dfn> <a href="/en-US/docs/HTML/Content_categories#Flow_content" title="HTML/Content categories#Flow content">Flow content</a>, <a href="/en-US/docs/HTML/Content_categories#Phrasing_content" title="HTML/Content categories#Phrasing content">phrasing content</a>, <a href="https://developer.mozilla.org/en-US/docs/HTML/Content_categories#Embedded_content" title="HTML/Content_categories#Embedded content">embedded content</a>, palpable content.</li>
 <li><dfn>Permitted content</dfn> Transparent but with no <a href="/en-US/docs/HTML/Content_categories#Interactive_content">interactive content</a> descendants except for {{HTMLElement("a")}} elements, {{HTMLElement("button")}} elements, {{HTMLElement("input")}} elements whose {{htmlattrxref("type", "input")}} attribute is <code>checkbox</code>, <code>radio</code>, or <code>button</code>.</li>
 <li><dfn>Tag omission</dfn> {{no_tag_omission}}</li>
 <li><dfn>Permitted parent elements</dfn> Any element that accepts <a href="/en-US/docs/HTML/Content_categories#Phrasing_content" title="HTML/Content_categories#Phrasing_content">phrasing_content</a>.</li>
 <li><dfn>DOM interface</dfn> {{domxref("HTMLCanvasElement")}}</li>
</ul>
<h2 id="Attributes">Attributes</h2>
<p>This element includes the <a href="https://developer.mozilla.org/en-US/docs/HTML/Global_attributes">global attributes</a>.</p>
<dl>
 <dt>
  {{htmlattrdef("height")}}</dt>
 <dd>
  The height of the coordinate space in CSS pixels. Defaults to 150.</dd>
 <dt>
  {{htmlattrdef("moz-opaque")}} {{non-standard_inline}}</dt>
 <dd>
  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.</dd>
 <dt>
  {{htmlattrdef("width")}}</dt>
 <dd>
  The width of the coordinate space in CSS pixels. Defaults to 300.</dd>
</dl>
<h2>Description</h2>
<h3 id="Required_&lt;.2Fcanvas&gt;_tag">Required <code>&lt;/canvas&gt;</code> tag</h3>
<p>Unlike the {{HTMLElement("img")}} element, the {{HTMLElement("canvas")}} element <strong>requires</strong> the closing tag (<code>&lt;/canvas&gt;</code>).</p>
<h3>Sizing the canvas</h3>
<p>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 <code>width</code> and <code>height</code> attributes explicitly in the <code>&lt;canvas&gt;</code> attributes, and not using CSS.</p>
<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>
<p>If you use canvas and doesn’t need to be transparent, set the <code>moz-opaque</code> 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.</p>
<pre class="brush: html">
&lt;canvas id="mycanvas" moz-opaque&gt;&lt;/canvas&gt;</pre>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'the-canvas-element.html#the-canvas-element', '&lt;canvas&gt;')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', 'the-canvas-element.html#the-canvas-element', '&lt;canvas&gt;')}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>1.0</td>
    <td>{{CompatGeckoDesktop("1.8")}} [1][2][3]</td>
    <td>9.0</td>
    <td><a href="https://www.opera.com/docs/changelogs/windows/900/">9.0</a></td>
    <td><a href="https://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/HTML-canvas-guide/Introduction/Introduction.html">2.0</a> [4]</td>
   </tr>
   <tr>
    <td><code>moz-opaque</code></td>
    <td>{{CompatNo}}</td>
    <td>{{CompatGeckoDesktop("1.9.1")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</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>
   <tr>
    <td><code>moz-opaque</code></td>
    <td>{{CompatGeckoMobile("1.9.1")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h3 id="Browser-specific_notes">Browser-specific notes</h3>
<h4 id="Firefox_(Gecko)">Firefox (Gecko)</h4>
<p>[1] Before Gecko 5.0 {{geckoRelease("5.0")}}, the canvas width and height were signed integers instead of unsigned integers.</p>
<p>[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.</p>
<p>[3] 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="https://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">specification</a>. Now the fallback content is rendered instead.</p>
<p>[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.</p>
<h2 id="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/Web/API/Canvas_API" title="https://developer.mozilla.org/en-US/docs/HTML/Canvas">MDN canvas portal</a></li>
 <li><a href="/en-US/docs/Web/API/Canvas_API/Tutorial" title="Canvas tutorial">Canvas tutorial</a></li>
 <li><a href="https://simon.html5.org/dump/html5-canvas-cheat-sheet.html">Canvas cheat sheet</a></li>
 <li><a href="/en-US/demos/tag/tech:canvas" title="demos/tag/tech:canvas/">Canvas-related demos</a></li>
</ul>
<div>
 {{HTMLRef}}</div>
Revert to this revision