Function weight

Applies a function fn to the elements of an array, weighting them based on their relative position.

import { weight } from 'https://unpkg.com/ixfx/dist/numbers.js';
import { gaussian } from 'https://unpkg.com/ixfx/dist/modulation.js';
// Six items
weight([1,1,1,1,1,1], gaussian());

// Yields:
// [0.02, 0.244, 0.85, 0.85, 0.244, 0.02]

fn is expected to map (0..1) => (0..1), such as an easing function. The input to the fn is the relative position of an element. Thus the first element will be 0, the middle 0.5 and so on. The output of fn is then multiplied by the original value.

In the below example (which is also the default if fn is not specified), the relative position is how values are weighted:

weight([1,1,1,1,1,1], (relativePos) => relativePos);
// Yields:
// [0, 0.2, 0.4, 0.6, 0.8, 1]

Non-numbers in data will be silently ignored (this filtering happens first, so relative index values are sane still).

  • Parameters

    • data: number[] | readonly number[]

      Array of numbers

    • Optionalfn: ((relativePos: number) => number)

      Returns a weighting based on the given relative position. If unspecified, (x) => x is used.

        • (relativePos): number
        • Parameters

          • relativePos: number

          Returns number

    Returns number[]