search
HomeWeb Front-endCSS TutorialParticles.js: Introduction

Particles.js: Introduction

There are a large number of tiny particles moving around and interacting with each other—or interacting with you—with a unique attraction. Particles.js will be very useful if you need to deal with large amounts of particles. As the name implies, it is a JavaScript library that helps you create particle systems. Plus, it's lightweight, easy to use, and gives you plenty of control.

This tutorial will cover all the features of this library and help you get started. This tutorial is the first part of this series and covers only the basics.

Installation and use

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

<code><br></code>

You also need to create a DOM element where Particles.js will create particles. Give it a circle that is easy to identify. At this point, your file should look like this:

<code>{<br>  "particles": {<br>    "number": {<br>      "value": 100<br>    },<br>    "shape": {<br>      "type": "circle"<br>    }<br>  },<br>  "interactivity": {<br><br>  }<br>}<br></code>

I use a value of 10 to set the size of the snowflake. Since the snowflakes vary in size, I set random to true. This way, the size of the snowflake can vary between the zero and maximum limits we specify. To disable or delete all lines connecting these particles, you can set line_linked's enable to false.

To move particles, you must set the enable attribute to true. Without any other settings, the particles will move in a mess, just like they are in space. You can set the orientation of these particles using string values ​​(such as "bottom"). Even if the overall motion of particles is downward, they still need to move slightly randomly to look natural. This can be done by setting straight to false . At this point, snowflakes.json will contain the following code:

<code>{<br>  "particles": {<br>    "number": {<br>      "value": 100<br>    },<br>    "shape": {<br>      "type": "circle"<br>    },<br>    "size": {<br>      "value": 10,<br>      "random": true<br>    },<br>    "line_linked": {<br>      "enable": false<br>    },<br>    "move": {<br>      "enable": true,<br>      "speed": 2,<br>      "direction": "bottom",<br>      "straight": false<br>    }<br>  },<br>  "interactivity": {<br><br>  }<br>}<br></code>

Use the above JSON code, you will get the following results:

Change interactive behavior

If you hover over the demo above, you will notice that the lines still exist, but only temporarily display during the hover. To delete them completely, you can set the onhover property of the enable event to false. Try clicking on 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 push attribute under particles_nb. In this case, I have set this number to 12.

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

The following is the complete code of the JSON file:

<code><br></code>

As you can see, I don't have to enable the onclick event specifically. It is enabled by default. Similarly, I can delete other options such as interactivity and "detect_on": "canvas" under move. I keep them so that beginners won't be confused about questions like why particles don't move in a straight line. "straight": false"

You can try different values ​​to modify the snowflakes in the CodePen above. Just click the

JS tab to edit JSON.

Final Thoughts

Particles.js is easy to get started. If you have never used a particle system before, this library will help you get started right away. This tutorial is just a basic introduction to the library. In the next two tutorials in this series, I will cover all aspects of the library in more detail.

If you have any questions about this tutorial, please let me know in the forum.

The above is the detailed content of Particles.js: Introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor