HTMLCanvasElement: contextlost event
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Thecontextlost event of theCanvas API is fired if the user agent detects that the backing storage associated with aCanvasRenderingContext2D context is lost.Contexts can be lost for several reasons like driver crashes or the application runs out of memory, etc.
By default the user agent will attempt to restore the context and then fire thecontextrestored event.User code can prevent the context from being restored by callingEvent.preventDefault() during event handling.
In this article
Syntax
Use the event name in methods likeaddEventListener(), or set an event handler property.
addEventListener("contextlost", (event) => { })oncontextlost = (event) => { }Event type
A genericEvent.
Example
The code fragment below detects thecontextlost event.
const canvas = document.getElementById("canvas");canvas.addEventListener("contextlost", (event) => { console.log(event);});To prevent the context from being restored the code might instead look like this:
const canvas = document.getElementById("canvas");canvas.addEventListener("contextlost", (event) => { event.preventDefault();});Specifications
| Specification |
|---|
| HTML> # event-contextlost> |