WEBGL_lose_context 扩展

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有浏览器中可用。

WEBGL_lose_context 扩展是 WebGL API 的一部分,它公开了用于模拟丢失和恢复 WebGLRenderingContext 的函数。

可以使用 WebGLRenderingContext.getExtension() 方法来访问 WebGL 扩展。有关更多信息,请参阅 WebGL 教程中的 使用扩展

注意: 此扩展同时适用于 WebGL1WebGL2 上下文。

实例方法

WEBGL_lose_context.loseContext()

模拟丢失上下文。

WEBGL_lose_context.restoreContext()

模拟恢复上下文。

示例

使用此扩展,您可以模拟 webglcontextlostwebglcontextrestored 事件

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

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

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

// WebGLContextEvent event with type "webglcontextlost" is logged.

规范

规范
WebGL WEBGL_lose_context Khronos 批准的扩展规范

浏览器兼容性

另见