Hello Guys,
In this article, we will learn about how to use Particles in react.
Particle.js is a fantastic JavaScript framework for creating 2d and 3d particle effects on your website.
However, because using Particle.js is difficult, a new version of Particle.js has been built for component-based frameworks such as React, Vue, and Angular, which is called TsParticles and has a dedicated package for easy integration in React called react-tsparticles.
In React.js, react-tsparticles is a fantastic library for producing particles.
How to use react-tsparticles
in React.js:
You must first install react-tsparticles
as well as rtsparticles
, as react-tsparticles
is dependent on it.
npm i react-tsparticles npm i tsparticles
Use —force if any legacy errors appear.
npm i react-tsparticles --force npm i tsparticles --force
Import Particles and loadFull from react-tsparticles and tsparticles, respectively.
import "./styles.css"; import Particles from "react-tsparticles"; import { loadFull } from "tsparticles"; export default function App() { return ( <div className="App"> <h1>Hello Coders!</h1> </div> ); }
We can now utilise the Particles component by supplying some props like id, init (which will be an initialization method), options (which will be the particle configurations we want to display), or url (which will use options from a remote URL with a JSON URL).
import "./styles.css"; import Particles from "react-tsparticles"; import { loadFull } from "tsparticles"; export default function App() { return ( <div className="App"> <h1>Hello Coders!</h1> <Particles id="particles-here" init={anInitFunction} options={ // an config object } /> </div> ); }
The aforesaid method’s working code is shown below.
import "./styles.css"; import Particles from "react-tsparticles"; import { loadFull } from "tsparticles"; export default function App() { const particlesInit = async (main) => { console.log(main); // you can initialize the tsParticles instance (main) here, adding custom shapes or presets // this loads the tsparticles package bundle, it's the easiest method for getting everything ready // starting from v2 you can add only the features you need reducing the bundle size await loadFull(main); }; return ( <div className="App"> <h1>Hello Coders!</h1> <Particles id="tsparticles" init={particlesInit} options={{ "fullScreen": { "enable": true, "zIndex": 1 }, "particles": { "number": { "value": 10, "density": { "enable": false, "value_area": 800 } }, "color": { "value": "#fff" }, "shape": { "type": "star", "options": { "sides": 5 } }, "opacity": { "value": 0.8, "random": false, "anim": { "enable": false, "speed": 1, "opacity_min": 0.1, "sync": false } }, "size": { "value": 4, "random": false, "anim": { "enable": false, "speed": 40, "size_min": 0.1, "sync": false } }, "rotate": { "value": 0, "random": true, "direction": "clockwise", "animation": { "enable": true, "speed": 5, "sync": false } }, "line_linked": { "enable": true, "distance": 600, "color": "#ffffff", "opacity": 0.4, "width": 2 }, "move": { "enable": true, "speed": 2, "direction": "none", "random": false, "straight": false, "out_mode": "out", "attract": { "enable": false, "rotateX": 600, "rotateY": 1200 } } }, "interactivity": { "events": { "onhover": { "enable": true, "mode": ["grab"] }, "onclick": { "enable": false, "mode": "bubble" }, "resize": true }, "modes": { "grab": { "distance": 400, "line_linked": { "opacity": 1 } }, "bubble": { "distance": 400, "size": 40, "duration": 2, "opacity": 8, "speed": 3 }, "repulse": { "distance": 200 }, "push": { "particles_nb": 4 }, "remove": { "particles_nb": 2 } } }, "retina_detect": true, "background": { "color": "#111", "image": "", "position": "50% 50%", "repeat": "no-repeat", "size": "cover" } }} /> </div> ); }
Output: