xxxxxxxxxx
68
let particles = [];
function setup() {
createCanvas(800, 800);
}
function draw() {
noFill();
background(20);
stroke(255);
circle( width / 2, height / 2,400);
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].update();
particles[i].display();
if (particles[i].isDead()) {
particles.splice(i, 1);
}
}
if (frameCount % 5 == 0) {
// Emit a new particle every 5 frames
const p = new Particle();
particles.push(p);
}
}
// Particle class
class Particle {
constructor() {
// Set the starting position of the particle
this.x = width / 2;
this.y = height/2;
// Set the initial velocity of the particle
this.velocity = createVector(random(-0.1, 0.1), random(-0.1, 0.1));
// Set the life of the particle
this.life = 256;
}
update() {
// Gradually change the velocity of the particle towards the y direction
this.velocity.y = lerp(this.velocity.y, -2, 0.05);
// Update the position of the particle using the velocity vector
this.x += this.velocity.x;
this.y += this.velocity.y;
// Decrease the life of the particle
this.life -= 2;
}
display() {
// Set the color of the particle
fill(255, this.life);
// Draw the particle
ellipse(this.x, this.y, 5);
}
isDead() {
// Return true if the particle's life is less than or equal to 0
return this.life <= 0;
}
}