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

30 min. Processing

毎日30分、Processingで何かを作る

ランダムに揺れる画面とサウンド

p5.js サウンド
/*
 * noisy screen & sound
 *
 * @author aadebdeb
 * @date 2017/02/01
 */

var rects;
var rgb;

var osc, lpf;

function setup() {
  createCanvas(windowWidth, windowHeight);
  rectMode(CENTER);
  rects = [];
  for (var i = 0; i < 50; i++) {
    rects.push(new Rect());
  }
  rgb = [color(160, 160, 0), color(160, 0, 160), color(0, 160, 160)];
  
  osc = new p5.Oscillator('sawtooth');
  osc.freq(600);
  osc.amp(0.5);
  osc.disconnect();
  lpf= new p5.LowPass();
  osc.connect(lpf);
  osc.start();
  
  background(0);
}

function draw() {
  blendMode(BLEND);
  rectMode(CORNER);
  fill(0, 170);
  rect(0, 0, width, height);
  
  blendMode(ADD);
  rectMode(CENTER);
  var noiseVal = pow(random(1), 20); 
  var varianceSize = noiseVal * 100;
  var variances = [];
  for (var i = 0; i < 3; i++) {
    var variance = createVector(random(-1, 1), random(-1, 1));
    variance.normalize();
    variance.mult(varianceSize);
    variances.push(variance);
  }
  for (var i = 0; i < rects.length; i++) {
    rects[i].render(variances);
  }
  
  lpf.freq(map(noiseVal, 0, 1, 50, 400));
}

function Rect(){
  this.pos = createVector(random(width), random(height));
  this.size = randomGaussian(100, 20);

  this.render = function(variances) {
    noFill();
    strokeWeight(1);
    for (var i = 0; i < 3; i++) {
      stroke(rgb[i]);
      rect(this.pos.x + variances[i].x, this.pos.y + variances[i].y, this.size, this.size);
    }
  } 
}
f:id:aa_debdeb:20170129105405j:plain