p5((s) => {
let capture;
const sortFrequency = 300;
s.setup = function () {
s.createCanvas(width, height / 2);
capture = s.createCapture(s.VIDEO);
capture.hide();
};
s.draw = function () {
if (s.frameCount % sortFrequency === 0) {
capture.loadPixels();
let pixels = capture.pixels;
let pixelArray = [];
for (let i = 0; i < pixels.length; i ++) {
let r = pixels[i];
let g = pixels[i + 1];
let b = pixels[i + 2];
let a = pixels[i + 3];
pixelArray.push({ r, g, b, a });
}
pixelArray.sort((a, b) => {
return a.r - b.r;
});
s.loadPixels();
for (let i = 0; i < pixelArray.length; i++) {
let index = i * 4;
s.pixels[index] = pixelArray[i].r;
s.pixels[index + 1] = pixelArray[i].g;
s.pixels[index + 2] = pixelArray[i].b;
s.pixels[index + 3] = pixelArray[i].a;
}
s.updatePixels();
}
};
})