我怎样才能 clear/reset canvas 与处理中的计数器或倒计时?

How can I clear/reset the canvas with a counter or countdown in Processing?

我想知道如何设置重置循环: 当 draw() 进行时,我想清除 canvas 以便我可以开始一次又一次地绘制干净的 canvas。

(我不想使用 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;
  }
}

感谢您的帮助指教!

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

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[=18 一起传递=]

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

}