Creating Easing Operators
Explore how to build easing operators in RxJS that modify animation progress for smoother transitions. Understand percentDone and easingMap operators to control timing and acceleration, and compose these for advanced animated effects.
We'll cover the following...
Easing
Now that the observable stream emits progressive values, we can run them through a converter to create different styles of transitioning between two points (also known as easing).
Each possible easing relies on an equation that answers the question, “Given that we are halfway through, where should the center be rendered?” For instance, the following starts slowly, and then accelerates to catch up at the end:
Easing operators
Many other easing functions exist, but it’s a lot more fun to see them in action than to drearily read code.
Let’s put everything together from this section to create a tool that allows moving elements with an arbitrary easing function to see a few of these in action.
percentDone operator
First, we’ll create a percentDone operator that takes a duration in milliseconds and returns a percentage of how far the animation has gone.
startTime variable
While this function creates a startTime variable, rest assured that the inner function won’t be called until something subscribes to the observable. This is another advantage of lazy observables. This means that startTime won’t be set until the animation begins.
easingMap operator
The next operator uses a map that takes an easing function. Right now, this is just a wrapper around the map operator.
This snippet may seem superfluous (why not just use map?), but it helps communicate intent. Later, you can add more concrete types to your operator with TypeScript, so that it will allow only certain types of functions.
finalTween function
Finally, let’s create a new tween function that takes advantage of these two new operators you created.
We can then compose these together to demonstrate what several different types of easing operators look like:
MultiTween animation
Now, let’s see all of our easing operators into action.