Barrie Reader
Barrie Reader

Reputation: 10713

Limiting Framerate on Canvas application

I have built a very basic platform "engine":
[Fiddle Removed]

When you move with the arrow keys - the framerate is mentally fast!

I had tried to limit the framerate by using:

var fps = 30;
var now;
var then = Date.now();
var interval = 1000/fps;
var delta;

Then in the draw function:

now = Date.now();
delta = now - then;
if (delta > interval) {
    //redrawing scene here
    then = now - (delta % interval);
}

But unfortunetely this still has the same frame rate but the performance decreased (like a frame skip instead of a framerate limit).

Please help me! :(

I'll give you a cookie!

Upvotes: 2

Views: 1567

Answers (1)

enhzflep
enhzflep

Reputation: 13089

You should review your GameLoop function, methinks. As it stands, you're asking for 1000 frames a second. oops!

When I edit it into this, it gives me about 33 fps.

    GameLoop: function() { //initialise game loop
        Game.Vars.GameLoop = setTimeout(function() { 
            requestAnimationFrame(Game.Functions.Update, Game.C); 
        }, 30);
    },

You had that 30 written as a 1.. :) Choc-chip, if that's okay. If not, I'll take white chocolate and raspberry!

EDIT: Effing Hell, you made it! - gotta love those messages one writes for oneself. :D

Upvotes: 3

Related Questions