--- title: HTMLCanvasElement slug: Web/API/HTMLCanvasElement tags: - API - Canvas - HTML DOM - Interface - NeedsTranslation - Reference - TopicStub translation_of: Web/API/HTMLCanvasElement --- <div> <div>{{APIRef("Canvas API")}}</div> </div> <p><strong><code><span>HTMLCanvasElement</span></code></strong> 介面提供控制 canvas 元素的屬性和方法. <code>HTMLCanvasElement</code> 介面也繼承了 {{domxref("HTMLElement")}} 介面的屬性和方法.</p> <p>{{InheritanceDiagram}}</p> <h2 id="屬性">屬性</h2> <p><em>從父代繼承的屬性,</em><em>{{domxref("HTMLElement")}}.</em></p> <dl> <dt>{{domxref("HTMLCanvasElement.height")}}</dt> <dd>Is 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>Is 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>Is 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> <dt>{{domxref("HTMLCanvasElement.mozPrintCallback")}}{{non-standard_inline}}</dt> <dd>Is a <code>function</code> that is Initially null, Web content can set this to a JavaScript function that will be called if the page is printed. This function can then redraw the canvas at a higher resolution that is suitable for the printer being used. <a href="https://blog.mozilla.org/labs/2012/09/a-new-way-to-control-printing-output/">See this blog post.</a></dd> </dl> <h2 id="方法">方法</h2> <p><em>從父代繼承的方法, </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.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="規範">規範</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">規範</th> <th scope="col">狀態</th> <th scope="col">註</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> </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="瀏覽器相容性">瀏覽器相容性</h2> <div>{{CompatibilityTable}}</div> <div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Edge</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>{{CompatVersionUnknown}}</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>50</td> <td>{{CompatNo}}</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>{{CompatUnknown}}</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>{{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>{{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>{{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>Edge</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>{{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>{{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="http://crbug.com/67587">67587</a>)</td> <td>50</td> <td>{{CompatNo}}</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>{{CompatUnknown}}</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>{{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>{{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>{{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>