Easing functions

Easing functions specify the rate of change of a parameter over time.

Linear interpolation

A degenerate case, same as lerp.

function linear(t) {
    return t
}

Quadratic easing

function easeInQuad(t) {
    return t * t
}

function easeOutQuad(t) {
    return t * (2 - t)
}

function easeInOutQuad(t) {
    return t < 0.5 ?
        2 * t * t :
        2 * t * (2 - t) - 1
}

Cubic easing

function easeInCubic(t) {
    return t * t * t
}

function easeOutCubic(t) {
    --t
    return t * t * t + 1
}

/* See below */
const easeInOutCubic = easeInOut(easeInCubic)

Sinusoidal easing

function easeInSine(t) {
    return 1 - Math.cos(t * Math.PI * 0.5)
}

function easeOutSine(t) {
    return Math.sin(t * Math.PI * 0.5)
}

function easeInOutSine(t) {
    return (1 - Math.cos(t * Math.PI)) * 0.5
}

Utility functions

The following helpers are used to modify easing functions.

/* Run an easing function backwards */
function easeOut(easing) {
    return function (t) {
        return 1 - easing(1 - t)
    }
}

/* Make an easing function symmetrical */
function easeInOut(easing) {
    return function (t) {
        if (t < 0.5) {
            return easing(t * 2) * 0.5
        }
        return 1 - easing((1 - t) * 2) * 0.5
    }
}