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 994917 of HTMLCanvasElement

  • Revision slug: Web/API/HTMLCanvasElement
  • Revision title: HTMLCanvasElement
  • Revision id: 994917
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment Add transferControlToOffscreen (bug 709490)

Revision Content

{{APIRef("Canvas API")}}

The HTMLCanvasElement interface provides properties and methods for manipulating the layout and presentation of canvas elements. The HTMLCanvasElement interface also inherits the properties and methods of the {{domxref("HTMLElement")}} interface.

Properties

Inherits properties from its parent, {{domxref("HTMLElement")}}.

{{domxref("HTMLCanvasElement.height")}}
A positive integer reflecting the {{htmlattrxref("height", "canvas")}} HTML attribute of the {{HTMLElement("canvas")}} element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of 150 is used.
{{domxref("HTMLCanvasElement.mozOpaque")}} {{non-standard_inline}}
A {{jsxref("Boolean")}} reflecting the {{htmlattrxref("moz-opaque", "canvas")}} HTML attribute of the {{HTMLElement("canvas")}} element. It 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.
{{domxref("HTMLCanvasElement.width")}}
A positive integer reflecting the {{htmlattrxref("width", "canvas")}} HTML attribute of the {{HTMLElement("canvas")}} element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of 300 is used.

Methods

Inherits methods from its parent, {{domxref("HTMLElement")}}.

{{domxref("HTMLCanvasElement.captureStream()")}} {{experimental_inline}}
Returns a {{domxref("CanvasCaptureMediaStream")}} that is a real-time video capture of the surface of the canvas.
{{domxref("HTMLCanvasElement.getContext()")}}
Returns a drawing context on the canvas, or null if the context ID is not supported. A drawing context lets you draw on the canvas. Calling getContext with "2d" returns a {{domxref("CanvasRenderingContext2D")}} object, whereas calling it with "experimental-webgl" (or "webgl") returns a {{domxref("WebGLRenderingContext")}} object. This context is only available on browsers that implement WebGL.
{{domxref("HTMLCanvasElement.toDataURL()")}}
Returns a data-URL containing a representation of the image in the format specified by the type parameter (defaults to png). The returned image is in a resolution of 96dpi.
{{domxref("HTMLCanvasElement.toBlob()")}}
Creates a {{domxref("Blob")}} object representing the image contained in the canvas; this file may be cached on the disk or stored in memory at the discretion of the user agent.
{{domxref("HTMLCanvasElement.transferControlToOffscreen()")}} {{experimental_inline}}
Transfers control to an {{domxref("OffscreenCanvas")}} object, either on the main thread or on a worker.
{{domxref("HTMLCanvasElement.mozGetAsFile()")}} {{non-standard_inline}} {{deprecated_inline}}
Returns a {{domxref("File")}} object representing the image contained in the canvas; this file is a memory-based file, with the specified name. If type is not specified, the image type is image/png.

Specifications

Specification Status Comment
{{SpecName('Media Capture DOM Elements', '#html-media-element-media-capture-extensions', 'HTMLCanvasElement')}} {{Spec2('Media Capture DOM Elements')}} Adds the method captureStream().
{{SpecName('HTML WHATWG', "#the-canvas-element", "HTMLCanvasElement")}} {{Spec2('HTML WHATWG')}} The method getContext() now returns a {{domxref("RenderingContext")}} rather than an opaque object.
The methods probablySupportsContext(), setContext() and transferControlToProxy()have been added.
{{SpecName('HTML5.1', "scripting-1.html#the-canvas-element", "HTMLCanvasElement")}} {{Spec2('HTML5.1')}}  
{{SpecName('HTML5 W3C', "scripting-1.html#the-canvas-element", "HTMLCanvasElement")}} {{Spec2('HTML5 W3C')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (2D context) 4.0 {{CompatGeckoDesktop('1.9.2')}} 9.0 9.0 [1] 3.1
toBlob() {{CompatNo}} (bug 67587) {{CompatGeckoDesktop('19')}} [2] {{CompatUnknown}} {{CompatUnknown}} {{CompatNo}} (bug 71270)
probablySupportsContext(),
setContext(),
transferControlToProxy() {{experimental_inline}}
{{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
mozGetAsFile() {{non-standard_inline}} {{deprecated_inline}} {{CompatNo}} {{CompatGeckoDesktop('2')}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
captureStream() {{experimental_inline}} {{CompatNo}} {{CompatGeckoDesktop('41')}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
transferControlToOffscreen() {{experimental_inline}} {{CompatNo}} {{CompatGeckoDesktop(44)}} [3] {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (2D context) 2.1 {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} 10.0 [1] 3.2
webgl context {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} as experimental-webgl {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
toBlob() {{CompatNo}} (bug 67587) {{CompatNo}} (bug 67587) {{CompatGeckoMobile('18')}} [2] {{CompatUnknown}} {{CompatUnknown}} {{CompatNo}} (bug 71270)
probablySupportsContext(),
setContext(),
transferControlToProxy() {{experimental_inline}}
{{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
mozGetAsFile() {{non-standard_inline}} {{deprecated_inline}} {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile('2')}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
captureStream() {{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile('41')}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
transferControlToOffscreen() {{experimental_inline}} {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile(44)}} [3] {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] Opera Mini 5.0 and later has partial support.

[2] Support for the third parameter, has been added in Gecko 25 only: when used with the "image/jpeg" type, this argument specifies the image quality.

[3] This feature is behind a feature preference setting. In about:config, set gfx.offscreencanvas.enabled to true.

See also

  • HTML element implementing this interface: {{HTMLElement("canvas")}}.

Revision Source

<div>
<div>{{APIRef("Canvas API")}}</div>
</div>

<p>The <strong><code>HTMLCanvasElement</code></strong> interface provides properties and methods for manipulating the layout and presentation of canvas elements. The <code>HTMLCanvasElement</code> interface also inherits the properties and methods of the {{domxref("HTMLElement")}} interface.</p>

<h2 id="Properties">Properties</h2>

<p><em>Inherits properties from its parent, </em><em>{{domxref("HTMLElement")}}.</em></p>

<dl>
 <dt>{{domxref("HTMLCanvasElement.height")}}</dt>
 <dd>A positive <code>integer</code> reflecting the {{htmlattrxref("height", "canvas")}} HTML attribute of the {{HTMLElement("canvas")}} element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of <code>150</code> is used.</dd>
 <dt>{{domxref("HTMLCanvasElement.mozOpaque")}} {{non-standard_inline}}</dt>
 <dd>A {{jsxref("Boolean")}} reflecting the {{htmlattrxref("moz-opaque", "canvas")}} HTML attribute of the {{HTMLElement("canvas")}} element. It 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>{{domxref("HTMLCanvasElement.width")}}</dt>
 <dd>A positive <code>integer</code> reflecting the {{htmlattrxref("width", "canvas")}} HTML attribute of the {{HTMLElement("canvas")}} element interpreted in CSS pixels. When the attribute is not specified, or if it is set to an invalid value, like a negative, the default value of <code>300</code> is used.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>Inherits methods from its parent, </em><em>{{domxref("HTMLElement")}}.</em></p>

<dl>
 <dt>{{domxref("HTMLCanvasElement.captureStream()")}} {{experimental_inline}}</dt>
 <dd>Returns a {{domxref("CanvasCaptureMediaStream")}} that is a real-time video capture of the surface of the canvas.</dd>
 <dt>{{domxref("HTMLCanvasElement.getContext()")}}</dt>
 <dd>Returns a drawing context on the canvas, or null if the context ID is not supported. A drawing context lets you draw on the canvas. Calling getContext with <code>"2d"</code> returns a {{domxref("CanvasRenderingContext2D")}} object, whereas calling it with <code>"experimental-webgl"</code> (or <code>"webgl"</code>) returns a {{domxref("WebGLRenderingContext")}} object. This context is only available on browsers that implement <a href="/en-US/docs/Web/WebGL">WebGL</a>.</dd>
 <dt>{{domxref("HTMLCanvasElement.toDataURL()")}}</dt>
 <dd>Returns a data-URL containing a representation of the image in the format specified by the <code>type</code> parameter (defaults to <code>png</code>). The returned image is in a resolution of 96dpi.</dd>
 <dt>{{domxref("HTMLCanvasElement.toBlob()")}}</dt>
 <dd>Creates a {{domxref("Blob")}} object representing the image contained in the canvas; this file may be cached on the disk or stored in memory at the discretion of the user agent.</dd>
 <dt>{{domxref("HTMLCanvasElement.transferControlToOffscreen()")}} {{experimental_inline}}</dt>
 <dd>Transfers control to an {{domxref("OffscreenCanvas")}} object, either on the main thread or on a worker.</dd>
 <dt>{{domxref("HTMLCanvasElement.mozGetAsFile()")}} {{non-standard_inline}} {{deprecated_inline}}</dt>
 <dd>Returns a {{domxref("File")}} object representing the image contained in the canvas; this file is a memory-based file, with the specified <code>name</code>. If <code>type</code> is not specified, the image type is <code>image/png</code>.</dd>
</dl>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Media Capture DOM Elements', '#html-media-element-media-capture-extensions', 'HTMLCanvasElement')}}</td>
   <td>{{Spec2('Media Capture DOM Elements')}}</td>
   <td>Adds the method <code>captureStream()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "#the-canvas-element", "HTMLCanvasElement")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>The method <code>getContext()</code> now returns a {{domxref("RenderingContext")}} rather than an opaque <code>object</code>.<br />
    The methods <code>probablySupportsContext()</code>, <code>setContext()</code> and <code>transferControlToProxy()</code>have been added.</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', "scripting-1.html#the-canvas-element", "HTMLCanvasElement")}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', "scripting-1.html#the-canvas-element", "HTMLCanvasElement")}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<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 (<code>2D</code> context)</td>
   <td>4.0</td>
   <td>{{CompatGeckoDesktop('1.9.2')}}</td>
   <td>9.0</td>
   <td>9.0 [1]</td>
   <td>3.1</td>
  </tr>
  <tr>
   <td><code>toBlob()</code></td>
   <td>{{CompatNo}} (bug <a href="https://crbug.com/67587">67587</a>)</td>
   <td>{{CompatGeckoDesktop('19')}} [2]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} (bug <a href="https://bugs.webkit.org/show_bug.cgi?id=71270">71270</a>)</td>
  </tr>
  <tr>
   <td><code>probablySupportsContext()</code>,<br />
    <code>setContext()</code>,<br />
    <code>transferControlToProxy()</code> {{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>mozGetAsFile()</code> {{non-standard_inline}} {{deprecated_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop('2')}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>captureStream()</code> {{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop('41')}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>transferControlToOffscreen()</code> {{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop(44)}} [3]</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>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support (<code>2D</code> context)</td>
   <td>2.1</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>10.0 [1]</td>
   <td>3.2</td>
  </tr>
  <tr>
   <td><code>webgl</code> context</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}} as <code>experimental-webgl</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>toBlob()</code></td>
   <td>{{CompatNo}} (bug <a href="https://crbug.com/67587">67587</a>)</td>
   <td>{{CompatNo}} (bug <a href="https://crbug.com/67587">67587</a>)</td>
   <td>{{CompatGeckoMobile('18')}} [2]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}} (bug <a href="https://bugs.webkit.org/show_bug.cgi?id=71270">71270</a>)</td>
  </tr>
  <tr>
   <td><code>probablySupportsContext()</code>,<br />
    <code>setContext()</code>,<br />
    <code>transferControlToProxy() </code>{{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>mozGetAsFile()</code> {{non-standard_inline}} {{deprecated_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile('2')}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>captureStream() </code>{{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile('41')}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>transferControlToOffscreen()</code> {{experimental_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(44)}} [3]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Opera Mini 5.0 and later has partial support.</p>

<p>[2] Support for the third parameter, has been added in Gecko 25 only: when used with the <code>"image/jpeg"</code> type, this argument specifies the image quality.</p>

<p>[3] This feature is behind a feature preference setting. In about:config, set <code>gfx.offscreencanvas.enabled</code> to <code>true</code>.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>HTML element implementing this interface: {{HTMLElement("canvas")}}.</li>
</ul>
Revert to this revision