From Jittery to Jazzy: The Ultimate Guide to Perfecting Car Animation on Web.

Sahil Kapoor
Technogise
Published in
4 min readSep 3, 2024

Creating smooth, captivating animations is essential for maintaining user engagement in the dynamic web design world. This guide delves into the art of crafting a sleek, seamless animation — imagine a car gliding effortlessly across the screen — using a combination of JavaScript and CSS. If you’ve ever noticed stuttering or choppy animations and wondered how to make them flow effortlessly, this guide is for you!

Horizontal car animation sequence

Buckle up, and let’s embark on a journey to bring this animation to life. This blog navigates through the process of animating a car from scratch, exploring the key coding techniques that make it all possible. Let’s start the engine and dive in! 🚗

Journey from Learnings to Implementation

Before diving into the implementation, it’s crucial to understand the fundamentals of smooth web animations. Here’s an overview of the learning journey:

  1. Researching Animation Techniques: The journey began with exploring various animation techniques and best practices. Different methods for achieving smooth animations were studied, such as using setTimeout, and setInterval for frequent updates, and CSS transitions for smooth visual effects.
  2. Experimenting with JavaScript: Initial experiments involved using JavaScript’s setTimeout and setInterval functions to update element positions. setTimeout executes a function once after a specified delay and requires recursive calls for continuous movement, which can sometimes lead to issues if not managed properly, potentially affecting frontend performance.
    Conversely, setInterval executes functions repeatedly at set intervals without needing recursion. Through experimenting with different intervals and increments, it was observed that shorter intervals (e.g. 10 milliseconds) result in smoother animations by increasing the frequency of updates, leading to finer changes in the animated element’s position or property value.
  3. Exploring CSS Styling: The next focus was on CSS styling for the animation. It was discovered how proper styling can complement the animation—explored CSS Transition to achieve a motion on the object, which typically animates the change of CSS properties over time. But, controlling this animation manually from JS helped to build more custom and conditional behavior which allowed more control over it.
  4. Testing Browser Performance: The exploration extended to understanding how modern browsers handle animations and leverage hardware acceleration for smooth rendering. This knowledge was crucial in enhancing animations for peak performance.

The Code and Implementation

1. JavaScript’s setInterval for Continuous Updates

Here’s an example of how it’s implemented:

  • High Frequency Updates: setInterval executes the method repeatedly at a set interval which in this case we have set to the interval of 10 milliseconds, this ensures frequent updates, resulting in smooth animation. Additionally, by adjusting the left position by 2 pixels with each interval, we increase the number of frames per second. This smaller, consistent movement between frames helps create a smoother and more seamless animation, enhancing the overall fluidity of the motion on the screen.
  • Stopping the Car: clearInterval(interval) is used to stop the setInterval loop, once the car reaches the desired position, I have set the position to 1600, value was likely chosen based on the width of the container or the screen size, It’s far enough from the starting point (0px) to allow the car to visibly move across a significant portion of the screen. We can change it accordingly.

2. CSS for Additional Smoothness

CSS was used to style the car and test its animation:

Here’s an example of how it’s implemented:

Styling the Car: The .box class is styled to look like a car, with a specific colour, rounded corners and a background image. I also created wheels using css pseudo elements, This styling helps in visually representing the moving car.

Note: We can also use the SVG icon of the car and apply styling on it 😄

3. Leveraging Browser Performance and Testing

Modern browsers use hardware acceleration to render animations smoothly. By understanding these techniques, the animations were ensured to be fluid and responsive.

Hardware Acceleration: This technique leverages the GPU to perform operations traditionally handled by the CPU, such as rendering CSS animations, transitions, and transformations, thus enhancing performance by reducing CPU load.

https://developer.chrome.com/blog/hardware-accelerated-animations

https://www.howtogeek.com/how-to-turn-hardware-acceleration-on-and-off-in-chrome/

Run the code here 💻

Conclusion: Bringing Animation to Life

The journey from learning to implementing smooth animations involved exploring JavaScript methods and CSS styling. By using setInterval for frequent updates and applying effective CSS styling, fluid, engaging animations were achieved, significantly enhancing the user experience. Experiment with these techniques to add some magic to your web animations and create seamless, visually stunning effects.

Happy Coding! ✨

--

--

Sahil Kapoor
Technogise
0 Followers
Writer for

Tech consultant with a love for travel, cultural exploration and photography.