in

How to Create Jaw-Dropping Animated SVGs in 2023

default image

Hey friend! Have you heard about the power of animating SVGs? As a fellow tech geek, I’m pumped to show you how to take your SVG game to the next level with motion and interactivity this year.

Scalable Vector Graphics (SVGs) are incredible – they‘re like responsive, editable code images! But most developers still treat them as static assets. What a missed opportunity!

Adding tasteful animations takes SVGs from meh to WOW. I‘ll walk you through everything you need to create jaw-dropping animated SVGs from scratch in 2023. Let‘s do this!

Why Animate SVGs Anyway?

"Nice graphics, but why animate them?"

Great question! Animated SVGs create more immersive, engaging user experiences. Subtle motion guides users, visualizes concepts, and brings branding to life.

Let me share some killer examples:

  • Animated data visualizations clearly show complex insights through motion.
  • Animated logos boost brand memorability by up to 55% [1].
  • Illustrated concept explainers bring boring topics to life with animation.
  • Smooth loading animations improve perceived performance.

See what I mean? With some creativity, you can use SVG animation to:

  • Improve UX – Guide users, show relationships, provide feedback.
  • Increase accessibility – Motion conveys meaning for those with disabilities.
  • Boost conversions – Engaging motion persuades users to take action.
  • Reinforce branding – Animated logos are more memorable and sharable.
  • Explain complex topics – Animations clarify abstract ideas and data.
  • Delight users – Unexpected motion sparks joy and delight.

Animated SVGs level up user experiences when applied judiciously. Let‘s explore how to create them!

SVG Animation Methods

There are 3 main approaches to animating SVGs:

  1. SMIL – Native SVG animation markup.
  2. CSS – Animate with CSS transforms, transitions, etc.
  3. JavaScript – Use JS libraries like Snap.svg, Anime.js, Vivus, etc.

I‘ll summarize the pros and cons of each option:

1. SMIL Animation

SMIL (Synchronized Multimedia Integration Language) enables animation directly within SVG files using <animate> tags.

For example:

<rect x="0" y="0" width="50" height="50">
  <animate attributeName="x" to="500" dur="2s" fill="freeze"/> 
</rect>

Animates the x position from 0 to 500 over 2 seconds.

Pros

  • Lightweight, native SVG method
  • Declarative animation syntax
  • Works without JavaScript

Cons

  • Browser support is incomplete
  • Complex syntax and markup
  • Limited animation options

Verdict: Excellent for simple animations on supported browsers.

2. CSS Animation/Transitions

By animating SVG attributes with CSS, you gain access to declarative animations using @keyframes, transitions, and more.

For example:

@keyframes slide {
  to {
    transform: translateX(500px);
  }
} 

rect {
  animation: slide 2s forwards;
}

This animates an SVG <rect> element using CSS.

Pros

  • Familiar web animation model
  • Centralized control in CSS files
  • Hardware accelerated performance
  • Excellent browser support

Cons

  • Not as lightweight as native SVG methods
  • Can cause class name conflicts
  • Limited to styling/transform changes

Verdict: A great pick for reusable SVG animations on modern websites.

3. JavaScript Animation Libraries

For maximum control, JavaScript animation libraries like Snap.svg, Anime.js, Vivus, and GreenSock can animate SVGs programatically.

For example, with Snap.svg:

var s = Snap(‘#svg‘);

Snap.animate(0, 500, function(val) {
  s.rect.attr({ x: val}) 
}, 2000)

This animates the x position of an SVG <rect> from 0 to 500px over 2 seconds.

Pros

  • Full programmatic control over animations
  • Integration with other JS behavior
  • Animate complex sequences
  • Access to script events/callbacks

Cons

  • Requires managing external libraries
  • JavaScript has performance overheads
  • More complex setup than CSS or SMIL

Verdict: Best for complex, sequenced animations activated through code.

So those are the top 3 approaches! Each has strengths for different use cases.

Now let‘s walk through building a real animated icon to see SVG animation in action.

Animated SVG Icon Case Study

To demonstrate, we‘ll build an animated SVG icon for a weather app using CSS animation.

Here are the steps:

Step 1: Export an SVG Icon

I‘ll start by exporting a "sunny" icon SVG from Figma – any vector tool like Sketch or Illustrator works too.

Here‘s the initial SVG code:

<svg>
  <g id="sun">
    <!-- sun shapes -->
  </g>

  <g id="rays" >
    <!-- sunray shapes -->
  </g>
</svg>

It has two layers – the sun and sunrays. This will allow us to animate them separately.

Step 2: Clean Up and Optimize the SVG

Next, I‘ll optimize the SVG by:

  • Removing unnecessary metadata and IDs
  • Adding a viewBox attribute for responsive sizing
  • Running it through an SVG optimizer like SVGOMG

This produces clean, compact SVG markup ready for animation.

Step 3: Import SVG and Add Styles

Now I‘ll add the optimized SVG directly into my HTML:

<div class="icon">
  <svg viewBox="0 0 96 96">  
    <!-- cleaned SVG contents -->
  </svg>
</div>

I‘ll also create some CSS styles for sizing and coloring:

.icon {
  width: 100px;
}

svg {
  width: 100%;
  fill: #FDC830; 
}

This sets up the SVG with proper sizing and colors.

Step 4: Animate with CSS

Here‘s the fun part – animating!

I‘ll use CSS @keyframes to animate the sun growing and shining:

/* Sun pulse animation */
@keyframes sunPulse {
  0%, 40% {
    transform: scale(1);
  }
  60% {
    transform: scale(1.1);
  }
}

#sun {
  animation: sunPulse 3s ease-in-out infinite;
}

And fading rays with opacity:

/* Rays fade animation */ 
@keyframes raysFade {
  0% {
    opacity: 0.2;
  }
  50% { 
    opacity: 1;
  }
  100% {
    opacity: 0.2;
  }
}

#rays {
  animation: raysFade 2s ease-in-out infinite;
}

The CSS animation property ties the @keyframes together for reusable animations. Sweet!

Step 5: Optimization and Export

Finally, I‘ll optimize the animated SVG by:

  • Removing any unnecessary precision from SVG data points
  • Using CSS transforms for smooth animation instead of redrawing paths
  • Defining reusable gradients in <defs> instead of duplicating
  • Keeping animation minimal and purposeful

And that‘s it – a production-ready animated SVG icon created with standard web technologies!

Here‘s the sunny icon in action:

The complete code for this demo is available on CodePen.

Let‘s move on to some tools that can speed up SVG animation!

Tools for Visual SVG Animation

Writing SVG animation code can be tedious. Thankfully, designers have created fantastic tools for animating SVGs visually without coding skills.

Here are 5 of my favorites:

1. SVGator

SVGator provides a simple visual timeline for adding animations to SVGs. Just click elements and add pre-made effects – super beginner friendly!

It exports clean SVG/JS code to easily integrate graphics into sites. At $6/month for full access, SVGator is an awesome bang-for-buck tool.

2. Vivus

Vivus is an open source JavaScript library that allows animating SVGs declaratively through code.

It offers options like sequential path drawing, delays, easings and more. I especially like using Vivus for animating SVGs within React apps.

3. Adobe After Effects

While overkill for simple icon animations, After Effects makes it easy to create complex, multi-step SVG animations with its robust timeline tools.

The integration with other Creative Cloud apps like Illustrator helps mainstream designers work more comfortably with SVG animation.

4. Haiku Animator

Haiku Animator lets developers quickly build production-ready animations with proprietary features like scripting timelines and automatic device integration.

I love using Haiku to hand off polished animated SVGs to engineering teams. They make implementation so much easier.

5. Framer

Framer‘s visual timeline editor and large component library enable prototyping complex animated interfaces without code.

While not SVG-specific, Framer can help animate vector graphics for sites and apps via generated React components.

There are tons of other great tools available too – see what fits your needs!

Performance Tips and Tricks

To ensure flawless performance, keep these tips in mind when optimizing animated SVGs:

  • Simplify paths – Shave off unnecessary data points using SVGO or SVGOMG.

  • Use CSS transforms – Opt for rotations, translations and scaling over path redraws.

  • Limit precision – Round values to a max of 4 decimal points.

  • Avoid filters – Effects like <feGaussianBlur> are expensive.

  • Animate opacity – Smooth and cheap for fading elements in/out.

  • Reference don‘t duplicate – Use <defs> for reusable gradients etc.

  • Enhance purposefully – Use animations only when they add real value.

Following best practices results in buttery smooth 60fps animations on both desktop and mobile. Perceived performance matters!

Key Takeaways and Resources

Let‘s recap what we learned:

  • Why animate SVGs? Visual appeal, usability, branding, explanation, delight!

  • How to animate? CSS, JS libraries, SMIL, and visual tools are all options.

  • Performance matters Follow optimization guidelines for smooth animations.

To learn more, check out these pro tips from Google‘s SVG guru Sarah Drasner:

Codepen also has some awesome demos for SVG CSS animation and SVG JS animation.

I hope this guide gets you excited to start animating SVGs in your own projects. Feel free to reach out with any other questions my friend!

Let‘s create some magic in 2023.

Written by