xxxxxxxxxx
/*
Glitchy image
A submission to the glitch art coding challenge.
Author:
Jason Labbe
Site:
jasonlabbe3d.com
*/
var img;
var spacing = 20;
var blocks = [];
var rgbIndex = 0;
var mod = 0;
var mult = 0;
function preload() {
img = loadImage("toffee.jpg");
}
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(10);
noStroke();
img.loadPixels();
for (let y = 0; y < img.height; y+=spacing) {
for (let x = 0; x < img.width; x+=spacing) {
block = img.get(x, y, spacing, spacing);
blocks.push(block);
}
}
}
function draw() {
background(0);
translate(width / 2 - img.width / 2, height / 2 - img.height / 2);
let offset = map(noise(frameCount * 0.1, 2000 + frameCount * 0.1), 0, 1, -1.5, 1.5);
let columnCount = round(img.width / spacing) + offset;
if (frameCount % 5 == 0) {
glitch();
}
for (let i = 0; i < blocks.length; i++) {
let x = int(i % columnCount) * spacing;
let y = int(i / columnCount) * spacing;
image(blocks[i], x, y);
let r = 125;
let g = 125;
let b = 125;
if (rgbIndex == 0) {
r = ((frameCount + i) * mult) % mod;
} else if (rgbIndex == 1) {
g = ((frameCount + i) * mult) % mod;
} else {
b = ((frameCount + i) * mult) % mod;
}
fill(r, g, b, 150);
rect(x, y, spacing, spacing);
}
}
function mousePressed() {
glitch();
}
function glitch() {
frameCount = int(random(1000));
rgbIndex = int(random(3));
mod = int(random(255));
mult = random(100);
}