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

语法

gl.canvas;

返回值

HTMLCanvasElementOffscreenCanvasnull

示例

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 初始定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
canvasChrome Full support 9Edge Full support 12Firefox Full support 4IE Full support 11Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support 12Safari iOS Full support 8.1Samsung Internet Android Full support Yes
OffscreenCanvas
Experimental
Chrome No support NoEdge No support NoFirefox Full support 44
Disabled
Full support 44
Disabled
Disabled From version 44: this feature is behind the gfx.offscreencanvas.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.

相关链接

文档标签和贡献者

此页面的贡献者: mdnwebdocs-bot, wymm0008, luojia, LiebeU
最后编辑者: mdnwebdocs-bot,