xxxxxxxxxx
107
let cam;
let filterMode=4;
let vScale = 16;
var particles = [];
var slider;
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1);
cam = createCapture(VIDEO);
//cam.hide();
cam.size(width/vScale, height/vScale);
for (var i = 0; i < 200; i++) {
particles[i] = new Particle(random(width), random(height));
}
slider = createSlider(0, 255, 127);
//background(51);
}
function draw() {
background(220);
image(cam, 0, 0);
loadPixels();
for (let i = 0; i < width; i += 1) {
for (let j = 0; j < height; j += 1) {
let colorFromVideo = cam.get(i, j);
let index=4*(j*width+i);
pixelFilter(index,i,j);
}
}
updatePixels();
tint(200,80);
image(cam,0,0,width,height);
for(var i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].show();
}
}
function pixelFilter(index,i,j){
if(filterMode===1){
pixels[index]=mouseX/8;
pixels[index+1]=i/10;
pixels[index+2]=i/10+j/10;
pixels[index+3]=255;
}
if(filterMode===2){
pixels[index]=mouseY/4;
pixels[index+1]=j/20;
pixels[index+2]=i/5+j/20;
pixels[index+3]=255;
}
if(filterMode===3){
pixels[index]=i/10+j/10;
pixels[index+1]=i/10;
pixels[index+2]=mouseX/8;
pixels[index+3]=255;
}
if(filterMode===4){
pixels[index]=i/20+j/20;
pixels[index+1]=j/10;
pixels[index+2]=mouseX/8;
pixels[index+3]=255;
}
}
function mouseClicked(){
let filterModes=['1','2','3','4'];
var filterMode=random(filterModes);
console.log(filterMode);
}
function Particle(x, y) {
this.x = x;
this.y = y;
this.r = random(4, 32);
this.update = function() {
this.x += random(-10, 10);
this.y += random(-10, 10);
this.x = constrain(this.x, 0, width);
this.y = constrain(this.y, 0, height);
}
this.show = function() {
noStroke();
var px = floor(this.x / vScale);
var py = floor(this.y / vScale);
var col = cam.get(px, py);
//console.log(col);
fill(col[0], col[1], col[2], slider.value());
ellipse(this.x, this.y, this.r, this.r);
}
}