Daily Creative Coding

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

イージングで飛び出る円

/**
* easing test
*
* @author aa_debdeb
* @date 2016/11/20
*/

float time = 0;
float duration = 200; 
ArrayList<Circle> circles;
color c1, c2;

void setup(){
  size(640, 640); 
  mousePressed();
}

void draw(){
  translate(width / 2, height / 2);
  background(255);
  for(Circle c: circles){
    c.display();
  }
  time += 1;
}

void mousePressed(){
  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) * 1.4){
        isOverlapped = true;
        continue;
      }
    }
    if(!isOverlapped){
      circles.add(new Circle(loc, radius));
    }
  }  
  time = 0;
}

class Circle {
  
  PVector loc;
  float radius;
  color c;
  
  Circle(PVector _loc, float _radius){
    loc = _loc;
    radius = _radius;
    c = lerpColor(c1, c2, sq(loc.mag() / 250));
  }
  
  void display(){
    fill(c);
    noStroke();
    float r = time > duration ? radius: easeOutElastic(time, 0, radius, duration);
    ellipse(loc.x, loc.y, r * 2, r * 2);
  }
  
  float easeOutElastic(float t, float b, float c, float d){
    float s = 1.70158;
    float a = c;
    if(t == 0)
      return b;
    t /= d;
    if(t == 1)
      return b + c;
    float p = d * 0.3;
    if(a < abs(c)){
      a = c;
      s = p / 4;
    } else {
      s = p / (2 * PI) * asin(c / a);
    }
    return a * pow(2, -10 * t) * sin((t * d - s) * (2 * PI) / p) + c + b;
  }
}  
f:id:aa_debdeb:20161114214205j:plain