FILEEM

POWER OF DREAM

particles.js在uniapp上的运用

安装

在项目根目录下打开终端安装particles.js

cnpm install --save particles.js

配置particles.js

template

这个就是动态粒子要展示的位置。

<view id="particles"></view>

script

因为涉及到dom树,所以必须在挂载结束后初始化particles.js。第一个参数id就是你在template上取得id名,像我要写的话就是particles。第二个参数是你的data存放的路径,个人建议使用相对路径。

mounted(){
    particlesJS.load('id','path to your particles.data');
}

id是view的id,path是json放的位置,不会写的复制粘贴demo里面的就可以

style

#particles{
      position: absolute;
      width: 100%;
      height: 100%;
      background-color: #000022;
      background-repeat: no-repeat;
      background-size: cover;
      background-position: 50% 50%;
}

particles.js 引入

main.js

import particles from 'particles.js'
Vue.use(particles)

particles.json

这个文件就相当于配置文件,用于控制粒子在页面中所呈现的状态。通过修改里边的字段,来得到自己想要的效果。如修改particles.color.value 的值就是修改粒子的颜色;修改particle.shape就是修改粒子的外观。至于详细的参数解析可以参考官网:https://github.com/VincentGarreau/particles.js
建议放在静态资源文件夹里,这里我用了particles.js作者搞的NASA效果,没有用默认

{
  "particles": {
    "number": {
      "value": 160,
      "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": 1,
      "random": true,
      "anim": {
        "enable": true,
        "speed": 1,
        "opacity_min": 0,
        "sync": false
      }
    },
    "size": {
      "value": 3,
      "random": true,
      "anim": {
        "enable": false,
        "speed": 4,
        "size_min": 0.3,
        "sync": false
      }
    },
    "line_linked": {
      "enable": false,
      "distance": 150,
      "color": "#ffffff",
      "opacity": 0.4,
      "width": 1
    },
    "move": {
      "enable": true,
      "speed": 1,
      "direction": "none",
      "random": true,
      "straight": false,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": false,
        "rotateX": 600,
        "rotateY": 600
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": true,
        "mode": "bubble"
      },
      "onclick": {
        "enable": true,
        "mode": "repulse"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 400,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 250,
        "size": 0,
        "duration": 2,
        "opacity": 0,
        "speed": 3
      },
      "repulse": {
        "distance": 400,
        "duration": 0.4
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true
}

最后呈现的效果如下:

《particles.js在uniapp上的运用》

点赞