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 批准的擴充套件規範

瀏覽器相容性

另見