Status: explainer.
2D Canvas Rendering contexts are currently required to have persistent backing stores. This proposal aims to relax that requirement by introducing an API that allows canvases to be discarded by the browser and re-drawn by the web application on demand.
This is a long standing request from developers. It's particularly useful on Mobile, where context lost is more common.
// WebIDL changes
interface mixin CanvasContextLost {
boolean isContextLost();
};
CanvasRenderingContext2D includes CanvasContextLost;
OffscreenCanvasRenderingContext2D includes CanvasContextLost;
When the user agent detects that the backing storage associated with a Canvas2D context has been lost, it must run the following steps:
- Let canvas be the context's canvas.
- If canvas's context lost flag is set, abort these steps.
- Set context lost flag.
- Queue a task to perform the following steps:
- Fire a event
contextlost
at canvas - If the event's canceled flag is set, abort these steps. The backing buffer for the context will not be restored.
- Queue a task to restore the backing buffer for context.
- Fire a event
contextrestored
at canvas on completion of that task.
- Fire a event
UA could add support for "context loss test" by creating a
console.resetGraphicsContext()
method.
- Deprecate
webglcontextlost
in favor ofcontextlost
? - Deprecate
webglcontextrestored
in favor ofcontextrestored
? - What the default behavior should be?
- Currently the canvas has to be modified in order for context lost/restored events to fire. Is that the right behavior?
- The canvas has to be in the DOM and also visible in order for context lost/restored events to fire. Is that the right behavior?
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.addEventListener("contextlost", redraw);
canvas.addEventListener("contextrestored", redraw);