DEV Community

Rahul Kumar
Rahul Kumar

Posted on

Implementing React tsparticles in website

React-tsparticles is a powerful library that allows you to add customizable particle animations to your React applications. In this guide, we'll walk through the process of implementing react-tsparticles in your project.
Installation
First, you need to install the necessary packages. Open your terminal and run the following command:

npm install tsparticles @tsparticles/react
Enter fullscreen mode Exit fullscreen mode

This will install both the core tsparticles library and the React wrapper.
Creating the Particles Component
Create a new file in your components directory, for example, Particle.js. This file will contain the configuration for your particle system.
Here's the code for the Particle component:

import { useCallback, useEffect, useState } from "react";
import Particles, { initParticlesEngine } from "@tsparticles/react";
import { loadFull } from "tsparticles";

export default function Particle() {
  const [init, setInit] = useState(false);

  useEffect(() => {
    console.log("init");
    initParticlesEngine(async (engine) => {
      await loadFull(engine);
    }).then(() => {
      setInit(true);
    });
  }, []);

  const particlesLoaded = (container) => {
    // You can add any logic here that should run when particles are loaded
  };

  return (
    <>
      {init && (
        <Particles
          id="tsparticles"
          particlesLoaded={particlesLoaded}
          style={{
            zIndex: 1,
          }}
          options={{
            fpsLimit: 120,
            interactivity: {
              events: {
                onClick: {
                  enable: true,
                  mode: "push",
                },
                onHover: {
                  enable: true,
                  mode: "repulse",
                },
                resize: true,
              },
              modes: {
                push: {
                  quantity: 4,
                },
                repulse: {
                  distance: 200,
                  duration: 0.4,
                },
              },
            },
            particles: {
              color: {
                value: "#bae6fd",
              },
              links: {
                color: "#e0f2fe",
                distance: 150,
                enable: true,
                opacity: 0.5,
                width: 1,
              },
              move: {
                direction: "none",
                enable: true,
                outModes: {
                  default: "bounce",
                },
                random: false,
                speed: 1.2,
                straight: false,
              },
              number: {
                density: {
                  enable: true,
                  area: 800,
                },
                value: 160,
              },
              opacity: {
                value: 0.5,
              },
              shape: {
                type: "circle",
              },
              size: {
                value: { min: 1, max: 5 },
              },
            },
            detectRetina: true,
          }}
        />
      )}
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Let's break down the key parts of this component:

Initialization: The useEffect hook initializes the particles engine when the component mounts.
Rendering: The Particles component is only rendered after initialization (init state is true).
Configuration: The options prop of the Particles component contains all the configuration for the particle system. This includes interactivity settings, particle appearance, movement, and more.
_
Using the Particle Component_
To use this component in your React application, simply import and render it where you want the particles to appear. For example, in your App.js:

import React from 'react';
import Particle from './components/Particle';

function App() {
  return (
    <div className="App">
      <Particle />
      {/* Your other components */}
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Customization
The options object in the Particle component is where you can customize the behavior and appearance of your particles. Here are some key areas you can modify:

  • Color: Change the color.value in the particles object to set a different particle color.
  • Shape: Modify shape.type to use different particle shapes (e.g., "square", "triangle").
  • Number: Adjust number.value to increase or decrease the number of particles.
  • Movement: Change settings in the move object to alter how particles move.
  • Interactivity: Modify the interactivity object to change how particles react to user input.

Performance Considerations
While particles can create engaging visual effects, they can also be resource-intensive. Consider the following tips:

Limit the number of particles for better performance on lower-end devices.
Use the fpsLimit option to cap the frame rate.
Test on various devices to ensure smooth performance.
_
Conclusion_
React-tsparticles offers a flexible way to add dynamic, interactive backgrounds to your React applications. By following this guide, you should now have a working implementation of tsparticles in your project. Experiment with different configurations to create the perfect particle effect for your application!
Remember, the key to mastering react-tsparticles is experimentation. Don't be afraid to play around with different settings to achieve unique and captivating effects.

Top comments (0)