A lightweight TypeScript library for creating particles. Dependency free (*) and browser ready!
Particles.js converted in TypeScript, dependency free (*), improved with new cool 😎 features and various bugs fixed and it's actively maintained!
master |
staging |
dev |
---|---|---|
This library is available on the two most popular CDNs and it's easy and ready to use, if you were using particles.js it's even easier.
You'll find the instructions below, with all the links you need, and don't be scared by TypeScript, it's just the source language.
The output files are just JavaScript. 🤩
CDNs and npm
have all the sources you need in Javascript, a bundle browser ready (tsparticles.min.js) and all files splitted for import
syntax.
If you are still interested some lines below there are some instructions for migrating from the old particles.js library.
npm install jquery-particles
or from jsDelivr
<!-- first include tsParticles -->
<script src="https://cdn.jsdelivr.net/npm/tsparticles@1.16.1/dist/tsparticles.min.js"></script>
<!-- then include jquery wrapper -->
<script src="https://cdn.jsdelivr.net/npm/jquery-particles@1.16.1/dist/jquery.particles.min.js"></script>
HTML
<div id="tsparticles"></div>
$("#tsparticles")
.particles() .init({
background: {
color: {
value: "#0d47a1"
}
},
fpsLimit: 60,
interactivity: {
detectsOn: "canvas",
events: {
onClick: {
enable: true,
mode: "push"
},
onHover: {
enable: true,
mode: "repulse"
},
resize: true
},
modes: {
bubble: {
distance: 400,
duration: 2,
opacity: 0.8,
size: 40,
speed: 3
},
push: {
quantity: 4
},
repulse: {
distance: 200,
duration: 0.4
}
}
},
particles: {
color: {
value: "#ffffff"
},
links: {
color: "#ffffff",
distance: 150,
enable: true,
opacity: 0.5,
width: 1
},
collisions: {
enable: true
},
move: {
direction: "none",
enable: true,
outMode: "bounce",
random: false,
speed: 6,
straight: false
},
number: {
density: {
enable: true,
value_area: 800
},
value: 80
},
opacity: {
value: 0.5
},
shape: {
type: "circle"
},
size: {
random: true,
value: 5
}
},
detectRetina: true
}, function (container) { // container is the particles container where you can play/pause or stop/start. // the container is already started, you don't need to start it manually. } );
// or
$("#tsparticles")
.particles() .ajax("particles.json", function (container) { // container is the particles container where you can play/pause or stop/start. // the container is already started, you don't need to start it manually. });
yarn add particles.vue
import Particles from 'particles.vue';
export default {
name: 'App', components: { Particles }}
<template>
<div id="app"> <Particles id="tsparticles" lineLinked="true"/> </div>
</template>
<template>
<div id="app">
<particles id="tsparticles"
color="#dedede"
particleOpacity="0.7"
particlesNumber="80"
shapeType="circle"
particleSize="5"
linesColor="#dedede"
linesWidth="1"
lineLinked="true"
lineOpacity="0.4"
linesDistance="150"
moveSpeed="6"
hoverEffect="true"
hoverMode="bubble"
clickEffect="true"
clickMode="push"></particles>
</div>
</template>
npm install react-tsparticles
or
yarn add react-tsparticles
Example:
import Particles from 'react-tsparticles';
class App extends Component{
render(){
return (
<Particles id="tsparticles" params={{
background: {
color: {
value: "#0d47a1"
}
},
fpsLimit: 60,
interactivity: {
detectsOn: "canvas",
events: {
onClick: {
enable: true,
mode: "push"
},
onHover: {
enable: true,
mode: "repulse"
},
resize: true
},
modes: {
bubble: {
distance: 400,
duration: 2,
opacity: 0.8,
size: 40,
speed: 3
},
push: {
quantity: 4
},
repulse: {
distance: 200,
duration: 0.4
}
}
},
particles: {
color: {
value: "#ffffff"
},
links: {
color: "#ffffff",
distance: 150,
enable: true,
opacity: 0.5,
width: 1
},
collisions: {
enable: true
},
move: {
direction: "none",
enable: true,
outMode: "bounce",
random: false,
speed: 6,
straight: false
},
number: {
density: {
enable: true,
value_area: 800
},
value: 80
},
opacity: {
value: 0.5
},
shape: {
type: "circle"
},
size: {
random: true,
value: 5
}
},
detectRetina: true
}} />
);
}
}
Prop | Type | Definition |
---|---|---|
width | string | The width of the canvas. |
height | string | The height of the canvas. |
params | object | The parameters of the particles instance. |
style | object | The style of the canvas element. |
className | string | The class name of the canvas wrapper. |
canvasClassName | string | the class name of the canvas. |
container | object | The instance of the particles container |
Find your parameters configuration here.
If you have typescript errors tsParticles
uses TypeScript 3.9.3
so try installing at least 3.8 for import type
syntax.
A 3rd-party component is available too, read more here
npm install preact-particles
or
yarn add preact-particles
Example:
import Particles from 'preact-particles';
class App extends Component{
render(){
return (
<Particles id="tsparticles" params={{
background: {
color: {
value: "#0d47a1"
}
},
fpsLimit: 60,
interactivity: {
detectsOn: "canvas",
events: {
onClick: {
enable: true,
mode: "push"
},
onHover: {
enable: true,
mode: "repulse"
},
resize: true
},
modes: {
bubble: {
distance: 400,
duration: 2,
opacity: 0.8,
size: 40,
speed: 3
},
push: {
quantity: 4
},
repulse: {
distance: 200,
duration: 0.4
}
}
},
particles: {
color: {
value: "#ffffff"
},
links: {
color: "#ffffff",
distance: 150,
enable: true,
opacity: 0.5,
width: 1
},
collisions: {
enable: true
},
move: {
direction: "none",
enable: true,
outMode: "bounce",
random: false,
speed: 6,
straight: false
},
number: {
density: {
enable: true,
value_area: 800
},
value: 80
},
opacity: {
value: 0.5
},
shape: {
type: "circle"
},
size: {
random: true,
value: 5
}
},
detectRetina: true
}} />
);
};
}
Prop | Type | Definition |
---|---|---|
width | string | The width of the canvas. |
height | string | The height of the canvas. |
params | object | The parameters of the particles instance. |
style | object | The style of the canvas element. |
className | string | The class name of the canvas wrapper. |
canvasClassName | string | the class name of the canvas. |
container | object | The instance of the particles container |
Find your parameters configuration here.
If you have typescript errors tsParticles
uses TypeScript 3.9.3
so try installing at least 3.8 for import type
syntax.
npm install ng-particles
or
yarn add ng-particles
template.html
<ng-particles id="tsparticles" [options]="particlesOptions"></ng-particles>
app.ts
export class AppComponent {
particlesOptions = {
background: {
color: {
value: "#0d47a1"
}
},
fpsLimit: 60,
interactivity: {
detectsOn: "canvas",
events: {
onClick: {
enable: true,
mode: "push"
},
onHover: {
enable: true,
mode: "repulse"
},
resize: true
},
modes: {
bubble: {
distance: 400,
duration: 2,
opacity: 0.8,
size: 40,
speed: 3
},
push: {
quantity: 4
},
repulse: {
distance: 200,
duration: 0.4
}
}
},
particles: {
color: {
value: "#ffffff"
},
links: {
color: "#ffffff",
distance: 150,
enable: true,
opacity: 0.5,
width: 1
},
collisions: {
enable: true
},
move: {
direction: "none",
enable: true,
outMode: "bounce",
random: false,
speed: 6,
straight: false
},
number: {
density: {
enable: true,
value_area: 800
},
value: 80
},
opacity: {
value: 0.5
},
shape: {
type: "circle"
},
size: {
random: true,
value: 5
}
},
detectRetina: true
};
}
app.module.ts
import { NgParticlesModule } from 'ng-particles';
import { NgModule } from "@angular/core";
@NgModule({
declarations: [ /* AppComponent */ ],
imports: [ /* other imports */ NgParticlesModule // NgParticlesModule is required ],
providers: [],
bootstrap: [ /* AppComponent */ ]
})
export class AppModule { }
I've created a tsParticles collection on CodePen 😮 or you can checkout my profile
Otherwise there's the demo page link below. Just click/tap the Coronavirus below, don't be scared. It's safe 😷.
Want to see ever more demos? Clone the repository on your computer and follow these instructions
yarn install && yarn start
Boom! 💥 http://localhost:3000 and you can checkout other demos.
If you are brave enough you can switch to the dev
branch for trying the features under development.
You may have notices the * near dependency free. Well almost all features works without any dependency, but... Well there's a little but. The Polygon Mask feature requires [pathseg](https://github.com/progers/pathseg)
for some browsers to work fine, and obviously the Icon Fonts (like FontAwesome
) must be included in your page.
tsParticles library is fully compatible with the particles.js configuration.
Seriously, you just need to change the script source et-voilà, you're ready 🧙!
You can read more here
Want to know 5 reasons to do the switch? Read here
Below you can find all the information you need to install tsParticles and its new syntax.
tsParticles now supports some customizations 🥳.
NOW YOU CAN CREATE YOUR OWN PLUGINS
Read more here...
Documentation and Development references here 📖
Please use this hosts or your own to load tsParticles on your projects
https://unpkg.com/tsparticles/
npm install tsparticles
yarn add tsparticles
Starting from version 1.12.11 import
and require
can be used to import tsParticles
.
Now you can write something like this
const tsParticles = require("tsparticles");
// or
import { tsParticles } from "tsparticles";
the imported tsParticles
is the same instance you have including the script.
Load tsParticles and configure the particles:
index.html
<div id="tsparticles"></div>
<script src="tsparticles.min.js"></script>
app.js
// @path-json can be an object or an array, the first will be loaded directly, the object from the array will be random selected
/* tsParticles.loadJSON(@dom-id, @path-json, @callback (optional)); */
tsParticles
.loadJSON("tsparticles", "presets/default.json") .then((container) => { console.log("callback - tsparticles config loaded"); }) .catch((error) => { console.error(error); });
//or
/* tsParticles.load(@dom-id, @options); */
tsParticles.load("tsparticles", {
/* options here */});
//or
/* tsParticles.loadFromArray(@dom-id, @options, @index (optional)); */
tsParticles.loadFromArray("tsparticles", [
{ /* options here */ }, { /* other options here */ },]); //random object
tsParticles.loadFromArray(
"tsparticles", [ { /* options here */ }, { /* other options here */ }, ], 1); //the second one
// Important! If the index is not in range 0...array.length, the index will be ignored.
// after initialization this can be used.
/* tsParticles.setOnClickHandler(@callback); */
/* this will be fired from all particles loaded */
tsParticles.setOnClickHandler((e) => {
/* custom on click handler */});
// now you can control the animations too, it's possible to pause and resume the animations
// these methods don't change the config so you're safe with all your configurations
// domItem(0) returns the first tsParticles instance loaded in the dom
const particles = tsParticles.domItem(0);
// play will start the animations, if the move is not enabled it won't enable it, it just updates the frame
particles.play();
// pause will stop the animations
particles.pause();
Starting from version 1.12.7 import
and require
can be used to import tsParticles
.
Now you can write something like this
const tsParticles = require("tsparticles");
// or
import { tsParticles } from "tsparticles";
tsParticles ❤️ react-particles-js
Starting from react-particles-js version 3.0.0 it has added tsParticles as a dependency.
You can follow the instructions here to install react-particles-js
and use all the configurations described in this readme to configure the particles.
https://particles.matteobruni.it/
Don't click! DON'T CLICK! OH NO IT'S SPREADING!!!!
particles.json
You can find a sample config here 📖
You can find all options available here 📖
Huge thanks to JetBrains for the 2020 Open Source License!
JetBrains WebStorm is used to maintain this project.