xxxxxxxxxx
79
// basic camera demo code
// arts 444
// grosser, apr 2019
let cam;
let xScale;
let yScale;
let zScale;
let z=0.001;
let a = 0;
function setup() {
createCanvas(600,450);
// setup the camera
cam = createCapture(VIDEO);
cam.size(40,20);
cam.hide();
angleMode (DEGREES);
// setup some scale variables we'll use
// for sizing rects later
xScale = width/cam.width;
yScale = height/cam.height;
zScale = 40;
}
function draw() {
background(20);
// tell p5 we're going to use the pixel []
cam.loadPixels();
// go through all the camera pixels in this frame
// first by row (y)
for(let y=4;y<cam.height;y++) {
// then by column (x)
for(let x=0;x<cam.width;x++) {
// get the r,g,b of each pixel
let i = (x + y * cam.width) * 4;
let r = cam.pixels[i+0];
let g = cam.pixels[i+1];
let b = cam.pixels[i+2];
// use that color to draw a rect to the screen
noStroke();
fill(r,g,random(b),200);
push ();
translate ((cam.width-x-1)*xScale, y*yScale);
rotate (a);
triangle(1,0,z*zScale, xScale-yScale,yScale-xScale*3,zScale*5);
a=a+0.001;
pop();
}
}
for(let y=4;y<cam.height;y++) {
// then by column (x)
for(let x=0;x<cam.width;x++) {
// get the r,g,b of each pixel
let i = (x + y * cam.width) * 4;
let r = cam.pixels[i+0.5];
let g = cam.pixels[i+0];
let b = cam.pixels[i+1];
frameRate (15);
fill (r,g,b,100);
ellipse (x*xScale*1.5,random(y*75),xScale*1.5, yScale);
} // end x for()
} // end y for()
// tell p5 we're done messing with pixel []
cam.updatePixels();
}