WEBGL_lose_context: loseContext() 方法

Baseline 已广泛支持

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

WEBGL_lose_context.loseContext() 方法是 WebGL API 的一部分,它允许您模拟 WebGLRenderingContext 上下文的丢失。

它会触发 WebGL 规范中描述的用于处理上下文丢失的步骤。上下文将一直保持丢失状态,直到调用 WEBGL_lose_context.restoreContext() 为止。

语法

js
loseContext()

参数

无。

返回值

无(undefined)。

示例

通过此方法,您可以模拟 webglcontextlost 事件。

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

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

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

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

规范

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

浏览器兼容性

另见