Wave

Values that change over time on their own: sine and pulse repeat, and noise wanders. Each is a function of time in seconds, or of any number, for something that bobs, breathes or drifts.

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

const bob = Wave.sine(8, 2) // 8 px up and down, every 2 s
const y = bob(seconds)

Functions

noise()

function noise(seed?): (x) => number;

Smooth random noise from -1 to 1: a function that wanders between a random value at each whole number. The same seed gives the same curve, so a scene drifts the same way each time it plays.

Parameters

seed?

number = 0

Returns

(x) => number

Example

const drift = Wave.noise(3)
const x = drift(seconds * 0.5) * 20 // within 20 px either side, changing every 2 s

pulse()

function pulse(period): (t) => number;

A pulse from 0 to 1 and back every period seconds: 0 as each cycle starts, 1 halfway. A period of 0 stays at 0.

Parameters

period

number

Returns

(t) => number

Example

const glow = Wave.pulse(1.5)
const opacity = 0.4 + glow(seconds) * 0.6

sine()

function sine(amplitude, period): (t) => number;

A sine wave: a function of time, in seconds, from -amplitude to amplitude and back every period seconds. A period of 0 stays at 0.

Parameters

amplitude

number

period

number

Returns

(t) => number