xxxxxxxxxx
29
//Brownian Motion - Create a sketch in which a small 2d element travels erratically from one moment to the next, leaving a trail across the canvas as it moves. At every timestep, it should update its current position with a small random displacement in both X and Y.
let currentX;
let currentY;
let lastX;
let lastY;
function setup() {
createCanvas(400, 400);
background(255);
lastX = lastY = currentX = currentY = width/2;
}
function draw() {
stroke(random(0, 255), random(0, 255), random(0, 255));
let randomX = random(-400, 400);
let randomY = random(-400, 400);
currentX = randomX;
currentY = randomY;
line(lastX, lastY, currentX, currentY);
lastX = currentX;
lastY = currentY;
}