Re: requestAnimationFrame

On Tue, Nov 16, 2010 at 12:03 PM, Gregg Tavares (wrk) <gman@google.com>wrote:

> One is, how should this api be used if I want an app to update at 10hz.  It
> seems to be designed to assume I want the maximum frame rate. If I want to
> run slower would I just use
>
> setInterval(function() {
>    window.requestAnimationFrame();
>   }, 100); // request frames at 10hz?
>
> That's fine if that's the answer
>

If you really want to animate in 10Hz steps, then I suggest you do something
like
var start = window.animationTime;
var rate = 10; // Hz
var duration = 10; // s
var lastFrameNumber;
function animate() {
  var elapsed = window.animationTime - start;
  if (elapsed < duration) {
    window.requestAnimationFrame(animate);
  }
  var frameNumber = Math.round(elapsed/(1000/rate));
  if (frameNumber == lastFrameNumber)
    return;
  lastFrameNumber = frameNumber;
  // ... update the display based on frameNumber ...
}
window.requestAnimationFrame(animate);

(Out of curiosity, what are the use-cases for this?)

That seems to have 2 issues.
>
> 1) All of them will get a beforePaint event even if most or all of them are
> scrolled off the visible area since there is only 1 beforePaint event
> attached to the window.
>

Something could be done here, but it seems rather complex to specify things
in a way that avoids firing callbacks when relevant content is "out of
view".

2) All of them will get beforePaint events at the speed of the fastest one.
> If one ad only needs to update at 5hz and other updates at 60hz both will
> update at 60hz.
>

The above approach solves this I think.

Rob
-- 
"Now the Bereans were of more noble character than the Thessalonians, for
they received the message with great eagerness and examined the Scriptures
every day to see if what Paul said was true." [Acts 17:11]

Received on Monday, 15 November 2010 23:59:26 UTC