
let speed = 8;
let d = 50;
let ellipses = [];
let numEllipses = 5;
function setup() {
createCanvas(400, 400);
colorMode(HSB, 360, 100, 100, 5);
for (let j = 0; j < numEllipses; j++) {
ellipses.push({ x: random(width), y: random(height), hue: random(360) });
}
}
function draw() {
for (let e of ellipses) {
e.hue = (e.hue + 0.1) % 360;
e.x = constrain(e.x + random(-speed, speed), 0, width);
e.y = constrain(e.y + random(-speed, speed), 0, height);
noStroke();
fill(e.hue, 100, 100, 0.5);
ellipse(e.x, e.y, d, d);
}
}
speed, which determines the movement speed of the ellipses.d, which represents the diameter of the ellipses.ellipses, an array where we store the attributes of all the ellipses.numEllipses, which defines the number of ellipses we want to draw.Setup
createCanvas(400, 400);
colorMode(HSB, 360, 100, 100, 5);
draw
for (let j = 0; j < numEllipses; j++) { ... }
numEllipses.ellipses.push({ x: random(width), y: random(height), hue: random(360) });
ellipses array. Each ellipse object has three properties:
x: A random x-coordinate for the ellipse, ranging from 0 to the canvas width.y: A random y-coordinate for the ellipse, ranging from 0 to the canvas height.hue: A random hue chosen for the ellipse, ranging from 0 to 360.for (let e of ellipses) { ... }
This is a for...of loop, iterating over each ellipse stored in the ellipses array.
e.hue = (e.hue + 0.1) % 360;
e.x = constrain(e.x + random(-speed, speed), 0, width);
constrain function to ensure the x-coordinate remains within the bounds of the canvas width.e.y = constrain(e.y + random(-speed, speed), 0, height);
constrain function to make sure the y-coordinate remains within the canvas height.noStroke();
fill(e.hue, 100, 100, 0.5);
ellipse(e.x, e.y, d, d);
d.