xxxxxxxxxx
// Set the size of each cell in the grid
let cellSize = 30;
// Variables to store the current colors for squares and circles
let squareColor;
let circleColor;
function setup() {
// Create a canvas
createCanvas(600, 600);
// Stroke kalınlığı
strokeWeight(2);
noFill();
background(255);
// Initialize the colors with random values
squareColor = color(random(255), random(255), random(255));
circleColor = color(random(255), random(255), random(255));
// Stop the draw loop (noLoop) since we are creating a static pattern
noLoop();
// Loop through the canvas to draw the pattern in a grid
for (let x = 0; x < width; x += cellSize) {
for (let y = 0; y < height; y += cellSize) {
// Call the drawPattern function for each cell in the grid
drawPattern(x, y, cellSize);
}
}
}
// Function to draw a pattern in a given cell
function drawPattern(x, y, size) {
// Generate a random choice for the pattern (0 to 3)
let choice = floor(random(4));
// Generate a random rotation angle
let rotation = random(TWO_PI);
// Push and pop to isolate transformations to this specific cell
push();
// Translate to the center of the cell
translate(x + size / 2, y + size / 2);
// Apply a random rotation
rotate(rotation);
// Fill shapes with the random color
fill(choice === 3 ? circleColor : squareColor);
// Begin a shape that will represent the chosen pattern
beginShape();
switch (choice) {
case 0:
// Draw a line from the top-left to the bottom-right corner
stroke(choice === 3 ? circleColor : squareColor);
vertex(-size / 2, -size / 2);
vertex(size / 2, size / 2);
break;
case 1:
// Draw a horizontal line
stroke(choice === 3 ? circleColor : squareColor);
vertex(-size / 2, 0);
vertex(size / 2, 0);
break;
case 2:
// Draw a vertical lines
stroke(choice === 3 ? circleColor : squareColor);
vertex(0, -size / 2);
vertex(0, size / 2);
break;
case 3:
// Draw circles in a circular pattern
noStroke();
for (let i = 0; i < 360; i += 10) {
let angle = radians(i);
let xpos = cos(angle) * (size * 0.4);
let ypos = sin(angle) * (size * 0.4);
vertex(xpos, ypos);
}
break;
}
// End the shape
endShape(CLOSE);
// Restore the transformation to its previous state
pop();
}
// Function called when a key is pressed
function keyPressed() {
// If the key is 'a' or 'A', change the current colors and redraw the pattern
if (key === "a" || key === "A") {
squareColor = color(random(255), random(255), random(255));
circleColor = color(random(255), random(255), random(255));
background(255);
// Redraw the pattern with the new colors
for (let x = 0; x < width; x += cellSize) {
for (let y = 0; y < height; y += cellSize) {
drawPattern(x, y, cellSize);
}
}
//save the project when press m/M
} else if (key === "m" || key === "M") {
save("homework8.jpeg");
}
}