xxxxxxxxxx
let particles = []; // main array of 3000 particles
let proximityArray = []; // array to store nearby particles
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i=0; i<3000; i++) particles.push(createVector(random(width), random(height)));
noStroke();
}
function draw() {
background(0);
fill(127);
// draw all particles
for (let p of particles) ellipse(p.x, p.y, 5, 5);
fill(255,0,0);
// draw particles that are near the mouse
for (let p of proximityArray) ellipse(p.x, p.y, 5, 5);
noLoop(); // since the particles are not moving
}
// Only update proximityArray when mouse moves
function mouseMoved() {
proximityArray = particles.filter(p => dist(p.x, p.y, mouseX, mouseY) < 100);
proximityArray.sort((a, b) => dist(a.x, a.y, mouseX, mouseY) - dist(b.x, b.y, mouseX, mouseY));
loop(); //re-render the scene
}