math Class
The grapefruit math library, used to abstract commonly used math operations
Item Index
Methods
Properties
Methods
abs
-
num
Returns the absolute value of a number, currently uses native Math.abs since it is more performant than tricks you can use. see: http://jsperf.com/math-abs-vs-bitwise/7 http://jsperf.com/abs-value http://jsperf.com/math-abs-vs-bitwise/3
Parameters:
-
num
NumberThe number to get the absolute value for
Returns:
The absolute value
angleBetween
-
pos1
-
pos2
Calculates the angle between two points
Returns:
The angle in radians
ceil
-
num
Applys a Ceiling operation to a value, currently uses native Math.ceil since it deals with all edge cases
Parameters:
-
num
NumberThe number to ceil
Returns:
The ceiling value
clamp
-
num
-
min
-
max
Clamps a number between two values.
Parameters:
-
num
NumberThe number to clamp
-
min
NumberThe minimum value the number is allowed to be
-
max
NumberThe maximum value the number is allowed to be
Returns:
The clamped value
degreesToRadians
-
angle
Convert radians to degrees
Parameters:
-
angle
NumberThe angle in degrees to convert
Returns:
The angle in radians
floor
-
num
Applys a Floor operation to a value, currently uses native Math.floor
since it deals with all edge cases that quicker solutions like ~~value
or value | 0
do not.
Parameters:
-
num
NumberThe number to floor
Returns:
The floored value
max
-
num
Returns the max of the values passed
Parameters:
-
num
Number... multiple
Returns:
The max value
min
-
num
Returns the min of the values passed
Parameters:
-
num
Number... multiple
Returns:
The min value
radiansToDegrees
-
angle
Convert radians to degrees
Parameters:
-
angle
NumberThe angle in radians to convert
Returns:
The angle in degrees
random
()
Number
Generates a random number between 0 and 1, NON DETERMINISTIC
Returns:
The random value
randomBool
-
[chance=50]
Returns a random boolean based on the provided chance. The chance represents the percentage chance of returning: true.
Parameters:
-
[chance=50]
Number optionalThe % chance of getting true (0 - 100), defaults to 50%
Returns:
randomBytes
-
[output]
Fills a Typed Array with random bytes. If you do not pass an output param, then a default Uint8Array(16) is created and returned for you.
Parameters:
-
[output]
TypedArray optionalThe output array for the random data, if none specified a new Uint8Array(16) is created
randomElement
-
array
-
start
-
end
Returns a random element of an array.
Parameters:
-
array
ArrayThe array to choose from
-
start
NumberThe index of the first element to include, defaults to 0
-
end
NumberThe index of the last element to include, defaults to array.length - 1
Returns:
either 1 or -1
randomInt
-
[min=0]
-
[max=100]
Returns a random int between min and max.
Parameters:
-
[min=0]
Number optionalThe minimun number that the result can be
-
[max=100]
Number optionalThe maximun number that the result can be
Returns:
randomReal
-
[min=0]
-
[max=1]
Returns a random real number between min and max.
Parameters:
-
[min=0]
Number optionalThe minimun number that the result can be
-
[max=1]
Number optionalThe maximun number that the result can be
Returns:
randomSign
-
chance
Returns a random sign based on the provided chance. The chance represents the percentage chance of returning 1 (positive).
Parameters:
-
chance
NumberThe % chance of getting positive (0 - 100), defaults to 50%
Returns:
either 1 or -1
randomString
()
String
Returns a random string based on a random value between 0 and 1, multiplied by the current date. Ex: "1158014093337", "86371874178", etc
Returns:
A random string
randomUuid
()
String
Generates a random RFC4122 compliant (v4) UUID
Returns:
A random guid
round
-
num
Quickly rounds a number. This is about twice as fast as Math.round()
Parameters:
-
num
NumberThe number to round
Returns:
The rounded value
snap
-
num
-
gap
-
[offset=0]
Snaps a number to a grid value. For example, if you have a grid with gaps the size of 10 horizontally, and a position of 11, it would snap to 10; a position of 18 would snap to 20
Parameters:
-
num
NumberThe number to snap
-
gap
NumberThe gap size of the grid (the tile size)
-
[offset=0]
Number optionalThe starting offset of a grid slice (aka tile)
Returns:
The snapped value
snapCeil
-
num
-
gap
-
[offset=0]
Snaps a number to a grid value, using ceiling. For example, if you have a grid with gaps the size of 10 horizontally, and a position of 11, it would snap to 20; a position of 18 would also snap to 20
Parameters:
-
num
NumberThe number to snap
-
gap
NumberThe gap size of the grid (the tile size)
-
[offset=0]
Number optionalThe starting offset of a grid slice (aka tile)
Returns:
The snapped value
snapFloor
-
num
-
gap
-
[offset=0]
Snaps a number to a grid value, using floor. For example, if you have a grid with gaps the size of 10 horizontally, and a position of 11, it would snap to 10; a position of 18 would also snap to 10
Parameters:
-
num
NumberThe number to snap
-
gap
NumberThe gap size of the grid (the tile size)
-
[offset=0]
Number optionalThe starting offset of a grid slice (aka tile)
Returns:
The snapped value
sqrt
-
num
Returns the square root of a number, currently uses native Math.sqrt
Parameters:
-
num
NumberThe number to get the sqrt of
Returns:
The sqrt value
truncate
-
num
Truncates the decimal from a number
Parameters:
-
num
NumberThe number to truncate
Returns:
The truncated value
Properties
DEG_TO_RAD
Number
The factor to multiply by to convert Degrees into Radians. The value is π/180
mat3
Object
A 3x3 Matrix namespace, directory exposes PIXI.mat3. You can use this to create 3x3 Matrix classes.
mat3
Object
A 4x4 Matrix namespace, directory exposes PIXI.mat4. You can use this to create 4x4 Matrix classes.
Matrix
Matrix
A Matrix class, directory exposes PIXI.Matrix.
RAD_TO_DEG
Number
The factor to multiply by to convert Radians into Degrees. The value is 180/π
SEED
Number
The RNG seed that allows for deterministic random numbers. Change this to a certain value to ensure you will have the same sequence of "random" numbers. Useful for playbacks, save files, procedural generation, etc.
Note: Deterministic randomness is not yet implemented. Scheduled for v0.2.0
Default: Math.random()