xxxxxxxxxx
140
let video;
let cols = 10;
let rows = 8;
let frameRateSlider, colSlider, rowSlider;
let globalFrameBuffer = [];
let bufferSize = 30;
let borderColor;
let transitionStartTime = 0; // Time when the current transition started
let borderAlpha = 255; // Initial alpha value for the border color (fully opaque)
let transitioning = false; // Flag to indicate if we are currently in a transition
let transitionDuration = 3500; // Duration of the transition in milliseconds
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
frameRateSlider = createSlider(1, 60, 30, 1);
frameRateSlider.position(10, height + 25);
colSlider = createSlider(1, 70, cols, 1);
colSlider.position(10, height + 50);
rowSlider = createSlider(1, 70, rows, 1);
rowSlider.position(10, height + 75);
calculateCellDimensions();
video.hide();
}
function draw() {
background(0);
frameRate(frameRateSlider.value());
// Update cols and rows from sliders
cols = colSlider.value();
rows = rowSlider.value();
calculateCellDimensions();
// Maintain the global frame buffer
if (globalFrameBuffer.length >= bufferSize) {
globalFrameBuffer.shift();
}
globalFrameBuffer.push(video.get());
// Draw each cell based on the global frame buffer
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
drawCell(i, j);
}
}
updateBorderColor();
drawCellBorders(); // Apply the current border visibility setting
drawSettingsInfo();
}
function calculateCellDimensions() {
cellWidth = width / cols;
cellHeight = height / rows;
}
function drawCell(col, row) {
let x = col * cellWidth;
let y = row * cellHeight;
let frameIndex = floor(random(globalFrameBuffer.length));
let frame = globalFrameBuffer[frameIndex].get(); // Make a copy to avoid altering the original in the buffer
// Convert the frame to grayscale
convertFrameToGrayscale(frame); // Apply grayscale conversion
image(frame, x, y, cellWidth, cellHeight, x, y, cellWidth, cellHeight);
}
function updateBorderColor() {
let currentTime = millis();
// Check if we should start a new transition
if (!transitioning && currentTime - transitionStartTime > 5000 + 7000) { // Wait for 10s opaque, 7s transparent, start new cycle
transitioning = true;
transitionStartTime = currentTime;
}
if (transitioning) {
let elapsed = currentTime - transitionStartTime;
if (elapsed <= transitionDuration) { // Fade to transparent
borderAlpha = map(elapsed, 0, transitionDuration, 255, 0);
} else if (elapsed <= transitionDuration + 7000) { // Hold transparent
borderAlpha = 0;
} else if (elapsed <= 2 * transitionDuration + 7000) { // Fade to opaque
borderAlpha = map(elapsed, transitionDuration + 7000, 2 * transitionDuration + 7000, 0, 255);
} else {
transitioning = false;
borderAlpha = 255;
transitionStartTime = currentTime; // Reset for the next cycle
}
}
borderColor = color(0, 0, 0, borderAlpha);
}
function drawCellBorders() {
stroke(borderColor);
strokeWeight(0.9); // Set the stroke weight for visibility
noFill();
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
rect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
}
}
}
function convertFrameToGrayscale(img) {
img.loadPixels();
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
let index = (x + y * img.width) * 4;
let r = img.pixels[index];
let g = img.pixels[index + 1];
let b = img.pixels[index + 2];
// Average of RGB or a better formula for perceived luminance
let gray = 0.299 * r + 0.587 * g + 0.114 * b;
img.pixels[index] = gray;
img.pixels[index + 1] = gray;
img.pixels[index + 2] = gray;
}
}
img.updatePixels();
}
function drawSettingsInfo() {
fill(255);
noStroke();
text(`Frame Rate: ${frameRateSlider.value()} FPS`, 20, height + 40);
text(`Columns: ${cols}`, 20, height + 65);
text(`Rows: ${rows}`, 20, height + 90);
}