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
- Source
(static, constant) BLUE :Color
Color - Blue #0000ff
- Source
(static, constant) CYAN :Color
Color - Cyan #00ffff
- Source
(static, constant) GRAY :Color
Color - Gray #808080
- Source
(static, constant) GREEN :Color
Color - Green #00ff00
- Source
(static, constant) MAGENTA :Color
Color - Magenta #ff00ff
- Source
(static, constant) ORANGE :Color
Color - Orange #ff8000
- Source
(static, constant) PI :number
A shortcut to get Math.PI
- number
- Default Value
- Math.PI
- Source
(static, constant) PURPLE :Color
Color - Purple #8000ff
- Source
(static, constant) RED :Color
Color - Red #ff0000
- Source
(static, constant) WHITE :Color
Color - White #ffffff
- Source
(static, constant) YELLOW :Color
Color - Yellow #ffff00
- Source
Methods
(static) abs(value) → {number}
Returns absolute value of value passed in
Name | Type | Description |
---|---|---|
value | number |
- Source
- Type:
- number
(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
(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) 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) isOverlapping(posA, sizeA, posB, sizeBopt) → {boolean}
Returns true if two axis aligned bounding boxes are overlapping
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, a point if undefined |
- Source
- True if overlapping
- Type:
- boolean
(static) isVector2(v) → {boolean}
Check if object is a valid Vector2
Name | Type | Description |
---|---|---|
v | any |
- Source
- Type:
- boolean
(static) lerp(percent, valueA, valueB) → {number}
Linearly interpolates between values passed in using percent
Name | Type | Description |
---|---|---|
percent | number | |
valueA | number | |
valueB | number |
- Source
- Type:
- number
(static) lerpAngle(percent, angleA, angleB) → {number}
Linearly interpolates between the angles passed in with wrapping
Name | Type | Description |
---|---|---|
percent | number | |
angleA | number | |
angleB | number |
- Source
- Type:
- number
(static) lerpWrap(percent, valueA, valueB, wrapSizeopt) → {number}
Linearly interpolates between values passed in with wrapping
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
percent | number | |||
valueA | number | |||
valueB | number | |||
wrapSize | number | <optional> | 1 |
- Source
- Type:
- number
(static) max(valueA, valueB) → {number}
Returns highest of two values passed in
Name | Type | Description |
---|---|---|
valueA | number | |
valueB | number |
- Source
- Type:
- number
(static) min(valueA, valueB) → {number}
Returns lowest of two values passed in
Name | Type | Description |
---|---|---|
valueA | number | |
valueB | number |
- Source
- Type:
- number
(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 then 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) 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) sign(value) → {number}
Returns the sign of value passed in
Name | Type | Description |
---|---|---|
value | number |
- Source
- Type:
- number
(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 another Vector2 to copy, 2 scalars, or 1 scalar
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | Vector2 | | <optional> | 0 | |
y | number | <optional> |
- Source
- Type:
- Vector2
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
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 |
- Source
- Value waving between 0 and amplitude
- Type:
- number