HTMLCanvasElement: webglcontextlost event

如果浏览器检测到与 WebGLRenderingContext对象关联的图形缓冲区已丢失,则会触发WebGL API 中的webglcontextlost 事件.

冒泡 Yes
可取消 Yes
继承 WebGLContextEvent
事件处理程序属性 none

例子

WEBGL_lose_context 扩展的帮助下,您可以模拟 webglcontextlost 事件:

const canvas = document.getElementById('canvas');
const gl = canvas.getContext('webgl');

canvas.addEventListener('webglcontextlost', (event) => {
  console.log(event); 
});

//WEBGL_lose_context是webgl是属于 WebGLAPI 的一个扩展API,它提供一组方法用来模拟一个 WebGLRenderingContext 上下文的丢失和恢复。
gl.getExtension('WEBGL_lose_context').loseContext();

// "webglcontextlost" event is logged.

标准

Specification Status Comment
WebGL 1.0
webglcontextlost
Recommendation Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
webglcontextlost eventChrome 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 25Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 8Samsung Internet Android Full support 1.5

Legend

Full support  
Full support

请参阅