xxxxxxxxxx
var capture; // get a live video camera
function setup() {
createCanvas(windowWidth, windowHeight);
capture = createCapture(VIDEO); // opens up the camera
pixelDensity(1); // turns off oversampling ("retina display")
capture.size(320, 240); // in pixels
capture.hide(); // telling the browser to hide the camera so we can draw it ourselves
}
function draw() {
background(0);
capture.loadPixels(); // uncompress the image to the computer's RAM and store in the pixel array
// process image here:
let howmanypixels = capture.pixels.length
let rowstride = capture.width*4;
let saturation = map(mouseX, 0, width, -5, 5);
// chromatic (pixel-by-pixel only) processing of an image:
for(let i = 0;i<howmanypixels;i+=4)
{
let r = capture.pixels[i+0];
let g = capture.pixels[i+1];
let b = capture.pixels[i+2];
let lum = 0.2126*r + 0.7152*g + 0.0722*b; // luminosity formula
capture.pixels[i+0] = lum + (r-lum)*saturation;
capture.pixels[i+1] = lum + (g-lum)*saturation;
capture.pixels[i+2] = lum + (b-lum)*saturation;
}
capture.updatePixels(); // puts the modified pixel array back and xfers it to the GPU
noSmooth();
image(capture, 0, 0, width, height);
}