← Back to release summary

[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

Ecosystem

Explainers: https://github.com/fserb/canvas2D/blob/master/spec/layers.md https://github.com/whatwg/html/issues/8476

View on chromestatus.com