hansschenker / tsparticles

tsParticles - Particles.js on steroids

Home Page:https://particles.matteobruni.it

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

banner

tsParticles - TypeScript Particles

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!

jsDelivr Cdnjs npmjs npm

master staging dev
Master Build Status Staging Build Status Dev Build Status

lerna CodeFactor Codacy Badge Codacy Badge Sauce Test Status

Slack tsParticles Product Hunt

Do you want to use it on your website?

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.

Related projects

jQuery

npm npm

Installation
npm install jquery-particles  

or from jsDelivr

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>  
How to use

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. });

VueJS

npm npm

Installation
yarn add particles.vue  
Usage
import Particles from 'particles.vue';  
  
export default {  
 name: 'App', components: { Particles }}  
Simple config
<template>  
 <div id="app"> <Particles id="tsparticles" lineLinked="true"/> </div>
</template>
Full Config
<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>

ReactJS

npm npm

Installation

npm install react-tsparticles

or

yarn add react-tsparticles

How to use
Code

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
}} />
     );
  } 
}
Props
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.

Errors

If you have typescript errors tsParticles uses TypeScript 3.9.3 so try installing at least 3.8 for import type syntax.

3rd Party

A 3rd-party component is available too, read more here

Preact

npm npm

Installation

npm install preact-particles

or

yarn add preact-particles

How to use
Code

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
}} />
    );
  };  
}  
  
Props
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.

Errors

If you have typescript errors tsParticles uses TypeScript 3.9.3 so try installing at least 3.8 for import type syntax.

Angular CLI

npm npm

How to use it
Install
npm install ng-particles  

or

yarn add ng-particles  
Usage

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 { }  

Want to see it in action and try it?

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 😷.

tsParticles demo

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.

Dependencies

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.


Migrating from Particles.js

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.


Plugins/Customizations

tsParticles now supports some customizations 🥳.

NOW YOU CAN CREATE YOUR OWN PLUGINS

Read more here...


API Docs

Documentation and Development references here 📖


Library installation

Hosting / CDN

Please use this hosts or your own to load tsParticles on your projects

jsDelivr

jsDelivr

cdnjs

Cdnjs

unpkg

https://unpkg.com/tsparticles/


npm

npmjs npmjs

npm install tsparticles  

yarn

yarn add tsparticles  

Import and require

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.


NuGet

Nuget


Usage

Load tsParticles and configure the particles:

tsParticles demo

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();  

Import and require

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";  

React.js

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.


Demo / Generator

https://particles.matteobruni.it/

Particles demo


Characters as particles

Particles chars demo


Mouse hover connections

Particles mouse connections demo


Polygon mask

tsParticles Polygon Mask demo


Animated stars

Particles NASA demo


Nyan cat flying on scrolling stars

Particles Nyan Cat demo


Snow particles

tsParticles Snow demo


Background Mask particles

tsParticles Background Mask demo


COVID-19 SARS-CoV-2 particles

tsParticles COVID-19 demo

Don't click! DON'T CLICK! OH NO IT'S SPREADING!!!!


particles.json

You can find a sample config here 📖


Options

You can find all options available here 📖


Stargazers over time

Stargazers over time


JetBrains JetBrains

Huge thanks to JetBrains for the 2020 Open Source License!

JetBrains WebStorm is used to maintain this project.

Huge thanks to SauceLabs for the Open Source License!

Testing Powered By SauceLabs

About

tsParticles - Particles.js on steroids

https://particles.matteobruni.it

License:MIT License


Languages

Language:TypeScript 93.8%Language:JavaScript 3.6%Language:Vue 1.4%Language:HTML 1.2%Language:CSS 0.0%