WebGLRenderingContext.canvas 只读属性,对 HTMLCanvasElement 和 OffscreenCanvas 对象的引用。如果绘图上下文没有相关联的 <canvas> 元素或 OffscreenCanvas 对象,值为 null。
句法
gl.canvas;
返回值
HTMLCanvasElement 或 OffscreenCanvas 或 null。
示例
Canvas 元素
指定 <canvas> 元素:
<canvas id="canvas"></canvas>
你可以通过 canvas 得到一个从 WebGLRenderingContext 返回的引用:
var canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl");
gl.canvas; // HTMLCanvasElement
离屏Canvas
下面是一个使用试验阶段 OffscreenCanvas 对象的示例:
var offscreen = new OffscreenCanvas(256, 256);
var gl = offscreen.getContext("webgl");
gl.canvas; // OffscreenCanvas
规范
| Specification | Status | Comment |
|---|---|---|
| WebGL 1.0 WebGLRenderingContext.canvas |
Recommendation | 初始定义 |
浏览器兼容性
| Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
|---|---|---|---|---|---|---|---|
| Basic Support | 9 | 121 | 4.0 | 111 | 12 | 5.1 | No support |
| OffscreenCanvas | No support | No support | No support2 | No support | No support | No support | No support |
| Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic Support | (Yes) | 25 | No support | (Yes) | 111 | 12 | 8.1 |
| OffscreenCanvas | No support | No support | No support | No support2 | No support | No support | No support |
1. To access the WebGL context, use experimental-webgl rather than the standard webgl
2. This feature is experimentally implemented since Firefox 44; to activate it, in about:config, set gfx.offscreencanvas.enabled to true