WebGLRenderingContext: canvas プロパティ

WebGLRenderingContext.canvas プロパティは、コンテキストに関連付けられた HTMLCanvasElement または OffscreenCanvas (en-US) オブジェクトへの読み取り専用の参照です。また、 <canvas> 要素や OffscreenCanvas (en-US) オブジェクトに関連付けられていない場合、 null になる可能性があります。

構文

js
gl.canvas

返値

HTMLCanvasElement または OffscreenCanvas (en-US) オブジェクトか、 null のいずれか。

canvas 要素

この <canvas> 要素があるとします。

html
<canvas id="canvas"></canvas>

WebGLRenderingContext から canvas プロパティを使用して、参照を取得することができます。

js
const canvas = document.getElementById("canvas");
const gl = canvas.getContext("webgl");
gl.canvas; // HTMLCanvasElement

オフスクリーンキャンバス

実験的な OffscreenCanvas (en-US) オブジェクトを使用した例です。

js
const offscreen = new OffscreenCanvas(256, 256);
const gl = offscreen.getContext("webgl");
gl.canvas; // OffscreenCanvas

仕様書

Specification
WebGL Specification
# DOM-WebGLRenderingContext-canvas

ブラウザーの互換性

BCD tables only load in the browser

関連情報