获取 Demo
源代码请在公众号回复“react粒子动画” 。

项目简介
tsparticles-demo 是一个基于 React 和 tsparticles 的可视化粒子特效演示项目,旨在为开发者和设计师提供丰富、炫酷的粒子动画效果参考。项目支持多种粒子特效切换,界面美观,交互友好,适合用于网页背景、登录页、展示页等多种场景。
无依赖项(*),浏览器直接可用,且兼容 React.js、Vue.js(2.x 和 3.x)、Angular、Svelte、jQuery、Preact、Inferno、Riot.js、Solid.js 以及 Web Components
主要功能
- 支持多种粒子特效(如默认、闪烁、彩色气泡、雪花、五彩三角、彩色星星等)一键切换
- 粒子动画支持鼠标悬停、点击等交互效果
- 响应式设计,适配多种屏幕尺寸
- 代码结构清晰,易于二次开发和集成
技术栈
- React 18
- tsparticles & @tsparticles/react
- Tailwind CSS(用于样式美化)
- Vite(前端构建工具)
- 其他依赖:react-router-dom、react-hook-form 等
亮点特色
- 高可定制性:所有粒子特效参数均可通过 JSON 配置,便于扩展和自定义
- 丰富的交互体验:支持鼠标悬停、点击等多种交互模式,提升用户参与感
- 多主题切换:内置多种粒子主题,满足不同场景需求
- 性能优异:基于 Vite 构建,开发体验流畅,页面加载速度快
适用场景
- 企业/产品官网背景动画
- 登录/注册页面视觉增强
- 活动/节日专题页氛围营造
- 个人作品集、博客等展示类网站
依赖安装
npm install @tsparticles/engine @tsparticles/react tsparticles
示例
基础示例
import React, { useEffect, useState } from "react";
import Particles, { initParticlesEngine } from "@tsparticles/react";
import { loadFull } from "tsparticles";
function App() {
const [init, setInit] = useState(false);
useEffect(() => {
if (init) {
return;
}
initParticlesEngine(async (engine) => {
await loadFull(engine);
}).then(() => {
setInit(true);
});
}, []);
return (
<div className="App">
{init && (
<Particles
options={{
background: {
color: "#282c34",
},
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: "#ffffff",
},
links: {
color: "#ffffff",
distance: 150,
enable: true,
opacity: 0.5,
width: 1,
},
collisions: {
enable: true,
},
move: {
direction: "none",
enable: true,
outModes: {
default: "bounce",
},
random: false,
speed: 6,
straight: false,
},
number: {
density: {
enable: true,
},
value: 80,
},
opacity: {
value: 0.5,
},
shape: {
type: "circle",
},
size: {
random: true,
value: 5,
},
},
}}
/>
)}
</div>
);
}
export default App;
闪烁效果
{
"autoPlay": true,
"background": {
"color": {
"value": "#0d47a1"
},
"image": "",
"position": "",
"repeat": "",
"size": "",
"opacity": 1
},
"backgroundMask": {
"composite": "destination-out",
"cover": {
"opacity": 1,
"color": {
"value": ""
}
},
"enable": false
},
"clear": true,
"defaultThemes": {},
"delay": 0,
"fullScreen": {
"enable": true,
"zIndex": 0
},
"detectRetina": true,
"duration": 0,
"fpsLimit": 120,
"interactivity": {
"detectsOn": "window",
"events": {
"onClick": {
"enable": true,
"mode": "push"
},
"onDiv": {
"selectors": {},
"enable": false,
"mode": {},
"type": "circle"
},
"onHover": {
"enable": true,
"mode": "repulse",
"parallax": {
"enable": false,
"force": 2,
"smooth": 10
}
},
"resize": {
"delay": 0.5,
"enable": true
}
},
"modes": {
"trail": {
"delay": 1,
"pauseOnStop": false,
"quantity": 1
},
"attract": {
"distance": 200,
"duration": 0.4,
"easing": "ease-out-quad",
"factor": 1,
"maxSpeed": 50,
"speed": 1
},
"bounce": {
"distance": 200
},
"bubble": {
"distance": 400,
"duration": 2,
"mix": false,
"opacity": 0.8,
"size": 40,
"divs": {
"distance": 200,
"duration": 0.4,
"mix": false,
"selectors": {}
}
},
"connect": {
"distance": 80,
"links": {
"opacity": 0.5
},
"radius": 60
},
"grab": {
"distance": 400,
"links": {
"blink": false,
"consent": false,
"opacity": 1
}
},
"push": {
"default": true,
"groups": [],
"quantity": 4
},
"remove": {
"quantity": 2
},
"repulse": {
"distance": 200,
"duration": 0.4,
"factor": 100,
"speed": 1,
"maxSpeed": 50,
"easing": "ease-out-quad",
"divs": {
"distance": 200,
"duration": 0.4,
"factor": 100,
"speed": 1,
"maxSpeed": 50,
"easing": "ease-out-quad",
"selectors": {}
}
},
"slow": {
"factor": 3,
"radius": 200
},
"particle": {
"replaceCursor": false,
"pauseOnStop": false,
"stopDelay": 0
},
"light": {
"area": {
"gradient": {
"start": {
"value": "#ffffff"
},
"stop": {
"value": "#000000"
}
},
"radius": 1000
},
"shadow": {
"color": {
"value": "#000000"
},
"length": 2000
}
}
}
},
"manualParticles": [],
"particles": {
"bounce": {
"horizontal": {
"value": 1
},
"vertical": {
"value": 1
}
},
"collisions": {
"absorb": {
"speed": 2
},
"bounce": {
"horizontal": {
"value": 1
},
"vertical": {
"value": 1
}
},
"enable": false,
"maxSpeed": 50,
"mode": "bounce",
"overlap": {
"enable": true,
"retries": 0
}
},
"color": {
"value": "#ff0000",
"animation": {
"h": {
"count": 0,
"enable": false,
"speed": 1,
"decay": 0,
"delay": 0,
"sync": true,
"offset": 0
},
"s": {
"count": 0,
"enable": false,
"speed": 1,
"decay": 0,
"delay": 0,
"sync": true,
"offset": 0
},
"l": {
"count": 0,
"enable": false,
"speed": 1,
"decay": 0,
"delay": 0,
"sync": true,
"offset": 0
}
}
},
"effect": {
"close": true,
"fill": true,
"options": {},
"type": {}
},
"groups": [],
"move": {
"angle": {
"offset": 0,
"value": 90
},
"attract": {
"distance": 200,
"enable": false,
"rotate": {
"x": 3000,
"y": 3000
}
},
"center": {
"x": 50,
"y": 50,
"mode": "percent",
"radius": 0
},
"decay": 0,
"distance": {},
"direction": "none",
"drift": 0,
"enable": true,
"gravity": {
"acceleration": 9.81,
"enable": false,
"inverse": false,
"maxSpeed": 50
},
"path": {
"clamp": true,
"delay": {
"value": 0
},
"enable": false,
"options": {}
},
"outModes": {
"default": "out",
"bottom": "out",
"left": "out",
"right": "out",
"top": "out"
},
"random": false,
"size": false,
"speed": 2,
"spin": {
"acceleration": 0,
"enable": false
},
"straight": false,
"trail": {
"enable": false,
"length": 10,
"fill": {}
},
"vibrate": false,
"warp": false
},
"number": {
"density": {
"enable": true,
"width": 1920,
"height": 1080
},
"limit": {
"mode": "delete",
"value": 0
},
"value": 80
},
"opacity": {
"value": {
"min": 0.1,
"max": 0.5
},
"animation": {
"count": 0,
"enable": true,
"speed": 3,
"decay": 0,
"delay": 0,
"sync": false,
"mode": "auto",
"startValue": "random",
"destroy": "none"
}
},
"reduceDuplicates": false,
"shadow": {
"blur": 0,
"color": {
"value": "#000"
},
"enable": false,
"offset": {
"x": 0,
"y": 0
}
},
"shape": {
"close": true,
"fill": true,
"options": {},
"type": "circle"
},
"size": {
"value": {
"min": 0.1,
"max": 5
},
"animation": {
"count": 0,
"enable": true,
"speed": 20,
"decay": 0,
"delay": 0,
"sync": false,
"mode": "auto",
"startValue": "random",
"destroy": "none"
}
},
"stroke": {
"width": 0
},
"zIndex": {
"value": 0,
"opacityRate": 1,
"sizeRate": 1,
"velocityRate": 1
},
"destroy": {
"bounds": {},
"mode": "none",
"split": {
"count": 1,
"factor": {
"value": 3
},
"rate": {
"value": {
"min": 4,
"max": 9
}
},
"sizeOffset": true,
"particles": {}
}
},
"roll": {
"darken": {
"enable": false,
"value": 0
},
"enable": false,
"enlighten": {
"enable": false,
"value": 0
},
"mode": "vertical",
"speed": 25
},
"tilt": {
"value": 0,
"animation": {
"enable": false,
"speed": 0,
"decay": 0,
"sync": false
},
"direction": "clockwise",
"enable": false
},
"twinkle": {
"lines": {
"enable": true,
"frequency": 0.005,
"opacity": 1,
"color": {
"value": "#ff0000"
}
},
"particles": {
"enable": true,
"frequency": 0.05,
"opacity": 1,
"color": {
"value": "#ffff00"
}
}
},
"wobble": {
"distance": 5,
"enable": false,
"speed": {
"angle": 50,
"move": 10
}
},
"life": {
"count": 0,
"delay": {
"value": 0,
"sync": false
},
"duration": {
"value": 0,
"sync": false
}
},
"rotate": {
"value": 0,
"animation": {
"enable": false,
"speed": 0,
"decay": 0,
"sync": false
},
"direction": "clockwise",
"path": false
},
"orbit": {
"animation": {
"count": 0,
"enable": false,
"speed": 1,
"decay": 0,
"delay": 0,
"sync": false
},
"enable": false,
"opacity": 1,
"rotation": {
"value": 45
},
"width": 1
},
"links": {
"blink": false,
"color": {
"value": "#ffffff"
},
"consent": false,
"distance": 150,
"enable": true,
"frequency": 1,
"opacity": 0.4,
"shadow": {
"blur": 5,
"color": {
"value": "#000"
},
"enable": false
},
"triangles": {
"enable": false,
"frequency": 1
},
"width": 1,
"warp": false
},
"repulse": {
"value": 0,
"enabled": false,
"distance": 1,
"duration": 1,
"factor": 1,
"speed": 1
}
},
"pauseOnBlur": true,
"pauseOnOutsideViewport": true,
"responsive": [],
"smooth": false,
"style": {},
"themes": [],
"zLayers": 100,
"key": "twinkle",
"name": "Twinkle",
"motion": {
"disable": false,
"reduce": {
"factor": 4,
"value": true
}
}
}
官方在线生成器
原文链接:https://code.ifrontend.net/archives/692,转载请注明出处。
评论0