How to easily create beautiful particles animations for your website using tsParticles
Tags: tutorial, javascript, html, typescript
Published: 2021-06-19 21:15:00


## How to add tsParticles in your website

Have you seen particles effect in some websites and you want one too?

Do you have particles.js installed but it have problems or it's too heavy for your users?

Are you searching a new animation for your website?

Well, you are in the right place. `tsParticles` is a new library, started from the particles.js codebase, to have some particles animations in your website.

[![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles/badge?style=rounded)](https://www.jsdelivr.com/package/npm/tsparticles) [![Cdnjs](https://img.shields.io/cdnjs/v/tsparticles?v=1.29.0)](https://cdnjs.com/libraries/tsparticles) [![npm version](https://img.shields.io/npm/v/tsparticles)](https://www.npmjs.com/package/tsparticles) [![npm dls](https://img.shields.io/npm/dw/tsparticles)](https://www.npmjs.com/package/tsparticles)

{% github matteobruni/tsparticles %}

Let's start with the installation

### Setup

#### CDN

If you want to use a CDN to link the library, or use them to download the files here are the instructions

##### cdnjs

Let's start with the most famous and used.

[![cdnjs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/642y2uvateqnnes28o2z.png)](https://cdnjs.com/libraries/tsparticles)

The `tsparticles.min.js` file is marked as default and you can see it highlighted, you can use the right buttons to copy the url, copy all the `script` tag or just the SRI hash.

I recommend to copy the `script` tag so you can use it safely in your website

##### jsDelivr

Another famous CDN is supported too and this is easy to use too.

[![jsDelivr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89yvraapj8jnxlmctupw.png)](https://www.jsdelivr.com/package/npm/tsparticles)

Just copy the script tag (I recommend the integrity check) and you're ready to include it in your page.

#### NPM

If you are using NPM you can simply run one of these commands

```shell
npm install tsparticles
```

or 

```shell
yarn add tsparticles
```

And you have it ready in your `node_modules` folder.

#### Wrappers

This project have also some official wrappers for some Javascript frameworks to easily use and configure this library

##### Web Components

![npm](https://img.shields.io/npm/v/web-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/web-particles?version=1.29.0)

If you want to use tsParticles with Web Components checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/web/README.md)

##### jQuery

![npm](https://img.shields.io/npm/v/jquery-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/jquery-particles?version=1.29.0)

If you want to use tsParticles with jQuery checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/jquery/README.md)

##### VueJS 2.x

![npm](https://img.shields.io/npm/v/particles.vue?version=1.29.0) ![npm](https://img.shields.io/npm/dm/particles.vue?version=1.29.0)

If you want to use tsParticles with VueJS checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/vue/README.md)

##### VueJS 3.x

![npm](https://img.shields.io/npm/v/particles.vue3?version=1.29.0) ![npm](https://img.shields.io/npm/dm/particles.vue3?version=1.29.0)

If you want to use tsParticles with VueJS 3.x checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/vue3/README.md)

##### ReactJS

![npm](https://img.shields.io/npm/v/react-tsparticles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/react-tsparticles?version=1.29.0)

If you want to use tsParticles with ReactJS checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/react/README.md)

##### Angular CLI

![npm](https://img.shields.io/npm/v/ng-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/ng-particles?version=1.29.0)

If you want to use tsParticles with Angular CLI checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/angular/README.md)

##### Svelte

![npm](https://img.shields.io/npm/v/svelte-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/svelte-particles?version=1.29.0)

If you want to use tsParticles with Svelte checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/svelte/README.md?version=1.29.0)

##### Preact

![npm](https://img.shields.io/npm/v/preact-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/preact-particles?version=1.29.0)

If you want to use tsParticles with Preact checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/preact/README.md)

##### Riot.js

![npm](https://img.shields.io/npm/v/riot-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/riot-particles?version=1.29.0)

If you want to use tsParticles with Riot.js checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/riot/README.md)

##### Inferno

![npm](https://img.shields.io/npm/v/inferno-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/inferno-particles?version=1.29.0)

If you want to use tsParticles with Inferno checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/inferno/README.md)

##### Solid.js

![npm](https://img.shields.io/npm/v/solid-particles?version=1.29.0) ![npm](https://img.shields.io/npm/dm/solid-particles?version=1.29.0)

If you want to use tsParticles with Solid.js checkout [this README](https://github.com/matteobruni/tsparticles/tree/main/components/solid/README.md)

#### Usage

First of all you need to find the `tsparticles.min.js` downloaded with the instructions above.

Once you are ready with the `script` tag included you have two option to start using tsParticles.

If you are using it with NPM, you need to import tsParticles like this:

``` javascript
const { tsParticles } = require("tsparticles");
```

or

``` javascript
import { tsParticles } from "tsparticles"; // this is supported in TypeScript too
```

##### Javascript Object

You can use a Javascript object containing all options like this

``` javascript
let options = { /* omitted for brevity, I'll describe the options in this series */};

tsParticles.load('<element id>', options);
//<element id> is a placeholder sample, use it without <>
```

##### External Json File

Otherwise you can use an external JSON file, it's easier to maintain because you need to change only this file and not your scripts that could be minified or something like that.

The JSON file is loaded like this

*particles.json*
``` javascript
{
    // omitted for brevity, I'll describe all the options in this series
}
```

*app.js*
``` javascript
tsParticles.loadJSON('<element id>', 'particles.json');
//<element id> is a placeholder sample, use it without <>
```

#### Particles Manager object

`load` and `loadJSON` methods returns a `Promise<Container>` object, the `Container` object is the object containing the canvas, the particles and all is needed to work.

You can access it using the method `tsParticles.dom()` which returns a `Container[]` with all containers initialized or `tsParticles.domItem(index)` which returns the specified `Container` if found, `index` is the array index, just a managed version of `tsParticles.dom()[index]`.

If you want to unwrap the `Promise` you can await the load methods if you are using an async function or use the `then` method (Official API [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then)).

Let's see a sample of `then` method:

*app.js*
```javascript
tsParticles.load('<element id>', { /* omitted for brevity */ }).then(function (container) {
  // container is ready to be used
});
```

The container object is really useful if you want to make particles more interactive or customizable.

##### Properties

`actualOptions`: The current options loaded in the object, some changes to this object can be effective only after a `refresh()`

##### Methods

`play(force)`: Starts the animations or resume from `pause`, `force` is an optional boolean parameter to force the animation. This method doesn't allocate resources, just plays the animations.

`pause()`: Pauses the animations. This method doesn't clean any resource, just pauses the animations.

`start()`: Starts the container, reallocates all the resources freed by `stop`. Can't start after `destroy`.

`stop()`: Stops the container, frees unneeded resources.

`destroy()`: Destroys the container and invalidates it. The container will be unusable after this, any method called on it can return an error.

`refresh()`: This methods is a shorthand of `stop`/`start` cycle. This method is good to reload options changed by code.

`exportImage(callback, type, quality)`: Exports the current canvas image, `background` property of `options` won't be rendered because it's css related. The `callback` is a function that handles the exported image, `type` is the image type you want to export and `quality` the quality of the image, these two parameters are optional.

`exportConfiguration()`: Exports the current configuration using `options` property returning a JSON string representing the `options` object.

`draw()`: Draws a single frame of animation, if you want to handle it yourself just `pause` the container and call `draw` when you need it.

`getAnimationStatus()`: Returns a boolean with the animation status, `true` is playing, `false` is paused

`addClickHandler(callback)`: Adds a click event handler for this particles container. The callback must accept 2 parameters: (the mouse event and the clicked particles array, all the particles that are in the click position will be there)