xxxxxxxxxx
49
let listOfParticles = [];
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.vx = random(-1, 1);
this.vy = random(-1, 1);
this.startedAt = millis();
this.lifetime = 3000;
}
draw() {
let transparency = map(
millis(),
this.startedAt, this.startedAt + this.lifetime, 255, 0
);
fill(255, transparency);
ellipse(this.x, this.y, 10);
}
update() {
this.x = this.x + this.vx;
this.y = this.y + this.vy;
}
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 10; i++) {
particle = new Particle(random(width), random(height));
listOfParticles.push(particle);
}
}
function draw() {
background(220);
if (mouseIsPressed) {
particle = new Particle(mouseX, mouseY);
listOfParticles.push(particle);
}
for (let particle of listOfParticles) {
particle.draw();
particle.update();
}
}