You have two endpoints.

At x 0, y is 20. At x 200, y is 50.

You’re going to plot x along some dimension. If you’re building an animation, this dimension is “time”. In other cases, you’ll plot x along an actual axis of a graph.

You need to choose midpoints. You can generalize this problem as

Now you just need to define an easing function (E). You’ll have a conversation that goes something like:

E: On a scale of 0 to 1, what is x?
You: ___
E: Okay, on a scale of 0 to 1, y is ___
You (sometimes): That’s not between 0 and 1!
E (sometimes): I do what I want.

A linear E is simply:

A sinusoidal E is more gentle.

These are fine. But we’re speaking the wrong language. You’re trying to do something visual… why would you speak in equations? Let’s remove the handcuffs.

## Cubic Bézier curves

A 2D cubic Bézier curve is defined by:

You just plug in four points. If you want to coerce a cubic Bézier curve into an easing function, two of these points are already known.

The others are up to you. For example:

So this seems useful for drawing a line… but how do we get an easing function out of this? We need a function of x, not a function of t.

Here’s a useful strategy: appeal to authority!

CSS transitions can use “cubic-bezier” as their timing function. So let’s just assume that the browser vendors have figured out the best way to do this.

### The general approach:

Search for the t value that corresponds with the given x. Use the Newton-Raphson method for better perf, but fall back to bisection as necessary / convenient.

All three use Horner’s method to calculate both the Bézier of t and the slope at t.

### Bisection

The Chrome Compositor’s approach is the least advanced – it jumps right to bisection. It starts with t of 0 and takes steps of size 1, 0.5, 0.25, 0.125 … until it finds a t that results in an x value within 0.0000001 of the specified value.

WebKit and Blink’s fallback is just slightly more advanced. It starts with a better guess, using t of x.

Gecko stores 11 sample values for a Bézier function and uses them to choose a good first guess. So its first guess is going to choose a t within 0.1 of the correct answer, and its steps will be 0.05, 0.025, …

### Newton’s method

We can calculate the slope of a Bézier curve w.r.t. t via:

Instead of binary-searching for the correct value, use a smarter step approach.

$[next\ guess] = [previous\ guess] - \frac{[previous\ error]}{[slope\ at\ previous\ guess]}$

The WebKit / Blink code starts by guessing t of x, and does up to 8 steps until it finds an x within [epsilon] of the specified value. It uses epsilon: (via accuracyForDuration) $\epsilon = \frac{1}{200 * [animation\ duration]}$

It also gives up if it sees a slope of less than 0.000001. If it doesn’t find what it’s looking for, it falls back to bisection.

Gecko starts with a better guess (via its 11 sample values), then does 4 steps and then just returns whatever it ends up with. It only uses the fallback if it sees the slope for its first guess is less than 0.02. And it only returns early if it reaches a point where the slope is 0, to avoid divide-by-zero – i.e., error handling. x’(t) should never be 0, otherwise this wouldn’t be a valid easing function.

### Horner’s method

All three implemenations transform

into

by plugging in the start and end points and then using Horner’s method. Blink / WebKit only calculate these coefficients once. In fact, the code is quite pretty.

## The code

Enter ClojureScript.

Now just do

## Freedom

You’ve escaped the world of equations. Go choose your Bézier curve!

You might appreciate http://cubic-bezier.com