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
- Source
Members
(static, constant) BLACK :Color
Color - Black #000000
- Color
- Source
(static, constant) BLUE :Color
Color - Blue #0000ff
- Color
- Source
(static, constant) CLEAR_BLACK :Color
Color - Clear Black #000000 with 0 alpha
- Color
- Source
(static, constant) CLEAR_WHITE :Color
Color - Clear White #757474ff with 0 alpha
- Color
- Source
(static, constant) CYAN :Color
Color - Cyan #00ffff
- Color
- Source
(static, constant) GRAY :Color
Color - Gray #808080
- Color
- Source
(static, constant) GREEN :Color
Color - Green #00ff00
- Color
- Source
(static, constant) MAGENTA :Color
Color - Magenta #ff00ff
- Color
- Source
(static, constant) ORANGE :Color
Color - Orange #ff8000
- Color
- Source
(static, constant) PI :number
The value of PI
- number
- Default Value
- Math.PI
- Source
(static, constant) PURPLE :Color
Color - Purple #8000ff
- Color
- Source
(static, constant) RED :Color
Color - Red #ff0000
- Color
- Source
(static, constant) WHITE :Color
Color - White #ffffff
- Color
- Source
(static, constant) YELLOW :Color
Color - Yellow #ffff00
- Color
- Source
(static, constant) abs
Returns absolute value of value passed in
- Source
(static, constant) atan2
Returns atan2 of values passed in
- Source
(static, constant) ceil
Returns ceiled value of value passed in
- Source
(static, constant) cos
Returns cos of value passed in
- Source
(static, constant) floor
Returns floored value of value passed in
- Source
(static, constant) hypot
Returns hypotenuse of values passed in
- Source
(static, constant) log2
Returns log2 of value passed in
- Source
(static, constant) max
Returns highest value passed in
- Source
(static, constant) min
Returns lowest value passed in
- Source
(static, constant) round
Returns rounded value passed in
- Source
(static, constant) sign
Returns the sign of value passed in
- Source
(static, constant) sin
Returns sin of value passed in
- Source
(static, constant) tan
Returns tan of value passed in
- Source
Methods
(static) clamp(value, minopt, maxopt) → {number}
Clamps the value between max and min
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
value | number | |||
min | number | <optional> | 0 | |
max | number | <optional> | 1 |
- Source
- Type:
- number
(static) distanceAngle(angleA, angleB) → {number}
Returns signed wrapped distance between the two angles passed in
| Name | Type | Description |
|---|---|---|
angleA | number | |
angleB | number |
- Source
- Type:
- number
(static) distanceWrap(valueA, valueB, wrapSizeopt) → {number}
Returns signed wrapped distance between the two values passed in
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
valueA | number | |||
valueB | number | |||
wrapSize | number | <optional> | 1 |
- Source
- Type:
- number
(async, static) fetchJSON(url) → {Promise.<object>}
Fetches a JSON file from a URL and returns the parsed JSON object. Must be used with await!
| Name | Type | Description |
|---|---|---|
url | string | URL of JSON file |
- Source
- Type:
- Promise.<object>
(static) formatTime(t) → {string}
Formats seconds to mm:ss style for display purposes
| Name | Type | Description |
|---|---|---|
t | number | time in seconds |
- Source
- Type:
- string
(static) hsl(hopt, sopt, lopt, aopt) → {Color}
Create a color object with HSLA values, white by default
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
h | number | <optional> | 0 | hue |
s | number | <optional> | 0 | saturation |
l | number | <optional> | 1 | lightness |
a | number | <optional> | 1 | alpha |
- Source
- Type:
- Color
(static) isArray(a) → {boolean}
Check if object is an array
| Name | Type | Description |
|---|---|---|
a | any |
- Source
- Type:
- boolean
(static) isColor(c) → {boolean}
Check if object is a valid Color
| Name | Type | Description |
|---|---|---|
c | any |
- Source
- Type:
- boolean
(static) isIntersecting(start, end, pos, size) → {boolean}
Returns true if a line segment is intersecting an axis aligned box
| Name | Type | Description |
|---|---|---|
start | Vector2 | Start of raycast |
end | Vector2 | End of raycast |
pos | Vector2 | Center of box |
size | Vector2 | Size of box |
- Source
- True if intersecting
- Type:
- boolean
(static) isNumber(n) → {boolean}
Check if object is a valid number, not NaN or undefined, but it may be infinite
| Name | Type | Description |
|---|---|---|
n | any |
- Source
- Type:
- boolean
(static) isOverlapping(posA, sizeA, posB, sizeBopt) → {boolean}
Returns true if two axis aligned bounding boxes are overlapping this can be used for simple collision detection between objects
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
posA | Vector2 | Center of box A | ||
sizeA | Vector2 | Size of box A | ||
posB | Vector2 | Center of box B | ||
sizeB | Vector2 | <optional> | (0,0) | Size of box B, uses a point if undefined |
- Source
- True if overlapping
- Type:
- boolean
(static) isPowerOfTwo(value) → {boolean}
Checks if the value passed in is a power of two
| Name | Type | Description |
|---|---|---|
value | number |
- Source
- Type:
- boolean
(static) isString(s) → {boolean}
Check if object is a valid string or can be converted to one
| Name | Type | Description |
|---|---|---|
s | any |
- Source
- Type:
- boolean
(static) isVector2(v) → {boolean}
Check if object is a valid Vector2
| Name | Type | Description |
|---|---|---|
v | any |
- Source
- Type:
- boolean
(static) lerp(valueA, valueB, percent) → {number}
Linearly interpolates between values passed in using percent
| Name | Type | Description |
|---|---|---|
valueA | number | |
valueB | number | |
percent | number |
- Source
- Type:
- number
(static) lerpAngle(angleA, angleB, percent) → {number}
Linearly interpolates between the angles passed in with wrapping
| Name | Type | Description |
|---|---|---|
angleA | number | |
angleB | number | |
percent | number |
- Source
- Type:
- number
(static) lerpWrap(valueA, valueB, percent, wrapSizeopt) → {number}
Linearly interpolates between values passed in with wrapping
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
valueA | number | |||
valueB | number | |||
percent | number | |||
wrapSize | number | <optional> | 1 |
- Source
- Type:
- number
(static) lineTest(posStart, posEnd, testFunction, normalopt) → {Vector2|undefined}
Casts a ray and returns position of the first collision found, or undefined if none are found
| Name | Type | Attributes | Description |
|---|---|---|---|
posStart | Vector2 | ||
posEnd | Vector2 | ||
testFunction | LineTestFunction | Check if colliding | |
normal | Vector2 | <optional> | Optional vector to store the normal |
- Source
- Position of the collision or undefined if none found
- Type:
- Vector2 |
undefined
(static) mod(dividend, divisoropt) → {number}
Returns first parm modulo the second param, but adjusted so negative numbers work as expected
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
dividend | number | |||
divisor | number | <optional> | 1 |
- Source
- Type:
- number
(static) nearestPowerOfTwo(value) → {number}
Returns the nearest power of two not less than the value
| Name | Type | Description |
|---|---|---|
value | number |
- Source
- Type:
- number
(static) percent(value, valueA, valueB) → {number}
Returns what percentage the value is between valueA and valueB
| Name | Type | Description |
|---|---|---|
value | number | |
valueA | number | |
valueB | number |
- Source
- Type:
- number
(static) percentLerp(value, percentA, percentB, lerpA, lerpB) → {number}
Gets percent between percentA and percentB and linearly interpolates between lerpA and lerpB A shortcut for lerp(lerpA, lerpB, percent(value, percentA, percentB))
| Name | Type | Description |
|---|---|---|
value | number | |
percentA | number | |
percentB | number | |
lerpA | number | |
lerpB | number |
- Source
- Type:
- number
(static) rgb(ropt, gopt, bopt, aopt) → {Color}
Create a color object with RGBA values, white by default
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
r | number | <optional> | 1 | red |
g | number | <optional> | 1 | green |
b | number | <optional> | 1 | blue |
a | number | <optional> | 1 | alpha |
- Source
- Type:
- Color
(static) smoothStep(percent) → {number}
Applies smoothstep function to the percentage value
| Name | Type | Description |
|---|---|---|
percent | number |
- Source
- Type:
- number
(static) vec2(xopt, yopt) → {Vector2}
Create a 2d vector, can take 1 or 2 scalar values
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
x | number | <optional> | 0 | |
y | number | <optional> | if y is undefined, x is used for both |
- Source
- Type:
- Vector2
let a = vec2(0, 1); // vector with coordinates (0, 1)
a = vec2(5); // set a to (5, 5)
b = vec2(); // set b to (0, 0)(static) wave(frequencyopt, amplitudeopt, topt, offsetopt) → {number}
Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default
| 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 |
offset | number | <optional> | 0 | Value to use for time offset of the wave |
- Source
- Value waving between 0 and amplitude
- Type:
- number