Animating React With Finesse!

Rate this content
Bookmark

Animations are the heartbeat of captivating web experiences but wield them carelessly, and the enchantment fades into frustration. Join Nikhil on an exhilarating journey as he reveals the secrets to crafting mesmerizing, high-performance animations in your React apps.

In this dynamic talk, Nikhil delves deep into the world of animation, dissecting the common bottlenecks that threaten your app's responsiveness. Learn how to identify, debug, and eliminate these performance roadblocks, ensuring your animations are as snappy as they are spellbinding.

This talk has been presented at React Summit 2024, check out the latest edition of this React Conference.

FAQ

The speaker is Nikhil, a software engineer at Boosman.

Nikhil enjoys discussing design systems, performance, and React.

Nikhil enjoys giving talks, traveling, and playing sports, especially cricket and table tennis.

In a browser, any user interaction such as scrolling, clicking, or hovering is considered an animation.

Frame rate is important because it ensures smooth animations. The browser has 16.7 milliseconds to generate each frame to achieve 60 frames per second.

Frame drops occur when the browser's 16.7-millisecond window to generate a frame is exceeded, causing jank and performance issues.

The five phases are JavaScript evaluation, style calculation, layout, paint, and composition.

You can avoid style recalculation by using CSS properties that are less paint-intensive, such as using opacity instead of display:none.

requestAnimationFrame is more performance-efficient as it runs animations when the browser is ready to paint, reducing the likelihood of frame drops and jank.

The Web Animation API allows you to animate elements using JavaScript with similar performance benefits to CSS animations.

Nikhil Sharma
Nikhil Sharma
22 min
18 Jun, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

In this Talk, the speaker discusses how to enhance animation in React apps and optimize animation in browsers. They explain the browser's animation pipeline and the importance of avoiding frame drops. The speaker compares CSS and JavaScript animations, highlighting the benefits of using the requestAnimationFrame API. They also discuss combining CSS and JavaScript animations using the Web Animation API. The Talk concludes with tips on avoiding redundant calculations and provides additional resources for further learning.
Available in Español: Animando React con Maestría

1. Introduction to React Animation

Short description:

Welcome to React Summit. In this session, we'll explore how to enhance animation in React apps. I'm Nikhil, a software engineer at Boosman, passionate about design systems, performance, and React. Connect with me on GitHub and Twitter. Let's get started!

Hey everyone, welcome back to another session of React Summit. And in this session we'll be talking about how we can make our animation game in React apps to the next level by animating React with Finesse.

So before diving in, here's a little bit about me. I'm Nikhil and I'm a software engineer here at Boosman. I love to talk about design systems, performance, and React in general.

So if you're also a front-end geek like me, I have left my GitHub and my Twitter handles here in the slides. So feel free to connect, it would be a great chat. And other than this, I love giving talks and also I love to travel, I love to play sports, especially cricket and table tennis. I have been learning table tennis specially for quite some time and it's super fun thing. So yeah, it's going to be a good chat. So would love to connect with all of us, so that's it.

2. Understanding Animation in Browsers

Short description:

An animation is everything a user does in an app. Browsers create frames to analyze user interactions. Frame drops occur when the browser's 16.7-millisecond window is compromised. The browser goes through code, generates frames, calculates styles, determines layout, and then paints everything in pixels.

All right, so with this, let's start with our talk with a very simple statement that what exactly is an animation, right? The sentence actually answers it all. Everything that you do in an app as a user is basically an animation.

Now, you might be thinking that, oh, isn't a floating button or properties like transform, et cetera, or some carousel appearing, are all of these things animations and not other stuff? So basically, what browser understands is whatever you as a user do, be it like scrolling or you're clicking on a button or you're hovering on a button, even if it doesn't show any animation, everything is actually an animation to a browser, right?

And if you want to visualize it in your mind, it's basically like a flipbook like a browser also maintains. So each and every time you're scrolling or you're interacting with your website, your browser actually creates a set of frames for you to actually analyze what's actually happening and it just runs them all together like a flipbook sort of thing, so that you get an idea that, oh, it's an animation of things actually been moving.

Now, where the problem actually lies is when the frames that actually the browser has, if this frame cycle gets messed up in some time, that's where actually our jank happens or when the performance of animations actually drops. Now, if you try to understand what actually is a frame drop or why the frames actually drop when we're animating.

It's basically because each and every time your browser has a window of 16.7 milliseconds, how that math comes is basically what you see on the screen right here. Now, if you want to achieve 60 frames per second and you have thousand milliseconds, if you divide them both, you'll get like 60, roughly 16.7 milliseconds. So your browser actually has 16.7 milliseconds to generate each and every frame.

Now, what it actually needs to do when there is basically during this particular set of time. Right. So basically, let's take an example. I have written this two or three lines of JavaScript code here, which actually is creating, basically adding a class, which is my special box to my box. Now, let's see how the browser actually goes through and scans all of your code. So first, it actually figures out, oh, now something has changed. It encountered some JavaScript and it says, oh, I need to generate another frame. And now the 16 millisecond time starts for the browser.

Now, the next thing the browser analyzes is, oh, OK, there is some style change that is also been there in this JavaScript file, which is I'm adding some classes and I need to also figure out what these classes are doing, what's basically sort of style recalculation do I need to do. Right. So that would be a second step. So in the third step, now the browser figures out that, OK, I've calculated the styles, I have run the JavaScript. Now I need to figure out how do I render all of these boxes on the screen. Right. For this, the browser needs to know the layout. Right. What's going to be the dimensions of each of the divs that you have and how it has to be positioned in the screen. Right. So this is like that layout phase where the browser is actually trying to do those calculations.

In the fourth step, when it is finished, it tries to decide that, OK, now I have figured out the layout. I need to paint everything in in terms of pixels and for painting.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

The Whimsical Potential of JavaScript Frameworks
React Summit US 2023React Summit US 2023
28 min
The Whimsical Potential of JavaScript Frameworks
Top Content
Watch video: The Whimsical Potential of JavaScript Frameworks
The speaker discusses the whimsical and detailed work of Stripe, particularly their interactive and dynamic pages. They explain the use of React for building whimsical details and tracking mouse position. The speaker introduces React Spring for smooth animation and React3 Fiber for creating a 3D egg model. They also mention the use of Framer Motion and React server components for animating CSS properties.
Emotional & Functional UI Animations in React
React Day Berlin 2022React Day Berlin 2022
28 min
Emotional & Functional UI Animations in React
Today's Talk discussed the importance of UI animations in React, both functional and emotional. Examples were given using CSS and Framer Motion, with a focus on user feedback and accessibility. Design guidelines and animation usage were highlighted, as well as the consideration of negative emotions in animations. The Talk also touched on designing 404 error pages and concluded with gratitude to the audience and organizers.
Keep Scrolling
JSNation Live 2021JSNation Live 2021
33 min
Keep Scrolling
Scroll animations can enhance user experience when done properly, but should not distract from important information. CSS, JavaScript, and plugins like Scroll Trigger can be used to achieve scroll animations. GreenSock's ScrollTrigger provides a powerful JavaScript animation library for more complex animations. It is important to consider accessibility and provide reduced motion fallbacks. CodePen and GreenSock's documentation are valuable resources for learning and inspiration in creative coding.
React Native Animations Should Be Fun
React Advanced Conference 2022React Advanced Conference 2022
28 min
React Native Animations Should Be Fun
This talk is about animations in React Native, specifically focusing on React Native Reanimated. It covers the use of interpolations and extrapolations in animations, animating items in a carousel or list, sticky elements and interpolation, and fluidity and layout animations. The talk provides valuable tips and tricks for creating performant animations and explores the use of math formulas for natural movements.
Animation and Vue.js
Vue.js London Live 2021Vue.js London Live 2021
32 min
Animation and Vue.js
Today's Talk covers animation in Vue JS apps, including CSS animations, JavaScript animations using the GSAP library, and handling multiple elements with transition groups. The Talk also discusses different kinds of movements, state transitions, and animating numbers and SVGs. Overall, it provides a comprehensive overview of animation techniques and tools for enhancing Vue JS apps.
Animations with JS
JSNation 2022JSNation 2022
19 min
Animations with JS
Today's talk is about animations in Javascript, covering frame rates and different methods like CSS transitions and animations. JavaScript provides more control over animations, allowing interpolation and simulation of real-world scenarios. Different approaches to animating a box from zero to 100 pixels are discussed, including for loops, timers, and the web animations API. Request Animation Frame is highlighted as a solution for consistent and smooth animations. The Web Animations API is introduced as a powerful tool alongside CSS animations and transitions, although it has limited browser support.

Workshops on related topic

How to make amazing generative art with simple JavaScript code
JS GameDev Summit 2022JS GameDev Summit 2022
165 min
How to make amazing generative art with simple JavaScript code
Top Content
WorkshopFree
Frank Force
Frank Force
Instead of manually drawing each image like traditional art, generative artists write programs that are capable of producing a variety of results. In this workshop you will learn how to create incredible generative art using only a web browser and text editor. Starting with basic concepts and building towards advanced theory, we will cover everything you need to know.