HTMLCanvasElement: webglcontextlost event

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对象关联的图形缓冲区已丢失,则会触发WebGL API 中的 webglcontextlost 事件。

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

例子

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

js
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
WebGL Specification
# 5.15.2

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
webglcontextlost event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

请参阅