Search code examples
particles.js

Particles.js : limit number of particles


I can't find if it is possible to limit the total number of particles.

Is there any way of doing this?

Particles.js Github


Solution

  • You can modify particles.js (row 750) adding an additional check in the push function:

    /* ---------- pJS functions - modes events ------------ */
    
      pJS.fn.modes.pushParticles = function(nb, pos){
    
        pJS.tmp.pushing = true;
    
        if(pJS.particles.array.length<140){
            for(var i = 0; i < nb; i++){
              pJS.particles.array.push(
                new pJS.fn.particle(
                  pJS.particles.color,
                  pJS.particles.opacity.value,
                  {
                    'x': pos ? pos.pos_x : Math.random() * pJS.canvas.w,
                    'y': pos ? pos.pos_y : Math.random() * pJS.canvas.h
                  }
                )
              )
              if(i == nb-1){
                if(!pJS.particles.move.enable){
                  pJS.fn.particlesDraw();
                }
                pJS.tmp.pushing = false;
              }
            }
        }
    
      };
    

    I used 140 as maximum number since it is a good value in order to not lose performance. Obviously you can modify it as needed.