Comments (6)
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.
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.
Thanks it's working currently now
from react.
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.
No changes will be done on the codebase. The types are good and the default values too.
from react.
Ok thanks, for your reply it means a lot.
Happy Coding!!!
from react.
Related Issues (20)
- 404 for configuration options HOT 2
- Link in readme about particles container not working
- `console.dir` is left in code HOT 2
- Particles not showing. HOT 1
- How do i stop ts particles from taking the whole background and only the background of the div its in HOT 1
- Changing theme crashes page HOT 7
- hexagons preset is not working for me HOT 1
- Convert the component using hooks HOT 7
- Fast route changes causes component render outside #root HOT 1
- How to set it to render within a certain div range instead of the entire screen HOT 1
- tsparticles causing build to fail HOT 1
- How to change the fixed of canvas to absolute? HOT 1
- Update license to MIT same as @tsparticles/engine
- Particles on one component, staying even when I scroll to another component HOT 5
- Vector.js error HOT 1
- Next JS HOT 1
- Flickering Particles on projects build with React and Vite HOT 1
- [Feature Request] Support to React Native HOT 1
- Particles started loading very slowly recently HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.