fserb / canvas2D

Update Canvas 2D API
Other
151 stars 25 forks source link

`beginLayer` vs `beginFilter` #32

Open ianstormtaylor opened 2 years ago

ianstormtaylor commented 2 years ago

I'm curious about why the naming uses "layers" as a concept with beginLayer/endLayer, instead of calling them simply beginFilter/endFilter if their sole purpose is to be able to apply filters to groups of commands. Because I excepted "layers" to be something else, more analogous to layers in a graphics editor that can be shown/hidden/reordered/etc.

graveljp commented 1 year ago

Layers are not only useful for filtering operations. They can also be used to apply blending to the result of a group of draw calls, or more generally apply composite operations to such group.

The canvas API is procedural, not declarative. It would be wasteful to have the JavaScript build a whole layer, just for it to be marked as hidden later. If a layer is to be hidden, it's better to simply not render it in the first place. Perhaps what you have in mind aligns with the Recorded Picture proposal (also called Display List). Display Lists would allow a group of draw calls to be replayed multiple times. Layers and Display Lists would be very different in terms of implementation, and in terms for their performance characteristics. We think that Layers and Display Lists provide solutions for two different problems and are both complementary. It would be a shame to impose the cost of building optimizing and caching display list if it's to be rasterized only once. We would rather have a high-performance code-path for single-use draw call group rasterizing, and another API focused on making draw calls rasterizable multiple times.

Does that make sense?

ianstormtaylor commented 1 year ago

Hey @graveljp thanks for taking the time to write that up. That makes total sense on the reasoning for why it's designed the way it is. For actual layering I'd probably use multiple canvases stacked on top of each other.

I think my point is mostly about naming. Using the "layer" in the name is likely going to require every explanation of the concept to be prefaced by something like:

Note that the beginLayer / endLayer methods don't deal with layers in the traditional graphics editor sense, but are instead used to compose blending modes and filters.

(If you search "html canvas layers" you'll see lots of results all trying to solve the familiar problem of layering with canvas.)

So I'd suggest using a different naming scheme that avoids that confusion. Something like beginComposition, beginCompose, beginBlend, beginFilter, or some other name I'm not knowledgeable enough to know of.