PixelAddict & IronWisp
Hey, I was thinking—what if we take one of your travel shots and turn it into a glitchy interactive art piece that reacts to the light? You up for a little coding experiment?
Absolutely, let's dive into that! Grab a snap of that sunset over the canyon and I’ll mess it up into a glitchy, light‑reactive canvas. Bring the code, I’ll bring the chaos. Let's make the pixels dance!
Here’s a quick p5.js sketch you can drop into a new file and open in your browser. It loads the image, splits it into a grid of blocks, and makes each block shift its hue based on the light that falls on it—so the pixels really do dance when the sun moves. Remember to put the sunset image in the same folder and name it “canyon.jpg” (or change the filename in the loadImage line). Enjoy the glitchy light show!
function preload() {
canyon = loadImage('canyon.jpg')
}
function setup() {
createCanvas(800, 600)
pixelDensity(1)
canyon.resize(width, height)
canyon.loadPixels()
// divide the image into a grid
gridSize = 20
}
function draw() {
background(0)
// update and display each block
for (let y = 0; y < height; y += gridSize) {
for (let x = 0; x < width; x += gridSize) {
// sample the color from the original image
let idx = 4 * ((y * width) + x)
let r = canyon.pixels[idx]
let g = canyon.pixels[idx + 1]
let b = canyon.pixels[idx + 2]
// convert to hue-saturation-lightness
let hsl = rgbToHsl(r, g, b)
// get the lightness of the block based on the pixel under the mouse
let mouseIdx = 4 * ((mouseY * width) + mouseX)
let mouseL = hsl[2] // keep the block’s lightness close to the mouse lightness
// apply a slight hue shift based on distance to center
let distCenter = dist(x, y, width/2, height/2)
let shift = map(distCenter, 0, dist(width/2, height/2, 0, 0), 0, 180)
hsl[0] = (hsl[0] + shift) % 360
// convert back to RGB
let col = hslToRgb(hsl[0], hsl[1], hsl[2])
fill(col[0], col[1], col[2])
noStroke()
rect(x, y, gridSize, gridSize)
}
}
}
function rgbToHsl(r, g, b) {
r /= 255; g /= 255; b /= 255
let max = maxVal(r, g, b), min = minVal(r, g, b)
let h, s, l = (max + min) / 2
if (max == min) {
h = s = 0 // achromatic
} else {
let d = max - min
s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break
case g: h = (b - r) / d + 2; break
case b: h = (r - g) / d + 4; break
}
h /= 6
}
return [h * 360, s, l]
}
function hslToRgb(h, s, l) {
h /= 360
let r, g, b
if (s == 0) {
r = g = b = l // achromatic
} else {
function hue2rgb(p, q, t) {
if (t < 0) t += 1
if (t > 1) t -= 1
if (t < 1/6) return p + (q - p) * 6 * t
if (t < 1/2) return q
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6
return p
}
let q = l < 0.5 ? l * (1 + s) : l + s - l * s
let p = 2 * l - q
r = hue2rgb(p, q, h + 1/3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1/3)
}
return [r * 255, g * 255, b * 255]
}
function maxVal(a, b, c) { return max(a, max(b, c)) }
function minVal(a, b, c) { return min(a, min(b, c)) }
That’s wild, man—exactly the kind of glitch‑y vibe I’m craving. Load up the sketch, fire it up, and let the light chase the pixels. Just gotta keep the mouse steady or we’ll end up with a psychedelic sunburst! Let's see those hues dance.
Glad it hits the spot! I’ve got the sketch ready—just drop it in a folder with the canyon photo and hit run. If the hues start dancing too wildly, try pinning the mouse or adding a small delay to the shift. Let me know how the light‑reactive canvas turns out!