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

Daily Creative Coding

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

光の流れ

/*
 * light flow
 *
 * @author aadebdeb
 * @date 2017/01/28
 */

var movers;
var colors;
var noiseScales, noiseOffsets;


function setup() {
  colors = [color(4, 1, 1), color(1, 2, 3), color(2, 3, 1)];
  
  createCanvas(windowWidth, windowHeight); 
  blendMode(ADD);
  movers = [];
  for (var i = 0; i < 2000; i++) {
    movers.push(new Mover());
  }
  noiseScales = [];
  noiseOffsets = [];
  for (var i = 0; i < colors.length; i++) {
    noiseScales.push(createVector(random(0.01, 0.03), random(0.01, 0.03)));
    noiseOffsets.push([createVector(random(10000), random(10000)), createVector(random(10000), random(10000))]);
  }
  background(0);
  stroke(0, 0, 10);
}

function draw() {
  var nmovers = [];
  for (var i = 0; i < movers.length; i++) {
    var mover = movers[i];
    mover.update();
    if((mover.pos.x < -100 || mover.pos.x > width + 100 ||
       mover.pos.y < -100 || mover.pos.y > height + 100) ||
       random(1) < 0.001) {
         nmovers.push(new Mover());
    } else {
      nmovers.push(mover);
    }
  }
  movers = nmovers;
}

function Mover() {
  this.pos = createVector(random(width), random(height));
  this.vel = createVector(0, 0);
  this.status = int(random(colors.length));
  
  this.update = function(){
    stroke(colors[this.status]);
    var npos = p5.Vector.add(this.pos, this.vel);
    line(this.pos.x, this.pos.y, npos.x, npos.y);
    this.pos = npos;
  
    var x = map(noise(this.pos.x * noiseScales[this.status].x + noiseOffsets[this.status][0].x,
      this.pos.y * noiseScales[this.status].y + noiseOffsets[this.status][0].y), 0, 1, -1, 1);
    var y = map(noise(this.pos.x * noiseScales[this.status].x + noiseOffsets[this.status][1].x,
      this.pos.y * noiseScales[this.status].y + noiseOffsets[this.status][1].y), 0, 1, -1, 1);
    var dvel = createVector(x, y);
    dvel.normalize();
    dvel.mult(0.1);
    this.vel.add(dvel);
    this.vel.limit(2);
  }
}
f:id:aa_debdeb:20170124084113j:plain