Is it possible to animate a transition between two images in p5?
This should be so simple but I can’t find any examples on how to do it.
I looked into using createGraphics and an offscreen buffer but apparently it doesn’t have the pixel array property and blend() is static.
So I thought that by finding the difference in the rgb values of each pixel from each image and then using that difference to transition from one frame to the next might work… but I’m stuck.
So far this is what I’ve tried, for some reason when I console.log r it results undefined.
Also unsure about the logic. Any help would be super appreciated, thanks
let img0, img1;
function preload() {
img0 = loadImage('data/image0.jpeg');
img1 = loadImage('data/image1.jpeg');
}
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1);
image(img0, 0, 0, 256, 256);
image(img1, 300, 0, 256, 256);
img0.loadPixels;
img1.loadPixels;
}
function draw() {
for (var x = 0; x < img0.width; x++) {
for (var y = 0; y < img0.height; y++) {
var index = (x + y * img0.width) * 4;
var r = img0.pixels[index + 0];
var g = img0.pixels[index + 1];
var b = img0.pixels[index + 2];
var a = img0.pixels[index + 3];
var r2 = img1.pixels[index + 0];
var g2 = img1.pixels[index + 1];
var b2 = img1.pixels[index + 2];
var a2 = img1.pixels[index + 3];
var dR = r2 - r;
var dG = g2 - g;
var dB = b2 - b;
for (var i = 0.0; i < dR; i++){
r = lerp(r, r2, i);
}
for (var i = 0.0; i < dG; i++){
g = lerp(g, g2, i);
}
for (var i = 0.0; i < dB; i++){
b = lerp(b, b2, i);
}
}
}
img0.updatePixels();
image(img0, 0, 0, 256, 256);
}