xxxxxxxxxx
40
// Define the number of squares, size, border, and initial speed
let numSquares = 25;
let squareSize = 80;
let borderSize = 10;
let speedFactor = 0.05;
// Setup function to create canvas
function setup() {
createCanvas(600, 400);
}
// Draw function to draw squares
function draw() {
background('black');
// Increase speed factor if mouse is pressed, otherwise reset speed factor
if (mouseIsPressed) {
speedFactor = 0.5;
} else {
speedFactor = 0.05;
}
// Loop through each square
for (let i = 0; i < numSquares; i++) {
// Calculate x and y positions based on sine and cosine functions
let x = map(sin(frameCount * speedFactor + i), -1, 1, 0, width - squareSize);
let y = map(cos(frameCount * speedFactor + i), -1, 1, 0, height - squareSize);
// Draw square border
strokeWeight(borderSize); // Set the thickness of the border
if (i % 2 === 0) {
stroke('#FFEEBD'); // Set border color to light yellow for even-indexed squares
} else {
stroke('gold'); // Set border color to yellow for odd-indexed squares
}
noFill();
rect(x, y, squareSize, squareSize); // Draw the square at position (x, y) with size squareSize
}
}