Utilities

Utilities

LittleJS Utility Classes and Functions
- General purpose math library
- Vector2 - fast, simple, easy 2D vector class
- Color - holds a rgba color with some math functions
- Timer - tracks time automatically

Members

# (static, constant) PI

A shortcut to get Math.PI

Methods

# (static) abs(value) → {Number}

Returns absoulte value of value passed in
Parameters:
Name Type Description
value Number
Returns:
Type
Number

# (static) clamp(value, maxopt, minopt) → {Number}

Clamps the value beween max and min
Parameters:
Name Type Attributes Default Description
value Number
max Number <optional>
1
min Number <optional>
0
Returns:
Type
Number

# (static) formatTime(t) → {String}

Formats seconds to mm:ss style for display purposes
Parameters:
Name Type Description
t Number time in seconds
Returns:
Type
String

# (static) isOverlapping(pointA, sizeA, pointB, sizeB) → {Boolean}

Returns true if two axis aligned bounding boxes are overlapping
Parameters:
Name Type Description
pointA Vector2 Center of box A
sizeA Vector2 Size of box A
pointB Vector2 Center of box B
sizeB Vector2 Size of box B
Returns:
- True if overlapping
Type
Boolean

# (static) lerp(percent, maxopt, minopt) → {Number}

Linearly interpolates the percent value between max and min
Parameters:
Name Type Attributes Default Description
percent Number
max Number <optional>
1
min Number <optional>
0
Returns:
Type
Number

# (static) max(valueA, valueB) → {Number}

Returns highest of two values passed in
Parameters:
Name Type Description
valueA Number
valueB Number
Returns:
Type
Number

# (static) min(valueA, valueB) → {Number}

Returns lowest of two values passed in
Parameters:
Name Type Description
valueA Number
valueB Number
Returns:
Type
Number

# (static) mod(dividend, divisoropt) → {Number}

Returns first parm modulo the second param, but adjusted so negative numbers work as expected
Parameters:
Name Type Attributes Default Description
dividend Number
divisor Number <optional>
1
Returns:
Type
Number

# (static) nearestPowerOfTwo(value) → {Number}

Returns the nearest power of two not less then the value
Parameters:
Name Type Description
value Number
Returns:
Type
Number

# (static) percent(value, maxopt, minopt) → {Number}

Returns what percentage the value is between max and min
Parameters:
Name Type Attributes Default Description
value Number
max Number <optional>
1
min Number <optional>
0
Returns:
Type
Number

# (static) sign(value) → {Number}

Returns the sign of value passed in (also returns 1 if 0)
Parameters:
Name Type Description
value Number
Returns:
Type
Number

# (static) smoothStep(value) → {Number}

Applies smoothstep function to the percentage value
Parameters:
Name Type Description
value Number
Returns:
Type
Number

# (static) vec2(xopt, yopt) → {Vector2}

Create a 2d vector, can take another Vector2 to copy, 2 scalars, or 1 scalar
Parameters:
Name Type Attributes Default Description
x Number <optional>
0
y Number <optional>
0
Returns:
Type
Vector2
Example
let a = vec2(0, 1); // vector with coordinates (0, 1)
let b = vec2(a);    // copy a into b
a = vec2(5);        // set a to (5, 5)
b = vec2();         // set b to (0, 0)

# (static) wave(frequencyopt, amplitudeopt, topt) → {Number}

Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default
Parameters:
Name Type Attributes Default Description
frequency Number <optional>
1 Frequency of the wave in Hz
amplitude Number <optional>
1 Amplitude (max height) of the wave
t Number <optional>
time Value to use for time of the wave
Returns:
- Value waving between 0 and amplitude
Type
Number