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

Is it possible to force a lower framerate than 60? #68

Open
JL102 opened this issue Mar 9, 2021 · 2 comments
Open

Is it possible to force a lower framerate than 60? #68

JL102 opened this issue Mar 9, 2021 · 2 comments

Comments

@JL102
Copy link

JL102 commented Mar 9, 2021

Hi,

I'm kinda surprised that having an idle animation with glslCanvas causes a non-negligible amount of CPU usage. When my animation is idle, it uses 6-7% of my CPU, and when I mouse over it, it uses ~15% of my CPU. I figure it would be less resource intensive if I force it to refresh at 30 frames per second instead of 60. Is there a simple way of doing that?

@JL102
Copy link
Author

JL102 commented Mar 10, 2021

I edited glslCanvas myself to add that functionality, and it works quite well. Here's what I changed:

  • Added this on line 1451 (after it loads the shader) to take a "data-framerate" attribute:
// Set framerate
if (canvas.hasAttribute('data-framerate')) {
    this.framerate = parseInt(canvas.getAttribute('data-framerate'));
}
  • Changed RenderLoop:
function RenderLoop() {
    if (sandbox.nMouse > 1) {
        sandbox.setMouse(mouse);
    }

    if (sandbox.resize()) {
        sandbox.forceRender = true;
    }

    sandbox.render();
    if (sandbox.framerate) {
        window.setTimeout(() => {
            sandbox.animationFrameRequest = window.requestAnimationFrame(RenderLoop);
        }, Math.floor(1000 / sandbox.framerate));
    }
    else {
        sandbox.animationFrameRequest = window.requestAnimationFrame(RenderLoop);
    }
}

With the framerate set to 30, there's not even a noticeable difference, and idle CPU usage is down to 2-3%!
Others might like the functionality, if you want to add it to the source :)

@benosse
Copy link

benosse commented Dec 27, 2021

Thanks, works like a charm! I think it would be a nice addition to the source :)

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

2 participants