# Animation


Nothing in `fx` keeps time. A scene is handed the time and computes every position, size and opacity from it, so any frame can be drawn on its own, in any order, on any thread. The procedures on this page are the small pieces that turn a time into a number you can draw with.

## Progress

Almost every animation starts with the same question: how far is `t` through the stretch from `a` to `b`?

```odin
prog :: proc(t, a, b: f32) -> f32                      // 0 before a, 1 after b, linear between
ep   :: proc(t, a, b: f32, e: Ease = ease_out) -> f32  // the same, eased
```

`prog` is the raw answer, clamped to 0..1. `ep` passes it through an ease, and is what you will write most of the time:

```odin
rise := 30 * (1 - fx.ep(s.t, s.start, s.start + 0.6)) // 30 px below, settled after 0.6 s
```

`lerp(a, b, p)` and `lerp2(a, b, p)` take a progress and return the value (or point) that far from `a` to `b`. `clamp01(x)` clamps to 0..1.

## Eases

An `Ease` is any `proc(p: f32) -> f32` that maps 0..1 to 0..1. These are built in; each dot below travels the same distance over the same 1.6 seconds.

```odin
anim_ease :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	eases := [?]struct {
		name: string,
		e:	fx.Ease,
	}{
		{"ease_linear", fx.ease_linear},
		{"ease_out", fx.ease_out},
		{"ease_out5", fx.ease_out5},
		{"ease_in", fx.ease_in},
		{"ease_in_out", fx.ease_in_out},
		{"ease_expo", fx.ease_expo},
		{"ease_back", fx.ease_back},
	}
	for it, i in eases {
		y := 204 + f32(i) * 112
		p := fx.ep(s.lt, 0.4, 2.0, it.e) // time's progress through [0.4, 2.0], eased
		fx.label(it.name, {160, y}, fx.mono(32), fx.theme.nm, {0, 0.5})
		fx.hline(520, y, 1200, fx.theme.nb, 2)
		fx.circle({520 + 1200 * p, y}, 22, fx.theme.na)
	}
}
```

| Ease | Shape |
| --- | --- |
| `ease_linear` | Constant speed. |
| `ease_out` | Cubic, fast then settling. The default everywhere. |
| `ease_out5` | Quintic: a harder start and a longer settle. |
| `ease_in` | Cubic, slow then fast. For things that leave. |
| `ease_in2` | Quadratic, a gentler `ease_in`. |
| `ease_in_out` | Cubic on both ends. For moves between two rests. |
| `ease_expo` | Exponential: most of the way there almost at once. |
| `ease_back` | Overshoots by a little and comes back. |

Your own ease is one line: `snap :: proc(p: f32) -> f32 {return p < 0.5 ? 0 : 1}`.

## Spring

```odin
spring :: proc(p: f32, freq: f32 = 2.2, damping: f32 = 0.45) -> f32
```

A damped spring that starts at 0 and settles at 1. `p` is a linear progress (use `prog`, not `ep`), `freq` is how many times it swings on the way, `damping` how quickly the swings die. It is not an `Ease` because of its extra arguments, so call it directly.

```odin
anim_spring :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	p := fx.prog(s.lt, 0.3, 2.3)
	k := fx.spring(p, knob("freq", 2.2, 0.5, 6), knob("damping", 0.45, 0.1, 1))
	if fx.group({s = k, origin = {960, 500}}) {
		fx.rrect({760, 300, 400, 400}, 56, fx.theme.na)
	}
	fx.label("s = fx.spring(p, freq, damping)", {960, 900}, fx.mono(34), fx.theme.nm, {0.5, 0.5})
}
```

## Windows and staggers

```odin
win     :: proc(t, a, b: f32, fi: f32 = 0.4, fo: f32 = 0.4) -> f32
stagger :: proc(t: f32, i: int, a, step, dur: f32, e: Ease = ease_out) -> f32
```

`win` is a visibility window: 0 before `a`, rising to 1 over `fi` seconds, falling back to 0 over the `fo` seconds before `b`. Use it as an opacity. [`fx.fade(s)`](/docs/scenes/#fade) is `win` over a scene's own span.

`stagger` is `ep` for the `i`-th member of a group: each one starts `step` seconds after the one before and takes `dur` to arrive.

```odin
anim_stagger :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	step := knob("step", 0.08, 0, 0.3)
	for i in 0 ..< 12 {
		k := fx.stagger(s.lt, i, 0.3, step, 0.7, fx.ease_back)
		h := 560 * k * (0.35 + 0.65 * fx.hash01(7, i))
		fx.rrect({294 + f32(i) * 112, 840 - h, 80, h}, 12, i == 8 ? fx.theme.na : fx.theme.nt)
	}
}
```

## Text and numbers

```odin
typed :: proc(text: string, t, t0, t1: f32) -> string
count :: proc(t, t0, t1, a, b: f32, e: Ease = ease_out) -> f32
```

`typed` returns the part of `text` that has been typed by time `t`, always cut at a whole character. `count` is a number running from `a` to `b`; format it however the frame needs.

```odin
anim_typed :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	o := fx.win(s.lt, 0.2, 3.8) // in over the first 0.4 s, out over the last
	if fx.group({o = o, y = 24 * (1 - o)}) {
		line := fx.typed("$ effects render projects/spot", s.lt, 0.5, 1.8)
		fx.text(line, 200, 360, fx.mono(64), fx.theme.nt)
		frames := fx.count(s.lt, 2.0, 3.2, 0, 744)
		fx.text(fmt.tprintf("%.0f frames", frames), 200, 500, fx.sans(150, 700, -0.035), fx.theme.na)
	}
}
```

## Keyframes

```odin
Key  :: struct {t: f32, v: f32}
Key2 :: struct {t: f32, p: Vec2}

keys  :: proc(ks: []Key,  t: f32, e: Ease = ease_in_out) -> f32
keys2 :: proc(ks: []Key2, t: f32, e: Ease = ease_in_out) -> Vec2
```

When a value passes through more than two states, list them. `keys` eases each segment on its own and holds the first and last value outside the range. `keys2` does the same for points.

```odin
anim_keys :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	path := [?]fx.Key2{{0.3, {360, 760}}, {1.2, {760, 300}}, {2.2, {1200, 760}}, {3.4, {1560, 320}}}
	for k in path do fx.ring(k.p, 18, fx.theme.ns, 2)
	p := fx.keys2(path[:], s.lt)
	r := fx.keys([]fx.Key{{0.3, 28}, {1.2, 70}, {2.2, 28}, {3.4, 70}}, s.lt)
	fx.circle(p, r, fx.theme.na)
}
```

`bez(p, a, b, c, d)` evaluates a cubic bezier with those four control values at `p`, for curves that keyframes are too coarse for.

## Random, but the same every time

A render must produce the same frame every time it is asked, so there is no global random state.

```odin
rng    :: proc(seed: u32) -> Rng
rand   :: proc(r: ^Rng) -> f32               // next number in 0..1
hash01 :: proc(seed: u32, i: int) -> f32     // a number in 0..1 for (seed, i), no state
wob    :: proc(t: f32, seed: f32 = 0) -> f32 // smooth drift in -1..1
```

Seed an `Rng` at the top of a scene and draw from it in a fixed order, or use `hash01` when you want "the random number of item `i`" without carrying state around (the bar heights in the stagger demo). `wob` is for motion: two slow sines that never quite repeat, good for camera drift and hand-held jitter.

```odin
anim_wob :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	amount := knob("amount", 40, 0, 120)
	for i in 0 ..< 5 {
		seed := f32(i)
		c := fx.Vec2{480 + f32(i) * 240, 540}
		p := c + {fx.wob(s.t, seed), fx.wob(s.t, seed + 0.5)} * amount
		fx.ring(c, 8, fx.theme.ns, 2)
		fx.circle(p, 36, i == 2 ? fx.theme.na : fx.theme.nt)
	}
}
```

## Reference

| Procedure | Returns |
| --- | --- |
| `clamp01(x)` | `x` clamped to 0..1. |
| `lerp(a, b, p)`, `lerp2(a, b, p)` | The value or point `p` of the way from `a` to `b`. |
| `prog(t, a, b)` | Linear progress of `t` through `[a, b]`, clamped. |
| `ep(t, a, b, e)` | Eased progress. |
| `win(t, a, b, fi, fo)` | 1 inside `[a, b]`, fading at both ends. |
| `stagger(t, i, a, step, dur, e)` | Eased progress for the `i`-th of a set. |
| `spring(p, freq, damping)` | Damped oscillation from 0, settling at 1. |
| `wob(t, seed)` | Smooth pseudo-random value in -1..1. |
| `bez(p, a, b, c, d)` | Point on a cubic bezier. |
| `typed(text, t, t0, t1)` | The prefix of `text` typed so far. |
| `count(t, t0, t1, a, b, e)` | A number counting from `a` to `b`. |
| `keys(ks, t, e)`, `keys2(ks, t, e)` | Value or point between keyframes. |
| `rng(seed)`, `rand(&r)`, `hash01(seed, i)` | Deterministic random numbers. |
