visit
In this post I’ll implement a more elaborative animation using a more advanced layout. The check-mark animation in the previous post is more one-dimensional. There the animation steps occurred in sequence. Here more things are happening at the same time. This one is for marking tweets as a favourite: . This is a beautiful animation designed by a designer with an eye for visual details. To recreate it I’ll use a screen-capturing tool to record it off the screen. This will allow me to dissect and set the timing of the different parts. As in the previous article, let’s start out with the end result. Then let’s break them down into steps discussed one by one. Here’s the result:
Twitter-fav Using a screen-capture tool is a great way to break down the anatomy of complex animations. It lets you stop and pause, seek to certain positions and reduce the frame rate. Doing so helps you to get a deeper understanding of how to build animations in general. If you’re in to details then make sure you use a screen recorder showing milliseconds on playback. With milliseconds you’ll be able to recreate the timing with more fine grained precision.
To recreate the whole thing I’m using a combination of HTML, CSS, SVG and Javascript. Noteworthy here is that CSS and SVG are overlapping technologies. Some things that you can create in either one of these. I tend to, as a rule of thumb, use CSS for simpler shapes. I find the CSS syntax is generally easier to work with. SVG is, in some other cases, better suited. Some things are hard to do right now in CSS like e.g. Bezier curves. All in all, it depends on the context when to use one over the other. To me, there is no clear rule anno 2017 when to use which. Sometimes I use one, sometimes the other and sometimes I use them combined.
When recreating animations it is generally good to break it down into smaller parts. Creating these parts one by one makes it easier to keep focus. This will also make the process seem less overwhelming. Actually, a bit like the composition of this article. To me this animation is best divided up into the following parts:
The above looks like the below in code (omitting the outer svg node). The ‘c’ found early on in the d attribute string tells us that is a Bezier curve.
Downscaling the star This step should be pretty straightforward by now. Once again, the class name of the node creates a relation between the DOM node and its animation. The star scales from 1 (visible) to 0 (completely gone) in a second. Note the easing. It’s set to “in” to make star to speed up when downscaling. Next up is creating the halo which is trickier. To create the halo let’s use two plain circles. One inner and one outer. The outer circle is opaque to set the halo color. The inner circle needs to be transparent to display other elements inside the halo. Note: The halo animation starts before the downscaling of the star ends. Thus, it’s important that elements inside the halo are visible. A SVG mask is a convenient way to create transparent parts in SVGs. SVG masks are more in detail discussed here: . In essence you create a binary (black and white) masking image that you combine with other SVG content. The mask will then punch a hole through the opaque object it’s applied to. Other objects below and inside that particular object will now be visible. Let’s have a look at what this looks like in code:
The first thing to note here is the last element. This is the outer circle, favorite__halo-outer. It’s as a regular SVG circle element with proportions, colors and a mask attribute. The masking syntax here is a bit weird as it refers to the mask through a url and its id. Anyway, it points to the mask element, the favorite__halo-mask. Which, in turn, contains the inner circle node, another SVG circle. This in total gives us the two circles referred to above. In essence we now have everything we need to create this part of the animation. Also worth mentioning is the white rect inside the mask. This rect marks the area which should stay intact. I.e. everything outside of the inner circle. Failing to include this node will make the mask not to work.
As a starting point the inner circle is completely gone (radius r = 0px). Here, once again let’s apply the concept of offset and delay when upscaling the circles. This is where the upscaling of the inner circle is a bit delayed compared to when the outer circle starts. Doing so will create the effect where the halo is getting thinner and thinner. In code:
Halo animation (Slow motion) Moving on to the sprinkles, the splash, the extra sugar on the top.
Disney’s Goofy Splashes are no new concept when it comes to cartoons and animations. They are often used to exaggerate motion and to reinforce actions surrounding them. This animation is no different. Here the splash emphasizes and underlines the user action. The UI gives positive reinforcement to the user, creating good UX. This moment of charm will encourage the user to mark more tweets as favourites. Generally this will lead to an increase in user engagement. There are five splashes in total and they’re positioned by even angles. They are in essence the same animation running in five different places at the same time. Reusing the same animation here is the quickest way to recreate the sprinkles part. All we need to do is to create one of the sprinkles. Repeat that animation 5 times. And, finally, spread out the 5 duplicated animations by even angles. In HTML:
The animation itself consists of 3 parts. They are: A crossfade (opacity), stretching the circle and moving the circle. To stretch the node I’m using a one dimensional scale. I noticed when writing this article that I’ve been a bit sloppy. I used the CSS height rule to stretch the sprinkles. One should use scale rather than width/height to change elements proportions. Not doing so will generally be slower. This since translate/scale/rotate are hardware (GPU) accelerated. As with all optimizations, apply them where needed and follow the guidelines. Below is what the whole sequence looks like:
Sprinkles The last and final part is the reverse of the first step. This is where the star once again scales up. Now, in its active color. I won’t cover this part as it is the same as the first step. Voila, that’s it! Below is the source code on codepen.