xxxxxxxxxx
185
let video;
let audio;
let level;
let pixelArray; // Array to hold grayscale values
let numWords = 5000; // Number of random words to generate
let j = 150; // Width of the pixel array
let k = 150; // Height of the pixel array
// Define an array of words to display
let words = [
"Dream",
"Hope",
"Journey",
"Peace",
"Love",
"Harmony",
"Spirit",
"Light",
"Energy",
"Balance",
"Serenity",
"Freedom",
"Wisdom",
"Courage",
"Joy",
"Faith",
"Unity",
"Grace",
"Inspire",
"Create"
];
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(5);
// Create a video capture from the webcam
video = createCapture(VIDEO, { flipped: true });
video.size(j, k); // Set the size of the video
video.hide(); // Hide the default video element
// Create an audio input from the microphone
audio = new p5.AudioIn();
audio.start(); // Start microphone
}
function draw() {
background('#240035'); // Set background color
videoToGray(); // Convert video to grayscale
// Create array of only the first pixels in the 4-element group from video
pixelArray = video.pixels.filter((_, index) => index % 4 === 0);
// Map the audio level to contrast factor
let level = map(audio.getLevel(), 0, 1, 1.25, 30);
// Apply high contrast transformation
pixelArray = applyHighContrast(pixelArray, level);
// Set text properties
fill('#fbf2ff'); // Text color
noStroke();
textAlign(CENTER, CENTER);
textSize(12); // You can adjust the text size as needed
// Generate and draw random words
for (let i = 0; i < numWords; i++) {
// Random positions within the canvas
let x = random(-windowWidth / 2, windowWidth / 2);
let y = random(-windowHeight / 2, windowHeight / 2);
if (shouldDrawWord(x, y)) {
// Select a random word from the array
let word = random(words);
// Optionally, vary the text size based on probability
// let size = map(probability, 0, 1, 8, 24);
// textSize(size);
// Draw the word at the calculated position
text(word, x + windowWidth / 2, y + windowHeight / 2);
}
}
}
// Function to convert video to grayscale
function videoToGray() {
// Load the pixels from the video
video.loadPixels();
// Check if the video has pixels loaded
if (video.pixels.length > 0) {
// Convert to grayscale
for (let i = 0; i < video.pixels.length; i += 4) {
let r = video.pixels[i]; // Red
let g = video.pixels[i + 1]; // Green
let b = video.pixels[i + 2]; // Blue
// Calculate grayscale value
let gray = (r + g + b) / 3;
// Set the pixel color to the grayscale value
video.pixels[i] = gray; // Red
video.pixels[i + 1] = gray; // Green
video.pixels[i + 2] = gray; // Blue
// Alpha channel (video.pixels[i + 3]) remains unchanged
}
// Update the video pixels
video.updatePixels();
}
}
// Function to apply high contrast based on contrast factor
function applyHighContrast(array, contrastFactor) {
// Stretch the grayscale values to increase contrast
let minVal = Math.min(array);
let maxVal = Math.max(array);
// Prevent division by zero if all values are the same
if (minVal === maxVal) {
return array.map(() => 255);
}
// Apply contrast stretching with a scaling factor
return array.map(value => {
// Apply contrast stretching
let stretchedValue = ((value - minVal) * (255 / (maxVal - minVal))) * contrastFactor;
// Clip the value to ensure it stays within bounds
return constrain(Math.round(stretchedValue), 0, 255);
});
}
// Optional: Function to draw the grayscale array as a grid of rectangles
function drawGrayscaleArray() {
for (let y = 0; y < k; y++) {
for (let x = 0; x < j; x++) {
let index = x + y * j;
let grayValue = pixelArray[index];
fill(grayValue);
noStroke();
rect((x / j) * windowWidth, (y / k) * windowHeight, windowWidth / j, windowHeight / k);
}
}
}
// Function to map canvas coordinates to pixel array index
function mapToPixelIndex(x, y) {
// Map the x and y coordinates to the pixel grid
let pixelX = Math.floor((x + windowWidth / 2) * j / windowWidth);
let pixelY = Math.floor((y + windowHeight / 2) * k / windowHeight);
// Ensure the pixel indices are within bounds
pixelX = constrain(pixelX, 0, j - 1);
pixelY = constrain(pixelY, 0, k - 1);
// Convert 2D indices to 1D index
return pixelX + pixelY * j;
}
// Function to determine whether to draw a word at the given coordinates
function shouldDrawWord(x, y) {
let index = mapToPixelIndex(x, y);
// Ensure the index is valid before accessing pixelArray
if (index < 0 || index >= pixelArray.length) {
return false; // Prevent out-of-bounds access
}
let grayscaleValue = pixelArray[index];
// Invert the grayscale value to calculate probability
let probability = 1 - (grayscaleValue / 255); // Darker areas have higher probability
// Decide to draw the word based on probability
return random() < probability; // Return true or false based on random chance
}
// Optional: Adjust canvas size on window resize
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}