Git Product home page Git Product logo

Comments (6)

sudsarkar13 avatar sudsarkar13 commented on May 8, 2024 1

Here's the improved code

Hope you would like to test it

TypeScript support - object : Formatted using Prettier
import { useEffect, useMemo, useState } from "react";
import Particles, { initParticlesEngine } from "@tsparticles/react";
// import { loadAll } from "@/tsparticles/all"; // if you are going to use `loadAll`, install the "@tsparticles/all" package too.
// import { loadFull } from "tsparticles"; // if you are going to use `loadFull`, install the "tsparticles" package too.
import { loadSlim } from "@tsparticles/slim"; // if you are going to use `loadSlim`, install the "@tsparticles/slim" package too.
// import { loadBasic } from "@tsparticles/basic"; // if you are going to use `loadBasic`, install the "@tsparticles/basic" package too.
import { Container } from "@tsparticles/engine";

const Particle = () => {
	const [init, setInit] = useState(false);

	// this should be run only once per application lifetime
	useEffect(() => {
		initParticlesEngine(async (engine) => {
			// you can initiate the tsParticles instance (engine) 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 loadAll(engine);
			//await loadFull(engine);
			await loadSlim(engine);
			//await loadBasic(engine);
		}).then(() => {
			setInit(true);
		});
	}, []);

	const particlesLoaded = (container?: Container) => {
		return new Promise<void>((resolve) => {
			console.log(container);
			resolve();
		});
	};

	const options = useMemo(
		() => ({
			fpsLimit: 120,
			interactivity: {
				events: {
					onClick: {
						enable: true,
						mode: "push",
					},
					onHover: {
						enable: true,
						mode: "repulse",
					},
				},
				modes: {
					push: {
						quantity: 4,
					},
					repulse: {
						distance: 200,
						duration: 0.4,
					},
				},
			},
			particles: {
				color: {
					value: "#ffffff",
				},
				links: {
					color: "#ffffff",
					distance: 150,
					enable: true,
					opacity: 0.5,
					width: 1,
				},
				move: {
					direction: "none" as const,
					enable: true,
					outModes: {
						default: "bounce" as const,
					},
					random: false,
					speed: 6,
					straight: false,
				},
				number: {
					density: {
						enable: true,
					},
					value: 80,
				},
				opacity: {
					value: 0.5,
				},
				shape: {
					type: "circle",
				},
				size: {
					value: { min: 1, max: 5 },
				},
			},
			detectRetina: true,
		}),
		[]
	);

	if (init) {
		return (
			<Particles
				id='tsparticles'
				particlesLoaded={particlesLoaded}
				options={options}
			/>
		);
	}

	return <></>;
};

export default Particle;

Changelogs

Particle Component Changes

Problem

Type '{ fpsLimit: number; interactivity: { events: { onClick: { enable: boolean; mode: string; }; onHover: { enable: boolean; mode: string; }; }; modes: { push: { quantity: number; }; repulse: { distance: number; duration: number; }; }; }; particles: { ...; }; detectRetina: boolean; }' is not assignable to type 'RecursivePartial'.
The types of 'particles.move.outModes' are incompatible between these types.
Type '{ default: string; }' is not assignable to type '"none" | "bounce" | "split" | IOutModes | OutMode | "bounceHorizontal" | "bounceVertical" | "out" | "destroy" | OutModeAlt | undefined'.
Type '{ default: string; }' is not assignable to type 'IOutModes'.
Types of property 'default' are incompatible.
Type 'string' is not assignable to type '"none" | "bounce" | "split" | OutMode | "bounceHorizontal" | "bounceVertical" | "out" | "destroy" | OutModeAlt'.ts(2322)
IParticlesProps.d.ts(5, 5): The expected type comes from property 'options' which is declared here on type 'IntrinsicAttributes & IParticlesProps'

Solution

Adjust the type of outModes to match the required type in IParticlesProps. Specifically, ensure that the default property of outModes is assigned a value of type "bounce" as const instead of a plain string.

Updated Code

const options = useMemo(
  () => ({
    fpsLimit: 120,
    interactivity: {
      events: {
        onClick: {
          enable: true,
          mode: "push",
        },
        onHover: {
          enable: true,
          mode: "repulse",
        },
      },
      modes: {
        push: {
          quantity: 4,
        },
        repulse: {
          distance: 200,
          duration: 0.4,
        },
      },
    },
    particles: {
      color: {
        value: "#ffffff",
      },
      links: {
        color: "#ffffff",
        distance: 150,
        enable: true,
        opacity: 0.5,
        width: 1,
      },
      move: {
        direction: "none" as const,
        enable: true,
        outModes: {
          default: "bounce" as const,
        },
        random: false,
        speed: 6,
        straight: false,
      },
      number: {
        density: {
          enable: true,
        },
        value: 80,
      },
      opacity: {
        value: 0.5,
      },
      shape: {
        type: "circle",
      },
      size: {
        value: { min: 1, max: 5 },
      },
    },
    detectRetina: true,
  }),
  []
);

from react.

matteobruni avatar matteobruni commented on May 8, 2024

If enums are returning errors, you can use the string values. Exporting non-const enums wastes a lot of code, increasing the bundle size without a valid reason.

Refer to tsparticles/tsparticles#3073 for an old similar discussion.

from react.

sudsarkar13 avatar sudsarkar13 commented on May 8, 2024

Thanks it's working currently now

from react.

sudsarkar13 avatar sudsarkar13 commented on May 8, 2024

If enums are returning errors, you can use the string values. Exporting non-const enums wastes a lot of code, increasing the bundle size without a valid reason.

Refer to tsparticles/tsparticles#3073 for an old similar discussion.

Well I have provide a better solution, Please kindly check it out. and reply as soon as possible.

from react.

matteobruni avatar matteobruni commented on May 8, 2024

No changes will be done on the codebase. The types are good and the default values too.

from react.

sudsarkar13 avatar sudsarkar13 commented on May 8, 2024

Ok thanks, for your reply it means a lot.
Happy Coding!!!

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.