Jump to content
  • Advertisement
Sign in to follow this  

Javascript/Canvas constan frame rate

This topic is 2378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For a long time I have thought my code to ensure constant frame rate was right but recently I noted that the simple code to calculate when to run the next update is imprecise and always triggering at wrong time.

I discovered It when I changed the frame rate to 100 in place of 60 or 30, and the game started running at 90-65 fps. This means my browser can easily run It at 60 fps constant.

Here the code I think is wrong:

var nextTickTimeout = 1000/60 - (startTime - (new Date().getTime()));
if (nextTickTimeout < 0) nextTickTimeout = 0;
if (!quit) setTimeout(gameTick, nextTickTimeout);

And this is the whole function:

function gameTick ()
var startTime = new Date().getTime();
date = new Date();
lastTime = currentTime;
currentTime = date.getTime();
deltaTime = currentTime - lastTime;
milliseconds += deltaTime;

// update game objects status

// render graphics
if (frameCount == Math.floor(frameCount / frameSkip) * frameSkip) engine.draw();

// code to measure FPS (Frames per Second)
if (milliseconds >= 1000)
fps = frameCount;

// update FPS for this frame
if (fpsElement != null) fpsElement.innerHTML = frameCount + " FPS";

// reset frameCount and milliseconds
frameCount = 0;
milliseconds = milliseconds - Math.floor(milliseconds / 1000) * 1000;

frameCount += 1;

var nextTickTimeout = 1000/60 - (startTime - (new Date().getTime()));
if (nextTickTimeout < 0) nextTickTimeout = 0;
if (!quit) setTimeout(gameTick, nextTickTimeout);

Maybe is not possible to do It in javascript? I don't know how precise is setTimeout.

This is more of an experiment I'm doing to found out how complex the game can be and still run at least at 30-25 fps in a not so new machine. I will change "nextTickTimeout = 1000/60" for "nextTickTimeout = 1000/30" or 25 if 60 probes to be too much.
I have seen HTML 5 games of comparable complexity to the one I want to make so my game should be possible.

Share this post

Link to post
Share on other sites
JavaScript implementations have a lot of issues with garbage collection, and other things, that don't really allow you to have a constant frame rate (especially Firefox's GC which is horrible).

Instead of trying to get correct frame rates with intervals or requestFrameAnimation (which is even worse than intervals as far as I can see), make your physics/animation/logic/whatever part to run in a constant frame rate using a method that is explained here: gafferongames.com/game-physics/fix-your-timestep/

Share this post

Link to post
Share on other sites
Thank you.

I think that will work. As It still count as constant frame my already written code won't break.

One more think, last time I did (while (!quit)) in Javascript the page became unresponsive (can't click buttons, write in text fields, etc). For those reading this post that may not know yet, in Javascript, the most similar to a main loop you can do and still maintain responsiveness on the page is this:

var quit = false;

function run () {
var span = document.getElementById("thespan");
if (span.innerHTML == "Hello") span.innerHTML = "Bye"; else span.innerHTML = "Hello"; // Quit this

// Apply what I learned at gafferongames.com here

// Call engine.update() one, two or more times per run. In some cases update will be called one time each two or more run.

if (!quit) setTimeout(run, 0); // second parameter will be always 0

I want to post one more time to this topic to show the results obtained from applying what I learned from gafferongames.com in case It is of interest to some.

Yes, this is the way to go.

My update cycles are called assuming a fixed delta time (1000/60 for example). Per each canvas refresh engine.update can be called two or more times, or even no called at all. Time between canvas render accumulates in a counter, when it is equal or greater than the desired delta time I start calling engine.update until it "consumes" the accumulated time, eating "desired delta time" each call.

I don't have use to the linear interpolation showed at gafferongames.com. Even if I calculate alpha, I cannot pass it to a render function, I don't have one. The more I can do is to call engine.update one more time if alpha is between [0.6-1.0] or similar. My update function does not accept parameters, It assumes a fixed delta thus no need for extra parameters.

I changed my method to measure FPS too.

Now I have 60 FPS most of the time in Chrome, with occasional 61 or 59 values. I have to add a lot of sprites to make it go to 30.

The strange thing, is that Firefox says 60 FPS too, but you can tell just by looking that animations aren't smooth. Completely different than Chrome. The FPS counter is measuring correctly, the time needed for a sprite to go from A to B is the same in both browsers, but animations looks strange in Firefox, this is more noticeable if Firefox is running on Linux. I thought solving the constant frame rate thing solves anything but it seems this is something specific to Firefox. Don't sure what is in the way now, maybe the garbage collector or the canvas implementation of each browser.

Edit 2: the solution to that was to use requestAnimationFrame and not setTimeout to end the run function. Animations play smoothly in both browsers.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!