MDN self-hosted library · e01.org

HTMLCanvasElement: webglcontextlost event

WebGL API

WebGL API

The webglcontextlost event of the WebGL API is fired if the user agent detects that the drawing buffer associated with a WebGLRenderingContext object has been lost.

This event does not bubble.

Syntax

Use the event name in addEventListener():

addEventListener("webglcontextlost", (event) => { })

Alternatively, assign to the event handler property of the interface:

onwebglcontextlost = (event) => { }

Event type

A WebGLContextEvent. Inherits from Event.

WebGLContextEvent

Example

With the help of the WEBGL_lose_context extension, you can simulate the webglcontextlost event:

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

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

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

// "webglcontextlost" event is logged.

Specifications

Browser compatibility

See also

  • WebGLContextEvent
  • WebGLRenderingContext.isContextLost()
  • WEBGL_lose_context, WEBGL_lose_context.loseContext(), WEBGL_lose_context.restoreContext()