Reset frameRate()

Hey there,
I’m having a demo where I need the frameRate to be set to 5.
But later on the demo I need to reset the frameRate to the default one.
I don’t want to set a fixed value frameRate(30) as I want my frameRate to follow the default requestAnimationFrame callback method.

How could I “reset” the frameRate to its initial value?

Thanks!
Mamboleoo

2 Likes

Don’t.

Instead, when you want to limit the frame rate to 5fps, restructure your drawing code so that it only updates what is drawn every 200ms.

Example:

boolean draw_at_five_fps = true;
int time_to_draw = 0;

void draw(){
  if( draw_at_five_fps ){
    if( time_to_draw >= millis() ){
      time_to_draw = millis() + 200;
      // Update variables and draw something slowly.
    }
  } else {
    // Update variables and draw something quickly.
  }
} // End draw()

void toggle_slowmode(){
  draw_at_five_fps = !draw_at_five_fps;
  if( draw_at_five_fps ){
    time_to_draw = millis();
  }
}

… or just set it to frameRate(60). Processing won’t go any faster than that anyway.

2 Likes

I’m not familiar with Processing, but P5.js can go faster than 60fps since the frame rate is linked to the screen refresh rate.
And I don’t want to set frameRate(200) as it won’t be useful for most of the users.

You should re-read the answer given! :wink:

I do understand the first answer and the end result would indeed be the same.
In my case I wanted to know if there was a way to reset the frameRate setting to its default.
Since you can set a specific frameRate I was interested to know if there was a way to do the opposite like frameRate(AUTO).
I looked into the source code but I couldn’t find anything :frowning:
(For my specific case, I’m showing demos to beginners and I don’t want to overload them with more logic & code outside of P5)

From Processing’s reference Processing.org/reference/frameRate_.html:

The default rate is 60 frames per second.

See reference | p5.js and you’ll notice it’s different.

However, it does look like you can call frameRate() to get the existing rate, so do that before switching from the default? (noting the caveat that you can’t do it on first draw call)

1 Like

Oops! Forgot this is p5js category! :woozy_face:

However, this statement below from p5js’ frameRate() reference doesn’t seem correct: :thinking:

The default frame rate is based on the frame rate of the display (here also called “refresh rate”), which is set to 60 frames per second on most computers.

In the source code, the internal variable _targetFrameRate is initialized w/ 60; same as Processing’s Java Mode: :relieved:
https://github.com/processing/p5.js/blob/1.0.1/src/core/environment.js#L16-L17
Looking up the p5js repo I couldn’t find anywhere it could be based on monitor’s refresh rate. :tv:

1 Like

The reason why is that method p5::frameRate() returns the internal property p5::_frameRate, which is merely the current temporary FPS calculation for p5::deltaTime purposes: :timer_clock:
https://github.com/processing/p5.js/blob/1.0.1/src/core/main.js#L376-L380
Instead of p5::_targetFrameRate, which is the actual desired FPS: :expressionless:
https://github.com/processing/p5.js/blob/1.0.1/src/core/environment.js#L295-L306

In short, when p5::frameRate() is invoked as a setter, it mutates p5::_targetFrameRate. :+1:

But when it’s invoked as a getter, it returns p5::_frameRate instead. :crazy_face:

2 Likes

Good call! So it’s basically exactly like the Java API with different documentation. :roll_eyes: At least it’s just safe to treat 60 as the default still.

1 Like