Processing 入门教程(三十七)图层动画

int frames = 20;
int framesToSave = 0;
// 设置当前有20个图层
PGraphics pg[] = new PGraphics[frames];

void setup() {
  size(500, 500);
  prepareGraphics();
}
void draw() {
  int currFrame = frameCount % frames; // 0 .. 19
  if (mousePressed) {
    pg[currFrame].beginDraw();
    pg[currFrame].line(mouseX, mouseY, pmouseX, pmouseY);
    pg[currFrame].endDraw();
  }
  // 显示当前图层
  image(pg[currFrame], 0, 0);
  if (framesToSave > 0) {
    // 保存 gif 文件
    saveFrame("loop####.gif");
    framesToSave--;
  }
}
void keyPressed() {
  if (key == 's') {
    framesToSave = frames;
  }
  if (key == ' ') {
    prepareGraphics();
  }
}
// 按空格键重置画板
void prepareGraphics() {
  // 初始化创建20个图层
  for (int i=0; i

效果图如下:

Processing 入门教程(三十七)图层动画_第1张图片

你可能感兴趣的:(Processing,图层)