• 18-19 College Green, Dublin 2
  • 01 685 9088
  • info@cunninghamwebsolutions.com
  • cunninghamwebsolutions
    Cunningham Web Solutions
    • Home
    • About Us
    • Our Services
      • Web Design
      • Digital Marketing
      • SEO Services
      • E-commerce Websites
      • Website Redevelopment
      • Social Media Services
    • Digital Marketing
      • Adwords
      • Social Media Services
      • Email Marketing
      • Display Advertising
      • Remarketing
    • Portfolio
    • FAQ’s
    • Blog
    • Contact Us
    MENU CLOSE back  

    How To Create A Particle Trail Animation In JavaScript

    You are here:
    1. Home
    2. Web Design
    3. How To Create A Particle Trail Animation In JavaScript
    Thumbnail for 24790

    How To Create A Particle Trail Animation In JavaScript

    How To Create A Particle Trail Animation In JavaScript

    Anna Prenzel

    2020-04-14T11:00:00+00:00
    2020-04-14T12:34:57+00:00

    Have you ever thought about distracting visitors of your website with a fancy, glittering particle animation for a few moments, while some data is loaded in the background? Fortunately, it’s not necessary to go very deep into graphics programming with 3D libraries like three.js. All you need instead is some basic knowledge of CSS and JavaScript and a lightweight animation library such as anime.js. In the end, we should have the following result:

    A spiral shaped particle trail animation

    Download And Integration Of Anime.js

    You can download the anime.js library from the official GitHub site. Download the file anime.js or anime.min.js from the lib/ folder.

    In my example, the HTML part looks like this:

    <!DOCTYPE html>
    <html lang="en" >
    <head>
      <meta charset="UTF-8">
      <title>Anime.js Particles</title>
      <!--or use anime.min.js-->
      <script src="anime.js"></script>
      <link rel="stylesheet" href="style.css">
    </head>
    <body>
    <div class="anime-container">
    </div>
    <script src="script.js"></script>
    </body>
    </html>

    The CSS file styles.css defines the background color for the page and for the individual particles. The position settings are necessary so that we can later position the particles freely on the page using the CSS properties left and top.

    body {
     background-color: hsl(30, 3%, 14%);
    }
    .anime-container {
      position: relative;
    }
     
    .anime-container .dot{
      position: absolute;
      /*draw particles as circles:*/
      border-radius: 50%;
      background-color: hsl(60, 100%, 80%);
    }

    The content of the file script.js is covered in the following section.

    Generating The Particles

    As the name suggests, a particle animation consists of many small particles moving in space while following a certain pattern. All particles are generated simultaneously before the animation starts.

    For the following explanation, the official documentation of anime.js will be useful.

    In my example, the particles are located on an Archimedean spiral. The x and y position of a particle on the screen (aka left and top in CSS) is calculated from its position angle on the spiral:

    x=a*angle*cos(angle)
    y=a*angle*sin⁡(angle)
    

    The number of angles and thus the length of the spiral is determined by the parameter l. With the parameter a, you can control the density of the spiral.

    var container = document.querySelector(".anime-container");
    var n = 15;
    var a = 20;
    var l = 110;
    for (var i = 0; i 

    First version of our spiral (Large preview)

    This way, we get a spiral with exactly one particle per position, but a real trail effect can only be achieved if more than one particle is generated at each position. For the trail to look bushy, the positions of the individual particles must be slightly different. The anime-library provides a practical helper function for this:

    anime.random(minValue, maxValue);
    

    The size of the particles also varies randomly:

    for (var i = 0; i 

    The spiral with randomly placed particles (Large preview)

    Here you can play around with the intermediate result:

    See the Pen [anime js particles wip](https://codepen.io/smashingmag/pen/JjdqBve) by Anna Prenzel.

    See the Pen anime js particles wip by Anna Prenzel.

    Before the animation starts, all particles have to be invisible. So I will add:

    dot.style.opacity = "0";

    Animation Of The Particles

    Basic Settings Of The Animation

    The basic settings of my animation are made up as follows:

    • The animation is to be repeated continuously (loop: true),
    • The easing is linear (but you can try different values),
    • The targets are all elements with the class “dot”.
    anime({
      loop: true,
      easing: "linear",
      targets: document.querySelectorAll(".dot"),
    });

    In the next step I will animate various CSS properties of my targets. The basic steps for CSS animation can be found in the properties chapter of the anime.js documentation.

    Animation Of Opacity

    This is what our first property animation looks like, in which all particles are slowly made visible within 50ms:

    anime({
      loop: true,
      easing: "linear",
      targets: document.querySelectorAll(".dot"),
      opacity: { value: 1, duration: 50}
    });

    And now I will finally reveal the trick that creates a spiral movement of the particles! The idea is to make the particles visible with a certain time delay (e.g. in an interval of 2ms). The particles in the middle of the spiral are made visible at first, followed by all the other particles from inside to outside. The stagger function of anime.js is perfectly suited for this. In my opinion, staggering is one of the biggest strengths of the library that allows you to achieve great effects.

    opacity: { value: 1, duration: 50, delay: anime.stagger(2) }
    

    To create the illusion of a flying trail, the particles must start disappearing slowly as soon as they have appeared. Fortunately anime.js provides a keyframe notation for properties:

    opacity: [
        { value: 1, duration: 50, delay: anime.stagger(2) },
        { value: 0, duration: 1200}
      ],
    

    Here you can see the intermediate result:

    See the Pen [anime js particles wip 2](https://codepen.io/smashingmag/pen/ZEGNjjv) by Anna Prenzel.

    See the Pen anime js particles wip 2 by Anna Prenzel.

    Animation Of Size

    My comet trail should appear larger at the front end than at the back end. For this purpose, I let the particles shrink within 500ms to a diameter of 2px. It is important to choose the same time delay as for the opacity animation, so that each particle starts to shrink only after it has appeared:

    width: { value: 2, duration: 500, delay: anime.stagger(2) },
    height: { value: 2, duration: 500, delay: anime.stagger(2) },

    Individual Movement Of The Particles

    The typical thing about a particle animation is the individual, unpredictable behavior of the particles. I finally bring the particles to life with an individual movement in the x and y direction:

    translateX: {
        value: function() {
          return anime.random(-30, 30);
        },
        duration: 1500,
        delay: anime.stagger(2)
      },
    
    translateY: {
        value: function() {
          return anime.random(-30, 30);
        },
        duration: 1500,
        delay: anime.stagger(2)
      }

    Again, it is important that the movement starts with the same time delay as the appearance of the particles.

    Furthermore, it is absolutely necessary in this case to have functions calculating the values for translateX and translateY. Here we are using the parameters as function-based parameters whose values are determined for each target individually. Otherwise all targets would be shifted by the same (albeit randomly determined) amount.

    Final Thoughts

    You can see the final result over here:

    See the Pen [anime js particles](https://codepen.io/smashingmag/pen/yLNWqRP) by Anna Prenzel.

    See the Pen anime js particles by Anna Prenzel.

    You can modify the animation to your own taste by simply tweaking all the values. I have a little tip for the final touches: Now that we are familiar with function-based parameters, the opacity animation can be improved a bit:

    opacity: [
        { value: 1, duration: 50, delay: anime.stagger(2) },
        { value: 0, duration: function(){return anime.random(500,1500);}}
    ],

    The duration before a particle disappears is now set for each particle individually. This makes our animation visually even more sophisticated.

    I hope you are now as excited as I am about the possibilities that anime.js offers for particle animations! I recommend a visit to CodePen where you can see many more impressive examples.

    Further Reading on SmashingMag:

    • Including Animation In Your Design System
    • HTML5 SVG Fill Animation With CSS3 And Vanilla JavaScript
    • Introduction To Animation And The iMessage App Store With Shruggie
    • Exploring Animation And Interaction Techniques With WebGL (A Case Study)

    (ra, yk, il)

    From our sponsors: How To Create A Particle Trail Animation In JavaScript

    Posted on 14th April 2020Web Design
    FacebookshareTwittertweetGoogle+share

    Related posts

    Archived
    22nd March 2023
    Archived
    18th March 2023
    Archived
    20th January 2023
    Thumbnail for 25788
    Handling Continuous Integration And Delivery With GitHub Actions
    19th October 2020
    Thumbnail for 25778
    A Monthly Update With New Guides And Community Resources
    19th October 2020
    Thumbnail for 25781
    Supercharge Testing React Applications With Wallaby.js
    19th October 2020
    Latest News
    • Archived
      22nd March 2023
    • Archived
      18th March 2023
    • Archived
      20th January 2023
    • 20201019 ML Brief
      19th October 2020
    • Thumbnail for 25788
      Handling Continuous Integration And Delivery With GitHub Actions
      19th October 2020
    • Thumbnail for 25786
      The Future of CX with Larry Ellison
      19th October 2020
    News Categories
    • Digital Marketing
    • Web Design

    Our services

    Website Design
    Website Design

    A website is an important part of any business. Professional website development is an essential element of a successful online business.

    We provide website design services for every type of website imaginable. We supply brochure websites, E-commerce websites, bespoke website design, custom website development and a range of website applications. We love developing websites, come and talk to us about your project and we will tailor make a solution to match your requirements.

    You can contact us by phone, email or send us a request through our online form and we can give you a call back.

    More Information

    Digital Marketing
    Digital Marketing

    Our digital marketeers have years of experience in developing and excuting digital marketing strategies. We can help you promote your business online with the most effective methods to achieve the greatest return for your marketing budget. We offer a full service with includes the following:

    1. Social Media Marketing

    2. Email & Newsletter Advertising

    3. PPC - Pay Per Click

    4. A range of other methods are available

    More Information

    SEO
    SEO Services

    SEO is an essential part of owning an online property. The higher up the search engines that your website appears, the more visitors you will have and therefore the greater the potential for more business and increased profits.

    We offer a range of SEO services and packages. Our packages are very popular due to the expanse of on-page and off-page SEO services that they cover. Contact us to discuss your website and the SEO services that would best suit to increase your websites ranking.

    More Information

    E-commerce
    E-commerce Websites

    E-commerce is a rapidly growing area with sales online increasing year on year. A professional E-commerce store online is essential to increase sales and is a reflection of your business to potential customers. We provide professional E-commerce websites custom built to meet our clients requirements.

    Starting to sell online can be a daunting task and we are here to make that journey as smooth as possible. When you work with Cunningham Web Solutions on your E-commerce website, you will benefit from the experience of our team and every detail from the website design to stock management is carefully planned and designed with you in mind.

    More Information

    Social Media Services
    Social Media Services

    Social Media is becoming an increasingly effective method of marketing online. The opportunities that social media marketing can offer are endless and when managed correctly can bring great benefits to every business.

    Social Media Marketing is a low cost form of advertising that continues to bring a very good ROI for our clients. In conjuction with excellent website development and SEO, social media marketing should be an essential part of every digital marketing strategy.

    We offer Social Media Management packages and we also offer Social Media Training to individuals and to companies. Contact us to find out more.

    More Information

    Cunningham Web Solutions
    © Copyright 2025 | Cunningham Web Solutions
    • Home
    • Our Services
    • FAQ's
    • Account Services
    • Privacy Policy
    • Contact Us