Help creating simple animation

here is another way of achieving this using an explicit timer and a boolean variable isSmallRect that is set by the timer.

isSmallRect gets then evaluated to decide which rectangle to draw


int timer; 
boolean isSmallRect = true; 

void setup() {
  size(640, 360);
  // start timer
  timer = millis();
}

void draw() {
  background(0); // create black background
  fill(102); //set color to gray

  // timer to control isSmallRect 
  if ( millis() - timer > 3000 ) {
    // start timer  
    timer = millis(); 
    // toggle isSmallRect 
    isSmallRect = 
      ! isSmallRect; // the ! means not, so inverses the value of isSmallRect
  }

  // evaluate isSmallRect
  if (isSmallRect) {
    rect(81, 81, 63, 63); // draw a small rectangle
  } else {
    rect(81, 81, 150, 150); // draw slightly larger rectangle
  }
}
1 Like