Inertia

An inertia as a stepper: a glide that carries a value on from its velocity and eases to a stop, as a flick or a drag's release does, springing back from min or max if it would pass one. resolve works out the glide once, init starts it, and step moves it a frame. For something you step yourself each frame; otherwise Machine.inertia runs one as a machine.

It rests at initial + power × velocity, or where modifyTarget moves that, easing out over timeConstant seconds. Where the glide would cross an edge, or starts past one, a spring takes it to the nearest edge, from the glide's speed there.

import * as Inertia from "@wardic/core/Inertia"

const glide = Inertia.resolve(x, velocity, { min: 0, max: 400 })
let state = Inertia.init(x, glide)
// each frame:
const frame = Inertia.step(state, dt, glide)
state = frame.state
x = frame.value // and `frame.done` once it rests

Its exports also name types the module doesn't export: its internal types.

Interfaces

Resolved

An inertia's options with every default filled in, and its glide worked out from where it starts.

Properties

amplitude
readonly amplitude: number;

How far the glide goes: target − initial.

bounce
readonly bounce: BounceSpring;
max
readonly max: number;
min
readonly min: number;

The edges, −∞ and +∞ where none is given.

restDelta
readonly restDelta: number;
target
readonly target: number;

Where the glide rests: initial + power × velocity, or where modifyTarget moved that.

timeConstant
readonly timeConstant: number;

The glide's time constant, in seconds.


State

An inertia's state: its time, and once it reached an edge, the bounce toward it.

Properties

bounce
readonly bounce: Bounce | undefined;
elapsed
readonly elapsed: number;

Functions

init()

function init(initial, inertia): State;

An inertia at its start: gliding, or, when initial is already past an edge, springing back to it from the glide's speed at 0 (Motion's checkCatchBoundary(0)).

Parameters

initial

number

inertia

Resolved

Returns

State


resolve()

function resolve(
   initial, 
   velocity, 
   options
): Resolved;

An inertia's defaults filled in, and its glide worked out from initial at velocity: once each run. A value no inertia can take, a negative timeConstant say, throws an OptionError naming it. Its options are checked once a bag, kept by its identity: one changed after it's handed over isn't seen, so pass a new one.

Parameters

initial

number

velocity

number

options

InertiaOptions

Returns

Resolved


restOf()

function restOf(
   initial, 
   velocity, 
   options
): number;

Where an inertia from initial at velocity comes to rest: its glide's end, held to its edges, or the edge nearest a start past one. Where one that's skipped lands at once. Throws as resolve does.

Parameters

initial

number

velocity

number

options

InertiaOptions

Returns

number


step()

function step(
   state, 
   dt, 
   inertia
): PhysicsStepResult<State>;

One frame of a resolved inertia: its next state, value, velocity, and whether it's done. While it glides, its value is the glide's at its time; the frame that crosses an edge shows the glide past it, and the bounce takes over from there, resting on the edge within restDelta and restSpeed.

Parameters

state

State

dt

number

inertia

Resolved

Returns

PhysicsStepResult<State>