w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.5k stars 661 forks source link

[resize-observer] why we need `ResizeObserver.takeRecords()` or `ResizeObserver.hasRecords()` (the canvas flickering problem) #9717

Open trusktr opened 10 months ago

trusktr commented 10 months ago

Spec: https://drafts.csswg.org/resize-observer/#resize-observer-interface

There's one main problem with ResizeObserver and rendering using canvas:

So, how do we know that we need to avoid drawing in rAF, and instead draw in an RO callback?

That's where this issue comes in.

If we had ResizeObserver.takeRecords(), we could get the records, run the resize logic in rAF, and then draw.

If we had ResizeObserver.hasRecords(), we could skip drawing in rAF, then let the draw happen in the following RO callback.


Note, if we get other observers like ComputedStyleObserver and BoundingClientRectObserver or ClientRectObserver for short, the same problem will exist with those if they run after rAF but before browser paint, and they should also have takeRecords and preferably hasRecords.


An alternative API that would help with these problems would be requestFinalFrame or requestPaintFrame.

The convention for web developers would become:

trusktr commented 10 months ago

A solution someone mentioned is to double buffer using two canvas (double the memory):