How often does the draw function loop?

Hi, I’m new to processing and I noticed my draw function was looping. Does this always happen and if so how many times does it loop?

1 Like

Yes, the draw() function runs over and over again, and each time it finishes, what is displayed in your sketch is updated. It runs as fast as it can, but no faster than the frame rate is set to. Typically it will redraw your sketch 60 times a second.

Realize that it’s not “looping”, but running over and over again.

It will run over and over again until the sketch is closed, either by pressing the X on the window, killing it, or you call exit().

4 Likes

Hi,

Welcome to the community! :wink:

Yes it always happen luckily for us! Unless you use the noLoop() function which stops the loop.

As @TfGuy44 said, most of the time it’s running around 60 fps. You can check this by printing the frameRate variable :

void draw() {
  println(frameRate);
}

From the documentation :

The system variable frameRate contains the approximate frame rate of a running sketch. The initial value is 10 fps and is updated with each frame. The value is averaged over several frames, and so will only be accurate after the draw function has run 5-10 times.

And also the draw() function page : draw() \ Language (API) \ Processing 3+

Called directly after setup() , the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called. draw() is called automatically and should never be called explicitly. All Processing programs update the screen at the end of draw(), never earlier.

3 Likes

Welcome to the Processing community, @alex3!

If you wish to change the rate of repetition of the draw() function, you can call the frameRate() function, passing the desired number of frames per second as an argument. Here is an example:

void setup() {
  size(480, 360);
  frameRate(10);
}

See Processing Reference: frameRate().

Edited on March 23, 2021 to clarify the explanation.

1 Like

Hello,

Lots of resources (tutorials, examples, references, etc.) available here:
https://processing.org/

You are asking about draw():
https://processing.org/reference/draw_.html

This also has related references on the page such as:
setup() \ Language (API) \ Processing 3+
frameRate() \ Language (API) \ Processing 3+
And others…

And so your journey begins…

:)