Processing.js timer
Asked Answered
P

3

6

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.

Plant answered 6/6, 2012 at 10:54 Comment(0)
S
2

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/

Supine answered 6/6, 2012 at 11:58 Comment(0)
O
3

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.

Osseous answered 6/6, 2012 at 12:48 Comment(0)
S
2

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/

Supine answered 6/6, 2012 at 11:58 Comment(0)
A
0

I made up this class for a Game. It uses millis() and is independent of frameRate

class Timer{
  boolean increment, inProgress;
  int spawn, end;
  int seconds, tm_limit;

  Timer(int tm_limit){
    this.tm_limit = tm_limit;
    seconds = tm_limit;
    increment = false;
    inProgress = false;
  }

  Timer(){
    seconds = 0;
    increment = true;
    inProgress = false;
  }

  void start(){
    inProgress = true;
    spawn = millis();
  }

  void stop(){
    inProgress = false;
    end = millis();
  }

  int getSeconds(){
    if(inProgress){
      if(increment){
        seconds = int((millis() - spawn) / 1000);
      }
      else{
        if(seconds - int((millis() - spawn) / 1000) != seconds){
          seconds = seconds - int((millis() - spawn) / 1000);
          if(seconds <= 0) { stop(); }
          else spawn = millis();
        }
      }
    }
    return seconds;
  }

  void reset(){
    if(!increment)
      seconds = tm_limit;
    else
      seconds = 0;

    inProgress = false;
  }
}

If a Timer object is instantiated with a parameter, it is assumed that Timer should decrease. Otherwise, the exit condition can be checked by getting the value from getSeconds() method.

Aland answered 10/10, 2013 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.