particles.js icon indicating copy to clipboard operation
particles.js copied to clipboard

How to set up particles.js with a vue cli and webpack setup

Open ananddharne opened this issue 7 years ago • 6 comments

. I am using webpack with vue cli. This is my component App.vue. I did an npm install particlesjs and imported using 'from' as seen below Ap.vue component where I want to load particles.js at root

`

<template>
  <div id="app">

  <div id="particles-js"></div>
    
<router-link to  = "/"> Home </router-link>

    <router-view></router-view>

  </div>

</template>

<script>
/* eslint-disable */

import particles from 'particles.js'

export default {
  name: 'app',

  mounted()
  {
      console.log('mounted');

      particlesJS('app', {
        
  "particles": {
    "number": {
      "value": 380,
      "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": 6,
      "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": 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
});
    }


  



  }




    /* eslint-disable */

</script>


 
<style>
#app {
  position: absolute;
  width: 100%;
  height: 100%;
}
canvas {
  display: block;
  vertical-align: bottom;
}


  
</style>

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>portfolio2</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.0/css/bulma.css">
  </head>
  <body>
    <div id="app"></div>
     <div id="particles-js"></div>
    <script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
  </body>
</html>

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

index.js(routing)

/* eslint-disable */
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Home from '@/components/Home'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Hello',
      component: Hello
    },

    {
      path: '/',
      name: 'Home',
      component: Home
    }

   
  ]
})

The particles.js doesnt load up everything else works. When I go into console I can also see a container of canvas. What is it I that I am doing wrong? Any help would be greatly appreciated! Link to output - http://imgur.com/a/qaYzF. `

ananddharne avatar Apr 18 '17 14:04 ananddharne

Use vue-particles: https://github.com/creotip/vue-particles

creotip avatar May 02 '17 21:05 creotip

First, install particles.js as a dependency if you haven't already (npm i particles.js). The set up with require() is pretty straightforward, while mounting the component:

ParticlesJS.vue

<template>
  <div id='particles-js'></div>
</template>

<script>
export default {
  name: 'ParticlesJS',

  mounted () {
    require('particles.js')
    this.$nextTick(() => {
      this.initParticlesJS()
    })
  },

  methods: {
    initParticlesJS () {
      /* eslint-disable */
      particlesJS('particles-js', {
        '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': 10,
            'random': true,
            'anim': {
              'enable': false,
              'speed': 80,
              'size_min': 0.1,
              'sync': false
            }
          },
          'line_linked': {
            'enable': true,
            'distance': 300,
            'color': '#ffffff',
            'opacity': 0.4,
            'width': 2
          },
          'move': {
            'enable': true,
            'speed': 12,
            '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': true,
              'mode': 'push'
            },
            'resize': true
          },
          'modes': {
            'grab': {
              'distance': 800,
              'line_linked': {
                'opacity': 1
              }
            },
            'bubble': {
              'distance': 800,
              'size': 80,
              'duration': 2,
              'opacity': 0.8,
              'speed': 3
            },
            'repulse': {
              'distance': 400,
              'duration': 0.4
            },
            'push': {
              'particles_nb': 4
            },
            'remove': {
              'particles_nb': 2
            }
          }
        },
        'retina_detect': true
      })
    }
  }

}
</script>

<style>
/* Your styles */
</style>

App.vue

<template>
  <div id="app">
    <router-view/>
    <particlesJS/>
  </div>
</template>

<script>
import particlesJS from './components/particlesJS'

export default {
  name: 'app',
  components: { particlesJS }
}
</script>

whoisjorge avatar Oct 24 '17 03:10 whoisjorge

add css in the whoisjorge code solution:-

#particles-js { width: 100%; height: 100%; background: cornflowerblue; }

sunil16 avatar May 17 '18 07:05 sunil16

Directly importing particle.js (import 'particles.js';) in "ParticlesJS.vue" can replace "require('particles.js')" in the mounted lifecycle hook of @whoisjorge code solution and works greatly too.

MubarakSULAYMAN avatar Jul 12 '21 05:07 MubarakSULAYMAN

@MubarakSULAYMAN these instructions are good too:

Vue,js 2

Instructions

Vue,js 3

Instructions

matteobruni avatar Jul 12 '21 06:07 matteobruni

Thanks, @matteobruni. But that uses another module/package. Seems like a better alternative since it is more updated with lesser issues.

MubarakSULAYMAN avatar Jul 12 '21 06:07 MubarakSULAYMAN