Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Last active December 30, 2016 00:34
Show Gist options
  • Save VictorCoding/f32b6e3903c56aadba7382ac067f0aa9 to your computer and use it in GitHub Desktop.
Save VictorCoding/f32b6e3903c56aadba7382ac067f0aa9 to your computer and use it in GitHub Desktop.
class Particle {
x = Math.random() * canvas.width;
y = Math.random() * canvas.height;
vx = Math.random();
vy = Math.random();
id;
life = 0;
maxLife = 40;
constructor() {
particleIndex++;
particles[particleIndex] = this;
this.id = particleIndex;
}
draw() {
this.x += this.vx;
this.y += this.vy;
this.life++;
if (this.life === this.maxLife) delete particles[this.id];
ctx.fillStyle = 'white';
ctx.fillRect(this.x, this.y, 10, 10);
}
}
loop();
function loop() {
requestAnimationFrame(() => {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const particlesLen = particles.filter((a) => a !== undefined).length;
for (let i = 0; i < particleNum; i++) {
if (particlesLen < 5) {
new Particle();
}
}
particles.forEach(p => p.draw());
loop();
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment