particles.js is used in vue, and some bugs

Use of particles.js

Install particles.js globally

npm install --save particles.js

Configure particles.js to
import the particles.js file.
When your scope of use is relatively small, you can directly import it in the script of the current vue file

//vue单文件
import particles from 'particles.js'

When the scope is large, you can import it globally, and import it in main.js

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

I create a particles.json in the asset folder

{
    
    
    "particles": {
    
    
      "number": {
    
    
        "value": 60,
        "density": {
    
    
          "enable": true,
          "value_area": 800
        }
      },
      "color": {
    
    
        "value": ["#344d7b","#7A378B","#551A8B"]
      },
      "shape": {
    
    
        "type": "circle",
        "stroke": {
    
    
          "width": 3,
          "color": "#fff"
        },
        "polygon": {
    
    
          "nb_sides": 5
        }
      },
      "opacity": {
    
    
        "value": 1,
        "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": "#4381bd",
        "opacity": 0.6,
        "width": 2
      },
      "move": {
    
    
        "enable": true,
        "speed": 4,
        "direction": "none",
        "random": false,
        "straight": false,
        "out_mode": "out",
        "bounce": false,
        "attract": {
    
    
          "enable": false,
          "rotateX": 100,
          "rotateY": 1200
        }
      }
    },
    "interactivity": {
    
    
      "detect_on": "Window",
      "events": {
    
    
        "onhover": {
    
    
          "enable": true,
          "mode": "grab"
        },
        "onclick": {
    
    
          "enable": true,
          "mode": "push"
        },
        "resize": true
      },
      "modes": {
    
    
        "grab": {
    
    
          "distance": 140,
          "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
  }

Define a div in the template

<div id="particles"></div>

Configure in script

// 引入particles.json文件用于配置canvas
import particlesJson from '../assets/particles.json'

export default {
    
    
  name: 'particlesDemo',
  mounted(){
    
    
    // particles是我们设置的id,particlesJson是我们引入的json文件
    particlesJS('particles', particlesJson,);
}
}

Set in style

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

The final rendering effect is as follows:
Insert picture description here

Bugs encountered

1. Introduced according to the official document, the effect did not come out, the official document introduced the code as follows

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

Solution:
Just delete .load

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

2. The effect came out, but the style of json I configured did not go up.
Solution: The
path cannot be placed in particlesJS, imported from the outside, and assigned to it.

import particlesJson from '../assets/particles.json'
mounted(){
    
    
    particlesJS('id',particlesJson );
}

Guess you like

Origin blog.csdn.net/weixin_43236062/article/details/101644354