Pure CSS Bezier Curve Motion Paths | CSS-Tricks

Pure CSS Bezier Curve Motion Paths | CSS-Tricks

Are you a Bezier curve lover like I am?

Besides being elegant, Bezier curves have nice mathematical properties due to their definition and construction. No wonder they are widely used in so many areas:

Now, how about using Bezier curves as motion paths with CSS?

Depending on the context, when referring to a “Bezier curve”, we often assume a 2D cubic Bezier curve.

Such a curve is defined by four points:

Note: In this article, we generally refer to  and  as endpoints,  and  as control points.

The word “cubic” means the underlying function of the curve is a cubic polynomial. There are also “quadratic” Bezier curves, which are similar, but with one fewer control point.

Say you are given an arbitrary 2D cubic Beizer curve, how would you animate an element with pure CSS animation, such that it moves precisely along the curve?

As an example, how would you recreate this animation?

In this article we will explore three methods with different flavors. For each solution we will present an interactive demo, then explain how it works. There are lots of mathematical calculations and proofs behind the scene, but don’t worry, we will not go very deep.

Note: There are lots of examples and explanations in Temani Afif’s article (2021).

Using the function with correct parameters, we can create a motion path of any cubic Bezier curve:

This demo shows a pure CSS animation. Yet canvas and JavaScript are used, which serve two purposes:

You can drag the two endpoints (black dots) and the two control points (black squares). The JavaScript code will update the animation accordingly, by updating a few CSS variables.

Suppose the desired cubic Bezier curve is defined by four points: , , , and . We set up CSS rules as following:

The rules and determine the starting and finishing locations of the element. In we have two magical  functions, the parameters are calculated such that both and always have the correct values at any time.

I’ll skip the math, but I drafted a brief proof here, for your curious math minds.

This method should work well for most cases. You can even make a 3D cubic Bezier curve, by introducing another animation for the  value.

However there are a few minor caveats:

Note: In practice, you can just add a tiny offset as a workaround.

As a warm-up, imagine an element with two animations:

What is the motion path of the element, if the animations are defined as following:

As you may have guessed, it moves diagonally:

Now, what if the animations are defined like this instead:

“Aha, you cannot trick me!” you might say, as you noticed that both animations are changing the same property, “ must override like this:”

Well, earlier I had thought so, too. But actually we get this:

The trick is that does not have a  frame, which means the starting position is animated by .

In the following demo, the starting position of 2 is visualized as the moving blue dot:

The demo right above resembles the construction of a quadratic Bezier curve:

But they look different. The construction has three linearly moving dots (two green, one black), but our demo has only two (the blue dot and the target element).

Actually the motion path in the demo is a quadratic Bezier curve, we just need to tune the keyframes carefully. I will skip the math and just reveal the magic:

Suppose a quadratic Bezier curve is defined by points , , and . In order to move an element along the curve, we do the following:

Similar to the demo of Method 1, you can view or adjust the curve. Additionally, the demo also shows two more pieces of information:

Both can be toggled using the checkboxes.

This method works for cubic Bezier curves as well. If the curve is defined by points , , , and . The animations should be defined like this:

What about 3D Bezier Curves? Actually, the truth is, all the previous examples were 3D curves, we just never bothered with the  values.

What about higher-order Bezier curves? I am 90% sure that the method can be naturally extended to higher orders. Please let me know if you have worked out the formula for fourth-order Bezier curves, or even better, a generic formula for Bezier curves of order N.

The mathematical construction of Bezier Curves already gives us a good hint.

Step-by-step, we can determine the coordinates of all moving dots. First, we determine the location of the green dot that is moving between and :

Additional green dots can be constructed in a similar way.

Next, we can determine the location of a blue dot like this:

Rinse and repeat, eventually we will get the desired curve.

Similar to Method 2, with this method we can easily build a 3D Bezier Curve. It is also intuitive to extend the method for higher-order Bezier curves.

The only downside is the usage of , which is not supported by all browsers.

All the examples so far have the “linear” timing, what about easing or other timing functions?

Note: By “linear” we mean the variable t of the curve linearly changes from 0 to 1. In other words, is the same as animation progress.

is never used in Method 2 and Method 3. Like other CSS animations, we can use any supported timing function here, but we need to apply the same function for all animations (, , and ) at the same time.

Here’s an example of :

And here’s how it looks like with :

On the other hand, Method 1 is trickier, because it already uses a timing function. In the examples above we have and . In fact we can tweak the timing by changing both parameters. Again, all animations (e.g., and ) must have the same values of and .

Here’s how it looks like when and :

With Method 2, we can achieve exactly the same effect by setting to :

In fact, it works in a more general way:

Suppose that we are given a cubic Bezier curve, and we created two animations for the curve with Method 1 and Method 2 respectively. For any valid values of and , the following two setups have the same animation timing:

Now we see why Method 1 is “limited”: with Method 1 we can only  with two parameters, but with Method 2 and Method 3 we can use any CSS .

In this article, we discussed 3 different methods of moving elements precisely along a Bezier curve, using only CSS animations.

While all 3 methods are more or less practical, they have their own pros and cons:

That’s all! I hope you find this article interesting. Please let me know your thoughts!

Images Powered by Shutterstock