読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

マウスクリックに応じて現れたり消えたりする円

/**
* fade in / fade out
*
* @author aa_debdeb
* @date 2016/11/21
*/

boolean canShowCircles = true;
boolean isAnimating = false;
int duration = 100;
int animatingTime = 0;
ArrayList<Circle> circles;
color c1, c2;

void setup(){
  size(640, 640);
  circles = new ArrayList<Circle>();
}

void draw(){
  translate(width / 2, height / 2);
  background(0);
  for(Circle c: circles){
    c.display();
  }
  if(isAnimating){
    animatingTime++;
    if(animatingTime == duration){
      isAnimating = false;
      canShowCircles = !canShowCircles;
    }
  }
}

void mousePressed(){
  if(!isAnimating){
    if(canShowCircles){
      makeCircles();
      isAnimating = true;
      animatingTime = 0;
    } else {
      hideCircles();
      isAnimating = true;
      animatingTime = 0;
    }
  }
}

void makeCircles(){
  c1 = color(random(255), random(255), random(255));
  c2 = color(random(255), random(255), random(255));
  circles = new ArrayList<Circle>();
  for(int i = 0; i < 10000; i++){
    float locSize = map(sqrt(random(1)), 0, 1, 0, 250);
    float locAng = random(TWO_PI);
    PVector loc = new PVector(locSize * cos(locAng), locSize * sin(locAng));
    float radius = random(2, 30);
    boolean isOverlapped = false;
    for(Circle other: circles){
      if(PVector.dist(loc, other.loc) < radius + other.radius){
        isOverlapped = true;
        continue;
      }
    }
    if(!isOverlapped){
      circles.add(new Circle(loc, radius));
    }
  }    
}

void hideCircles(){

}

class Circle {
  
  PVector loc;
  float radius;
  color c;
  
  Circle(PVector _loc, float _radius){
    loc = _loc;
    radius = _radius;
    c = lerpColor(c1, c2, noise(loc.x * 0.01, loc.y * 0.01));
  }
  
  void display(){
    fill(c);
    noStroke();
    float r;
    if(isAnimating && canShowCircles){
      r = map(pow(float(animatingTime) / duration, 1.0 / 4), 0, 1, 0, radius);
    } else if(isAnimating && !canShowCircles){
      r = map(1.0 - pow(float(animatingTime) / duration, 1.0 / 4), 0, 1, 0, radius);    
    } else if(!canShowCircles){
      r = radius;
    } else {
      r = 0;
    }
    ellipse(loc.x, loc.y, r * 2, r * 2);
  }
}
f:id:aa_debdeb:20161114215024j:plain