WebGLRenderingContext: canvas 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

WebGLRenderingContext.canvas 屬性是一個只讀引用,指向與該上下文關聯的 HTMLCanvasElementOffscreenCanvas 物件。如果它未與 <canvas> 元素或 OffscreenCanvas 物件關聯,則可能為 null

HTMLCanvasElementOffscreenCanvas 物件,或 null

示例

Canvas 元素

給定此 <canvas> 元素

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

您可以使用 canvas 屬性從 WebGLRenderingContext 中獲取對它的引用。

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

離屏 Canvas

使用實驗性的 OffscreenCanvas 物件進行示例。

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

規範

規範
WebGL 規範
# DOM-WebGLRenderingContext-canvas

瀏覽器相容性

另見