xxxxxxxxxx
87
var canvas = null;
var modules = [];
var SIZE = 60;
var RECT_W = SIZE * .4;
var RECT_H = SIZE * .6;
var RECT_SCALE = 1.4;
var DARK_THEME = false;
function Module(indexX,indexY) {
this.width = 20;
this.angle = 0;
this.scale = 1;
this.center = createVector(indexX*SIZE+SIZE/2, indexY*SIZE+SIZE/2);
this.stiffness = .1;
this.damping = .75;
this.velocity = 0;
this.display = function() {
// stroke(200);
// strokeWeight(.5);
// rect( this.center.x-SIZE/2, this.center.y-SIZE/2, SIZE, SIZE);
this.update();
push();
translate(this.center.x, this.center.y);
scale(this.scale,this.scale);
rotate(this.angle);
rect(-this.width/2, -RECT_H/2, this.width, RECT_H);
pop();
}
this.update = function() {
var mousePosition = createVector(mouseX,mouseY);
var v = p5.Vector.sub(mousePosition,this.center);
var targetAngle = v.heading();
if(targetAngle<0) {
targetAngle += TWO_PI;
}
if( Math.abs(targetAngle-this.angle)>.8*TWO_PI ) {
this.angle = this.angle<PI ? TWO_PI : 0;
}
var force = (targetAngle - this.angle) * this.stiffness;
this.velocity = this.damping * (this.velocity + force);
this.angle += this.velocity;
var dist = p5.Vector.dist(mousePosition,this.center);
this.scale = map(dist, 0, windowWidth, RECT_SCALE, 1);
this.width = Math.ceil( map(dist, 0, windowWidth, RECT_W, 1) );
}
}
function setup() {
canvas = createCanvas(windowWidth, windowHeight);
background(DARK_THEME ? 0: 255);
fill(DARK_THEME ? 255: 10);
strokeWeight(0);
var totalX = Math.floor( windowWidth / SIZE );
var totalY = Math.floor( windowHeight / SIZE );
for(var i=0; i<totalX; i++) {
for(var j=0; j<totalY; j++) {
var m = new Module(i, j);
modules.push(m);
}
}
}
function keyPressed() {
var k = key.toLowerCase();
switch(k) {
case 's':
saveCanvas(canvas, 'canvas.jpg');
break;
}
}
function draw() {
var bg = DARK_THEME ? 0: 255;
background(bg);
for(var i=0; i<modules.length; i++) {
var m = modules[i];
m.display();
}
}