WEBGL_lose_context

WEBGL_lose_context 是属于 WebGL API 的一个扩展 API,它提供一组方法用来模拟一个 WebGLRenderingContext 上下文的丢失和恢复。

WebGL 扩展可以通过 WebGLRenderingContext.getExtension() 方法来使用。更多信息可参阅 WebGL 教程 中的 使用 WebGL 扩展

备注: 该扩展在 WebGL1WebGL2 上下文中都是可用的。

方法

WEBGL_lose_context.loseContext()

模拟上下文丢失。

WEBGL_lose_context.restoreContext()

模拟上下文恢复。

示例

使用这个扩展,你可以模拟 webglcontextlostwebglcontextrestored (en-US) 事件:

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

canvas.addEventListener(
  "webglcontextlost",
  function (e) {
    console.log(e);
  },
  false,
);

gl.getExtension("WEBGL_lose_context").loseContext();

// 打印了 "webglcontextlost" 类型的 WebGLContextEvent 事件。

规范

Specification
WebGL WEBGL_lose_context Khronos Ratified Extension Specification

浏览器兼容性

BCD tables only load in the browser

参见