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.
From the GreenSock website
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.
What's the JAMstack?
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 »