xxxxxxxxxx
47
let x, y;
let stepSize = 10;
function setup() {
createCanvas(800, 600);
background(255);
// Starting position
x = width / 2;
y = height / 2;
// Draw the initial point
stroke(0);
strokeWeight(2);
point(x, y);
// Loop to move 20 times in random directions
for (let i = 0; i < 20; i++) {
// Choose a random direction
let direction = floor(random(4));
// Update position based on the direction
if (direction === 0) {
y -= stepSize; // Up
} else if (direction === 1) {
x += stepSize; // Right
} else if (direction === 2) {
y += stepSize; // Down
} else if (direction === 3) {
x -= stepSize; // Left
}
// Ensure the position stays within the canvas
x = constrain(x, 0, width);
y = constrain(y, 0, height);
// Draw the point and a line from the previous position
stroke(random(255), random(255), random(255)); // Change color
line(pmouseX, pmouseY, x, y); // Draw line
point(x, y); // Draw the current position
}
}
function draw() {
// No need to draw anything in the draw loop
}