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.
Comments