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

浏览器兼容性

另见