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
  • RandomGenerator - seeded random number generator

Members

(static, constant) BLACK :Color

Color - Black

Type:

(static, constant) BLUE :Color

Color - Blue

Type:

(static, constant) CYAN :Color

Color - Cyan

Type:

(static, constant) GRAY :Color

Color - Gray

Type:

(static, constant) GREEN :Color

Color - Green

Type:

(static, constant) MAGENTA :Color

Color - Magenta

Type:

(static, constant) ORANGE :Color

Color - Orange

Type:

(static, constant) PI :Number

A shortcut to get Math.PI

Type:
  • Number
Default Value
  • Math.PI

(static, constant) PURPLE :Color

Color - Purple

Type:

(static, constant) RED :Color

Color - Red

Type:

(static, constant) WHITE :Color

Color - White

Type:

(static, constant) YELLOW :Color

Color - Yellow

Type:

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) distanceAngle(angleA, angleB) → {Number}

Returns signed wrapped distance between the two angles passed in

Parameters:
NameTypeDescription
angleANumber
angleBNumber
Returns:
Type: 
Number

(static) distanceWrap(valueA, valueB, wrapSizeopt) → {Number}

Returns signed wrapped distance between the two values passed in

Parameters:
NameTypeAttributesDefaultDescription
valueANumber
valueBNumber
wrapSizeNumber<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, white by default

Parameters:
NameTypeAttributesDefaultDescription
hNumber<optional>
0

hue

sNumber<optional>
0

saturation

lNumber<optional>
1

lightness

aNumber<optional>
1

alpha

Returns:
Type: 
Color

(static) isColor(c) → {Boolean}

Check if object is a valid Color

Parameters:
NameTypeDescription
cany
Returns:
Type: 
Boolean

(static) isIntersecting(start, end, pos, size) → {Boolean}

Returns true if a line segment is intersecting an axis aligned box

Parameters:
NameTypeDescription
startVector2

Start of raycast

endVector2

End of raycast

posVector2

Center of box

sizeVector2

Size of box

Returns:
  • True if intersecting
Type: 
Boolean

(static) isOverlapping(posA, sizeA, posB, sizeBopt) → {Boolean}

Returns true if two axis aligned bounding boxes are overlapping

Parameters:
NameTypeAttributesDefaultDescription
posAVector2

Center of box A

sizeAVector2

Size of box A

posBVector2

Center of box B

sizeBVector2<optional>
(0,0)

Size of box B, a point if undefined

Returns:
  • True if overlapping
Type: 
Boolean

(static) isVector2(v) → {Boolean}

Check if object is a valid Vector2

Parameters:
NameTypeDescription
vany
Returns:
Type: 
Boolean

(static) lerp(percent, valueA, valueB) → {Number}

Linearly interpolates between values passed in using percent

Parameters:
NameTypeDescription
percentNumber
valueANumber
valueBNumber
Returns:
Type: 
Number

(static) lerpAngle(percent, angleA, angleB) → {Number}

Linearly interpolates between the angles passed in with wrapping

Parameters:
NameTypeDescription
percentNumber
angleANumber
angleBNumber
Returns:
Type: 
Number

(static) lerpWrap(percent, valueA, valueB, wrapSizeopt) → {Number}

Linearly interpolates between values passed in with wrapping

Parameters:
NameTypeAttributesDefaultDescription
percentNumber
valueANumber
valueBNumber
wrapSizeNumber<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, valueA, valueB) → {Number}

Returns what percentage the value is between valueA and valueB

Parameters:
NameTypeDescription
valueNumber
valueANumber
valueBNumber
Returns:
Type: 
Number

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

Create a color object with RGBA values, white by default

Parameters:
NameTypeAttributesDefaultDescription
rNumber<optional>
1

red

gNumber<optional>
1

green

bNumber<optional>
1

blue

aNumber<optional>
1

alpha

Returns:
Type: 
Color

(static) sign(value) → {Number}

Returns the sign of value passed in

Parameters:
NameTypeDescription
valueNumber
Returns:
Type: 
Number

(static) smoothStep(percent) → {Number}

Applies smoothstep function to the percentage value

Parameters:
NameTypeDescription
percentNumber
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 | Vector2<optional>
0
yNumber<optional>
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