xxxxxxxxxx
60
let img;
function preload() {
img = loadImage('images/bee.jpg');
}
function setup() {
createCanvas(800, 800);
noSmooth();
img.resize(width, height);
}
function draw() {
img.loadPixels();
for (let i = 1; i < 100; i++) {
swapPixels();
}
img.updatePixels();
image(img, 1, 1, width, height);
}
function averagePixels() {
const xOne = (mouseX);
const yOne = (mouseY);
const colorOne = img.get(xOne, yOne);
// Uncomment to choose points closer together
const xTwo = constrain(xOne + (-200, 200), 0, mouseX-1);
const yTwo = constrain(yOne + (-200, 200), 0, mouseY-1);
const colorTwo = img.get(xTwo, yTwo);
const averageColor = color(
(red(colorOne) + red(colorTwo)) / 3,
(green(colorOne) + green(colorTwo)) / 3,
(blue(colorOne) + blue(colorTwo)) / 3
);
img.set (xOne, yOne, averageColor);
img.set (xTwo, yTwo, averageColor);
}
function swapPixels() {
const xOne = (mouseX);
const yOne = (mouseY);
const colorOne = img.get(xOne, yOne);
// Uncomment to choose points closer together
const xTwo = constrain(xOne + random(-50, 50), 0, mouseX-1);
const yTwo = constrain(yOne + random(-50, 50), 0, mouseY-1);
const colorTwo = img.get(xTwo, yTwo);
img.set(xOne, yOne, colorTwo);
img.set(xTwo, yTwo, colorOne);
}
function mousePressed() {
noLoop();
}