xxxxxxxxxx
86
function setup() {
createCanvas(400, 400);
initializeSketch(); // To set initial values and conditions
}
function draw() {
background(220);
updateSketch(); // Handles frame-by-frame updates
}
// Function to initialize the sketch
function initializeSketch() {
initializeColors(); // Sets up color schemes
initializeShapes(); // Define shapes to be drawn
}
// Function to update the sketch each frame
function updateSketch() {
drawBackgroundPattern(); // Background pattern logic
drawMovingShapes(); // Main dynamic content
}
// Function to initialize colors
function initializeColors() {
fillColor = color(255, 150, 150);
strokeColor = color(50, 50, 150);
}
// Function to initialize shapes
function initializeShapes() {
// Initial position of the circle and its velocity
circleX = width / 2;
circleY = height / 2;
// Speed for continuous straight line movement
circleSpeedX = 3;
circleSpeedY = 2;
}
// Background pattern
function drawBackgroundPattern() {
for (let x = 0; x <= width; x += 40) {
for (let y = 0; y <= height; y += 40) {
drawGridCircle(x, y); // Function for grid circles
}
}
}
// Circles in the grid
function drawGridCircle(x, y) {
noFill();
stroke(150, 200, 255, 100); // Light blue color with some transparency
ellipse(x, y, 30, 30);
}
// Function to draw moving shapes
function drawMovingShapes() {
moveCircle(); // Handles circle movement
drawCircle(circleX, circleY); // Draws circle with updated position
}
// Draws a circle
function drawCircle(x, y) {
fill(fillColor);
stroke(strokeColor);
ellipse(x, y, 50, 50);
}
// Function to move the circle in straight lines
function moveCircle() {
// Update position based on speed
circleX += circleSpeedX;
circleY += circleSpeedY;
// Reverse direction if the circle hits the edge of the canvas
if (circleX > width || circleX < 0) {
circleSpeedX *= -1;
}
if (circleY > height || circleY < 0) {
circleSpeedY *= -1;
}
}