Skip to content

Instantly share code, notes, and snippets.

@hamoid
Created May 3, 2013 12:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamoid/5508859 to your computer and use it in GitHub Desktop.
Save hamoid/5508859 to your computer and use it in GitHub Desktop.
In Processing, frameRate() sets the global frame rate. It does not allow to set independent frame rates for different objects. To achieve that, you can avoid drawing every object on every frame, effectively reducing the frame rate for some objects. You can learn more about the modulo operation (%), if statements and other concepts used in this p…
void setup() {
// Global frame rate is 30 fps
// 1 second = 30 frames
frameRate(30);
}
void draw() {
// Change the background once every 30 frames
// 30 frames is 1 second, so that means
// change the background at 1 fps
if(frameCount % 30 == 0) {
background(random(255), random(255), random(255));
}
// Draw an ellipse once every 10 frames
// 30 frames is 1 second, so 10 frames is one third
// of a second, which means Processing has time to
// draw 3 circles in one second: 3 fps
if(frameCount % 10 == 0) {
ellipse(random(width), random(height), 30, 30);
}
// You can think of the number behind the % sign
// as a way to divide the frames per second to slow
// things down.
// The movie runs at 30 fps.
// If you divide it by 30, you get 1 fps.
// If you divide it by 10, you get 3 fps.
// If you want 15 fps, divide by 2:
// frameCount % 2 == 0
// If you want 10 fps, divide by 3:
// frameCount % 3 == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment