--- 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>HTMLCanvasElement</code></strong>インタフェースはcanvas要素のレイアウトや表現の操作のための属性やメソッドを提供します。<code>HTMLCanvasElement</code>は{{domxref("HTMLElement")}}インタフェースのプロパティやメソッドも利用可能です。</p> <h2 id="属性">属性</h2> <p><em>{{domxref("HTMLElement")}}から継承する属性の他に以下の属性を持ちます。</em></p> <dl> <dt>{{domxref("HTMLCanvasElement.height")}}</dt> <dd>正の<code>integer</code> で {{htmlattrxref("height", "canvas")}} というHTMLの{{HTMLElement("canvas")}}要素の属性で指定されたCSSピクセルの値です。もしこの属性が指定されていないか、無効な値であるならば、デフォルト値として<code>150</code>を用います。</dd> <dt>{{domxref("HTMLCanvasElement.mozOpaque")}} {{non-standard_inline}}</dt> <dd>{{jsxref("Boolean")}}で{{htmlattrxref("moz-opaque", "canvas")}}という{{HTMLElement("canvas")}}要素で指定されたものを反映したものです。この属性はcanvasが半透明であることが重要か否かを指定します。もし半透明がないならば、描画を最適化できます。</dd> <dt>{{domxref("HTMLCanvasElement.width")}}</dt> <dd>正の<code>integer</code> で{{htmlattrxref("width", "canvas")}} というHTMLの{{HTMLElement("canvas")}}要素の属性で指定されたCSSピクセルの値です。もしこの属性が指定されていないか、無効な値であるならば、デフォルト値として<code>300</code>を用います。</dd> </dl> <h2 id="メソッド">メソッド</h2> <p><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> <p>Canvasの描画コンテキストを返すか、サポートされていないコンテキストIDの場合nullを返します。<br> 描画コンテキストを用いてCanvasに描画することができます。<br> getContextの引数に<code>"2d"</code>を渡すと{{domxref("CanvasRenderingContext2D")}} オブジェクトを、<code>"experimental-webgl"</code>(または<code>"webgl"</code>)を渡すと {{domxref("WebGLRenderingContext")}}オブジェクトを返します。<br> <code>"experimental-webgl"</code>または<code>"webgl"</code>コンテキストは、ブラウザがWebGLに対応している場合に限り使用できます。</p> </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> </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> <p>{{Compat("api.HTMLCanvasElement")}}</p> <h2 id="See_also">See also</h2> <ul> <li>HTML element implementing this interface: {{HTMLElement("canvas")}}.</li> </ul>