xxxxxxxxxx
53
let particles = [];
function setup() {
createCanvas(600, 400);
}
function draw() {
background(0);
for (let i = 0; i < 3; i++) {
// let p = new Particle();
particles.push(new Particle());
}
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].show();
if (particles[i].finished()) {
// remove this particle
particles.splice(i, 1);
}
}
}
class Particle {
constructor() {
this.x = width/2;
this.y = height/2;
this.vx = random(-5, 5);
this.vy = random(-5, 5);
this.alpha = 255;
}
finished() {
return this.alpha < 0;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.alpha -= 4;
}
show() {
noStroke();
fill(255, this.alpha);
ellipse(this.x, this.y, 10);
}
}