Using Millis() to scale an animation

I’m really confused as to how I can add millis() to animation I’ve already made. If you run the code I have below, it shows yellow stars on a blue background, moving from side to side. What I want to do is to change it is to have the stars move from side to side, and then scale up 20% after 3 seconds, and then return to normal, and keep looping. Does anyone know how I can achieve this with the code I already have?

int startTime;
int unit = 40;
int count;
Module[] mods;
  

void setup (){
  size (500, 500);
  noStroke();
  
  int wideCount = width / unit;
  int highCount = height / unit;
  count = wideCount * highCount;
  mods = new Module[count];
  
 int index = 0;
  for (int y = 0; y < highCount; y++) {
    for (int x = 0; x < wideCount; x++) {
      mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8), unit);
    }
  }
}

void draw () {
  background (#122e50);
   for (Module mod : mods) {
    mod.update();
    mod.display();
  }
}

class Module {
  int xOffset;
  int yOffset;
  float x, y;
  int unit;
  int xDirection = 1;
  int yDirection = 1;
  float speed;
  float scale;
   Module(int xOffsetTemp, int yOffsetTemp, int xTemp, int yTemp, float speedTemp, int tempUnit) {
    xOffset = xOffsetTemp;
    yOffset = yOffsetTemp;
    x = xTemp;
    y = yTemp;
    speed = speedTemp;
    unit = tempUnit;
    scale=width/20;
  }
  void update() {
    x = x + (speed * xDirection);
    if (x >= unit || x <= 0) {
      xDirection *= -1;
      x = x + (1 * xDirection);
      y = y + (1 * yDirection);
    }
    if (y >= unit || y <= 0) {
      yDirection *= -1;
      y = y + (1 * yDirection);
    }
  }
void display() {
  blendMode(EXCLUSION);
  fill (#FFF6DE);
  star (xOffset + x, yOffset +y, 4, 9, 5);
}
}

void star (float x, float y, float radius1, float radius2, int npoints) {
  float angle = PI / npoints;
  float halfAngle = angle/2.0;

beginShape(); 
for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius2;
    float sy = y + sin(a) * radius2;
    vertex(sx, sy);
    sx = x + cos(a+halfAngle) * radius1;
    sy = y + sin(a+halfAngle) * radius1;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}
void keyPressed() {

if (keyCode == ENTER) {

saveFrame("####.tif"); }

}
1 Like

hi, you already posted a other lengthy code,
want do timed animation, but not used like millis() in it.

so take a step back and concentrate just on a timer.
you need memory and settings variables for a timer,
like start / end / now.
and a check on timer elapsed.

play with this:

int x0=10, y0=10;
// use basic millis timer
long startT, actionT=3000, nowT;


void setup() {
  size(200, 200); 
  startT = millis();
}

void draw() {
  background(0, 200, 200);
  my_timer();
  rect(x0, y0, 30, 30);
}

void my_timer() {
  nowT = millis();
  if ( nowT > startT + actionT ) {
    startT = nowT;
    x0 +=10;
    if ( x0 > 170 ) x0 = 0;
    y0 +=10;
    if ( y0 > 170 ) y0 = 0;
  }
}

1 Like