xxxxxxxxxx
var xp, yp;
var preX, preY;
var stepSize;
var speed;
var radius;
var colors = [];
var clId;
function setup() {
createCanvas(800, 800);
background(50);
xp = width / 2; // set initial x position
yp = height / 2; // set initial y position
preX = xp;
preY = yp;
stepSize = 20;
speed = 1;
radius = 50;
// Define a color palette
colors = ['#FF6F61', '#6B5B95', '#88B04B', '#F7CAC9', '#92A8D1'];
clId = 0;
}
function draw() {
// Save previous position
preX = xp;
preY = yp;
//Cyclic movement
var dx = random(-radius, radius);
var dy = random(-radius, radius);
xp += dx + random(-stepSize, stepSize);
yp += dy + random(-stepSize, stepSize);
// Check the agent position
// if it goes out of the canvas
// set its initial position to the middle point again
if (xp < 0 || xp > width || yp < 0 || yp > height) {
xp = width / 2; // set initial x position
yp = height / 2; // set initial y position
preX = xp;
preY = yp;
// Choose random color from the colors array
clId = floor(random(colors.length));
// Adjust radius randomly
radius = random(20, 100);
}
stroke(colors[clId]);
strokeWeight(2);
line(preX, preY, xp, yp);
}