我一直试图在我的投资组合网站上实现Particles.js,但没有成功。下面是我在库中运行的代码行,以使其运行:
npm i react-tsparticles我不能把这个包添加到我的网站,所以我尝试把这个添加到一个新的项目。我尝试了两个不同的教程视频添加,但它没有成功。这些是视频
https://www.youtube.com/watch?v=F20SxgG5MlM
https://www.youtube.com/watch?v=NO76xNYkNGk&t
这是我的粒子配置文件
const particlesConfig = {
background: {
color: "#6f32a8"
},
fullScreen: {
enable: true,
zIndex: -1
},
particles: {
number: {
value: 80,
density: {
enable: true,
value_area: 800
}
},
color: {
value: "#ffffff"
},
shape: {
type: "circle",
stroke: {
width: 0,
color: "#000000"
},
polygon: {
nb_sides: 5
},
image: {
src: "img/github.svg",
width: 100,
height: 100
}
},
opacity: {
value: 0.5,
random: false,
anim: {
enable: false,
speed: 1,
opacity_min: 0.1,
sync: false
}
},
size: {
value: 3,
random: true,
anim: {
enable: false,
speed: 40,
size_min: 0.1,
sync: false
}
},
line_linked: {
enable: true,
distance: 150,
color: "#ffffff",
opacity: 0.4,
width: 1
},
move: {
enable: true,
speed: 3,
direction: "none",
random: false,
straight: false,
out_mode: "out",
bounce: false,
attract: {
enable: false,
rotateX: 600,
rotateY: 1200
}
}
},
interactivity: {
detect_on: "canvas",
events: {
onhover: {
enable: false,
mode: "repulse"
},
onclick: {
enable: false,
mode: "push"
},
resize: true
},
modes: {
grab: {
distance: 400,
line_linked: {
opacity: 1
}
},
bubble: {
distance: 400,
size: 40,
duration: 2,
opacity: 8,
speed: 3
},
repulse: {
distance: 200,
duration: 0.4
},
push: {
particles_nb: 4
},
remove: {
particles_nb: 2
}
}
},
retina_detect: true
};
export default particlesConfig;这是我的背景部分
import React from 'react';
import Particles from "react-tsparticles";
import particlesConfig from '../config/particles-config.js';
const particleBackground = () => {
return (
<Particles options={particlesConfig} height="50vh" width='50vw'/>
)
}
export default particleBackground这是我的应用程序组件
import React from "react";
import ParticleBackground from "./components/ParticleBackground";
import "./App.css"
const App = () => {
return (
<div className="App">
<ParticleBackground/>
<div className="particlesheader">
<h1>Particle.JS</h1>
</div>
</div>
);
};
export default App;希望你们能帮我一把!谢谢!
发布于 2022-04-17 15:59:44
我认为最新版本的反应粒子有问题。
这个密码箱反应- this粒子模板。使用版本1.40.2,但最新版本的react but为2.0.6。我在codesandbox中卸载了1.40.2并安装了2.0.6,我意识到问题是关于新版本的。
所以我在我的项目中安装了1.40.2版本,它现在正在工作。
发布于 2022-08-17 17:49:42
我也有过同样的问题。我安装了this npm i tsparticles并添加了以下代码
import Particles from "react-tsparticles"
import { loadFull } from "tsparticles"
import { useCallback } from "react"
const Particle = () => {
const particlesInit = useCallback(async (engine) => {
//console.log(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 loadFull(engine);
}, []);
const particlesLoaded = useCallback(async (container) => {
//console.log(container);
}, []);
return (
<Particles
id="tsparticles"
init={particlesInit}
loaded={particlesLoaded}
style={{
width: "100%",
height: "100%",
position: "absolute",
top: "0",
left: "0"
}}
params={{...}}
/>
);
};
export default Particle而且起作用了。
https://stackoverflow.com/questions/71903252
复制相似问题