You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 3, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Jun 3, 2020. It is now read-only.
Should resizing callbacks happen before animation frame? #37
Because, I imagine user code (for example) will want to update WebGL based on if the WebGL's canvas' parent element was resized, and the code to do this will be inside an animation frame.
The code to update the WebGL could possibly run in a resize observer callback, but then this can be sort of ugly because it can run more than once for the same browser paint (which could be expensive).
To prevent this, logic could be deferred to the following animation frame, but this is problematic too because then WebGL rendering will be behind DOM rendering by 1 frame all the time, causing visual disparity ("lag") during animation of a canvas' parent element's size.
I'm not sure if any microtasks fire between the resize observer callback loop and the browser paint (I imagine they should), but that would also be another way to workaround the problem, but it is potentially ugly and harder to reason code that is using Promise.resolve().then() in such a way.
So, I think that currently the best place to run the resize observer loop is before the user's animation frames are fired. The downside to this is that code in the animation frames can resize DOM, which means that those resize observer callbacks won't fire until the next frame which can also cause visual disparity bugs.
Maybe best solution is (?) to run resize observer callbacks before animation frames, then if size changes are detected in the animation frames also run the observer callbacks after the animation frames, then finally the browser can paint.
The code to update the WebGL could possibly run in a resize observer callback, but then this can be sort of ugly because it can run more than once for the same browser paint (which could be expensive).
Why would it run more than once? This would only happen if elements were being resized inside ResizeObserver callback.
ResizeObserver loop cannot run before rAF because it includes layout update, and layout always happens after rAF.
From the spec:
'ResizeObserver extends step 12 with resize notifications. It tries to deliver all pending notifications by looping until no pending notifications are available. This can cause an infinite loop. Infinite loop is prevented by shrinking the set of nodes that can notify at every iteration. In each iteration, only nodes deeper than the shallowest node in previous iteration can notify."
If I understand correctly, assuming that an "iteration" once per frame (between an animation frame and the browser paint), it is only possible for a single resize event to ever hit a WebGL's canvas' parent element. And that if it was resized for any reason by a deeper node's resize event, then the canvas's parent's resize event will happen after the next animation frame.
Is that what it means?
I see two potential problems with this:
Code that relies on an element's size inside an animation frame (not inside a ResizeObserver callback) will be one frame behind (if for whatever reason a resize event triggers a deepere resize event of the element who's size is depended upon inside the animation frame), which can result in visual disparity. Code that relies on sizing would have to be moved out of requestAnimationFrame and into a ResizeObserver.
Suppose an rAF frame re-draws a webgl scene. It has no guarantee that a following resize event will happen. A following resize event could possibly re-draw the scene a second wasteful time, which could cause a perceivable frame drop (a slight pause) due to a frame taking twice as long to render than expected.
The problems you mention are real. My intuition is that this case is rare enough that it does not need to be solved by RO API since there are workarounds. For both scenarios, the problem is the same:
Element drawn by rAF gets resized inside RO callback after being drawn inside rAF.
This can be avoided with some extra code. The workarond is to detect pending resize inside rAF, and then either:
a) perform resize inside rAF, then draw
b) skip drawing in rAF, draw in RO callback
Activity
trusktr commentedon Mar 18, 2017
Maybe best solution is (?) to run resize observer callbacks before animation frames, then if size changes are detected in the animation frames also run the observer callbacks after the animation frames, then finally the browser can paint.
atotic commentedon Mar 20, 2017
Why would it run more than once? This would only happen if elements were being resized inside ResizeObserver callback.
ResizeObserver loop cannot run before rAF because it includes layout update, and layout always happens after rAF.
From the spec:
'ResizeObserver extends step 12 with resize notifications. It tries to deliver all pending notifications by looping until no pending notifications are available. This can cause an infinite loop. Infinite loop is prevented by shrinking the set of nodes that can notify at every iteration. In each iteration, only nodes deeper than the shallowest node in previous iteration can notify."
trusktr commentedon Mar 21, 2017
@atotic
If I understand correctly, assuming that an "iteration" once per frame (between an animation frame and the browser paint), it is only possible for a single resize event to ever hit a WebGL's canvas' parent element. And that if it was resized for any reason by a deeper node's resize event, then the canvas's parent's resize event will happen after the next animation frame.
Is that what it means?
I see two potential problems with this:
requestAnimationFrameand into aResizeObserver.atotic commentedon Mar 27, 2017
The problems you mention are real. My intuition is that this case is rare enough that it does not need to be solved by RO API since there are workarounds. For both scenarios, the problem is the same:
This can be avoided with some extra code. The workarond is to detect pending resize inside rAF, and then either:
a) perform resize inside rAF, then draw
b) skip drawing in rAF, draw in RO callback