template<class T>
Magnum::Animation::BasicEasing struct new in Git master

Easing functions.

A collection of predefined easing / tweening functions for adding life to animation interpolation. Meant to be used through the Easing and Easingd typedefs.

This library is built as part of Magnum by default. To use this library with CMake, find the Magnum package and link to the Magnum::Magnum target:

find_package(Magnum REQUIRED)

# ...
target_link_libraries(your-app PRIVATE Magnum::Magnum)

See Downloading and building, Usage with CMake and Keyframe-based animation for more information.

The easing function is meant to be used to modify the interpolation factor, such as:

Vector3 result = Math::lerp(a, b, Animation::Easing::quadraticInOut(t));

The Animation library also provides the ease() utility that combines the interpolator together with the easing function:

auto lerpQuadraticInOut =
    Animation::ease<Vector3, Math::lerp, Animation::Easing::quadraticInOut>();

Vector3 result = lerpQuadraticInOut(a, b, t);

Equations

Every function documentation shows a plot of its behavior, together with a direction in which it extrapolates. Green color means the extrapolation goes in a reasonable monotonic direction outside of the range (also denoted by E in the above list), red color means the extrapolation is defined, but behaves in a probably unwanted or non-monotonic way (denoted by E in the above list). If neither is present, it means the function is not defined outside of the $ [ 0; 1 ] $ range and produces a NaN. You may want to ensure the factor stays in bounds, using either Math::clamp() or the easeClamped() function — the following two expressions are equivalent:

auto lerpCircularOutClamped = Animation::easeClamped<
    Vector3, Math::lerp, Animation::Easing::quadraticInOut>();

Vector3 result1 = Math::lerp(a, b,
    Math::clamp(0.0f, 1.0f, Animation::Easing::circularOut(t)));
Vector3 result2 = lerpCircularOutClamped(a, b, t);

Out-function $ f_\text{out} $ for a corresponding in-function $ f_\text{in} $ is defined as the following, the equations in the docs usually just show the final derived form. Similarly goes for combined in-/out-function $ f_\text{inout} $ :

\[ \begin{array}{rcl} f_\text{out}(x) & = & 1 - f_\text{in}(1 - x) \\[5pt] f_\text{inout}(x) & = & \left. \begin{cases} \frac{1}{2} f_\text{in}(2x), & x < 0.5 \\ \frac{1}{2} (1 + f_\text{out}(2x - 1)), & x \ge 0.5 \end{cases} \right\} = \begin{cases} \frac{1}{2} f_\text{in}(2x), & x < 0.5 \\ 1 - \frac{1}{2} f_\text{in}(2 - 2x), & x \ge 0.5 \end{cases} \end{array} \]

Easing functions defined by simple polynomials can have an exact (denoted by B in the above list) or approximate (denoted by B in the above list) cubic Math::Bezier curve representation (and thus, in turn, convertible to Cubic Hermite splines using Math::CubicHermite::fromBezier()). If that's the case, given function documentation also lists the corresponding Bézier representation and plots it with a thin blue line. The curve is always normalized to go from $ (0, 0)^T $ to $ (1, 1)^T $ , apply arbitrary transformation to each point as needed:

Matrix3 transformation;
CubicBezier2D easing;
CubicBezier2D transformed{
    transformation.transformPoint(easing[0]),
    transformation.transformPoint(easing[1]),
    transformation.transformPoint(easing[2]),
    transformation.transformPoint(easing[3])};

References

Functions follow the common naming from Robert Penner's Easing functions, http://robertpenner.com/easing/. Implementation based on and inspired by https://easings.net/, https://github.com/warrenm/AHEasing, https://github.com/bkaradzic/bx, https://blog.demofox.org/2014/08/28/one-dimensional-bezier-curves/.

Public static functions

static auto linear(T t) -> T
Linear.
static auto step(T t) -> T
Step.
static auto smoothstep(T t) -> T
Smoothstep.
static auto smootherstep(T t) -> T
Smootherstep.
static auto quadraticIn(T t) -> T
Quadratic in.
static auto quadraticOut(T t) -> T
Quadratic out.
static auto quadraticInOut(T t) -> T
Quadratic in and out.
static auto cubicIn(T t) -> T
Cubic in.
static auto cubicOut(T t) -> T
Cubic out.
static auto cubicInOut(T t) -> T
Cubic in and out.
static auto quarticIn(T t) -> T
Quartic in.
static auto quarticOut(T t) -> T
Quartic out.
static auto quarticInOut(T t) -> T
Quartic in and out.
static auto quinticIn(T t) -> T
Quintic in.
static auto quinticOut(T t) -> T
Quintic out.
static auto quinticInOut(T t) -> T
Quintic in and out.
static auto sineIn(T t) -> T
Sine in.
static auto sineOut(T t) -> T
Sine out.
static auto sineInOut(T t) -> T
Sine in and out.
static auto circularIn(T t) -> T
Circular in.
static auto circularOut(T t) -> T
Circular out.
static auto circularInOut(T t) -> T
Circular in and out.
static auto exponentialIn(T t) -> T
Exponential in.
static auto exponentialOut(T t) -> T
Exponential out.
static auto exponentialInOut(T t) -> T
Exponential in and out.
static auto elasticIn(T t) -> T
Elastic in.
static auto elasticOut(T t) -> T
Elastic out.
static auto elasticInOut(T t) -> T
Elastic in and out.
static auto backIn(T t) -> T
Back in.
static auto backOut(T t) -> T
Back out.
static auto backInOut(T t) -> T
Back in and out.
static auto bounceIn(T t) -> T
Bounce in.
static auto bounceOut(T t) -> T
Bounce out.
static auto bounceInOut(T t) -> T
Bounce in and out.

Function documentation

template<class T>
static T Magnum::Animation::BasicEasing<T>::linear(T t)

Linear.

\[ y = x \]

One possible exact Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{1.0f/3.0f},
              Vector2{2.0f/3.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::step(T t)

Step.

Similar to Math::select(), but does the step in the middle of the range instead of at the end. Implementation matching the GLSL step() function with edge = T(0.5).

\[ y = \begin{cases} 0, & x < 0.5 \\ 1, & x \ge 0.5 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::smoothstep(T t)

Smoothstep.

Implementation matching the GLSL smoothstep() function. Combine with Math::lerp() to get the equivalent result:

Math::lerp(a, b, Animation::Easing::smoothstep(t));
\[ y = \begin{cases} 0, & x < 0 \\ 3x^2 - 2x^3, & x \in [0, 1] \\ 1, & x > 1 \end{cases} \]

Exact Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{1.0f/3.0f, 0.0f},
              Vector2{2.0f/3.0f, 1.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::smootherstep(T t)

Smootherstep.

Improved version of smoothstep() by Ken Perlin.

\[ y = \begin{cases} 0, & x < 0 \\ 6x^5 - 15x^4 + 10x^3, & x \in [0, 1] \\ 1, & x > 1 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::quadraticIn(T t)

Quadratic in.

\[ y = x^2 \]

Exact Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{1.0f/3.0f, 0.0f},
              Vector2{2.0f/3.0f, 1.0f/3.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::quadraticOut(T t)

Quadratic out.

\[ y = 1 - (1 - x)^2 = (2 - x) x \]

Exact Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{1.0f/3.0f, 2.0f/3.0f},
              Vector2{2.0f/3.0f, 1.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::quadraticInOut(T t)

Quadratic in and out.

Combination of quadraticIn() and quadraticOut().

\[ y = \begin{cases} 2 x^2, & x < 0.5 \\ 1 - 2 (1 - x)^2, & x \ge 0.5 \end{cases} \]

Approximate Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{0.455f, 0.0f},
              Vector2{0.545f, 1.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::cubicIn(T t)

Cubic in.

\[ y = x^3 \]

Exact Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{1.0f/3.0f, 0.0f},
              Vector2{2.0f/3.0f, 0.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::cubicOut(T t)

Cubic out.

\[ y = 1 - (1 - x)^3 \]

Exact Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{1.0f/3.0f, 1.0f},
              Vector2{2.0f/3.0f, 1.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::cubicInOut(T t)

Cubic in and out.

Combination of cubicIn() and cubicOut().

\[ y = \begin{cases} 4 x^3, & x < 0.5 \\ 1 - 4 (1 - x)^3, & x \ge 0.5 \end{cases} \]

Approximate Bézier representation:

CubicBezier2D{Vector2{0.0f}, Vector2{0.645f, 0.0f},
              Vector2{0.355f, 1.0f}, Vector2{1.0f}}

template<class T>
static T Magnum::Animation::BasicEasing<T>::quarticIn(T t)

Quartic in.

\[ y = x^4 \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::quarticOut(T t)

Quartic out.

\[ y = 1 - (1 - x)^4 \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::quarticInOut(T t)

Quartic in and out.

Combination of quarticIn() and quarticOut().

\[ y = \begin{cases} 8 x^4, & x < 0.5 \\ 1 - 8 (1 - x)^4, & x \ge 0.5 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::quinticIn(T t)

Quintic in.

\[ y = x^5 \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::quinticOut(T t)

Quintic out.

\[ y = 1 - (1 - x)^5 \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::quinticInOut(T t)

Quintic in and out.

Combination of quinticIn() and quinticOut().

\[ y = \begin{cases} 16 x^5, & x < 0.5 \\ 1 - 16 (1 - x)^5, & x \ge 0.5 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::sineIn(T t)

Sine in.

\[ y = 1 + \sin(\frac{\pi}{2} (x - 1)) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::sineOut(T t)

Sine out.

\[ y = \sin(\frac{\pi}{2} x) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::sineInOut(T t)

Sine in and out.

Combination of sineIn() and sineOut().

\[ y = \frac{1}{2} (1 - \cos(\pi x)) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::circularIn(T t)

Circular in.

\[ y = 1 - \sqrt{1 - x^2} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::circularOut(T t)

Circular out.

\[ y = \sqrt{(2 - x) x} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::circularInOut(T t)

Circular in and out.

Combination of circularIn() and circularOut().

\[ y = \begin{cases} \frac{1}{2} (1 - \sqrt{1 - (2x)^2}), & x < 0.5 \\ \frac{1}{2} (1 + \sqrt{1 - (2x - 2)^2}), & x \ge 0.5 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::exponentialIn(T t)

Exponential in.

Contrary to Robert Penner's book but consistently with other implementations has a special case for $ x \le 0 $ , because $ 2^{-10} = 0.0009765625 $ otherwise.

\[ y = \begin{cases} 0, & x \le 0 \\ 2^{10(x - 1)}, & x \ne 0 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::exponentialOut(T t)

Exponential out.

Contrary to Robert Penner's book but consistently with other implementations has a special case for $ x \ge 1 $ , because $ 2^{-10} = 0.0009765625 $ otherwise.

\[ y = \begin{cases} 2^{-10 x}, & x < 1 \\ 1, & x \ge 1 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::exponentialInOut(T t)

Exponential in and out.

Combination of exponentialIn() and exponentialOut(). Contrary to Robert Penner's book but consistently with other implementations has a special case for $ x \notin \{0, 1\} $ , because $ 2^{-10} = 0.0009765625 $ otherwise.

\[ y = \begin{cases} 0, & x \le 0 \\ \frac{1}{2} 2^{20 x - 10}, & x \in (0, 0.5) \\ 1 - \frac{1}{2} 2^{10 - 20 x}, & x \in [0.5, 1) \\ 1, & x \ge 1 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::elasticIn(T t)

Elastic in.

Combines sineIn() and exponentialIn().

\[ y = 2^{10 (p - 1)} \sin(13 \frac{\pi}{2} x) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::elasticOut(T t)

Elastic out.

Combines sineOut() and exponentialOut().

\[ y = 1 - 2^{-10 x} \sin(13 \frac{\pi}{2} (x + 1)) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::elasticInOut(T t)

Elastic in and out.

Combination of elasticIn() and elasticOut() (or sineInOut() and exponentialInOut()).

\[ y = \begin{cases} \frac{1}{2} 2^{10 (2x - 1)} \sin(13 \pi x), & x < 0.5 \\ 1 - \frac{1}{2} 2^{10 (1 - 2x)} \sin(13 \pi x), & x \ge 0.5 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::backIn(T t)

Back in.

\[ y = x^3 - x \sin(\pi x) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::backOut(T t)

Back out.

\[ y = 1 - ((1 - x)^3 - (1 - x) \sin(\pi (1 - x))) \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::backInOut(T t)

Back in and out.

Combination of backIn() and backOut().

\[ y = \begin{cases} \frac{1}{2} ((2x)^3 - 2x \sin(2 \pi x)), & x < 0.5 \\ 1 - \frac{1}{2} ((2 - 2x)^3 - (2 - 2x) \sin(\pi (2 - 2x)), & x \ge 0.5 \end{cases} \]

template<class T>
static T Magnum::Animation::BasicEasing<T>::bounceIn(T t)

Bounce in.

template<class T>
static T Magnum::Animation::BasicEasing<T>::bounceOut(T t)

Bounce out.

template<class T>
static T Magnum::Animation::BasicEasing<T>::bounceInOut(T t)

Bounce in and out.

Combination of bounceIn() and bounceOut().