xBlog

BLOG

Web Animations Made Easy With Lottie

Web Animations Made Easy With Lottie
Mohamed Ali Bouhoum

Mohamed Ali Bouhoum

12 May, 2021 Β· 4min πŸ“–

Your beginner handbook to make and add animations to your web app step by step.

The struggle to write an introduction of an article is probably harder than writing endless lines of css to animate a simple svg illustration. Web animations are satisfying and fun to use, they make users more engaged with your app. But they always come with a price, either time consuming to make with pure CSS, animate an SVG, or use a sprite sheet as twitter did for their heart animation.

Twitter's heart sprite sheet

Imagine if you can open after-effects, import your SVG, animate it, export it, then simply import it to your web app as an animated svg! Now it is possible with Airbnb's library Lottie for Android, iOS, Web, and Windows. But of course, as usual nothing comes without a price πŸ€·β€β™‚οΈ

In this article we will see how to make an animation on After Effects and how to add it to your web app. You can always skip to the second part where you we add a Lottie file to your web app.

I will be using a synth wave design example from the upcoming xHub's website, stay tuned!

1- Creating the animation in after effects

In order to export an animation from after-effects you will need to install bodymovin, refer to Lottie's documentation for how to install.

The website requires a moving synth wave road that loops in the background. Unfortunately the animation we had was a gif, so we needed to recreate it on after effects, main reason why I am writing this section 🎰

Synth Road of the website

Open After Effects

I assume you have installed bodymovin (will be available under window/extensions/bodymvin).

In composition settings set the size to 1920 x 253, or the size you want to set your own animation.
To make the animation easy, I have used the pen tool to draw the vertical lines, with a background of #591445.
Primitive yes, but Lottie has a lot of limitation (the price).

Background and Vertical lines

Then add horizontal lines using the rectangle tool. I have added 8 rectangles so I can animate them using position keyframes

Final Illustration

Add position keyframes to your animation, and make sure the last frame has the rectangles in the same position of the first frames so the loop looks real

Final Animation

Once you feel comfortable with your animation, go to window/extensions/bodymovin
Once you open it, you will see your composition name listed on the table as seen below, mine is Comp 1.

Bodymovin

Click on the 3 green buttons next to your comp name, and select where you want to save your json file. Once done, select the composition by clicking on the radio button under Selected and press render.

Once finished, press done then press the preview button to see if your animation works as expected

Bodymovin animation preview

If you have reached this far, then you are ready to move to the next (js) section, how to add this Lottie file to your web app

Lottie Limitation

Lottie unfortunately doesn't support all the sweet nice features after-effects provides, I am pretty sure someone will be like, why didn't you use the grid and animate the offset? It is because the limitations πŸ€·β€β™‚οΈ

  • Lottie does not yet support expressions, or any effects from the effects menu
  • No Blending modes or Luma mattes
  • No layer styles
  • And maybe some you will fin out by yourself

Check their documentation for more information

2- Adding The animation to your web app

If you have skipped the first part, fear not! You still don't need to learn how to make an animation to use one.
Thanks to the community, you can simply download ready animation files for free or paid on LottieFiles

For this article I will be using Next Js, but you can use anything else you want.

We will be using react-lottie to import and show our animation

npm install react-lottie

Create a js file and import react & react-lottie

import React from 'react'
import Lottie from 'react-lottie';

const Animation = () => {
  const defaultOptions = {
    loop: true,
    autoplay: true, 
    animationData: require('../public/lottie/synth.json'),
    rendererSettings: {
      preserveAspectRatio: 'xMidYMid slice'
    }
  }

  return (
    <>
      <Lottie options={defaultOptions} /> 
    </>
  );
};

export default Animation;

We first give options to the <Lottie> Components

loop options [default: false]
autoplay options [default: false]
animationData required
rendererSettings required

If you want to add a className, you have to add it under the rendererSettings

rendererSettings: {
      preserveAspectRatio: 'xMidYMid slice',
      className: 'w-screen mix-blend-luminosity transform scale-125'
    }

For example here I needed to use tailwind blending mode classes to achieve same look in the first picture

And this is the final results using Lottie

Lottie on web

With this I believe you are ready to rock some animations πŸ₯³

signature

Mohamed Ali Bouhoum has no other posts

Aloha from xHub team πŸ€™

We are glad you are here. Sharing is at the heart of our core beliefs as we like to spread the coding culture into the developer community, our blog will be your next IT info best friend, you will be finding logs about the latest IT trends tips and tricks, and more

Never miss a thing Subscribe for more content!

πŸ’Ό Offices

We’re remote friendly, with office locations around the world:
🌍 Casablanca, Agadir, Valencia, Quebec

πŸ“ž Contact Us:

🀳🏻 Follow us:

Β© XHUB. All rights reserved.

Made with πŸ’œ by xHub

Terms of Service