Right now you’ll need the Web Animations API polyfill to use the code above, though, but it’s pretty lightweight and does make life a lot easier!
If you want a more “in-production” context for FLIP check out the source for cards on the Chrome Dev Summit site.
There is a window of 100ms after someone interacts with your site where you’re able to do work without them noticing.
It’s absolutely superb for times where you are responding to user input and then animating something in response. So, for example, in the case of Chrome Dev Summit, I was expanding cards that the user tapped on. Often the start and end locations and dimensions of the elements aren’t known, because – say – the site is responsive and things move around. This will help you because it’s explicitly measuring elements and giving you the correct values at runtime.
The reason you can afford to do this relatively expensive precalculation is because there is a window of 100ms after someone interacts with your site where you’re able to do work without them noticing. If you’re inside that window users will feel like the site responded instantly! It’s only when things are moving that you need to maintain 60fps.
We can use that window of time to do all that getBoundingClientRect
work (or getComputedStyle
if that’s your poison) in JavaScript, and from there we make sure that we’re reducing the animation down to nice-and-fast, compositor-friendly, look-ma-no-paints transform
and opacity
changes. (Why just those? Check out my Pixels are Expensive post.)
Animations that can be remapped to transform and opacity changes are the perfect fit.
Animations that can be remapped to transform
and opacity
changes are the perfect fit. If you’re already limiting to those properties in your JS or CSS animations then you’re probably good to go; this technique works best when you’re changing layout properties (like width and height, left and top) and you want to remap them to cheaper properties.
Sometimes you will need to rethink your animations to fit this model, and on many occasions I’ve separated and animated elements individually just so that I can animate them without distortion, and FLIP as much as possible. You may feel like that’s overkill, but to me it’s not, and for two reasons:
There are a couple of things to bear in mind if you FLIP:
I’ve come to love FLIP as a way of thinking about animations, because it’s a good match of JavaScript and CSS. Calculate in JavaScript, but let CSS handle the animations for you. You don’t have to use CSS to do the animations, though, you could just as easily use the Web Animations API or JavaScript itself, whatever’s easiest. The main point is that you’re reducing the per-frame complexity and cost (which normally means transform
and opacity
) to try and give the user the best possible experience.
I have more to tell you about FLIP and a broader, unified model of performance, but that’s going to be the next blog post or so I’d say!
So, uhh, FLIP on.