xxxxxxxxxx
68
let particles = [];
const numParticles = 100;
const expansionLimit = 100; // Set your desired expansion limit
function setup() {
createCanvas(400, 400);
noStroke();
generateParticles(); // Generate the initial set of particles
}
function draw() {
background(0);
for (let particle of particles) {
particle.update();
particle.display();
}
// Remove particles that exceed the expansion limit
particles = particles.filter((particle) => !particle.isDead());
// Check if all particles have died
if (particles.length === 0) {
generateParticles(); // Generate a new set of particles
}
}
function generateParticles() {
for (let i = 0; i < numParticles; i++) {
let particle = new Particle(width / 2, height / 2);
particles.push(particle);
}
}
class Particle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = p5.Vector.random2D().mult(random(0.5, 2));
this.acceleration = createVector(0, 0.01);
this.lifespan = 255;
this.expansionLimit = expansionLimit;
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
// Adjust the expansion rate (decrease the distance moved per frame)
this.position.add(p5.Vector.random2D().mult(0.2));
this.lifespan -= 1;
// Check if the particle has exceeded the expansion limit
if (dist(this.position.x, this.position.y, width / 2, height / 2) >= this.expansionLimit) {
this.lifespan = 0;
}
}
display() {
fill(255, this.lifespan);
ellipse(this.position.x, this.position.y, 10, 10);
}
isDead() {
return this.lifespan <= 0;
}
}