- 10 Principles for smooth Web animations
- By Anand Sharma
- The Nuggets translation Project
- Translator: Wang Zijian
- Proofreader: Scarecrow, Gocy
Since we released Gyroscope last year, a lot of people have asked us what JavaScript library we use for animation, and we’ve thought about making it public, but that’s not really the trick.
We don’t want people to feel like they need to rely on a special dark magic JavaScript plugin to solve their problems. Most of the time, we just need to take advantage of the latest browser and GPU performance and CSS3.
There is no magic formula for animation, the only way is to spend a lot of time testing and optimizing. However, after years of experimentation and testing the limits of browser performance, we have found that there are a few design and coding principles that can be used to improve animation performance. These tips will keep your pages flowing and running in popular browsers on both desktop and mobile devices, and most importantly, they’re easy to maintain.
The technology and implementation may vary from person to person, but the principle of universality can be almost universal.
What is animation?
Animation was around long before the Internet was invented, and it probably took you a lifetime to learn how to make it brilliant. However, implementing animations on the Internet has its own unique limitations and challenges.
To achieve smooth 60 frames of animation, each frame needs to be rendered in 16 milliseconds! Time was short, so we needed to find the most efficient way to render each frame in order to achieve smooth performance.
Some classic principles of animation design
There are many different ways to achieve animation on a website. For example, film, which was ubiquitous before the Internet, uses hand-drawn, graded film to create the illusion of animation by playing multiple frames per second.
Twitter took advantage of this approach in the recent heart-shaped animation, using film to draw a moving Sprite.
This effect could also be achieved with many separate small element animations, or in SVG, but that would be too complex and probably not as smooth.
Many times, you’ll want to use CSS toggling properties to automatically animate elements. This technique is called “tweening” because it switches between two different property values. And tweening was set to be_tween_ two different values. The nice thing about it is that it is very easy to cancel or replace without having to reconstruct the logical content, which is perfect for once-and-for-all animations like intro prologues, or simple interactions like mouse hover.
More: All You Need to Know about CSS Transitions
Other times, keyframe-based CSS animation properties can be a good fit for changing background elements. For example, the ring button in a gyroscope rotates continuously as preset, and there are other types that can take advantage of CSS animations such as gears.
In case you don’t have to worry about it, here are some tips that will greatly improve your animation:
1
Even if you think it’s possible, don’t do it!
Eighty percent of optimizations in animation use this basic principle, even on mobile. You may have heard this rule before, it’s not my idea, but few people follow it. Like “Stop talking and start moving,” this is good advice and one of the easiest to ignore.
This is very simple for those who are used to this idea, but for those who are used to doing animations with traditional CSS properties, it’s a big leap forward.
For example, if you want to make an element small, you can use transform: scale() instead of changing the width; If you want to move it around, you can use a simple transform: translateX or transform: translateY instead of a messy margin or padding — the layout of the page that needs to be reconstructed for each frame.
Why do you do that?
For humans, changing widths, padding, or other attributes isn’t a big deal — even more so because it’s easier — but for computers, it feels like the sky is falling, or worse.
Browsers put a lot of effort into optimizing these operations, and transform is really easy and efficient, and takes full advantage of the graphics card without having to re-render elements.
The first time you load a page, you might feel crazy – dealing with all the rounded corners, introducing images, adding shadows to everything, or even doing a dynamic feathering if you don’t care. If this happens only once, a little more computation time doesn’t matter. But once the content is rendered, you never want to reload!
Moving Elements with Translate (Paul Irish)
Hide content in a very clear way
Use the pointer-Events attribute: Hide elements with transparency only
There may be cross-browser warnings, but if you’re only targeting WebKit and other popular browsers, it’ll give you a leg up.
Long ago, animation effects had to be handled by jQuery’s animate() method, and much of the complexity of fades in and out effects was done by switching display property values. Too early and the animation is not finished, but too late and the page is blank, always needing callback functions to clean up after the animation.
The pointer-events property in CSS (which has been around for a long time, but is not often used) simply renders elements unresponsive to clicks and interactions as if they didn’t exist. It can be shown or hidden via CSS without breaking the animation or affecting the rendering or visibility of the page.
Aside from setting opacity to zero, it has the same effect as setting display to None, but does not trigger a new rendering mechanism. If an element needs to be hidden, I set its opacity to 0 and pointer-events to off, and let it go on its own.
This is especially true for absolutely positioned elements, because you can be confident that they won’t affect any other elements on the page.
It’s also a bit off the mark, because the timing of the animation isn’t always perfect — for example, an element can be clicked or overwritten without being visible, or only clicked when the element fades to full display, but don’t lose heart, there will be a solution. (See below for solutions.)
Don’t animate everything at once
Use choreography instead
A single animation can be smooth, but with many other animations it can be completely confusing. It’s easy to write an example of a smooth full-member animation, but it’s hard to maintain performance when orders of magnitude scale up to the entire site. Therefore, it is important to arrange each element well.
You need to schedule all the time points so that all the animation doesn’t start or play at the same time. Typically, two or three animations running at the same time may not lag, especially if they start at slightly different times. But beyond that, the animation may lag.
It’s important to understand the concept of choreography unless your page really only has one element. It seems to be the domain of dance, but in animation it’s just as important. Everything has to go in the right direction and at the right time, even if they’re separate, but they have to feel like they’re on their way.
Google’s Material Design has some interesting suggestions for motion choreography, and while it’s not a sure-fire way to achieve your goals, there are a few things you should consider and try.
More: Google Material Design · Motion
4
Animation is very important, and there is a lot of experimentation and testing to get it right. However, the animating code is not very complex.
I usually change the class value of a parent element (usually body) to trigger a series of changes with varying switching delays so that they can be displayed in time. From the code alone, you only need to worry about state changes, not about maintaining a bunch of time nodes.
Gyroscope Chrome Extension animation
Interleaving a series of elements is an easy way to animate, and it works because it works well and looks good at the same time — but remember that you want several animations to happen at the same time. You want to spread out the animations so that each one behaves smoothly, rather than having too many animations at once and being too slow. The appropriate overlap will look like a continuous flow rather than a chain of separate animations.
Code sample
There are some easy tricks to stagger your elements — especially if there’s a lot of content in them. If the page has less than 10 items of content, or if the number of elements is predictable (like a static page), I usually specify specific values in the CSS. It’s the easiest thing to do.
A simple SASS loop
For more content or dynamic content, you can dynamically add time nodes to each item in a loop.
A simple JavaScript loop
There are two typical variables: the base delay and the delay for each item. It’s hard to coordinate, but once you find the right value, it works perfectly.
5
Then speed up the animation
In animation design, time nodes are everything. Twenty percent of the work is to achieve the effect, and the remaining 80 percent is to find the right parameters and duration to make everything flow when it happens simultaneously.
Especially when composing multiple animations, it’s easy to watch them in slow motion for high performance and high commonality.
Whether you’re using JavaScript or a CSS preprocessor like SASS (which we love), it’s easy to do some extra calculations and declare some useful variables.
You have to make it very easy to try different speeds or time points. For example, if an animation stutters at 1/10 of its speed, there may be some very basic errors. If you’re fluent at 50 times slower, you’ll find the maximum speed you can run smoothly at over time. A five-millisecond difference at normal speed might be hard to notice, but slow it down and it becomes very noticeable.
Especially for very complex animation analysis, or solving tricky performance bottlenecks, looking at elements in slow motion can be very useful.
The important thing is that in slow motion you get a lot of detail perfect, and when the animation is accelerated it feels flawless. While these may seem trivial, users will notice the smoothness and detail of the animation.
OS X only feature – if you shift + click the Minimize button or an app icon, you’ll see it move slowly. With this in mind, we even implemented this feature on the gyroscope, which activates the slow motion mode when you press the Shift key.
6
Sometimes a different perspective can help you see things more clearly, and video is a great way to do that.
Some people make videos with AE and put them on the site, but I do the opposite. I always try to record great videos on the site interface.
The bar for Posting a video is pretty high. One day I was so excited about what I had created that I wanted to record it and share it with my friends.
However, on the second look, I found some flaws, the timing nodes were not set properly and there was a delay spike. That kind of freaked me out a little bit, and I realized that there was a lot of content to polish, so I couldn’t just send it to my friends.
These flaws are easy to hide in the process, but watching the slow-motion animation again and again in the video makes everything very obvious.
Some would say it’s not exactly what it looks like, but maybe it’s more accurate.
It’s become a big part of my job, watching slow-motion videos and fixing anything I don’t think is right. It’s easy to blame poor browser performance for these problems, but with a little more optimization and testing, they can be fixed.
When you don’t notice any awkward latency spikes in the video and feel good enough to post, your page is ready to publish.
7
You should either preload or defer processing very large HTTP requests
Images are one of the culprits, whether it’s a few large images (a large background) or a lot of small ones (50 avatars), or a lot of content (a long page with a lot of images from beginning to end).
When the page first loads, many things are initialized and downloaded. Content parsing, advertising, and other third-party scripts can make performance worse. Sometimes, delaying animation effects by a few tenths of a second after the page loads can make a big difference in performance.
Don’t over-optimize animation delays if you don’t have to, a complex page requires very precise delays and time nodes to run smoothly. Usually you want to load as little data as possible at the beginning, and then load the rest of the content after the main content and intro animation is complete.
A page with a lot of data needs to load everything thoughtfully. An animation that works well on a static page may be slow to load real-time data. If something seems like it should be working but doesn’t, or doesn’t flow as smoothly as it should, I recommend checking your web activity to make sure you’re working on something else as well.
8
Sounds like a good idea, but it’s not
Scroll-based animation has been very popular for a while, especially with parallax and other special effects. Whether their design patterns are good or bad remains to be seen, but there are mixed ways to implement them technically.
A very popular way of doing this in scroll-based animation is to scroll a certain distance as an event that simultaneously triggers the animation content. Unless you know your behavior well, I would advise against using this approach because it’s really error-prone and difficult to maintain.
Even worse is customizing the scrollbar functionality instead of the default – aka scrollJacking. Please don’t take it too hard.
Of these ten principles, this one is particularly applicable to mobile development, and is probably a good practice for the ideal user experience.
If you really want a unique experience and you want it to be based on scrolling or other special events, I recommend creating a quick prototype for that rather than trying to design the event form.
9
Most web sites are built on computers, and the most commonly used testing machine. As a result, mobile experience and animation performance are secondary concerns. Some technologies (such as Canvas) or animation technologies may not work well on mobile.
However, if the code is well written and the optimizations are in place (see Rule #1), the mobile experience can be even smoother than the PC experience. Mobile optimization is a tricky business, but the new iPhone is faster than a laptop! If you follow the first few tips, you’ll get a great mobile performance.
Mobile access to websites is going to be very, very important. I recommend that you set aside a week to seriously check your site on your phone. This might be extreme, and you might feel like you’re being punished for using the mobile version, but you should adjust your mindset.
Continue to optimize design and performance until the site looks as beautiful and convenient on mobile as it does on computer.
If you visit your site on mobile for a week, you’ll have a much better experience than you would on your computer. It’s worth having annoying things happen, which means they’ll be solved before your users experience them!
10
Different screen sizes, resolutions, or styles of devices
There are many factors besides mobile and computer that can have a huge impact on performance, such as whether the screen is “Retina”, the resolution of the window, how old the hardware is, and so on.
Even though Both Chorme and Safari are WebKit-based browsers with similar syntax, they have their own unique features. Every Chrome update fixes some problems and introduces new ones, so you must always be on your guard.
Of course, you don’t just want to build a one-size-fits-all website for all browsers, so it’s useful to find flexible ways to add or remove features.
I usually alternate between using the site on a smaller MacBook Air and a larger iMac, and each time new issues are revealed and fixed — especially animation performance issues, but sometimes global design issues, information density issues, readability issues, and so on.
Media Queries is a very powerful tool. It is typically used to locate style differences due to height or width, but it can also be used to add target content or other attributes based on resolution. In addition, the ability to identify the system and device type is very useful, because the performance characteristics of mobile devices are very different from those of computers.