xxxxxxxxxx
101
class Module{
constructor(position,size,c,angle){
this.position = position;
this.velocity = new p5.Vector();
this.acceleration = new p5.Vector();
this.isStatic = true;
this.size = size;
this.color = c;
this.angle = angle;
}
update(){
// mouseX, mouseY
let mouse = new p5.Vector(mouseX,mouseY);
let d = this.position.dist(mouse);
if(mouseIsPressed && d < 30){
this.isStatic = false; // 고정해제
}
if(!this.isStatic && mouseIsPressed){
// p5.Vector.sub( v1, v2 )
let force = p5.Vector.sub( mouse, this.position );
// map 값의 범위를 변화
// contrain 특정 범위 안으로 제한
let power = constrain(map(d,100,0,0.2,1),0.2,1);
force.setMag(power);
this.acceleration = force;
}
this.velocity.add(this.acceleration);
this.velocity.limit(5);
this.position.add(this.velocity);
if(this.position.y > height) this.position.y = height;
}
display(){
rectMode(CENTER);
push();
translate(this.position.x,this.position.y);
fill(this.color);
rotate( radians(this.angle) );
rect(0,0,this.size.x,this.size.y);
pop();
}
}
let gravity = new p5.Vector(0,0.1);
let palette = ["#00ffff","#ff00ff","#ffff00"];
let m = new Module(
new p5.Vector(200,200),
new p5.Vector(10,10)
);
let modules = [];
function setup() {
createCanvas(400, 400);
background("white");
textSize(200);
textAlign(CENTER,CENTER);
textStyle(BOLD);
text("text",width/2,height/2);
let gap = 10;
let x = 0;
let y = 0;
for(let y=0; y<height; y=y+gap){
for(let x=0; x<width; x=x+gap){
let c = get(x,y);
let b = brightness(c);
if(b == 0){
modules.push(
new Module(
new p5.Vector(x,y),
new p5.Vector(gap*0.6,gap),
random(palette),
random(0,360)
)
);
}
}
}
}
function draw(){
background(220);
for(let i=0; i<modules.length; i=i+1){
modules[i].update();
modules[i].display();
}
}