效果如圖
1.首先安裝依賴
Npm
npm i react-particles-js
Yarn
yarn add react-particles-js
2.在項目中引入
import Particles from 'react-particles-js';
3.使用 Particles
<Particle style={{ width: '100%', height: '100%', position: 'absolute' }} params={{ particles: { number: { value: 60, //數量 density: { enable: true, value_area: 2000, //區域散布密度大小 }, }, color: { value: '#507D9F', //原子的顏色 }, line_linked: { //連接線 enable: true, // 連接線 color: '#507D9F', //連接線顏色 width: '0.5', //連接線的寬度 opacity: 0.5, //連接線不透明度 shadow: { enable: true, // 連接線 color: '#044568', //連接線顏色 blur: 15, opacity: 0.5, //連接線不透明度 }, }, shape: { //原子 type: 'circle', //原子的形狀 stroke: { width: 0, //原理的寬度 color: '#000000', //原子顏色 }, polygon: { nb_sides: 5, // 原子的多邊形邊數 }, }, opacity: { value: 1, //不透明度 random: true, //隨機不透明度 anim: { enable: true, //漸變動畫 speed: 1, //漸變動畫速度 opacity_min: 1, //漸變動畫不透明度 sync: false, }, }, size: { value: 8, // 原子大小 random: true, // 原子大小隨機 anim: { enable: false, //原子漸變 speed: 180, //原子漸變速度 size_min: 0.1, sync: false, }, }, move: { enable: true, //原子移動 speed: 4, //原子移動速度 (越大越快,越小越慢) direction: 'none', // 原子移動方向 random: true, // 移動隨機方向 straight: false, // 直接移動 out_mode: 'out', // 是否移動出畫布 bounce: false, //是否跳動移動 attract: { enable: false, //原子之間吸引 rotateX: 600, //原子之間吸引X水平距離 rotateY: 1200, //y垂直距離 }, }, }, interactivity: { detect_on: 'canvas', // 原子之間互動檢測 events: { onhover: { enable: true, //懸停 mode: 'repulse', //懸停模式 (擊退效果,"grab" 抓取臨近的,"bubble"泡沫球效果) }, }, modes: { grab: { distance: 100, // 原子互動抓取距離 line_linked: { opacity: 1, //原子互動抓取距離連線不透明度 }, }, bubble: { distance: 100, // 原子抓取泡沫效果之間的距離 size: 80, // 原子抓取泡沫效果之間的大小 duration: 2, //原子抓取泡沫效果之間的持續事件 opacity: 0.8, speed: 3, }, repulse: { distance: 150, //擊退效果距離 duration: 0.4, //擊退效果持續事件 }, push: { particles_nb: 4, //粒子推出的數量 }, remove: { particles_nb: 2, }, }, }, retina_detect: true, }} style={{ width: '100%', height: '100%', position: 'absolute', top: '0', left: '0', backgroundColor: '#000000', }} />
我自己配置了一些,具體參數可以去 https://github.com/Wufe/react-particles-js 查看
簡單寫了一點配置說明:
鍵值 | 參數選項/ 說明 | 實例 |
particles.number.value | number 數量 | 40 |
particles.number.density.enable | boolean | true / false |
particles.number.density.value_area | number 區域散布密度大小 | 800 |
particles.color.value | HEX (string) 原子的顏色 |
"#b61924" {r:182, g:25, b:36} {h:356, s:76, l:41} ["#b61924", "#333333", "999999"] "random" |
particles.shape.type | string array selection 原子的形狀 |
"circle" "edge" "triangle" "polygon" "star" "image" ["circle", "triangle", "image"] |
particles.shape.stroke.width | number 原理的寬度 | 2 |
particles.shape.stroke.color | HEX (string) 原子顏色 | "#222222" |
particles.shape.polygon.nb_slides | number 原子的多邊形邊數 | 5 |
particles.shape.image.src | path link svg / png / gif / jpg 原子的圖片可以使用自定義圖片 |
"assets/img/yop.svg" "http://mywebsite.com/assets/img/yop.png" |
particles.shape.image.width | number (for aspect ratio) 圖片寬度 |
100 |
particles.shape.image.height | number (for aspect ratio) 圖片高度 |
100 |
particles.opacity.value | number (0 to 1) 不透明度 | 0.75 |
particles.opacity.random | boolean 隨機不透明度 | true / false |
particles.opacity.anim.enable | boolean 漸變動畫 | true / false |
particles.opacity.anim.speed | number 漸變動畫速度 | 3 |
particles.opacity.anim.opacity_min | number (0 to 1) 漸變動畫不透明度 | 0.25 |
particles.opacity.anim.sync | boolean | true / false |
particles.size.value | number 原子大小 | 20 |
particles.size.random | boolean 原子大小隨機 | true / false |
particles.size.anim.enable | boolean 原子漸變 | true / false |
particles.size.anim.speed | number 原子漸變速度 | 3 |
particles.size.anim.size_min | number | 0.25 |
particles.size.anim.sync | boolean | true / false |
particles.line_linked.enable | boolean 連接線 | true / false |
particles.line_linked.distance | number 連接線距離 | 150 |
particles.line_linked.color | HEX (string) 連接線顏色 | #ffffff |
particles.line_linked.opacity | number (0 to 1) 連接線不透明度 | 0.5 |
particles.line_linked.width | number 連接線的寬度 | 1.5 |
particles.move.enable | boolean 原子移動 | true / false |
particles.move.speed | number 原子移動速度 | 4 |
particles.move.direction | string 原子移動方向 | "none" "top" "top-right" "right" "bottom-right" "bottom" "bottom-left" "left" "top-left" |
particles.move.random | boolean 移動隨機方向 | true / false |
particles.move.straight | boolean 直接移動 | true / false |
particles.move.out_mode | string (out of canvas) 是否移動出畫布 |
"out" "bounce" |
particles.move.bounce | boolean (between particles) 是否跳動移動 |
true / false |
particles.move.attract.enable | boolean 原子之間吸引 | true / false |
particles.move.attract.rotateX | number 原子之間吸引X水平距離 | 3000 |
particles.move.attract.rotateY | number y垂直距離 | 1500 |
interactivity.detect_on | string 原子之間互動檢測 | "canvas", "window" |
interactivity.events.onhover.enable | boolean 懸停 | true / false |
interactivity.events.onhover.mode | string 懸停模式 |
"grab" 抓取臨近的"bubble" 泡沫球效果"repulse" 擊退效果["grab", "bubble"] |
interactivity.events.onclick.enable | boolean 點擊效果 | true / false |
interactivity.events.onclick.mode | string 點擊效果模式 |
"push" "remove" "bubble" "repulse" ["push", "repulse"] |
interactivity.events.resize | boolean 互動事件調整 | true / false |
interactivity.events.modes.grab.distance | number 原子互動抓取距離 | 100 |
interactivity.events.modes.grab.line_linked.opacity | number (0 to 1) 原子互動抓取距離連線不透明度 | 0.75 |
interactivity.events.modes.bubble.distance | number 原子抓取泡沫效果之間的距離 | 100 |
interactivity.events.modes.bubble.size | number 原子抓取泡沫效果之間的大小 | 40 |
interactivity.events.modes.bubble.duration | number 原子抓取泡沫效果之間的持續事件 (second) |
0.4 |
interactivity.events.modes.repulse.distance | number 擊退效果距離 | 200 |
interactivity.events.modes.repulse.duration | number 擊退效果持續事件 (second) |
1.2 |
interactivity.events.modes.push.particles_nb | number 粒子推出的數量 | 4 |
interactivity.events.modes.remove.particles_nb | number | 4 |
retina_detect | boolean | true / false |
希望可以幫助的有需要的小伙伴們。。。