As the name suggests, the Particle.js library allows us to add particles to specific HTML elements. Additionally, we can change the number of shapes of the particles in the div.
We can add animation or motion to particles using the Particle.js library. Here we will learn to change the shape, color and movement of particles through different examples.
grammar
Users can use the Particle.js library in JavaScript projects according to the following syntax.
tsParticles.load("id", { particles: { // properties for the particles } });
In the above syntax, id represents the id of the div element to which we need to add particles.
Example
In the example below, we are using the Particles.JS library and using the CDN in the
tag. We created the element and assigned the id in the HTML body.In JavaScript, we added the tsarticles.load() method to load particles. As the first parameter of the load() method, we pass the id of the div element. We pass this object as the second parameter containing the particle properties.
<html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/tsparticles/1.18.11/tsparticles.min.js"> </script> <style> #tsparticles { width: 100%; height: 100%; background-color: grey; } </style> </head> <body> <div id = "tsparticles"> <h2 id="Using-the-i-particle-js-library-i-in-the-JavaScript-project"> Using the <i> particle.js library </i> in the JavaScript project. </h2> </div> <script> tsParticles.load("tsparticles", { particles: { number: { value: 500 }, } }); </script> </body> </html>
In the output, the user can observe the particles in the div element.
Example
The following example will add movement and color to particles. Users can use the move attribute to move particles. The "move" property takes the value as an object containing the enabled property with a boolean value.
<html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/tsparticles/1.18.11/tsparticles.min.js"> </script> <style> #particles { width: 100%; height: 100%; background-color: lightgreen; } </style> </head> <body> <div id = "particles"> <h2 id="Using-the-i-particle-js-library-i-in-the-JavaScript-project"> Using the <i> particle.js library </i> in the JavaScript project. </h2> </div> <script> tsParticles.load("particles", { particles: { number: { value: 1000 }, move: { enable: true }, color: { value: "#272701" }, } }); </script> </body> </html>
Users can use the color property to change the color of particles that contain this object as a value.
Users can use the following shapes of particles in the Particle.JS library.
>"Polygon"
"triangle"
"Circle"
"Edge"/"Square"
"image"/"image"
"Star"
"Character"/"Character"
Example
In the example below, we have added a polygon shape to the particles. Additionally, we changed the size of the particles using the size property. Additionally, we added animation to the particles themselves, increasing and decreasing the size of the particles that users can observe in the output.
<html> <head> <script src = "https://cdnjs.cloudflare.com/ajax/libs/tsparticles/1.18.11/tsparticles.min.js"> </script> <style> #particles { width: 100%; height: 100%; background-color: lightgreen; } </style> </head> <body> <div id = "particles"> <h2 id="Using-the-i-particle-js-library-i-in-the-JavaScript-project">Using the <i> particle.js library </i> in the JavaScript project. </h2> </div> <script> tsParticles.load("particles", { particles: { number: { value: 1000 }, move: { enable: true }, color: { value: "#ff0342" }, // adding shape of particles shape: { type: "polygon", }, // changing the size of elements size: { value: 8, random: true, animation: { enable: true, speed: 40, sync: true }, move: { enable: true, }, } } }); </script> </body> </html>
User learned to use keywords.js library in JavaScript projects. However, users can install particle.js library on their local machine and import it using path. Whenever users want to use the Particle.js library with a NodeJS application, they should install it using NPM commands.
The above is the detailed content of How to use Particle.js in JavaScript projects?. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
