1

I have this piece of code:

var framerate = 120;

function setup() {
  createCanvas(1920, 1080);
  frameRate(framerate);
}

function draw() {
  clear();
  textSize(13);
  text(frameRate(), 20, 20);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.0/p5.js"></script>

I want it to set the maximum frameRate to 120, and then display the current frameRate at the top right of the canvas. However, it is currently displaying numbers from 163 to 170. This shouldn't be my frameRate, as I set the maximum to 120 in the setup function.

6
  • I suggest taking a look at the docs: "Calling frameRate() with one numeric argument, as in frameRate(30), attempts to draw 30 frames per second (FPS). The target frame rate may not be achieved depending on the sketch's processing needs. Most computers default to a frame rate of 60 FPS. Frame rates of 24 FPS and above are fast enough for smooth animations. Calling frameRate() without an argument returns the current frame rate. The value returned is an approximation." Commented Jun 21 at 18:58
  • When I run your snippet it hovers around 60 FPS, MacBook M3, Sonoma 14.4, 60 hz monitor refresh rate on the display. What are your actual goals, and is there a problem to be solved here, or a user impact of the frame rate not being what you expect it to be? Commented Jun 21 at 18:59
  • @Nproductions I wonder if p5 might ignore the maximum frame rate in favour of your testing display refresh rate is ( vsync may be part of it). Can you try manually managing the draw loop ? (e.g. calling noLoop() in setup, then managing requestAnimationFrame() with the interval you need (120Hz)). Also, if p5.js isn't a must you can test a separate library such as pixi.js. (The API may not as novice friendly, however they have a lot of examples to get started and are performance oriented). HTH Commented Jun 26 at 9:44
  • @ggorlen im using windows 11 165hz monitor. my framerate is capped at 165. the script is supposed to set the maximun framerate that the sketch will run at to 120, and then display what the framerate of the sketch is running at. however, running the script on vs code returns numbers around 160, and sometimes numbers above my monitor's vsync. Commented Jul 3 at 16:45
  • @GeorgeProfenza my vsync is capped at 165, because my monitor is 165hz monitor. sometimes the numbers reach 170, which isnt even true because my monitor is capped at 165. Commented Jul 3 at 16:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.