# Easing

> Every easing curve vidmake understands, from `linear` to `easeOutElastic`, plus `cubic-bezier()`, `steps(n)`, `hold`, `spring(bounce)`, motion tokens (a curve with a duration, such as `smooth`, `snap` and `bouncy`), and easing functions inside expressions.

An easing curve maps progress through a segment (0 at its start, 1 at its end) to how far the value
has moved. `linear` moves at a constant speed; `easeOutCubic` starts fast and slows into place;
`easeOutBack` overshoots and comes back. Curves don't change *where* an animation starts and ends,
only how it gets there.

*(An interactive easing widget is shown here on the HTML page.)*

## Where eases go

| place | field | default |
|---|---|---|
| [keyframes](https://vidmake.app/docs/animation.md#keyframes) | third array item or `ease` on a keyframe object: shapes the segment **arriving** at that keyframe | `linear` |
| [keys object](https://vidmake.app/docs/animation.md#keyframes) | `ease`: default for every keyframe without its own | `linear` |
| [tween](https://vidmake.app/docs/animation.md#tweens) | `ease`, or `motion` | `easeInOut` |
| [enter / exit](https://vidmake.app/docs/presets.md) and [text stagger](https://vidmake.app/docs/text.md#stagger) | `motion`, or `ease` | `enter`: the `smooth` spring (`bouncy` for `pop`, `snap` for `rise`); `exit`: `easeInCubic` |
| [transition](https://vidmake.app/docs/series.md#transitions) (object form) | `ease`, or `motion` | `easeInOut` |
| [stagger](https://vidmake.app/docs/text.md#stagger) start times | `spread` | `linear` |
| [expressions](#easing-functions-in-expressions) | `easeOutBack(x)` and friends | |

```json
"x": [[0, 100], ["1s", 500, "easeOutCubic"], ["2s", 300, "cubic-bezier(0.8, 0, 0.2, 1)"]]
"opacity": { "from": 0, "to": 1, "duration": "0.6s", "ease": "easeOutQuart" }
"enter": { "type": "fadeUp", "ease": "easeOutBack" }
```

Where a field is called `motion`, it takes a [motion](#motion): a curve that brings its own duration.

## All easing names

The named curves come in ten families, each with an `In`, `Out` and `InOut` variant:

- `easeIn<Family>` starts slowly and accelerates (good for exits).
- `easeOut<Family>` starts fast and decelerates (good for entrances: things arrive and settle).
- `easeInOut<Family>` is slow at both ends (good for moves between two resting positions).

| family | character | names |
|---|---|---|
| Sine | gentlest | `easeInSine` `easeOutSine` `easeInOutSine` |
| Quad | gentle | `easeInQuad` `easeOutQuad` `easeInOutQuad` |
| Cubic | the standard UI curve | `easeInCubic` `easeOutCubic` `easeInOutCubic` |
| Quart | stronger | `easeInQuart` `easeOutQuart` `easeInOutQuart` |
| Quint | stronger still | `easeInQuint` `easeOutQuint` `easeInOutQuint` |
| Expo | very sharp start or stop | `easeInExpo` `easeOutExpo` `easeInOutExpo` |
| Circ | quarter-circle | `easeInCirc` `easeOutCirc` `easeInOutCirc` |
| Back | overshoots the target (or backs up first, for `In`) by about 10% | `easeInBack` `easeOutBack` `easeInOutBack` |
| Elastic | rings around the target like a plucked string | `easeInElastic` `easeOutElastic` `easeInOutElastic` |
| Bounce | bounces off the target | `easeInBounce` `easeOutBounce` `easeInOutBounce` |

Plus these:

| name | meaning |
|---|---|
| `linear` | constant speed |
| `ease` | same as `easeInOutCubic` |
| `easeIn`, `easeOut`, `easeInOut` | the Cubic variants (`easeIn` = `easeInCubic`, ...) |
| `hold` (alias `step`) | stays at the start value for the whole segment, then jumps to the end value at the very end |
| `cubic-bezier(x1, y1, x2, y2)` | a CSS-style cubic Bézier, see [below](#cubic-bezier) |
| `steps(n)` | `n` equal jumps, see [below](#steps-and-hold) |
| `spring(bounce)` | a spring that starts from rest and settles exactly at the end of the segment; `bounce` 0..1, see [springs](#springs) |

`spring` alone means `spring(0.25)`.

Here are nine of them on identical keyframes. Each dot goes out to the right and comes back; the ease
sits on both arriving keyframes, so `easeInBack` backs up before leaving *and* before returning:

**Demo: Nine easings on the same keyframes: out to the right and back again** (rendered video: https://vidmake.app/media/easing-compare.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "easing", "description": "Nine easings on the same keyframes: out to the right and back again",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "track", "type": "rect", "x": 805, "y": 358, "width": 630, "height": 2, "fill": "#d8d1c1",
        "repeat": { "count": 9, "layout": "column", "spacing": 62 } },
      { "id": "name", "type": "text", "text": "${pick(i, ['linear', 'ease', 'easeOutQuart', 'easeInBack', 'easeOutBack', 'easeOutElastic', 'easeOutBounce', 'cubic-bezier(0.8, 0, 0.2, 1)', 'steps(6)'])}", "font": "JetBrains Mono", "fontSize": 18, "fill": "#4a4640",
        "anchor": "right", "x": 390, "y": 358, "repeat": { "count": 9, "layout": "column", "spacing": 62 } },
      { "id": "d0", "type": "ellipse", "width": 26, "y": 110, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "linear"], ["2.3s", 1120], ["3.8s", 490, "linear"]] },
      { "id": "d1", "type": "ellipse", "width": 26, "y": 172, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "ease"], ["2.3s", 1120], ["3.8s", 490, "ease"]] },
      { "id": "d2", "type": "ellipse", "width": 26, "y": 234, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "easeOutQuart"], ["2.3s", 1120], ["3.8s", 490, "easeOutQuart"]] },
      { "id": "d3", "type": "ellipse", "width": 26, "y": 296, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "easeInBack"], ["2.3s", 1120], ["3.8s", 490, "easeInBack"]] },
      { "id": "d4", "type": "ellipse", "width": 26, "y": 358, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "easeOutBack"], ["2.3s", 1120], ["3.8s", 490, "easeOutBack"]] },
      { "id": "d5", "type": "ellipse", "width": 26, "y": 420, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "easeOutElastic"], ["2.3s", 1120], ["3.8s", 490, "easeOutElastic"]] },
      { "id": "d6", "type": "ellipse", "width": 26, "y": 482, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "easeOutBounce"], ["2.3s", 1120], ["3.8s", 490, "easeOutBounce"]] },
      { "id": "d7", "type": "ellipse", "width": 26, "y": 544, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "cubic-bezier(0.8, 0, 0.2, 1)"], ["2.3s", 1120], ["3.8s", 490, "cubic-bezier(0.8, 0, 0.2, 1)"]] },
      { "id": "d8", "type": "ellipse", "width": 26, "y": 606, "fill": "#161412",
        "x": [[0, 490], ["0.3s", 490], ["1.8s", 1120, "steps(6)"], ["2.3s", 1120], ["3.8s", 490, "steps(6)"]] }
    ]
  }]
}
```

> [!TIP]
> Back, Elastic and Bounce leave the 0..1 range: an `easeOutBack` on `opacity` overshoots past 1 and
> gets clamped, and on `width` or `scale` it briefly goes larger than the target. That's usually what
> you want for position and scale, and rarely what you want for opacity or color.

## cubic-bezier

`cubic-bezier(x1, y1, x2, y2)` takes the two control points of a Bézier curve from (0, 0) to
(1, 1), exactly like CSS. x values should stay within 0..1; y values may go outside it for overshoot.
You can paste curves from any CSS easing tool:

```json
"ease": "cubic-bezier(0.4, 0, 0.2, 1)"
"ease": "cubic-bezier(0.34, 1.56, 0.64, 1)"
```

In keyframes and tweens, the same curve can be written as a four-number array:

```json
"x": [[0, 0], ["1s", 800, [0.8, 0, 0.2, 1]]]
"y": { "from": 0, "to": 300, "duration": "1s", "ease": [0.4, 0, 0.2, 1] }
```

## Springs

`spring(bounce)` is a damped spring squeezed into the segment: `spring(0)` glides in with no overshoot,
`spring(0.3)` overshoots a little, `spring(0.6)` rings a few times. Unlike `easeOut*` curves, which start at
full speed, a spring starts from rest (zero speed) and accelerates, so nothing jumps on the first frame. It reaches
its target exactly at the end of the segment, so it never runs past a layer or a keyframe.

```json
"x": { "from": 0, "to": 600, "duration": "0.6s", "ease": "spring(0.3)" }
"y": [[0, 400], ["0.5s", 300, "spring(0.2)"]]
```

For the physical form (`damping`, `stiffness`, `mass`, as in Remotion), see [spring tweens](https://vidmake.app/docs/animation.md#springs).

## steps and hold

`steps(n)` divides the segment into `n` equal steps and jumps at the start of each one, like CSS
`steps(n)`: the value is the start value for the first `1/n` of the time and reaches the end value
only at the end. Use it for ticking clocks, frame-by-frame sprite counters or a deliberately choppy
look. Here is the `steps(6)` row from the demo above, out on the way right:

```console
$ vidmake trace easing-compare.json -l d8 --every 3 --from 0.3s --to 1.8s
 FRAME    TIME        X        Y       W       H    ROT  SCALE  OPAC  NOTES
     9   0.30s    490.0    606.0    26.0    26.0    0.0   1.00  1.00  
    12   0.40s    490.0    606.0    26.0    26.0    0.0   1.00  1.00  
    15   0.50s    490.0    606.0    26.0    26.0    0.0   1.00  1.00  
    18   0.60s    595.0    606.0    26.0    26.0    0.0   1.00  1.00  
    21   0.70s    595.0    606.0    26.0    26.0    0.0   1.00  1.00  
    24   0.80s    700.0    606.0    26.0    26.0    0.0   1.00  1.00  
    27   0.90s    700.0    606.0    26.0    26.0    0.0   1.00  1.00  
    30   1.00s    700.0    606.0    26.0    26.0    0.0   1.00  1.00  
    33   1.10s    805.0    606.0    26.0    26.0    0.0   1.00  1.00  
    36   1.20s    805.0    606.0    26.0    26.0    0.0   1.00  1.00  
    39   1.30s    910.0    606.0    26.0    26.0    0.0   1.00  1.00  
    42   1.40s    910.0    606.0    26.0    26.0    0.0   1.00  1.00  
    45   1.50s    910.0    606.0    26.0    26.0    0.0   1.00  1.00  
    48   1.60s   1015.0    606.0    26.0    26.0    0.0   1.00  1.00  
    51   1.70s   1015.0    606.0    26.0    26.0    0.0   1.00  1.00  
    54   1.80s   1120.0    606.0    26.0    26.0    0.0   1.00  1.00
```

`hold` is the extreme case: one step. In keyframes, put it on the keyframe you want to *jump to*:

```json
"opacity": [[0, 1], ["1s", 0, "hold"], ["1.5s", 1, "hold"]]
```

That keeps `opacity` at 1 until exactly 1 s, at 0 until 1.5 s, then 1 again: a blink with no fade.

## Motion

A **motion** is a curve with a duration. Entrances, exits, tweens and transitions take one as `motion`, so
the timing of a whole video can come from a few names:

```json
"enter": { "type": "fadeUp", "motion": "snap" }
"x": { "from": 160, "to": 1120, "motion": "bouncy" }
"transition": { "type": "slideLeft", "motion": "gentle" }
```

vidmake has four built in. All are springs from rest:

| name | bounce | duration | use |
|---|---|---|---|
| `smooth` | 0 | 0.7 s | the default entrance |
| `snap` | 0.15 | 0.5 s | quick, a hint of overshoot; the default for `rise` |
| `bouncy` | 0.45 | 0.8 s | visible overshoot, for pops and badges; the default for `pop` |
| `gentle` | 0 | 1.2 s | slow and soft, for big moves and backgrounds |

**Demo: The four built-in motions and a theme motion: each brings its own curve and duration** (rendered video: https://vidmake.app/media/easing-motion.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "theme": { "motion": { "house": { "ease": "cubic-bezier(0.22, 1, 0.36, 1)", "duration": "0.9s" } } },
  "compositions": [{
    "id": "motion", "description": "The four built-in motions and a theme motion: each brings its own curve and duration",
    "width": 1280, "height": 720, "fps": 30, "duration": "3s", "background": "#f4f1ea",
    "layers": [
      { "id": "name", "type": "text", "text": "${pick(i, ['smooth · 0.7s', 'snap · 0.5s', 'bouncy · 0.8s', 'gentle · 1.2s', 'house · 0.9s'])}",
        "font": "JetBrains Mono", "fontSize": 20, "fill": "#4a4640", "align": "left", "anchor": "left", "x": 140, "y": 140,
        "repeat": { "count": 5, "layout": "column", "spacing": 110, "origin": "start" } },
      { "id": "smooth", "type": "ellipse", "width": 40, "y": 180, "fill": "#161412",
        "x": { "from": 160, "to": 1120, "start": "0.3s", "motion": "smooth" } },
      { "id": "snap", "type": "ellipse", "width": 40, "y": 290, "fill": "#161412",
        "x": { "from": 160, "to": 1120, "start": "0.3s", "motion": "snap" } },
      { "id": "bouncy", "type": "ellipse", "width": 40, "y": 400, "fill": "#161412",
        "x": { "from": 160, "to": 1120, "start": "0.3s", "motion": "bouncy" } },
      { "id": "gentle", "type": "ellipse", "width": 40, "y": 510, "fill": "#161412",
        "x": { "from": 160, "to": 1120, "start": "0.3s", "motion": "gentle" } },
      { "id": "house", "type": "ellipse", "width": 40, "y": 620, "fill": "#c92a26",
        "x": { "from": 160, "to": 1120, "start": "0.3s", "motion": "house" } }
    ]
  }]
}
```

### Your own motions

Name motions in the project's [theme](https://vidmake.app/docs/theme.md), under `motion`:

```json title="project.json"
{
  "theme": { "motion": {
    "house": { "spring": 0.15, "duration": "0.5s" },
    "glide": { "ease": [0.22, 1, 0.36, 1], "duration": "0.6s" },
    "curve": "cubic-bezier(0.22, 1, 0.36, 1)"
  } },
  "compositions": [ ... ]
}
```

| form | meaning |
|---|---|
| `{ "spring": 0.15, "duration": "0.5s" }` | a spring with that bounce (0..1), settling at `duration`. `spring` also takes `true`, `{ "bounce" }` or `{ "damping", "stiffness", "mass" }`; without `duration` it takes 0.6 s (physical springs: their natural settle time) |
| `{ "ease": ..., "duration": "0.6s" }` | any easing (a name, `"cubic-bezier(...)"`, four numbers or another motion) with a duration |
| `"cubic-bezier(...)"`, `[x1, y1, x2, y2]`, a name | a bare easing with no duration of its own: an entrance keeps its default length, a tween needs `duration` |

- A motion name works as `"motion"` on enter, exit, tweens and transitions, and as `"ease"` anywhere (its
  curve without the duration).
- It is also an [expression](#easing-functions-in-expressions) function, which `cubic-bezier(...)` itself
  can't be: `"width": "=600 * house(clamp(t / 0.5, 0, 1))"`. The built-in names work too: `smooth(x)`.
- A theme may redefine a built-in: `"smooth": { "spring": 0.1, "duration": "0.6s" }` changes every default
  entrance in the project.
- Names are letters, digits and `_`, and must not clash with built-in easings, expression functions or
  variables; `validate` says so if they do.
- A motion object can also be written inline: `"motion": { "spring": 0.2, "duration": "0.4s" }`, or
  referenced from anywhere in the theme with `"$motion.house"`.

### Named easings

vidmake 0.5 named curves in a top-level `easings` object: `"easings": { "house": "cubic-bezier(0.22, 1, 0.36, 1)" }`.
It still works, with a warning; `vidmake migrate project.json` moves every entry to `theme.motion`, where a bare
easing is a motion without a duration and keeps working the same way.

## Easing functions in expressions

Every easing name is also a function in [expressions](https://vidmake.app/docs/expressions.md). It takes a progress value,
clamps it to 0..1, and returns the eased value. Combine it with `clamp()` or `map()` to turn time
into progress:

```json
"x": "=200 + 800 * easeOutCubic(t / 1.5)"
"scale": "=easeOutBack(clamp((t - 0.5) / 0.6, 0, 1))"
"opacity": "=easeInOutSine(map(t, 1, 2, 0, 1))"
```

This is the easiest way to give every copy of a [repeated](https://vidmake.app/docs/repeat.md) layer its own delayed
animation, because `i` is available inside the expression:

**Demo: Easing functions inside expressions: each bar runs easeOutBack on its own staggered clock** (rendered video: https://vidmake.app/media/easing-expr.mp4)

```json
{
  "compositions": [{
    "id": "bars", "description": "Easing functions inside expressions: each bar runs easeOutBack on its own staggered clock",
    "width": 1280, "height": 720, "fps": 30, "duration": "3s", "background": "#f4f1ea",
    "layers": [
      { "id": "base", "type": "rect", "y": 600, "width": 900, "height": 2, "fill": "#161412" },
      { "id": "bar", "type": "rect", "width": 70, "anchor": "bottom", "x": 640, "y": 598,
        "repeat": { "count": 8, "layout": "row", "spacing": 110 },
        "height": "=pick(i, [160, 290, 230, 380, 320, 420, 360, 460]) * easeOutBack(clamp((t - 0.2 - i * 0.08) / 0.7, 0, 1))",
        "fill": "=lerp('#d8d1c1', '#161412', i / (n - 1))", "exit": "fade" }
    ]
  }]
}
```

Each bar computes its own clock: `(t - 0.2 - i * 0.08) / 0.7` starts at 0.2 s for the first bar and
0.08 s later for each next one, and `easeOutBack` turns that clock into an overshooting grow.

Names that work as functions: `linear`, `ease`, `hold`, `easeIn`, `easeOut`, `easeInOut` and every
`easeIn|Out|InOut<Family>` name, the built-in motions (`smooth`, `snap`, `bouncy`, `gentle`) and your
[own motions](#your-own-motions). `cubic-bezier(...)`, `steps(n)` and `spring(bounce)` are not available as
functions directly; name them in the theme to use them in expressions.
`step(edge, x)` and `spring(frame)` are different functions with the same names as ease aliases; see
[Expressions](https://vidmake.app/docs/expressions.md#functions).

> [!AGENT]
> When an ease seems wrong, `vidmake trace project.json -l <id> --every 2` shows the real curve as numbers.
> A typo in an ease name fails `validate` with the full list of valid names.
