Daily Creative Coding

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

火花

/**
* sparks
*
* @author aa_debdeb
* @date 2016/11/28
*/

float MAX_VEL = 5;
float MAX_SIZE = 8.0;

ArrayList<Particle> particles;

void setup(){
  size(640, 640);
  noCursor();
  noStroke();
  particles = new ArrayList<Particle>();
  background(0);
}

void draw(){
  fill(0, 50);
  rect(0, 0, width, height);
  ArrayList<Particle> nextParticles = new ArrayList<Particle>();
  for(Particle p: particles){
    p.update();
    p.display();
    if(p.size > 0.1){
      nextParticles.add(p);
    }
  }
  particles = nextParticles;
  
  if(mousePressed){
    for(int i = 0; i < random(100); i++){
      particles.add(new Particle());
    }
  } else {
    for(int i = 0; i < random(2); i++){
      particles.add(new Particle());
    } 
  }
}

class Particle {
  
  PVector loc, vel;
  float size;
  float decay;
  
  Particle(){
    loc = new PVector(mouseX, mouseY);
    setVel();
    size = 0.0;  
    for(int i = 0; i < 5; i++){
      size += random(MAX_SIZE);
    }
    size /= 5.0;
    decay = random(0.95, 0.98);
  }
  
  void setVel(){
    float velSize = 0.0;
    for(int i = 0; i < 5; i++){
      velSize += random(MAX_VEL);
    }
    velSize /= 5.0;
    float velAng = random(TWO_PI);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
  }
  
  void display(){
    fill(lerpColor(color(255, 150, 0), color(255), sqrt(size / MAX_SIZE)), 150);
    ellipse(loc.x, loc.y, size, size);
  }
  
  void update(){
    if(random(1) < 0.1){
      setVel();
    }
    loc.add(vel);
    size *= decay;
  }
  
}
f:id:aa_debdeb:20161120184124j:plain