I’m trying to do a polygon mask on an svg with tsParticles.
However neither the particles or the svg show up, only the background color I define in my CSS.
Here is my particles component:
import { useCallback } from "react";
import Particles from "react-tsparticles";
import { loadFull } from "tsparticles";
import { loadPolygonMaskPlugin } from "@tsparticles/plugin-polygon-mask";
import { tsParticles } from "@tsparticles/engine";
import mysvg from "../assets/mysvg.svg";
const options = {
polygon: {
draw: {
enable: true,
stroke: {
color: "#fff",
width: 0.3,
opacity: 0.2,
},
},
move: {
radius: 10,
},
inline: {
arrangement: "equidistant",
},
scale: 0.5,
type: "inline",
url: "mysvg",
},
fpsLimit: 60,
particles: {
number: {
value: 100,
},
color: {
value: "#ff0000",
},
shape: {
type: ["image", "circle"],
image: {
src: "cural",
height: 500,
width: 500,
},
},
opacity: {
value: 0.5,
random: true,
},
size: {
value: 5,
random: true,
},
move: {
enable: true,
speed: 2,
direction: "none",
outModes: {
default: "out",
},
},
},
interactivity: {
events: {
onHover: {
enable: true,
mode: "repulse",
},
onClick: {
enable: true,
mode: "push",
},
},
modes: {
repulse: {
distance: 200,
duration: 0.4,
},
push: {
quantity: 4,
},
},
},
detectRetina: true,
};
const ParticleComponent = () => {
const particlesInit = useCallback(async (engine) => {
console.log("Initializing tsParticles");
await loadFull(engine);
console.log("tsParticles loaded");
await loadPolygonMaskPlugin(engine);
}, []);
> const particlesLoaded = useCallback(async (container) => {
> console.log("Particles loaded", container);
> }, []);
return (
<Particles
id="tsparticles"
init={particlesInit}
loaded={particlesLoaded}
options={options}
/>
);
};
CSS:
.particles-container {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
z-index: 1100;
#tsparticles {
position: relative;
width: 100%;
height: 100%;
background-color: lightblue;
top: 0;
left: 0;
z-index: 1200;
}
Where I render particles:
import ParticleComponent from "./Page2";
const Pricing = () => {
return (
<Section id="section2">
<div className="particles-container">
<div id="tsparticles">
<ParticleComponent />
</div>
</div>
</Section>
);
};
console log show the particles DO load:
Particles loaded beep beep
- Container {id: ‘tsparticles’, engine: Engine, fpsLimit: 120, intersectionManager: ƒ, _nextFrame: ƒ, …}
- actualOptions: Options {_engine: Engine, *container: Container, autoPlay: true, *findDefaultTheme: ƒ, _importPreset: ƒ, …}
- canvas: Canvas {container: Container, *applyPostDrawUpdaters: ƒ, *applyPreDrawUpdaters: ƒ, *applyResizePlugins: ƒ, *getPluginParticleColors: ƒ, …}
- destroyed: false
- drawers: Map(0) {size: 0}
- fpsLimit: 120
- id: "tsparticles"
- interactivity: {mouse: {…}, element: Window, status: ‘pointerleave’}
- lastFrameTime: 9867.8
- pageHidden: true
- particles: Particles {_engine: Engine, *applyDensity: ƒ, *initDensityFactor: ƒ, *pushParticle: ƒ, *removeParticle: ƒ, …}
- pathGenerators: Map(0) {size: 0}
- plugins: Map(0) {size: 0}
- retina: Retina {container: Container, pixelRatio: 1.3499999046325684, reduceFactor: 1, attractDistance: 269.9999809265137, maxSpeed: 67.49999523162842, …}
- smooth: false
- started: true
- zLayers: 100
- _delay: 0
- _delayTimeout: 13
- _duration: 0
- engine**: Engine {configs: Map(0), *domArray: Array(1), eventDispatcher: EventDispatcher, _initialized: true, plugins: Plugins}
- eventListeners: EventListeners {container: Container, *doMouseTouchClick: ƒ, *handleThemeChange: ƒ, *handleVisibilityChange: ƒ, _handleWindowResize: ƒ, …}
- _firstStart: true
- _initialSourceOptions: {background: {…}, fpsLimit: 120, interactivity: {…}, particles: {…}, detectRetina: true}
- _intersectionManager: (entries) => {…}
- _intersectionObserver: IntersectionObserver {root: null, rootMargin: ‘0px 0px 0px 0px’, thresholds: Array(1), delay: 0, trackVisibility: false, …}
- _lifeTime: 8407
- _nextFrame: async (timestamp) => {…}
- options: Options {engine: Engine, *container: Container, autoPlay: true, *findDefaultTheme: ƒ, _importPreset: ƒ, …}
- _paused: false
- _sourceOptions**: {background: {…}, fpsLimit: 120, interactivity: {…}, particles: {…}, detectRetina: true}
- options: (…)
- sourceOptions: (…)
- [[Prototype]]: Object
What am I doing wrong?
Made sure all packages are correctly installed. Tried a simpler page without svg but the result is always the same, I just see the background defined in CSS.
2
Answers
Ok, I managed to fix it by doing two things. I used loadSlim instead of loadFull as I was getting an "interactor.init is not a Function" error. I read up and found it might have to do with the current/newest version.
The other thing I did was to restructure my code like so:
It seems like the issue is with how the SVG URL is specified in your tsParticles configuration. You’ve set the url property in the polygon mask options to "mysvg", which doesn’t link to the actual SVG file. Try to replace "mysvg" with the imported mysvg variable that contains the path to your SVG.