Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying a different requestAnimationFrame function #15

Open
IceCreamYou opened this issue Jun 12, 2017 · 6 comments
Open

Allow specifying a different requestAnimationFrame function #15

IceCreamYou opened this issue Jun 12, 2017 · 6 comments

Comments

@IceCreamYou
Copy link
Owner

This library currently uses window.requestAnimationFrame if available and falls back to setTimeout otherwise. The upcoming WebVR API will use display.requestAnimationFrame to schedule frames on attached VR displays. This library should support such loops.

@Djedjeska
Copy link

Hello, the documentation says that it can be used in Node.js with require('mainloop')
But when I tested, it simply isn't possible because of the use of "window', which obviously isn't available server-side...

@IceCreamYou
Copy link
Owner Author

Hello, the documentation says that it can be used in Node.js with require('mainloop')
But when I tested, it simply isn't possible because of the use of "window', which obviously isn't available server-side...

As explained in #21, the comment above is incorrect - in Node.js, MainLoop.js uses a custom fallback for requestAnimationFrame. However, adding the ability to use an alternative requestAnimationFrame function would also allow Node.js users to provide their own fallback instead of the one the library provides.

@dakom
Copy link

dakom commented Sep 2, 2019

In terms of the one the library provides, would there be any advantage to scheduling the timeout to use performance? if that is available?

Use case is running the loop in a worker which does not have requestAnimationFrame but does have performance

@IceCreamYou
Copy link
Owner Author

In terms of the one the library provides, would there be any advantage to scheduling the timeout to use performance? if that is available?

I don't understand the question. window.performance does not have any scheduling APIs.

@dakom
Copy link

dakom commented Sep 3, 2019

Yes but in the fallback, you schedule it via passing Date.now (and an offset) to setTimeout()

Would there be any advantage to using performance.now instead of Date.now ?

@IceCreamYou
Copy link
Owner Author

The short answer is no, there wouldn't be a meaningful difference. The long answer is that the differences are:

  • performance.now() is slower in microbenchmarks in some browsers but it shouldn't make a meaningful difference to call it once per frame.
  • Date.now() can jump around if the system clock changes. In practice this isn't a big deal because it happens rarely and the requestAnimationFrame shim in this library accounts for backward jumps.
  • performance.now() was intended to be higher precision, but in practice it is not any more because it was found that high precision timers could be used to exploit CPU cache bugs.
  • Date.now() is more portable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants