Hey everyone! Welcome to the first post in a series where we’ll build a web animation library from scratch called Rocket Animation (or Rocket for short).
I was inspired to build a simple, fast animation library using TypeScript. After researching this topic for the past 10 months, I'm finally ready to show you how I did it. You can also treat the upcoming posts in this series as documentation to better understand how Rocket works.
My goal is to make Rocket a go-to, reliable animation library that people love to use and recommend—backed by a great, supportive community.
For the first phase, Rocket will cover these core features:
- Animation (Tween)
- Timeline
- Stagger / Keyframes
- Scroll animations
- Text animations
- SVG animations
With that out of the way, let’s talk about how JavaScript animations work and take a bird's-eye view of how Rocket handles them under the hood.
How do animations work in JavaScript?
For an animation to work, the browser has to repaint a new frame on the screen at regular intervals over a set duration. For example, if an animation lasts 2 seconds, we need to repaint the screen with a new frame roughly every 16ms (milliseconds).
So, how do we actually do this in JavaScript? We have two main options.
1. The setTimeout function
const square = document.querySelector(".square");
let time = 0, duration = 1500;
function animate(){
time += 16;
const position = (time / duration) * 535;
square.style.transform = `translateX(${position}px)`;
if (time <= duration) {
setTimeout(animate, 16);
}
}
animate();
The code above calls animate every 16ms and moves the square to the right. While setTimeout works for simple animations, it has a few major drawbacks:
- General-purpose: It's meant for scheduling delayed tasks, not rendering graphics.
- Out of sync: It isn't aligned with your screen's refresh rate, which causes the timing to drift and makes animations look choppy.
- Inefficient: It's not optimized for heavy visual updates.
Because of this, we need a better alternative to get smooth animations in JavaScript. Enter our second option.
2. The requestAnimationFrame function
const square = document.querySelector(".square");
let lastTime = 0, duration = 1500;
function animate(t){
if (lastTime == 0) lastTime = t;
const dt = t - lastTime;
const position = (dt / duration) * 535;
square.style.transform = `translateX(${position}px)`;
if (dt <= duration) {
requestAnimationFrame(animate);
}
}
requestAnimationFrame(animate);
This code also calls animate roughly every 16ms (since most screens run at 60fps, requestAnimationFrame naturally matches that pace, for more info about fps and Refresh rate Hz checkout this post on my Telegram Channel don’t forget to follow). However, it has huge advantages over setTimeout:
- Optimized: It's built specifically for smooth animations and visual updates.
- Synchronized: It aligns directly with your screen's refresh rate and the browser's repaint cycle.
Now that we know the tools, it’s clear that Rocket will use requestAnimationFrame to keep things smooth. (If you want to dive deeper, check out the MDN docs on requestAnimationFrame.) Next, let's look at how Rocket actually uses it under the hood.
Inside the Rocket Animation Engine
Here is a bird's-eye view of Rocket's internal mechanics. At its core, we have two main components:
-
Container: A class that starts and stops the animation loop. Its main job is to hold all the active animation components (Flowables).- It kicks off the loop and cancels it once all
Flowablecomponents finish their animations. - It uses a doubly linked list to store and manage the
Flowables. - It controls the master timing for the entire animation.
- It uses
requestAnimationFrameunder the hood.
- It kicks off the loop and cancels it once all
-
Flowable: An abstract class that controls the flow of a specific animation. Later in this series, we'll create other classes that extend this one.- It acts as a child of
Containerand holds all the details an animation needs—like duration, target elements, and the type of animation being applied. - It can also hold other
Flowables as children using a linked list.
- It acts as a child of
Note: In Rocket, we have one global
Containerthat acts as the parent for allFlowables and manages the clock. At least for now, no other class will extendContainer.
Notes before finishing this post.
-
Next up: We’ll be diving into
JSAnimation. - Under the hood: In upcoming posts, I'll walk you through how everything works. We won’t get entangled in heavy internal component code, but I’ll give you a nice, clear breakdown of the logic. If you're itching to look at the actual code, feel free to check out the GitHub repo (and hey, don't forget to star the project while you're there! ⭐).
- Let's chat: Drop a comment below, I’d genuinely love to hear your feedback 👇

Top comments (0)