xxxxxxxxxx
59
/*
loadImage()
createImage()
createImg()
createCapture()
image()
*/
// Declare a variable to hold my cat image
let cat;
// Preload function allows us to load data *before* our sketch runs setup()
function preload() {
// Load my cat image
cat = loadImage('cat.jpg');
}
function setup() {
// Make a canvas the size of my cat image
createCanvas(cat.width, cat.height);
noStroke();
}
function draw() {
background(255);
// Load the pixels[] for the scratch image into memory
cat.loadPixels();
// Loop through every 10th pixel of cat image
for(let x = 0; x < cat.width; x+=10) {
for(let y = 0; y < cat.height; y+=10){
// Every pixel in the pixels[] takes up 4 spots
let i = (y*cat.width + x)*4;
// Get the r,g,b values of the pixel at x,y
let r = cat.pixels[i];
let g = cat.pixels[i + 1];
let b = cat.pixels[i + 2];
// Put the rgb values into a color object
let col = color(r,g,b);
// OR use cat.get(x,y)
//let col = cat.get(x,y)
// Use col to fill a rectangle
fill(col);
rect(x,y, 10, 10);
}
}
// Write the updated pixels values back to the image object
cat.updatePixels();
}