WebGLRenderingContext.canvas

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

WebGLRenderingContext.canvas 只读属性,对 HTMLCanvasElementOffscreenCanvas 对象的引用。如果绘图上下文没有相关联的 <canvas> 元素或 OffscreenCanvas 对象,值为 null

语法

gl.canvas;

返回值

示例

Canvas 元素

指定 <canvas> 元素:

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

你可以通过 canvas 得到一个从 WebGLRenderingContext 返回的引用:

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

离屏 Canvas

下面是一个使用试验阶段 OffscreenCanvas 对象的示例:

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

规范

Specification
WebGL Specification
# DOM-WebGLRenderingContext-canvas

浏览器兼容性

BCD tables only load in the browser

相关链接