Particles.js: Introduction

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. 

You also need to create a DOM element where Particles.js will create the particles. Give it some easily identifiable id , for referencing later.

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

The particles are by default white. 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:

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 bigger in size 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. General syntax for calling the function will look like:

Here, dom-id is the id of element where you want the particles to appear. path-json is the path to the JSON file with all 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 snowfall using this awesome library. At first, our function will look like:

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:

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:

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:

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

Changing 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 :

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 starters 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 this CodePen demo.

Final Thoughts

Getting started with Particles.js is easy. If you have 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 in the comments.

Tags:

Comments

Related Articles