Daily Creative Coding

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

三角形の鳥群

/**
 * triangle birds
 * 
 * @author aadebdeb
 * @date 2017/02/17
 */

var particles;
var fillColors;

function setup() {
  createCanvas(windowWidth, windowHeight);
  fillColors = [
    color(113, 199, 209),
    color(212, 211, 202),
    color(230, 26, 105)
  ];
  frameRate(20);
  particles = [];
  for (var i = 0; i < 400; i++) {
    particles.push(new Particle());
  }
}

function draw() {
  blendMode(BLEND);
  background(0);
  blendMode(SCREEN);
  for (var i = 0; i < particles.length; i++) {
    var particle = particles[i];
    particle.render();
    particle.update();
  } 
}

function Particle() {
  this.loc = createVector(random(width), random(height));
  var velSize = random(15);
  var velAng = random(TWO_PI);
  this.vel = createVector(velSize * cos(velAng), velSize * sin(velAng));
  this.vertices = [];
  this.fillColor = fillColors[int(random(fillColors.length))];
}


Particle.prototype = {
  render: function() {
    noStroke();
    fill(this.fillColor);
    if (this.vertices.length < 3) {
      return; 
    }
    beginShape();
    for (var i = 0; i < 3; i++) {
      var v = this.vertices[i];
      vertex(v.x, v.y);
    }
    endShape(CLOSE);
  },
  
  update: function() {
    var mouse = createVector(mouseX, mouseY);
    var acc = p5.Vector.sub(mouse, this.loc).limit(1);
    acc.mult(randomGaussian(1, 1));
    acc.rotate(randomGaussian(0, PI / 3));
    this.vel.add(acc);
    this.vel.limit(15);
    this.loc.add(this.vel);
    this.vertices.push(p5.Vector.add(this.loc, this.vel.copy().rotate(random(TWO_PI))));
    if (this.vertices.length > 3) {
      this.vertices.shift();
    }
  }
}
f:id:aa_debdeb:20170214222313j:plain