xxxxxxxxxx
99
let cols, rows,
current = [],
previous = [],
curImg = [];
prevImg = [];
damping = 0.9;
let img;
function preload(){
img = loadImage('img.jpg');
}
function mouseDragged() {
current[mouseX][mouseY] = 255
}
function setup() {
pixelDensity(1)
createCanvas(400, 400)
cols = width
rows = height
for (let i = 0; i < cols; i++) {
current[i] = []
previous[i] = []
for (let j = 0; j < rows; j++) {
current[i][j] = 0
previous[i][j] = 0
let index = i+ j*cols;
for(let k = index+0; k < index+4; k++){
curImg[k]= img.pixels[index + k];
prevImg[k] = img.pixels[index + k];
}
}
}
previous[100][100] = 255
}
function draw() {
background(0)
// loadPixels();
img.loadPixels();
for (let i = 1; i < cols - 1; i++) {
for (let j = 1; j < rows - 1; j++) {
current[i][j] =
(previous[i - 1][j] + previous[i + 1][j] +
previous[i][j - 1] + previous[i][j + 1] +
previous[i - 1][j - 1] + previous[i - 1][j + 1] +
previous[i + 1][j - 1] + previous[i + 1][j + 1]
) / 4 - current[i][j];
current[i][j] = current[i][j] * damping
let index = (i + j * cols) * 4;
pixels[index + 0] = current[i][j] * 255
pixels[index + 1] = current[i][j] * 255
pixels[index + 2] = current[i][j] * 255
pixels[index + 3] = 255
if(current[i][j] > 0){
let index = i+ j*cols;
for(let k = index+0; k < index+3; k++){
curImg[k] =
(prevImg[k-4] + prevImg[k+4] +
prevImg[k-4*cols] + prevImg[k+4*cols] +
prevImg[k-4*cols - 4] + prevImg[k-4*cols + 4] +
prevImg[k+4*cols-4] + prevImg[k+4*cols+4]
) / 4 - curImg[k];
curImg[k] = curImg[k] * damping
}
curImg[index+3] = 255;
prevImg[index+3] = 255;
img.pixels[index+0] = curImg[index+0];
img.pixels[index+1] = curImg[index+1];
img.pixels[index+2] = curImg[index+2];
img.pixels[index+3] = curImg[index+3];
}
}
}
// updatePixels()
img.updatePixels();
image(img,0,0)
//swap buffers
let temp = previous
previous = current
current = temp
let tempImg = prevImg
prevImg = curImg
curImg = tempImg
}