contextlost and contextrestored
The CanvasRenderingContext2D.isContextLost() method of the Canvas 2D API returns true if the rendering context is lost (and has not yet been reset). This might occur due to driver crashes, running out of memory, and so on.
If the user agent detects that the canvas backing storage is lost it will fire the contextlost event at the associated HTMLCanvasElement. If this event is not cancelled it will attempt to reset the backing storage to the default state (this is equivalent to calling CanvasRenderingContext2D.reset()). On success it will fire the contextrestored event, indicating that the context is ready to reinitialize and redraw.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 99 | 99 | 125 | | 99 | | |
| The contextlost event of the Canvas API is fired if the user agent detects that the backing storage associated with a CanvasRenderingContext2D context is lost. Contexts can be lost for several reasons like driver crashes or the application runs out of memory, etc. | 99 | 99 | 125 | | 99 | |
| The contextrestored event of the Canvas API is fired if the user agent restores the backing storage for a CanvasRenderingContext2D. | 99 | 99 | 125 | | 99 | |
| The contextlost event of the OffscreenCanvas interface is fired if the browser detects that the OffscreenCanvasRenderingContext2D context is lost. Contexts can be lost for several reasons, such as an associated GPU driver crashes, or the application runs out of memory, and so on. | 99 | 99 | 125 | | 99 | |
| The contextrestored event of the OffscreenCanvas interface is fired if the browser restores an OffscreenCanvasRenderingContext2D context that was previously lost. | 99 | 99 | 125 | | 99 | |
| The CanvasRenderingContext2D.isContextLost() method of the Canvas 2D API returns true if the rendering context is lost (and has not yet been reset). This might occur due to driver crashes, running out of memory, and so on. | 99 | 99 | 125 | | 99 | |
Syntax
isContextLost() Use cases
-
Using contextlost and contextrestored
The CanvasRenderingContext2D.
Cautions
- Limited browser support. Check compatibility before use.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.