Scroll to top
This post is part of a series called An Introduction to Particles.js.
Particles.js: Control Particle Count and Shape

A lot of tiny particles moving around and interacting with each other—or with you—have a certain appeal to them. If you are ever in a situation where you need to work with a lot of particles, Particles.js will serve you well. As is evident from the name, it is a JavaScript library that can help you create particle systems. Moreover, it is lightweight, easy to use, and gives you a lot of control. 

In this tutorial, I will cover all the features of the library and help you get started. This tutorial is the first part of the series and will cover just the basics. 

Installation and Usage

First, you need to host the library. You can either upload it on your own server or use jsdeliver CDN like me. 

1
<script src="//cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>

You also need to create a DOM element where Particles.js will create the particles. Give it an easily identifiable id to refer to later.

1
<div id="particles-js"></div> 

Now, to create a basic system of particles with default settings, you just need a single line of JavaScript to initialize the library. 

1
particlesJS();

The particles are white by default. They are also interconnected with thin white lines. So, if you are not seeing anything right now, just change the background to something else. Here is my CSS for styling the particle div:

1
#particles-js {
2
  background: cornflowerblue;
3
}

Try clicking somewhere inside the demo below. After each click, Particles.js will generate four more new particles.

Setting Custom Options

Even though it took just four lines of code to create the previous demo, the end result might not be what you are looking for. To me, the particles seem to be a little too big and densely packed. Maybe you want the particles to be of a different shape or have a random size. Particles.js allows you to set all these and many more properties in JSON which you can refer to during the initialization. The general syntax for calling the function will look like this:

1
particlesJS(dom-id, path-json, callback (optional));

Here, dom-id is the id of the element where you want the particles to appear. path-json is the path to the JSON file with all the configuration options, and callback is an optional callback function. Instead of a path, you can directly put your JSON code in the second parameter.

Let's try to create falling snow using this awesome library. At first, our function will look like this:

1
particlesJS("snowfall", 'assets/snowflakes.json');

I have removed the callback function and changed the DOM Id to a more specific name. The snowflakes will mostly have a spherical shape. They will fall downwards and have a non-uniform size. Also, unlike in our first demo, they won't be connected by lines.

Moving Particles Around

In the beginning, our snowflakes.json file will have the following code:

1
{
2
  "particles": {
3
  
4
  },
5
  "interactivity": {
6
    
7
  }
8
}

All our configuration options related to physical properties like shape, size, and motion will go inside particles. All the configuration options that determine the interaction behavior will go inside interactivity.

I am setting the number of particles to 100. This will generally depend on the available space. As previously discussed, I will also set the shape to circle. At this point, your file should look like this:

1
{
2
  "particles": {
3
    "number": {
4
      "value": 100
5
    },
6
    "shape": {
7
      "type": "circle"
8
    }
9
  },
10
  "interactivity": {
11
    
12
  }
13
}

I am using a value of 10 to set the size of snowflakes. Since snowflakes vary in size, I will set random to true. This way, the snowflakes can have any size between zero and the maximum limit that we specified. To disable or remove all the lines that link these particles together, you can set enable to false for line_linked

To move particles around, you will have to set the enable property to true. Without any other setting, the particles will move haphazardly as if they are in space. You can set the direction of these particles with a string value like "bottom". Even though the general motion of particles is downwards, they still need to move a bit randomly to look natural. This can be achieved by setting straight to false. At this point, snowflakes.json will have the following code:

1
{
2
  "particles": {
3
    "number": {
4
      "value": 100
5
    },
6
    "shape": {
7
      "type": "circle"
8
    },
9
    "size": {
10
      "value": 10,
11
      "random": true
12
    },
13
    "line_linked": {
14
      "enable": false
15
    },
16
    "move": {
17
      "enable": true,
18
      "speed": 2,
19
      "direction": "bottom",
20
      "straight": false
21
    }
22
  },
23
  "interactivity": {
24
    
25
  }
26
}

With the JSON code above, you will get the following result:

Changing the Interaction Behavior

If you hover over the demo above, you will notice that the lines still exist but only show up temporarily during hover. To remove them completely, you can set the enable property for the onhover event to false. Try clicking inside the demo above, and you will notice that each click generates four particles. This is the default behavior. You can also change the number of particles using the particles_nb property under push. I have set this number to 12 in this case.

You can also determine whether to detect the events on the window or canvas using the detect_on option. 

Here is the complete code for the JSON file:

1
{
2
  "particles": {
3
    "number": {
4
      "value": 100
5
    },
6
    "shape": {
7
      "type": "circle"
8
    },
9
    "size": {
10
      "value": 10,
11
      "random": true
12
    },
13
    "line_linked": {
14
      "enable": false
15
    },
16
    "move": {
17
      "enable": true,
18
      "speed": 2,
19
      "direction": "bottom",
20
      "straight": false
21
    }
22
  },
23
  "interactivity": {
24
    "detect_on": "canvas",
25
    "events": {
26
      "onhover": {
27
        "enable": false
28
      }
29
    },
30
    "modes": {
31
      "push": {
32
        "particles_nb": 12
33
      }
34
    }
35
  }
36
}

As you can see, I did not have to specifically enable the onclick event. It is enabled by default. Similarly, I could remove other options like "detect_on": "canvas" under interactivity and "straight": false under move. I have kept them so that beginners don't get confused about things like why the particles are not moving in straight lines.

You can try out different values to modify the snowflakes in the CodePen above. Just click the JS tab to edit the JSON.

Final Thoughts

Getting started with Particles.js is easy. If you've never worked with particle systems before, this library will get you started in no time. This tutorial was just a basic introduction to the library. In the next two tutorials of this series, I will cover all the aspects of this library in much more detail.

If you have any questions regarding this tutorial, please let me know on the forum.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.