I would like to know how can I set a reset loop: While the draw() is going on I would like to clear the canvas so I can start to draw on a clean canvas again and again.
(I don’t want to use keyPressed() or mousePressed() it has to be automatic)
import ddf.minim.*; Minim minim; AudioInput in; void setup() { minim = new Minim(this); minim.debugOn (); in = minim.getLineIn(Minim.MONO, 100); fullScreen(); background(255, 60, 80); } void draw() { float soundlevel; float distance_top = random(100); int t; int interval = 10; String time = "010"; soundlevel = in.mix.get(0); stroke(255, random(90, 255)); line(0, distance_top + soundlevel * 4000, width, distance_top + soundlevel * 1000); line(0, distance_top + soundlevel * 1000, width, distance_top + soundlevel * 4000); t = interval-int(millis()/100); time = nf(t, 3); if (t == 0) { redraw(); interval = interval +10; } }
Thank you for your help in advice!
Advertisement
Answer
You can simply clear the background again, for example when you press a key:
void keyPressed(){ background(255, 60, 80); }
You can do the same on some other event (amount of time, loudness, etc.)
Here’s an example based on your code that clears the background every 3 seconds:
import ddf.minim.*; Minim minim; AudioInput in; //3s as millis int interval = 3 * 1000; int time; void setup() { fullScreen(); background(255, 60, 80); time = millis(); minim = new Minim(this); //minim.debugOn (); in = minim.getLineIn(Minim.MONO, 100); } void draw() { float soundlevel; float distance_top = random(100); soundlevel = in.mix.get(0); stroke(255, random(90, 255)); line(0, distance_top + soundlevel * 4000, width, distance_top + soundlevel * 1000); line(0, distance_top + soundlevel * 1000, width, distance_top + soundlevel * 4000); if(millis() - time >= interval){ // clear background background(255, 60, 80); // reset time for next interval time = millis(); // debug println("=========================> tick"); } }
For more info on millis() for a delay see this answer
Another option is to do the calculation frame based using frameCount
.
For example if the sketch’s frameRate
is about 60 fps and you want to clear roughly every 3 seconds you can check if the multiples of 180 (3 * 60) frames passed in tandem with the modulo(%) operator
import ddf.minim.*; Minim minim; AudioInput in; void setup() { //fullScreen(); size(300,300); background(255, 60, 80); minim = new Minim(this); //minim.debugOn (); in = minim.getLineIn(Minim.MONO, 100); } void draw() { float soundlevel; float distance_top = random(100); soundlevel = in.mix.get(0); stroke(255, random(90, 255)); line(0, distance_top + soundlevel * 4000, width, distance_top + soundlevel * 1000); line(0, distance_top + soundlevel * 1000, width, distance_top + soundlevel * 4000); if(frameCount % (3 * 60) == 0){ // clear background background(255, 60, 80); // debug println("=========================> tick"); } }