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

Daily Creative Coding

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

誘引する流れ

/*
 * attracting flow
 *
 * @author aadebdeb
 * @date 2017/01/27
 */

var movers;

function setup() {
  createCanvas(windowWidth, windowHeight); 
  movers = [];
  for (var i = 0; i < 2000; i++) {
    movers.push(new Mover());
  }
  background(255);
  stroke(0, 3);
}

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.update = function(){
    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 * 0.02, this.pos.y * 0.02), 0, 1, -1, 1);
    var y = map(noise(this.pos.x * 0.02 + 10000, this.pos.y * 0.02 + 10000), 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:20170123221137j:plain