如何在“处理”中使用计数器或倒数计时器清除/重置画布?

时间:2019-06-12 09:14:40

标签: java processing counter clear redraw

我想知道如何设置重置循环: 在draw()进行时,我想清除画布,以便可以一次又一次地在干净的画布上进行绘画。

(我不想使用keyPressed()或mousePressed()它必须是自动的)

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;
  }
}

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以再次简单地清除背景,例如当您按下一个键时:

void keyPressed(){
  background(255, 60, 80);
}

您可以在其他事件(时间量,响度等)上执行相同的操作

下面是一个基于您的代码的示例,该示例每3秒清除一次背景:

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");
  }

}

有关millis()的更多信息,请参见this answer

另一个选择是使用frameCount进行计算框架。 例如,如果草图的frameRate约为60 fps,并且您想每3秒钟清除一次,则可以检查180(3 * 60)帧的倍数是否与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");
  }

}