VincentGarreau / particles.js

A lightweight JavaScript library for creating particles
https://vincentgarreau.com/particles.js/
MIT License
28.98k stars 4.82k forks source link

Particlesjs refresh method (for React) #338

Open NolanCrochot opened 6 years ago

NolanCrochot commented 6 years ago

Hi,

I'm using particle.js as background on a React project with Routes. When I "change page" on the website, this is pratticaly the DOM content which changes, and so the height of the page changes too depending of the elements which are loaded at this moment in the app container.

However, the particles canvas isn't self-adapative. So I needed to create a function (based on https://github.com/VincentGarreau/particles.js/issues/86). Here's the "bad way" I used to fix my problem :

function particlesReload(){
    if(pJSDom[0]){
        cancelRequestAnimFrame(pJSDom[0].pJS.fn.checkAnimFrame);
        cancelRequestAnimFrame(pJSDom[0].pJS.fn.drawAnimFrame);
        pJSDom[0].pJS.tmp.source_svg = undefined;
        pJSDom[0].pJS.tmp.img_obj = undefined;
        pJSDom[0].pJS.tmp.count_svg = 0;
       // Resize the canvas
        pJSDom[0].pJS.canvas.el.width = window.innerWidth; 
        pJSDom[0].pJS.canvas.el.height = window.innerHeight;
        pJSDom[0].pJS.fn.particlesEmpty();
        pJSDom[0].pJS.fn.canvasClear();
        pJSDom[0].pJS.fn.vendors.start();
    }
};

It should be usefull to have a kind of "refresh" method nativally in the lib. If it exists, I didn't find one. Can you please enlightened me on the subject ?

transitive-bullshit commented 6 years ago

react-particle-animation may be easier for you to work with 😄