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

Daily Creative Coding

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

瞬く三角形の光

p5.js ランダム
/**
 * fragile triangles
 * 
 * @author aadebdeb
 * @date 2017/02/18
 */

var particles;
var fillColors;
var nOffsetX, nOffsetY, nOffsetSize;
var nScale = 0.001;

function setup() {
  createCanvas(windowWidth, windowHeight);
  nOffsetX = createVector(random(10000), random(10000));
  nOffsetY = createVector(random(10000), random(10000));
  nOffsetSize = createVector(random(10000), random(10000));
  frameRate(20);
  particles = [];
  for (var i = 0; i < 1500; i++) {
    particles.push(new Particle());
  }
}

function draw() {
  blendMode(BLEND);
  background(0);
  blendMode(ADD);
  var nextParticles = [];
  for (var i = 0; i < particles.length; i++) {
    var particle = particles[i];
    particle.render();
    if (random(1) < 0.8) {
      particle.update();
      nextParticles.push(particle);
    } else {
      nextParticles.push(new Particle());
    }
  }
  particles = nextParticles;
}

function Particle() {
  this.loc = createVector(random(width), random(height));
  var velSize = random(5);
  var velAng = random(TWO_PI);
  this.vel = createVector(velSize * cos(velAng), velSize * sin(velAng));
  this.vertices = [];
  this.fillColor = color(random(0, 200), 255, 0);
}


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 accX = map(noise(this.loc.x * nScale + nOffsetX.x, this.loc.y * nScale + nOffsetX.y, frameCount * 0.01), 0, 1, -1, 1);
    var accY = map(noise(this.loc.x * nScale + nOffsetY.x, this.loc.y * nScale + nOffsetY.y, frameCount * 0.01), 0, 1, -1, 1);
    var accSize = map(noise(this.loc.x * nScale + nOffsetSize.x, this.loc.y * nScale + nOffsetSize.y, frameCount * 0.01), 0, 1, 0, 0.5);
    var acc = createVector(accX, accY).normalize().mult(accSize);
    acc.mult(randomGaussian(1, 0.1));
    acc.rotate(randomGaussian(0, PI / 12));
    this.vel.add(acc);
    this.vel.limit(5);
    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:20170215080326j:plain