<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Richard Gres</title>
    <description>The latest articles on DEV Community by Richard Gres (@r1cc1).</description>
    <link>https://dev.to/r1cc1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F573305%2F0d61a65b-6a6d-4b34-8916-e268759734dd.png</url>
      <title>DEV Community: Richard Gres</title>
      <link>https://dev.to/r1cc1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/r1cc1"/>
    <language>en</language>
    <item>
      <title>How to use Lottie animations on web</title>
      <dc:creator>Richard Gres</dc:creator>
      <pubDate>Mon, 08 Feb 2021 11:04:49 +0000</pubDate>
      <link>https://dev.to/r1cc1/how-to-use-lottie-animations-on-web-1h25</link>
      <guid>https://dev.to/r1cc1/how-to-use-lottie-animations-on-web-1h25</guid>
      <description>&lt;p&gt;&lt;strong&gt;A guide to web Lottie animations: 5 steps to implement Lottie animations on your website.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5 Steps to implement web animations
&lt;/h2&gt;

&lt;p&gt;There are many reasons why you should use Lottie animation on your website. We summarised some of them in our &lt;a href="https://drawer.design/blog/why-we-prefer-lottie-over-other-animation/" rel="noopener noreferrer"&gt;previous blog&lt;/a&gt; post, where you can further understand why we love Lottie so much. One of the benefits is its simplicity of use. If you are afraid to use Lottie animations on your website because of its implementation, don’t worry; we got your back with this simple guideline.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Step – Animation file
&lt;/h2&gt;

&lt;p&gt;To implement Lottie animation, first, you need to have the animation itself. All you need is Lottie animation, which comes as a JSON file. Your designer can create animation, or you can simply purchase it or download it for free. If you don’t have any JSON Lottie file, go ahead and check out &lt;a href="https://drawer.design/?ref=dev.to"&gt;Lottie animation Market&lt;/a&gt; or &lt;a href="https://lottiefiles.com/" rel="noopener noreferrer"&gt;LottieFiles&lt;/a&gt;, sure you find some that fit your style.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Step – Test your animation
&lt;/h2&gt;

&lt;p&gt;Before you start implementing your web animation, check if it works properly. Since opening a JSON file doesn’t tell you much, we have created a &lt;a href="https://drawer.design/preview-lottie-animation/" rel="noopener noreferrer"&gt;Preview Lottie tool&lt;/a&gt; where you can drag and drop your Lottie animations and quickly see if it’s working correctly. You can also change the background color to simulate the placement of your design better.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Step – Implement Scripts
&lt;/h2&gt;

&lt;p&gt;When you have a working Lottie file, you are ready to display it on your web with implementing scripts needed for JSON to be converted to SVG animation. It’s simple as adding any other script library to your HTML page. Here are two options you can go with.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Lottie.js &lt;a href="https://github.com/airbnb/lottie-web" rel="noopener noreferrer"&gt;github&lt;/a&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.com/libraries/lottie-web"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See a basic implementation &lt;a href="https://codepen.io/airnan/project/editor/ZeNONO/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. or See examples on &lt;a href="http://codepen.io/collection/nVYWZR/" rel="noopener noreferrer"&gt;codepen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here’s a &lt;a href="https://www.youtube.com/watch?v=5XMUJdjI0L8" rel="noopener noreferrer"&gt;video tutorial&lt;/a&gt; explaining how to export a basic animation and load it on an HTML page&lt;/p&gt;

&lt;h4&gt;
  
  
  2. LottieFiles web player &lt;a href="https://github.com/LottieFiles/lottie-player" rel="noopener noreferrer"&gt;github&lt;/a&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See a basic implementation &lt;a href="https://lottiefiles.com/web-player" rel="noopener noreferrer"&gt;here&lt;/a&gt;. or See examples on &lt;a href="https://codesandbox.io/s/y2nxyvomyj" rel="noopener noreferrer"&gt;codesandbox&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Step – Load animation in HTML DOM
&lt;/h2&gt;

&lt;p&gt;Scripts are ready, and so your JSON file; let initialize it in HTML code. By adding the path to the JSON animation file.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Lottie.js &lt;a href="https://github.com/airbnb/lottie-web" rel="noopener noreferrer"&gt;github&lt;/a&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width:1067px;height:600px"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"lottie"&lt;/span&gt; &lt;span class="na"&gt;data-animation-path=&lt;/span&gt;&lt;span class="s"&gt;"animation/your-animation.json"&lt;/span&gt; &lt;span class="na"&gt;data-anim-loop=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;data-name=&lt;/span&gt;&lt;span class="s"&gt;"ninja"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. LottieFiles web player &lt;a href="https://github.com/LottieFiles/lottie-player" rel="noopener noreferrer"&gt;github&lt;/a&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;lottie-player&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"animation/your-animation.json"&lt;/span&gt; &lt;span class="na"&gt;background=&lt;/span&gt;&lt;span class="s"&gt;"transparent"&lt;/span&gt; &lt;span class="na"&gt;speed=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 300px; height: 300px;"&lt;/span&gt; &lt;span class="na"&gt;loop&lt;/span&gt; &lt;span class="na"&gt;controls&lt;/span&gt; &lt;span class="na"&gt;autoplay&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/lottie-player&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it; you should see the animation on your page. Simple right? If you are not satisfied with the animations’ behavior, there are still a few things you can adjust.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Step – Animation adjustments
&lt;/h2&gt;

&lt;p&gt;Adjustments are possible with both solutions and quite limited but enough for you to tweak it just right.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What can be adjusted:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speed&lt;/li&gt;
&lt;li&gt;Loop&lt;/li&gt;
&lt;li&gt;Autoplay&lt;/li&gt;
&lt;li&gt;Background-color&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  BONUS – Tips for performance
&lt;/h2&gt;

&lt;p&gt;We recommend using the CDN link because it has a fast connection response so that your script doesn’t affect the speed load too much.&lt;/p&gt;

&lt;p&gt;Adding script ideally to the &lt;code&gt;&amp;lt;footer&amp;gt;&lt;/code&gt; section to prevent loading too early.&lt;/p&gt;

&lt;p&gt;Preconnect library server in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; for faster script loading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preconnect"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/"&lt;/span&gt; &lt;span class="na"&gt;crossorigin&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First prefetch JSON animation file in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; for faster loading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt; &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"fetch"&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"application/json"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/animation/your-animation.json"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Enjoy your animation!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://drawer.design/blog/how-to-use-web-animations-with-lottie/" rel="noopener noreferrer"&gt;Original blog here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>design</category>
      <category>webdev</category>
      <category>html</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
