Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing.js timer

I am developing an application using Processing.js.

At each step in the drawing loop I increment the number of frames by one frame++ .

I want to know how much time has passed. Currently to get the time passed (knowing that my application is set to run on 60FPS) I do like so: time=frame/60 . But this only works if the application always run at exactly FPS and we all know that's not the case cause it depends on the users hardware.

I want the timer to be pretty accurate (having only like 0.0001s error).

Suggesting some javascript algorithm for calculating the difference between now() and start_time() is also welcome.

like image 684
XCS Avatar asked Jun 06 '12 10:06

XCS


2 Answers

If you want accuracy, take a look into high resolution timers. However, this feature isn't available on all browsers.

Also, Processing.js has a built-in read only variable named frameCount. You can query that instead of counting the frames yourself.

like image 164
Andor Avatar answered Nov 19 '22 12:11

Andor


You can store the start time at a variable.

Then create a new timer whenever you want and subtract the start time variable from it.. The result will be the difference in milliseconds..

You can also use the actual time divided by the frames counter to get an accurate average frame-rate of your application..

something like

var startTimer = new Date(); // at start (once)

and whenever you want to check

var passed = new Date() - startTimer; // in milliseconds

Demo at http://jsfiddle.net/gaby/CF4Ju/

like image 20
Gabriele Petrioli Avatar answered Nov 19 '22 10:11

Gabriele Petrioli