Animations

Easing functions compared

Eight easing curves side by side on animated bars: linear, cubic, back and more, each reaching the target at the same time.

// The same move with eight easing functions: each dot travels back and forth
// along its lane, and every move starts the next one when it is done. The
// lanes sit in a layer whose anchor follows the centre of the frame.

import {VisQuill, Reactive, Vectors, Animate, EasingFunctions} from "@visquill/visquill-gdk";
import './styles.css';

// ─── Layout (in the layer: 0, 0 is the centre of the frame) ───────────────
const laneStart = -150
const laneEnd   = 250
const laneGap   = 50
const duration  = 1600

const easings = [
    ["linear", EasingFunctions.linear],
    ["easeInCubic", EasingFunctions.easeInCubic],
    ["easeOutCubic", EasingFunctions.easeOutCubic],
    ["easeInOutCubic", EasingFunctions.easeInOutCubic],
    ["easeInSine", EasingFunctions.easeInSine],
    ["easeOutSine", EasingFunctions.easeOutSine],
    ["easeInOutSine", EasingFunctions.easeInOutSine],
    ["easeOutBack", EasingFunctions.easeOutBack],
] as const

export function run(div: HTMLDivElement) {
    const rvg   = VisQuill.create(div, "demos-easing-")
    const lanes = rvg.canvas.layer()
    Reactive.do([rvg.frame], () => {
        Vectors.copy(rvg.center, lanes.anchor)
    })

    easings.forEach(([name, easing], i) => {
        const y = (i - (easings.length - 1) / 2) * laneGap
        lanes.visuals.segment("lane", [laneStart, y], [laneEnd, y])
        lanes.text.labelAt(name, [laneStart - 20, y], "name")
        const dot = lanes.visuals.point("dot", [laneStart, y])

        // Back and forth: every animation starts the next one when it is done
        const go = (to: number) => {
            if (!div.isConnected) return   // the demo is gone: stop
            Animate.point.eased(dot, [to, y], duration, {
                easing,
                onComplete: () => go(to === laneEnd ? laneStart : laneEnd),
            })
        }
        go(laneEnd)
    })
}

Eight bars animate to the same target, each with a different easing function, so the easings can be compared on the same change. Each height is bound with Attach.rectangleHeightToValue and animated with Animate.real.eased.

How it works

Each lane has one dot. Animate.point.eased moves it to the other end of the lane in 1600 ms, with that lane's easing function. When the move is done, onComplete starts the move back:

const go = (to: number) => {
    if (!div.isConnected) return   // the demo is gone: stop
    Animate.point.eased(dot, [to, y], duration, {
        easing,
        onComplete: () => go(to === laneEnd ? laneStart : laneEnd),
    })
}

All moves take the same time, so the dots differ only in how they move, not in when they arrive. easeIn starts slowly, easeOut ends slowly, easeInOut does both; easeOutBack overshoots a little.

Notes

The lanes sit in a layer whose anchor follows rvg.center, so they stay in the middle when the frame is resized.

API

  • Animate.point.eased: moves a point to a target in a fixed time
  • EasingFunctions.easeOutBack: one of the eight built-in curves shown here
  • RvgEasedOptions.onComplete: called when the target is reached

See also

  • animations/02-state-machine
  • animations/18-pendulum-wave

API used

Also used (15)