[Canvas2D] Add BeginLayer/EndLayer
- Category
- Graphics
- Type
- New or changed feature
- Status
- In development (Chrome In development)
- Intent stage
- Start prototyping
Summary
Add beginLayer() and endLayer() methods to the CanvasRenderingContext2D interface, that create and close layers for Canvas. These layers behave as if all the draw calls they contain are rendered on a separate texture, which is then rendered into the Canvas (or parent layer for nested layers), using the drawing state as it was when beginLayer() was called. By drawing layers as a single unit, filters, blending, and compositing operations can be applied when drawing the final content of the layer onto the destination. This is more user friendly than using a temporary canvas and allows the browser to implement optimizations. Optionally, beginLayer() can be called with a filter as an argument, so that the resulting texture from the layer is rendered using that filter. Filters can be specified as a CSS filter string or as a CanvasFilterInput object using dictionaries to describe SVG filters with a JavaScript syntax. Note that beginLayer() and endLayer() save and restore the full current state of the context, similarly to save() and restore().
Motivation
To apply a filter or compositing to a set of draw operations (as opposed to a single one), the only current solution is draw to a temporary canvas and then draw the temporary canvas into the final destination. This proposal adds an alternative to that by creating a layer that then will be drawn as a single unit. This would be equivalent to having a temporary canvas, except not only in a much more friendly way, but with the added benefit that the browser can take care of deciding what's the best dimension of the temporary canvas, given the current transform/clip.
Standards & signals
- Specification: https://github.com/whatwg/html/pull/9537
- Firefox: Closed Without a Position — Waiting for feedback
- Safari: No signal — Waiting for feedback
- Web developers: No signals
- Tracking bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1396372
Ecosystem
- Web Platform Tests: https://wpt.fyi/results/?q=canvas2d-add-beginlayer-endlayer
- Chrome Platform Showcase: https://chrome-platform-showcase.paulkinlan-ea.deno.net/features
- GenDN reference: https://gendn.paulkinlan-ea.deno.net/features
- chrome.dev demos search: https://www.google.com/search?q=site%3Achrome.dev+%5BCanvas2D%5D%20Add%20BeginLayer%2FEndLayer
- MDN Web Docs: https://developer.mozilla.org/en-US/search?q=%5BCanvas2D%5D%20Add%20BeginLayer%2FEndLayer
- Baseline status: https://webstatus.dev/?q=%5BCanvas2D%5D%20Add%20BeginLayer%2FEndLayer
Explainers: https://github.com/fserb/canvas2D/blob/master/spec/layers.md https://github.com/whatwg/html/issues/8476