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 :Number

A shortcut to get Math.PI

Type:
  • Number
Default Value
  • Math.PI

Methods

(static) abs(value) → {Number}

Returns absoulte value of value passed in

Parameters:
NameTypeDescription
valueNumber
Returns:
Type: 
Number

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

Clamps the value beween max and min

Parameters:
NameTypeAttributesDefaultDescription
valueNumber
minNumber<optional>
0
maxNumber<optional>
1
Returns:
Type: 
Number

(static) formatTime(t) → {String}

Formats seconds to mm:ss style for display purposes

Parameters:
NameTypeDescription
tNumber

time in seconds

Returns:
Type: 
String

(static) hsl(hopt, sopt, lopt, aopt) → {Color}

Create a color object with HSLA values

Parameters:
NameTypeAttributesDefaultDescription
hNumber<optional>
0
sNumber<optional>
0
lNumber<optional>
1
aNumber<optional>
1
Returns:
Type: 
Color

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

Returns true if two axis aligned bounding boxes are overlapping

Parameters:
NameTypeAttributesDescription
pointAVector2

Center of box A

sizeAVector2

Size of box A

pointBVector2

Center of box B

sizeBVector2<optional>

Size of box B

Returns:
  • True if overlapping
Type: 
Boolean

(static) isVector2(vector) → {Boolean}

Check if object is a valid Vector2

Parameters:
NameTypeDescription
vectorVector2
Returns:
Type: 
Boolean

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

Linearly interpolates the percent value between max and min

Parameters:
NameTypeAttributesDefaultDescription
percentNumber
minNumber<optional>
0
maxNumber<optional>
1
Returns:
Type: 
Number

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

Returns highest of two values passed in

Parameters:
NameTypeDescription
valueANumber
valueBNumber
Returns:
Type: 
Number

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

Returns lowest of two values passed in

Parameters:
NameTypeDescription
valueANumber
valueBNumber
Returns:
Type: 
Number

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

Returns first parm modulo the second param, but adjusted so negative numbers work as expected

Parameters:
NameTypeAttributesDefaultDescription
dividendNumber
divisorNumber<optional>
1
Returns:
Type: 
Number

(static) nearestPowerOfTwo(value) → {Number}

Returns the nearest power of two not less then the value

Parameters:
NameTypeDescription
valueNumber
Returns:
Type: 
Number

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

Returns what percentage the value is between max and min

Parameters:
NameTypeAttributesDefaultDescription
valueNumber
minNumber<optional>
0
maxNumber<optional>
1
Returns:
Type: 
Number

(static) rgb(ropt, gopt, bopt, aopt) → {Color}

Create a color object with RGBA values

Parameters:
NameTypeAttributesDefaultDescription
rNumber<optional>
1
gNumber<optional>
1
bNumber<optional>
1
aNumber<optional>
1
Returns:
Type: 
Color

(static) sign(value) → {Number}

Returns the sign of value passed in (also returns 1 if 0)

Parameters:
NameTypeDescription
valueNumber
Returns:
Type: 
Number

(static) smoothStep(value) → {Number}

Applies smoothstep function to the percentage value

Parameters:
NameTypeDescription
valueNumber
Returns:
Type: 
Number

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

Create a 2d vector, can take another Vector2 to copy, 2 scalars, or 1 scalar

Parameters:
NameTypeAttributesDefaultDescription
xNumber<optional>
0
yNumber<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:
NameTypeAttributesDefaultDescription
frequencyNumber<optional>
1

Frequency of the wave in Hz

amplitudeNumber<optional>
1

Amplitude (max height) of the wave

tNumber<optional>
time

Value to use for time of the wave

Returns:
  • Value waving between 0 and amplitude
Type: 
Number