In this recitation section, I learned how to use camera and how to use R,G,B of each pixel to create some amazing effect. This is my work.
I use index and index2 to create 2 the “mirror effect”. And in each picture I use
if(int(x/10)/2>int(int(x/10)/2)&&int(y/10)/2>int(int(y/10)/2)){}
to check the position of each pixel to create different colors of the grid overlay effect. The first one overlay R and G, the second one overlay R and B, the third one B and G and the last one R, G and B.
Also, in the “for” loop, I didn’t use x=x++/y=y++, but use x+=2/y+=2 to create a special effect that many small dots form the whole picture. Actually, I originally tried to use a few big circle to form the photo but failed. I didn’t come up with a good solution yet.
Here is the link to my glitch: https://glitch.com/edit/#!/quintessential-almond-ghoul?path=script.js%3A114%3A69
And here is my code:
let img1, img2,img3,img4;
let cam;
let w=640, h=320;
let alpha=0, times=1
//let Check=true
function setup() {
createCanvas(w*2, h*2);
cam = createCapture(VIDEO);
cam.hide();
img1 = createImage(w, h);
img2 = createImage(w, h);
img3 = createImage(w, h);
img4 = createImage(w, h);
}
// function mousePressed(){
// Check=!Check
// m=1-m
// }
function draw() {
background(0);
cam.loadPixels();
img1.loadPixels();
img2.loadPixels();
img3.loadPixels();
img4.loadPixels();
// for(i=0;i<width;i=i+20){
// for(j=0;j<height;j=j+20){
// getCircle(i,j)
// }
// }
// if (Check===true){
for (let y = 0; y < img1.height; y+=1) {
for (let x = 0; x < img1.width; x+=1) {
let index = (x + y * img1.width) * 4;
let index2=(y*img1.width-x)*4;
let r = cam.pixels[index + 0];
let g = cam.pixels[index + 1];
let b = cam.pixels[index + 2];
if(int(x/10)/2>int(int(x/10)/2)&&int(y/10)/2>int(int(y/10)/2)){
img1.pixels[index2 + 0] = r; // R
img1.pixels[index2 + 1] = g; // G
img1.pixels[index2 + 2] = 0; // B
img1.pixels[index2 + 3] = 255; // A
}else if(int(x/10)/2>int(int(x/10)/2)){
img1.pixels[index2 + 0] = r; // R
img1.pixels[index2 + 1] = 0; // G
img1.pixels[index2 + 2] = 0; // B
img1.pixels[index2 + 3] = 255; // A
}else if(int(y/10)/2>int(int(y/10)/2)){
img1.pixels[index2 + 0] = 0; // R
img1.pixels[index2 + 1] = g; // G
img1.pixels[index2 + 2] = 0; // B
img1.pixels[index2 + 3] = 255; // A
}else{
img1.pixels[index2 + 0] = 0; // R
img1.pixels[index2 + 1] = 0; // G
img1.pixels[index2 + 2] = 0; // B
img1.pixels[index2 + 3] = 255; // A
}
}
}
for (let y = 0; y < img2.height; y+=1) {
for (let x = 0; x < img2.width; x+=1) {
let index = (x + y * img2.width) * 4;
let r = cam.pixels[index + 0];
let g = cam.pixels[index + 1];
let b = cam.pixels[index + 2];
if(int(x/10)/2>int(int(x/10)/2)&&int(y/10)/2>int(int(y/10)/2)){
img2.pixels[index + 0] = r; // R
img2.pixels[index + 1] = 0; // G
img2.pixels[index + 2] = b; // B
img2.pixels[index + 3] = 255; // A
}else if(int(x/10)/2>int(int(x/10)/2)){
img2.pixels[index + 0] = r; // R
img2.pixels[index + 1] = 0; // G
img2.pixels[index + 2] = 0; // B
img2.pixels[index + 3] = 255; // A
}else if(int(y/10)/2>int(int(y/10)/2)){
img2.pixels[index + 0] = 0; // R
img2.pixels[index + 1] = 0; // G
img2.pixels[index + 2] = b; // B
img2.pixels[index + 3] = 255; // A
}else{
img2.pixels[index + 0] = 0; // R
img2.pixels[index + 1] = 0; // G
img2.pixels[index + 2] = 0; // B
img2.pixels[index + 3] = 255; // A
}
}
}
for (let y = 0; y < img3.height; y+=1) {
for (let x = 0; x < img3.width; x+=1) {
let index = (x + y * img3.width) * 4;
let index2=(y*img1.width-x)*4;
let r = cam.pixels[index + 0];
let g = cam.pixels[index + 1];
let b = cam.pixels[index + 2];
// fill(r,g,b);
// ellipse(width-x,y,2,2);
if(int(x/10)/2>int(int(x/10)/2)&&int(y/10)/2>int(int(y/10)/2)){
img3.pixels[index2 + 0] = 0; // R
img3.pixels[index2 + 1] = g; // G
img3.pixels[index2 + 2] = b; // B
img3.pixels[index2 + 3] = 255; // A
}else if(int(x/10)/2>int(int(x/10)/2)){
img3.pixels[index2 + 0] = 0; // R
img3.pixels[index2 + 1] = 0; // G
img3.pixels[index2 + 2] = b; // B
img3.pixels[index2 + 3] = 255; // A
}else if(int(y/10)/2>int(int(y/10)/2)){
img3.pixels[index2 + 0] = 0; // R
img3.pixels[index2 + 1] = g; // G
img3.pixels[index2 + 2] = 0; // B
img3.pixels[index2 + 3] = 255; // A
}else{
img1.pixels[index2 + 0] = 0; // R
img1.pixels[index2 + 1] = 0; // G
img1.pixels[index2 + 2] = 0; // B
img1.pixels[index2 + 3] = 255; // A
}
}
}
for (let y = 0; y < img1.height; y+=1) {
for (let x = 0; x < img1.width; x+=1) {
let index = (x + y * img1.width) * 4;
let r = cam.pixels[index + 0];
let g = cam.pixels[index + 1];
let b = cam.pixels[index + 2];
if(int(x/10)/2>int(int(x/10)/2)&&int(y/10)/2>int(int(y/10)/2)){
img4.pixels[index + 0] = r; // R
img4.pixels[index + 1] = g; // G
img4.pixels[index + 2] = b; // B
img4.pixels[index + 3] = 255; // A
}else if(int(x/10)/2>int(int(x/10)/2)){
img4.pixels[index + 0] = r; // R
img4.pixels[index + 1] = 0; // G
img4.pixels[index + 2] = 0; // B
img4.pixels[index + 3] = 255; // A
}else if(int(y/10)/2>int(int(y/10)/2)){
img4.pixels[index + 0] = 0; // R
img4.pixels[index + 1] = g; // G
img4.pixels[index + 2] = 0; // B
img4.pixels[index + 3] = 255; // A
}else{
img4.pixels[index + 0] = 0; // R
img4.pixels[index + 1] = 0; // G
img4.pixels[index + 2] = b; // B
img4.pixels[index + 3] = 255; // A
}
}
}
img1.updatePixels();
image(img1, 0, 0);
img2.updatePixels();
image(img2, w, 0);
img3.updatePixels();
image(img3, 0, h);
img4.updatePixels();
image(img4, w, h);
}