Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "iter/interpolate"

Index

Functions

Functions

interpolate

  • interpolate<A, B, C>(n: number, minPos: number, maxPos: number, init: Fn2<A, A, B>, mix: Fn2<B, number, C>, ...stops: [number, A][]): IterableIterator<C>
  • Takes a number of keyframe tuples (stops) and yields a sequence of n+1 equally spaced, interpolated values. Keyframes are defined as [pos, value]. Only values in the closed minPos .. maxPos interval will be computed.

    Interpolation happens in two stages: First the given init function is called to transform/prepare pairs of consecutive keyframes into a single interval (user defined). Then to produce each interpolated value calls mix with the currently active interval and interpolation time value t (re-normalized and relative to current interval). The iterator yields results of these mix() function calls.

    Depending on the overall number of samples requested and the distance between keyframes, some keyframes MIGHT be skipped. E.g. if requesting 10 samples within [0,1], the interval between two successive keyframes at 0.12 and 0.19 would be skipped entirely, since samples will only be taken at multiples of 1/n (0.0, 0.1, 0.2... in this example).

    The given keyframe positions can lie outside the minPos/maxPos range and also don't need to cover the range fully. In the latter case, interpolated values before the first or after the last keyframe will yield the value of the 1st/last keyframe. If only a single keyframe is given in total, all n yielded samples will be that keyframe's transformed value.

    [...interpolate(
      10,
      0,
      100,
      (a, b) => [a, b],
      ([a, b], t) => Math.floor(a + (b - a) * t),
      [20, 100],
      [50, 200],
      [80, 0]
    )]
    // [ 100, 100, 100, 133, 166, 200, 133, 66, 0, 0, 0 ]

    Using easing functions (e.g. from thi.ng/math), non-linear interpolation within each keyframe interval can be achieved:

    import { mix, smoothStep } from "@thi.ng/math"
    
    [...interpolate(
      10,
      0,
      100,
      (a, b) => [a, b],
      ([a, b], t) => Math.floor(mix(a, b, smoothStep(0.1, 0.9, t))),
      [20, 100],
      [50, 200],
      [80, 0]
    )]
    // [ 100, 100, 100, 120, 179, 200, 158, 41, 0, 0, 0 ]

    Type parameters

    • A

    • B

    • C

    Parameters

    • n: number
    • minPos: number
    • maxPos: number
    • init: Fn2<A, A, B>

      interval producer (from 2 keyframe values)

    • mix: Fn2<B, number, C>

      interval interpolator

    • Rest ...stops: [number, A][]

      keyframe / stops

    Returns IterableIterator<C>

Generated using TypeDoc