xxxxxxxxxx
170
let capture;
let capimg;
let w=600;
let h=400;
let threshold=120;
let rose=[];
let roseNum=50;
let butterflyImg = [];
let butterfly=[];
let butterflyNum=20;
let timelength=0;
function preload(){
butterflyImg[0] = loadImage('butterfly0.png');
butterflyImg[1] = loadImage('butterfly1.png');
butterflyImg[2] = loadImage('butterfly2.png');
}
function setup() {
createCanvas(w,h);
pixelDensity(1);
capture = createCapture(VIDEO);
capture.size(w,h);
capture.hide();
for(var i=0; i<roseNum; i++){
rose.push(new Flower());
}
for(var n=0; n<butterflyNum; n++){
butterfly.push(new Bug());
}
}
function draw() {
background(0,10);
capimg = capture.get();
capimg.loadPixels();
let brightness=0;
let inc = 0;
for(x=0;x<width;x++){
for(y=0;y<height;y++){
let index=4*(y*width+x);
let r=capimg.pixels[index];
let g=capimg.pixels[index+1];
let b=capimg.pixels[index+2];
let a=capimg.pixels[index+3];
brightness += (r+g+b)/3;
inc++;
}
}
brightness = brightness/inc;
push();
noFill();
stroke(color(0,80,0,50));
strokeWeight(3);
for(x=-50;x<width;x+=width/20){
beginShape();
for(y=-80;y<height+80;y+=40){
curveVertex(x+noise(x*10+y)*150,y)
}
endShape();
}
pop();
for(var i=0; i<roseNum; i++){
push();
if(brightness>threshold){
rose[i].move();
rose[i].show();
}else{
rose[i].still();
rose[i].show();
}
pop();
}
for(var n=0; n<butterflyNum; n++){
push();
if(brightness>threshold){
butterfly[n].move();
butterfly[n].show();
}else{
butterfly[n].still();
butterfly[n].show();
}
pop();
}
}
function Bug(){
this.pos = createVector(random(width),random(height))
this.frame = 1;
this.still = function(){
this.frame = 1;
}
this.move = function(){
timelength+=1;
if(timelength>10){
timelength=0;
}
if(timelength==10){
this.frame +=1;
if(this.frame>2){
this.frame = 0;
}
}
}
this.show = function(){
image(butterflyImg[this.frame],this.pos.x,this.pos.y,30,20)
}
}
function Flower(){
this.pos = createVector(random(width),random(height))
this.t = random(10,50);
this.bigNum = round(random(4,6));
this.smallNum = round(random(4,5));
this.move = function(){
this.t+=0.01;
this.bigDist = 0.055*noise(this.t)*width;
this.smallDist = 0.025*noise(this.t)*width;
this.bigSize = 0.016*noise(this.t)*width;
this.smallSize = 0.01*noise(this.t)*width;
this.bigCol = color(noise(this.t+10)*500,noise(this.t+20)*500,noise(this.t+30)*500);
this.smallCol = color(noise(this.t+10)*500,noise(this.t+20)*500,noise(this.t+30)*500);
}
this.still = function(){
this.bigDist = 0.04*noise(this.t)*width;
this.smallDist = 0.025*noise(this.t)*width;
this.bigSize = 0.016*noise(this.t)*width;
this.smallSize = 0.01*noise(this.t)*width;
this.bigCol = color(200);
this.smallCol = color(200);
}
this.show = function(){
translate(this.pos);
rotate(frameCount*0.01);
for(n=0;n<this.bigNum;n++){
rotate(2*PI/this.bigNum);
push();
translate(0,this.bigDist);
noStroke();
fill(this.bigCol);
ellipse(0,0,this.bigSize);
for(i=0;i<this.smallNum;i++){
rotate(2*PI/this.smallNum);
fill(this.smallCol);
ellipse(0,this.smallDist,this.smallSize);
}
pop();
}
}
}