Maths utilities
Requirements
- ES6 Modules support
- Using a module bundler like Webpack, Rollup or Parcel
- Native support from browser
- From NodeJS with something like esm
Module Installation
# using npm
$ npm install --save @internet/maths
# or using yarn
$ yarn add @internet/maths
API
import { mod, map, lerp, ... } from '@internet/maths'
API
-
maths
-
.mod(dividend, divisor) ⇒
number
-
.map(value, start1, stop1, start2, stop2) ⇒
number
-
.clamp(value, min, max) ⇒
number
-
.norm(value, min, max) ⇒
number
-
.lerp(start, end, amount) ⇒
number
-
.damp(source, target, smoothing, dt) ⇒
number
-
.dist(x1, y1, x2, y2) ⇒
number
-
.sqdist(x1, y1, x2, y2) ⇒
number
-
.ang(x1, y1, x2, y2) ⇒
number
-
.polarToCart(radius, angle) ⇒
array
-
.radToDeg(angle) ⇒
number
-
.degToRad(angle) ⇒
number
-
.mean(values) ⇒
number
-
.median(values) ⇒
number
-
.mod(dividend, divisor) ⇒
number
maths.mod(dividend, divisor) ⇒ Perform a modulo operation.
Kind: static method of maths
Returns: number
- Result of the modulo operation
Param | Type | Description |
---|---|---|
dividend | number |
A dividend |
divisor | number |
A divisor |
Example
const resut = mod(-1, 5) // return 4
number
maths.map(value, start1, stop1, start2, stop2) ⇒ Re-maps a number from one range to another.
Kind: static method of maths
Param | Type | Description |
---|---|---|
value | number |
The incoming value to be converted |
start1 | number |
Lower bound of the value's current range |
stop1 | number |
Upper bound of the value's current range |
start2 | number |
Lower bound of the value`'s target range |
stop2 | number |
Upper bound of the value's target range |
number
maths.clamp(value, min, max) ⇒ Constrains a value to not exceed a maximum and minimum value.
Kind: static method of maths
Param | Type | Description |
---|---|---|
value | number |
The value to constrain |
min | number |
Minimum limit |
max | number |
Maximum limit |
number
maths.norm(value, min, max) ⇒ Normalizes a number from another range into a value between 0 and 1.
Kind: static method of maths
Returns: number
- Normalized value
Param | Type | Description |
---|---|---|
value | number |
The incoming value to be converted |
min | number |
Lower bound of the value's current range |
max | number |
Upper bound of the value's current range |
number
maths.lerp(start, end, amount) ⇒ Perform a linear interpolation between two values. Equivalent of mix in GLSL.
Kind: static method of maths
Returns: number
- Lerped value
Param | Type | Description |
---|---|---|
start | number |
Start of the range in which to interpolate |
end | number |
End of the range in which to interpolate |
amount | number |
Amount to lerp between the two number (from 0 to 1) |
number
maths.damp(source, target, smoothing, dt) ⇒ Frame-rate aware damping function
Kind: static method of maths
Returns: number
- Damped value
Param | Type | Description |
---|---|---|
source | number |
Initial value |
target | number |
Target value |
smoothing | number |
Smoothing rate |
dt | number |
Delta-time (in milliseconds) |
number
maths.dist(x1, y1, x2, y2) ⇒ Calculates the distance between two points (2D)
Kind: static method of maths
Returns: number
- Distance
Param | Type | Description |
---|---|---|
x1 | number |
x-coordinate of the first point |
y1 | number |
y-coordinate of the first point |
x2 | number |
x-coordinate of the second point |
y2 | number |
y-coordinate of the second point |
number
maths.sqdist(x1, y1, x2, y2) ⇒ Calculates the squared distance between two points (2D)
Kind: static method of maths
Returns: number
- Distance
Param | Type | Description |
---|---|---|
x1 | number |
x-coordinate of the first point |
y1 | number |
y-coordinate of the first point |
x2 | number |
x-coordinate of the second point |
y2 | number |
y-coordinate of the second point |
number
maths.ang(x1, y1, x2, y2) ⇒ Calculates the angle between two points (2D)
Kind: static method of maths
Returns: number
- Angle (in Radians)
Param | Type | Description |
---|---|---|
x1 | number |
x-coordinate of the first point |
y1 | number |
y-coordinate of the first point |
x2 | number |
x-coordinate of the second point |
y2 | number |
y-coordinate of the second point |
array
maths.polarToCart(radius, angle) ⇒ Calculates the angle between two points (2D)
Kind: static method of maths
Returns: array
- Array containing the cartesian coordinates [x, y]
Param | Type | Description |
---|---|---|
radius | number |
Radius / Distance |
angle | number |
Angle |
number
maths.radToDeg(angle) ⇒ Convert angle from radians to degrees
Kind: static method of maths
Returns: number
- Angle in degree
Param | Type | Description |
---|---|---|
angle | number |
Angle in radian |
number
maths.degToRad(angle) ⇒ Convert angle from degrees to radians
Kind: static method of maths
Returns: number
- Angle in radian
Param | Type | Description |
---|---|---|
angle | number |
Angle in degree |
number
maths.mean(values) ⇒ Get the mean average of values
Kind: static method of maths
Returns: number
- mean value
Param | Type |
---|---|
values | array |
number
maths.median(values) ⇒ Get the median average of values
Kind: static method of maths
Returns: number
- median value
Param | Type |
---|---|
values | array |