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

Real Time Animations and Maximum FPS Limiter #366

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

dgiangrave
Copy link

  1. Real Time Animation
  • Uses delta time provided by request anim frame to measure time since last animation and move particles according to real time.

  • Objects no longer move based on CPU power, but rather always a set distance based on time.

  1. FPS Limiter
  • Allows user to specify a maximum frame count for updates. Should the FPS be too fast, the CPU will not update the particles but call anim frame and return.
  • Greatly reduces CPU usage providing less usage between draws and sparing the CPU from a timeout function.
  1. Pause On Hidden
  • Recognizes when tab is not visible and cancels current animation frame, keeping existing state. Restarts draw frame from where it left off upon regaining visibility.

caldempsey and others added 3 commits May 21, 2018 12:30
Minor inconsistency bug fix. fps_limit variable lives at top level of JSON object, not in particles.
Added delta time measurment to update particles based on world time and adjusted max FPS to use request animation frame rather than set timeout.
@@ -153,6 +154,7 @@ particlesJS.load('particles-js', 'assets/particles.json', function() {

key | option type / notes | example
----|---------|------
`.fps_limit` | Limits the fps of the animation redraw, set to 0 for no limit | `30`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`.fps_limit` | Limits the fps of the animation redraw, set to 0 for no limit | `30`
`.fps_limit` | Limits the fps of the animation redraw, set to 0 for no limit | `60`

Should this be 60?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be merged is what it should be!

// If we are too fast, just draw without updating
var fps_limit = pJS.fps_limit;
if(timestamp < lastFrameTime + (1000 / fps_limit)) {
requestAnimFrame(pJS.fn.vendors.draw);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong but I think requestAnimationFrame MUST be called on each loop but you can then check if the function is not suppose to run and just return.

So basically but the requestAnimFrame(...) before the if statement.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the code below it's called the function, it's handled by other if/else because the animation stops if the particles.move.enable flag is disabled (and it shouldn't IMO). Thisif is used because it's needed to check the delta between the previous frame to see if a new draw is needed, if it's not needed the values are not updated so the next rAF call could be the right one to update everything. For example, if you want 30FPS instead of 60, you need to skip one frame, because rAF is called the fastest possible depending on the monitor refresh rate (60 Hz - 60 fps).

The issue solved by this PR is that this library doesn't handle well different frequencies, on my MBP 2021 when I use Chrome I have 120FPS I see faster animations than Safari, limited to 60FPS. Every speed is doubled on Chrome, and this is a bad behavior.

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

Successfully merging this pull request may close these issues.

5 participants