Example 1: Walker Class Array
Code
let walkers = [];
class Walker {
constructor() {
this.pos = createVector(random(width), random(height));
this.vel = p5.Vector.random2D().mult(random(0.4, 2));
this.r = random(4, 10);
}
update() {
this.pos.add(this.vel);
if (this.pos.x < 0 || this.pos.x > width) this.vel.x *= -1;
if (this.pos.y < 0 || this.pos.y > height) this.vel.y *= -1;
}
draw() {
circle(this.pos.x, this.pos.y, this.r * 2);
}
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 80; i++) walkers.push(new Walker());
noStroke();
}
function draw() {
background(249);
fill(255, 55, 145, 170);
for (let w of walkers) {
w.update();
w.draw();
}
}Try this: Increase the array size to 200 and test performance.