Our WorkAbout UsOur ApproachThe Journal
Contact »
« Back to the journal
Frontend Development + Animation + PHP, Javascript & React

SVG Morphing

By:

Nick Stewart
Nick Stewart

on 6/12/2019

SVGs are awesome – they can be targeted and customized, animated, styled and always stay great looking. Animating them can sometimes be a pain, especially when you want to “morph” one SVG into another. However, the GreenSock library is here to help.

GreenSock

GreenSock is a JavaScript library that creates “ultra high-performance, professional-grade animation for the modern web” . With it, you can create all kinds of effects with ease, such as having 3D transformations, a range of animations, and even casting multiple shadows to text.

From the GreenSock website

Think of GSAP as the Swiss Army Knife of javascript animation…but better. It animates anything JavaScript can touch (CSS properties, canvas library objects, SVG, generic objects, whatever) and it solves countless browser inconsistencies, all with blazing speed (up to 20x faster than jQuery), including automatic GPU-acceleration of transforms. Most other libraries only animate CSS properties. Plus, their sequencing abilities and runtime controls pale by comparison. Simply put, GSAP is the most robust high-performance JavaScript animation library on the planet, which is probably why Google recommends it for JS-based animations and every major ad network excludes it from file size calculations. Unlike monolithic frameworks that dictate how you structure your apps, GSAP completely flexible; sprinkle it wherever you want.

MorphSVG

Greenstock offers different plugins to extend the base functionality, such as TextTween and ScrollTo. One of the plugins is MorphSVG, which can animate SVG paths to have a smooth transition between SVG elements. It works by “adding enough points to … get a super smooth morph. It will rip through all that ugly path data, convert everything to cubic beziers, and dynamically subdivide them when necessary, adding points so that the beginning and ending quantities match (but visually it looks the same)”.

How To Use

Using MorphSVG is pretty easy – just pass the IDs of the SVG paths that you want to morph (you can also pass polygon points and path strings).

One thing to note is that you can only morph paths and polygons, however MorphSVG can convert other elements (such as circles) into paths. To do this, it’s simply just passing an ID to convertToPath – pretty easy!

The full docs on MorphSVG can be found here.

How We Used It

In our project, The Curious Case of Benjamin Button, we used MorphSVG to animate the different faces of the character Benjamin.

Our design team created a series of SVG illustrations that represented the age of Benjamin in each chapter of the book. Every animated component (facial features, hat, etc) is in it’s own SVG path element; Green Sock allowed us to morph the shape of these paths to the corresponding path in the next illustration

We are passing on the next SVG path (tag) along with animation time (timeScale – “factor that’s used to scale time in the animation”, its comes with GreenSock).

The SVG of a face kinda look like this, with all the different facial features being different paths and sections, all with IDs so they can be targeted.

This all can be used to where the face goes from stage one to stage two and the elements morph during the transition, all thanks to MorphSVG and GreenSock.

Share to

Related Posts

Websites
What's the JAMstack?

By:Alec Robertson

As a front-end developer in 2020, it is time that I fulfill the FEDeral mandate that I publish words regarding the JAMStack. If you follow too many developers on Twitter, it’s likely you’ve heard about the Next Big Thing in web development, but I’d like to put this trend (fad? movement?) into context to better understand what’s truly new about this approach and whether it’s worth using.

Read More »
Custom Bootstrap Shortcodes for WordPress

By: Natalie Miller on 6/27/2017

Bootstrap shortcodes are a quick way to make your WordPress content more dynamic.

Read More »