xxxxxxxxxx
91
let myStarColors = ["#8b97d5", "#FFEB00", "#BECD08", "#FF48B1"];
let pickedStarColors;
let video;
let time = 0; // Variable to control the animation
function setup() {
pickedStarColors = someStarColors(3, myStarColors);
createCanvas(windowWidth, windowHeight);
background('#21285F');
// Create a video capture object
video = createCapture(VIDEO);
video.size(width, height);
video.hide(); // Hide the default video element
}
function draw() {
background('#21285F');
// Get brightness of the webcam
video.loadPixels();
let brightness = 0;
for (let y = 0; y < video.height; y++) {
for (let x = 0; x < video.width; x++) {
let index = (x + y * video.width) * 4;
brightness += (video.pixels[index] + video.pixels[index + 1] + video.pixels[index + 2]) / 3;
}
}
brightness /= (video.width * video.height);
// Map brightness to rotation angle
let rotation = map(brightness, 0, 255, 0, TWO_PI);
// Map brightness to star scale
let starScale = map(brightness, 0, 255, 10, 100);
// Animate the size of the stars using a sine wave
let animatedScale = 0.5 * (sin(time) + 1);
let animatedStarScale = lerp(10, 100, animatedScale);
let space = 50;
for (let x = 0; x < width + 50; x += space) {
for (let y = 0; y < height + 50; y += space) {
drawStar(x, y, 5, 12, 5, rotation);
drawStar(x + space / 2, y + space / 2, 4, animatedStarScale * 0.4, animatedStarScale, rotation);
}
}
time += 0.05; // Increment the time variable for the next frame
}
function drawStar(x, y, radius1, radius2, npoints, rotation) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
push();
translate(x, y);
rotate(rotation);
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = cos(a) * radius2;
let sy = sin(a) * radius2;
vertex(sx, sy);
sx = cos(a + halfAngle) * radius1;
sy = sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
noStroke();
fill(pick(pickedStarColors));
endShape(CLOSE);
pop();
}
// Randomization functions
function randomInteger(min, max) {
return Math.floor(min + (max - min) * Math.random());
}
function pick(inputArray) {
return inputArray[randomInteger(0, inputArray.length)];
}
function someStarColors(n, possibleStarColors) {
let pickedStarColors = [];
for (let i = 0; i < n; i++) {
pickedStarColors.push(pick(possibleStarColors));
}
return pickedStarColors;
}