Thanks for the input guys. I didn’t know about requestIdleCallback until now. It has an interesting feature where it lets you detect time remaining until the end of the rAF (if we’re lucky enough for it to fire):
requestIdleCallback(function myNonEssentialWork (deadline) {
while (deadline.timeRemaining() > 0)
doWorkIfNeeded();
})
It seems deadline.timeRemaining()
shows the time remaining until the end of the rAF.
I’m trying to determine the best possible way to handle the animation loop for the engine we’re working on at infamous.io (name subject to change).
Maybe I can request a single animation frame callback, then place all rendering code into idle callbacks so I always know how much time is left in the frame. I’m also using raf-timeout to see if we can prevent 3rd party libs from running async code at times that might effect rendering. I haven’t experimented with monkey patching Promise.resolve().then
yet, which is faster than setTimeout(..., 0)
. In my apps, I never need (yet) to execute anything in a future-but-soon-as-possible tick, so monkey patching setTimeout, etc, with rAF is fine in my cases, and probably fine for the vast majority of apps.
@tabatkins @yoavweiss @jokeyrhyme One reason I’m researching this is I’m thinking that there might be a way to specify some animations in the engine to be secondary, so that they can run at lower FPS when necessary in order for primary animations to be at 60fps; if we know there’s no time remaining in the frame, then we can skip some frames of the secondary animation. Another thing I’m trying to achieve is figuring out if we’re in a frame or not when calling the setter of a Node in the scene graph: calling node.rotation = [0,0,20]
, for example, will trigger the engine to request an animation frame in order to call node.render()
, but, if we’re already in a frame, we don’t need to queue more rAF’s when the node.rotation
setter is used inside the rAF, so I’m determining how to handle that.