W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2010

Re: requestAnimationFrame

From: Robert O'Callahan <robert@ocallahan.org>
Date: Tue, 16 Nov 2010 12:58:57 +1300
Message-ID: <AANLkTi=RfwPwxtztYDsPcJ6N6WvCV-vkOJZhLF3j6zbg@mail.gmail.com>
To: "Gregg Tavares (wrk)" <gman@google.com>
Cc: "public-webapps@w3.org" <public-webapps@w3.org>
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 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:42 GMT