Continuous Programming Mode

Till now, our programs run their code once (from top to bottom) and stopped (basic programming mode). But for our programs to have any sort of animation or interaction, they need to run continuously. Enter the continuous programming mode!

// whatever is inside setup() is executed once
void setup() {
size(200,200);
}

// whatever is inside draw() is executed continuously
void draw() {
background(0);
ellipse(width/2,height/2,20,20);
//I still see one static image but this ellipse is constantly redrawn!
print("drawn");
}

setup()

setup() is called once when the program is started and is useful for setting up your environment

draw()

is called immediately after setup() and repeats its statements until the program is stopped (close the window or press the stop button)

*You can only have one setup() and one draw() in your program. You can not mix setup() and draw() with spare statements (except from variable declarations)

void setup() {
size(200,200);
...
}

//this will cause an error!
println(width);

void draw() {
...
}

How often does draw() execute?

frameRate(some number); //frames to be drawn per second

frameRate() defines how many times your screen will be refreshed. The default rate is 60.

 

*frameCount will report to you the number of the current frame (counting from the start of your program). Much like width and height, frameCount is one of the things that Processing already knows and can report back to you. You can be sure that you will get the proper, updated answer on every frame.

Note:

In the continuous programming mode, we have the possibility to create motion and animation by changing our drawings over time. But for that, fixed/ hand-typed numbers will be never enough since we have no way to change their value over time...