xxxxxxxxxx
50
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
if (this.x > width || this.x < 0) {
this.vx *= -1
}
}
}
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220)
if (mouseIsPressed) {
particle = new Particle(mouseX, mouseY)
listOfParticles.push(particle)
}
for (let particle of listOfParticles) {
particle.draw()
particle.update()
}
}