Techniqueintro
Ease-in and ease-out
Also called easing curves, slow in and slow out.
Reparametrize time with a monotone curve, such as a sine or a cubic, so that a motion starts from rest, speeds up, and slows down into rest at the next key.
Ease-in and ease-out shapes the timing of a motion. A path or an interpolation goes from a start value to an end value as a parameter goes from 0 to 1. Instead of , the technique uses for a monotone curve with , and . The motion then starts from rest, speeds up, and comes to rest at the end. This is the principle of slow in and slow out of traditional animation in a computable form.
In plain words
A real object does not jump from standing still to full speed. It speeds up at the start and slows down at the end. If you move an object between two keys at a steady pace, it starts and stops with a jolt. An ease curve fixes this. It takes the clock time and returns how far along the path the object should be. Near the start and the end, the curve is flat, so the object moves slowly there. In the middle, the curve is steep, so the object moves faster to make up the time. The path stays the same; only the timing changes.
When it applies
- A value moves between two keys, and it should be at rest at both keys: a camera move that starts and stops, a door that opens, a fade.
- A path is given, and the motion along it should start and stop smoothly. Then is the fraction of the arc length, and an arc length table turns it into the curve parameter.
- A keyframe interpolation through many keys is too uniform in speed, and the animator wants to accent some keys with a hold.
It does not apply when the motion must keep its speed through a key, as in a cycle or a moving hold, because an ease curve forces the speed to 0 at each end.
The template
- Normalize the time of the segment: for a clock time between the keys at and .
- Choose an ease curve and compute .
- If is the fraction of the distance along a path of length , find the curve parameter with arc length from an arc length table. Otherwise use directly as the interpolation parameter.
- Evaluate the path or the interpolation at that parameter.
The three common curves are these.
EASE(t) ▷ t is the normalized time- 1
t ← min(max(t, 0), 1) - 2
sine: return (sin(πt - π/2) + 1) / 2 - 3
cubic: return 3t² - 2t³ - 4
constant acceleration with ramps [0, t₁] and [t₂, 1]: v₀ ← 2 / (1 + t₂ - t₁) - 5
if t < t₁ then return v₀ t² / (2t₁) - 6
if t ≤ t₂ then return v₀ t₁ / 2 + v₀ (t - t₁) - 7
d ← t - t₂; return v₀ t₁ / 2 + v₀ (t₂ - t₁) + v₀ d (1 - d / (2(1 - t₂)))
Line L2 is half a period of a sine, shifted to run from 0 to 1. Its speed is , with the peak at . Line L3 is the cubic Hermite segment with zero tangents, often called smoothstep. Its speed is , with the peak . Lines L4 to L7 are the constant acceleration profile: the speed grows linearly from 0 to on , stays at , and falls linearly to 0 on . The area under the speed is , and the choice of in line L4 makes it 1.
Each curve is monotone, maps onto , and has zero slope at both ends. For the sine and the cubic this follows from and on . For the constant acceleration profile the speed is a trapezoid that is not negative and has area 1. These are proved properties of the formulas. Which curve looks right is a matter of taste.
Classic examples
- Move between two keys. With , and the cubic, the point at is . Without easing it would be .
- Values at . The sine curve gives , the cubic , and the constant acceleration curve with , gives . All three give at , by symmetry.
- The constant acceleration check. With and , , the acceleration is , and the distance at is .
- A move along a path. A camera dolly along a Catmull-Rom path of length 10 at the time with the cubic goes to the arc length . The arc length table gives the curve parameter, and the path gives the position.
- Rotations. The same curve times a slerp: turns about a fixed axis, from rest to rest.
Variations
- Ease-in only or ease-out only. Use the first half of a curve, scaled to , for example for ease-in. The speed is 0 at one end only.
- Sine with a constant speed part. Parent (2012) joins two quarter periods of a sine with a straight middle part, so the curve has a phase of constant speed, as in lines L4 to L7.
- Higher order. The quintic also has zero acceleration at both ends, so the acceleration has no jump when the motion starts. The time scaling of robotics uses the same cubic and quintic polynomials.
- Many keys. An animator edits the timing curve of a whole shot as a spline of time against distance, and the speed may stay above 0 at the inner keys. Steketee and Badler (1985) used a second spline for this timing.
- Tension at the keys. The tension of the Kochanek-Bartels spline slows the motion near a key without a separate timing curve, but it also changes the path.
- Overshoot curves. Interface toolkits add curves that pass beyond 1 and come back, or bounce. These are not monotone, so they are not easing in the sense of this page.
Pitfalls
- Easing the curve parameter instead of the distance. If the path parameter does not grow with arc length, applied to gives a speed that also depends on the spacing of the control points. Use an arc length table first.
- A higher peak speed. The motion takes the same time, so it must move faster in the middle: times the mean speed for the sine, for the cubic, and for the constant acceleration curve. A short ramp needs a large acceleration.
- Stops at every key. Easing each segment of a many-key motion makes the object stop at each key. This is the right result only for holds.
- A jump in the acceleration. The sine and the cubic start with a nonzero acceleration, and 6. For physical devices, use the quintic or a jerk-limited profile.
- Times outside . Clamp , as in line L1, or the cubic returns values outside .
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/ease-in-ease-out/. The authors are listed in prov/M1V8Y9.jsonl.
"""Ease-in and ease-out curves.
Each function maps the normalized time t in [0, 1] to the fraction s(t) of
the distance travelled, with s(0) = 0, s(1) = 1, s monotone and the slope
s'(0) = s'(1) = 0. The constant acceleration curve has ramps on [0, t1] and
[t2, 1], with 0 < t1 <= t2 < 1, and constant speed in between.
"""
import math
def _clamp(t):
return 0.0 if t < 0.0 else 1.0 if t > 1.0 else t # @line L1
def sine_ease(t):
"""Return (sin(pi t - pi / 2) + 1) / 2."""
t = _clamp(t)
return (math.sin(math.pi * t - math.pi / 2.0) + 1.0) / 2.0 # @line L2
def smoothstep(t):
"""Return 3 t^2 - 2 t^3, the cubic with zero slope at both ends."""
t = _clamp(t)
return t * t * (3.0 - 2.0 * t) # @line L3
def const_accel(t, t1, t2):
"""Return the distance fraction of the constant acceleration profile."""
if not 0.0 < t1 <= t2 < 1.0:
raise ValueError("need 0 < t1 <= t2 < 1")
t = _clamp(t)
v0 = 2.0 / (1.0 + t2 - t1) # @line L4
if t < t1:
return v0 * t * t / (2.0 * t1) # @line L5
if t <= t2:
return v0 * t1 / 2.0 + v0 * (t - t1) # @line L6
d = t - t2
return v0 * t1 / 2.0 + v0 * (t2 - t1) + v0 * d * (1.0 - d / (2.0 * (1.0 - t2))) # @line L7
def sine_speed(t):
"""Return the slope of sine_ease, pi / 2 sin(pi t)."""
return math.pi / 2.0 * math.sin(math.pi * _clamp(t))
def smoothstep_speed(t):
"""Return the slope of smoothstep, 6 t (1 - t)."""
t = _clamp(t)
return 6.0 * t * (1.0 - t)
def const_accel_speed(t, t1, t2):
"""Return the slope of const_accel: a trapezoid with the top v0."""
t = _clamp(t)
v0 = 2.0 / (1.0 + t2 - t1)
if t < t1:
return v0 * t / t1
if t <= t2:
return v0
return v0 * (1.0 - (t - t2) / (1.0 - t2))
def ease_between(a, b, t, ease=smoothstep):
"""Return the value at the time t between the keys a and b, eased."""
s = ease(t)
return tuple(x + s * (y - x) for x, y in zip(a, b))
test_ease_in_ease_out.py 93 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/ease-in-ease-out/. The authors are listed in prov/M1V8Y9.jsonl.
import math
import random
import pytest
from ease_in_ease_out import (
const_accel,
const_accel_speed,
ease_between,
sine_ease,
sine_speed,
smoothstep,
smoothstep_speed,
)
def curves(rng):
out = [(sine_ease, sine_speed), (smoothstep, smoothstep_speed)]
out.append((lambda t: const_accel(t, 0.2, 0.8), lambda t: const_accel_speed(t, 0.2, 0.8)))
for _ in range(5):
t1 = rng.uniform(0.01, 0.5)
t2 = rng.uniform(t1, 0.99)
out.append((lambda t, a=t1, b=t2: const_accel(t, a, b), lambda t, a=t1, b=t2: const_accel_speed(t, a, b)))
return out
def test_end_values_and_monotone(rng=random.Random(1)):
for f, _ in curves(rng):
assert abs(f(0.0)) < 1e-15 and abs(f(1.0) - 1.0) < 1e-12
prev = -1.0
for k in range(2001):
s = f(k / 2000.0)
assert s >= prev - 1e-15 and -1e-15 <= s <= 1.0 + 1e-12
prev = s
def test_zero_slope_at_both_ends(rng=random.Random(2)):
h = 1e-6
for f, v in curves(rng):
assert f(h) / h < 1e-4
assert (1.0 - f(1.0 - h)) / h < 1e-4
assert abs(v(0.0)) < 1e-12 and abs(v(1.0)) < 1e-12
def test_speed_is_the_derivative(rng=random.Random(3)):
h = 1e-6
for f, v in curves(rng):
for _ in range(50):
t = rng.uniform(0.01, 0.99)
num = (f(t + h) - f(t - h)) / (2 * h)
# Near a kink of the speed at t1 or t2, the centered difference errs
# by at most h / 4 times the acceleration, which is below 1e-4 here.
assert abs(num - v(t)) < 1e-4
def test_const_accel_against_integrated_speed(rng=random.Random(4)):
# Reference: integrate the trapezoid speed profile with a fine midpoint rule.
for _ in range(5):
t1 = rng.uniform(0.05, 0.5)
t2 = rng.uniform(t1, 0.95)
m = 4000
acc = 0.0
for k in range(m):
acc += const_accel_speed((k + 0.5) / m, t1, t2) / m
if (k + 1) % 400 == 0:
assert abs(acc - const_accel((k + 1) / m, t1, t2)) < 1e-5
def test_check_values():
assert abs(sine_ease(0.5) - 0.5) < 1e-15 and abs(smoothstep(0.5) - 0.5) < 1e-15
assert abs(sine_ease(0.25) - (1 - math.sqrt(0.5)) / 2) < 1e-15
assert abs(smoothstep(0.25) - 0.15625) < 1e-15
assert abs(sine_speed(0.5) - math.pi / 2) < 1e-15 and abs(smoothstep_speed(0.5) - 1.5) < 1e-15
assert abs(const_accel_speed(0.5, 0.2, 0.8) - 1.25) < 1e-15
assert abs(const_accel(0.2, 0.2, 0.8) - 0.125) < 1e-15
assert abs(const_accel(0.5, 0.2, 0.8) - 0.5) < 1e-15
with pytest.raises(ValueError):
const_accel(0.5, 0.6, 0.4)
def test_symmetry(rng=random.Random(5)):
for _ in range(100):
t = rng.random()
assert abs(sine_ease(t) + sine_ease(1 - t) - 1) < 1e-14
assert abs(smoothstep(t) + smoothstep(1 - t) - 1) < 1e-14
assert abs(const_accel(t, 0.3, 0.7) + const_accel(1 - t, 0.3, 0.7) - 1) < 1e-12
def test_ease_between():
p = ease_between((0.0, 2.0), (4.0, 6.0), 0.25)
assert abs(p[0] - 0.625) < 1e-14 and abs(p[1] - 2.625) < 1e-14
Exercises
- Show that the sine curve and the cubic differ by at most on . (Hint: the difference is at , and its maximum is near and .)
- Find and the distance at of the constant acceleration curve with and . (Answer: and .)
- Show that a cubic with , and is unique, and that it is .
- Compose the cubic with itself, . Is the result still an ease curve? What is its peak speed?
References
- Rick Parent, Computer Animation, Algorithms and Techniques, 3rd edition, Morgan Kaufmann, 2012, Chapter 3, Section 3.2 (controlling the motion of a point along a curve: the sine ease curve, the sine curve with a constant speed part, and constant acceleration)
- Scott N. Steketee and Norman I. Badler, Parametric keyframe interpolation incorporating kinetic adjustment and phrasing control, Computer Graphics (Proceedings of SIGGRAPH 1985) 19(3), 255-262, 1985, the whole paper (a separate spline for the timing of the keys)
- Frank Thomas and Ollie Johnston, Disney Animation, The Illusion of Life, Abbeville Press, New York, 1981, Chapter 3, The Principles of Animation (slow in and slow out)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag M1V8Y9 Edit this page History Report an error Markdown