UNPKG

1.28 kBJavaScriptView Raw
1/**
2 * Oscillate between -1 and 1 given a value and a period. This is basically a simplification on using Math.sin().
3 * @alias Splat.math.oscillate
4 * @param {number} current The current value of the number you want to oscillate.
5 * @param {number} period The period, or how often the number oscillates. The return value will oscillate between -1 and 1, depending on how close current is to a multiple of period.
6 * @returns {number} A number between -1 and 1.
7 * @example
8Splat.math.oscillate(0, 100); // returns 0
9Splat.math.oscillate(100, 100); // returns 0-ish
10Splat.math.oscillate(50, 100); // returns 1
11Splat.math.oscillate(150, 100); // returns -1
12Splat.math.oscillate(200, 100); // returns 0-ish
13 */
14function oscillate(current, period) {
15 return Math.sin(current / period * Math.PI);
16}
17
18/**
19 * @namespace Splat.math
20 */
21module.exports = {
22 oscillate: oscillate,
23 /**
24 * A seedable pseudo-random number generator. Currently a Mersenne Twister PRNG.
25 * @constructor
26 * @alias Splat.math.Random
27 * @param {number} [seed] The seed for the PRNG.
28 * @see [mersenne-twister package at github]{@link https://github.com/boo1ean/mersenne-twister}
29 * @example
30var rand = new Splat.math.Random(123);
31var val = rand.random();
32 */
33 Random: require("mersenne-twister")
34};