API Docs for: v0.1.0
Show:

math Class

Extends Object
Defined in: src\math\math.js:4

The grapefruit math library, used to abstract commonly used math operations

Methods

abs

(
  • num
)
Number

Defined in src\math\math.js:91

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 Number

    The number to get the absolute value for

Returns:

Number:

The absolute value

angleBetween

(
  • pos1
  • pos2
)
Number

Calculates the angle between two points

Parameters:

  • pos1 Vector | Point

    The first position

  • pos2 Vector | Point

    The second position

Returns:

Number:

The angle in radians

ceil

(
  • num
)
Number

Defined in src\math\math.js:75

Applys a Ceiling operation to a value, currently uses native Math.ceil since it deals with all edge cases

Parameters:

  • num Number

    The number to ceil

Returns:

Number:

The ceiling value

clamp

(
  • num
  • min
  • max
)
Number

Clamps a number between two values.

Parameters:

  • num Number

    The number to clamp

  • min Number

    The minimum value the number is allowed to be

  • max Number

    The maximum value the number is allowed to be

Returns:

Number:

The clamped value

degreesToRadians

(
  • angle
)
Number

Convert radians to degrees

Parameters:

  • angle Number

    The angle in degrees to convert

Returns:

Number:

The angle in radians

floor

(
  • num
)
Number

Defined in src\math\math.js:65

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 Number

    The number to floor

Returns:

Number:

The floored value

max

(
  • num
)
Number

Returns the max of the values passed

Parameters:

  • num Number... multiple

Returns:

Number:

The max value

min

(
  • num
)
Number

Returns the min of the values passed

Parameters:

  • num Number... multiple

Returns:

Number:

The min value

radiansToDegrees

(
  • angle
)
Number

Convert radians to degrees

Parameters:

  • angle Number

    The angle in radians to convert

Returns:

Number:

The angle in degrees

random

() Number

Defined in src\math\math.js:84

Generates a random number between 0 and 1, NON DETERMINISTIC

Returns:

Number:

The random value

randomBool

(
  • [chance=50]
)
Boolean

Returns a random boolean based on the provided chance. The chance represents the percentage chance of returning: true.

Parameters:

  • [chance=50] Number optional

    The % chance of getting true (0 - 100), defaults to 50%

Returns:

Boolean:

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 optional

    The output array for the random data, if none specified a new Uint8Array(16) is created

randomElement

(
  • array
  • start
  • end
)
Number

Returns a random element of an array.

Parameters:

  • array Array

    The array to choose from

  • start Number

    The index of the first element to include, defaults to 0

  • end Number

    The index of the last element to include, defaults to array.length - 1

Returns:

Number:

either 1 or -1

randomInt

(
  • [min=0]
  • [max=100]
)
Number

Returns a random int between min and max.

Parameters:

  • [min=0] Number optional

    The minimun number that the result can be

  • [max=100] Number optional

    The maximun number that the result can be

Returns:

Number:

randomReal

(
  • [min=0]
  • [max=1]
)
Number

Returns a random real number between min and max.

Parameters:

  • [min=0] Number optional

    The minimun number that the result can be

  • [max=1] Number optional

    The maximun number that the result can be

Returns:

Number:

randomSign

(
  • chance
)
Number

Returns a random sign based on the provided chance. The chance represents the percentage chance of returning 1 (positive).

Parameters:

  • chance Number

    The % chance of getting positive (0 - 100), defaults to 50%

Returns:

Number:

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:

String:

A random string

randomUuid

() String

Generates a random RFC4122 compliant (v4) UUID

Returns:

String:

A random guid

round

(
  • num
)
Number

Quickly rounds a number. This is about twice as fast as Math.round()

Parameters:

  • num Number

    The number to round

Returns:

Number:

The rounded value

snap

(
  • num
  • gap
  • [offset=0]
)
Number

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 Number

    The number to snap

  • gap Number

    The gap size of the grid (the tile size)

  • [offset=0] Number optional

    The starting offset of a grid slice (aka tile)

Returns:

Number:

The snapped value

snapCeil

(
  • num
  • gap
  • [offset=0]
)
Number

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 Number

    The number to snap

  • gap Number

    The gap size of the grid (the tile size)

  • [offset=0] Number optional

    The starting offset of a grid slice (aka tile)

Returns:

Number:

The snapped value

snapFloor

(
  • num
  • gap
  • [offset=0]
)
Number

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 Number

    The number to snap

  • gap Number

    The gap size of the grid (the tile size)

  • [offset=0] Number optional

    The starting offset of a grid slice (aka tile)

Returns:

Number:

The snapped value

sqrt

(
  • num
)
Number

Returns the square root of a number, currently uses native Math.sqrt

Parameters:

  • num Number

    The number to get the sqrt of

Returns:

Number:

The sqrt value

truncate

(
  • num
)
Number

Truncates the decimal from a number

Parameters:

  • num Number

    The number to truncate

Returns:

Number:

The truncated value

Properties

DEG_TO_RAD

Number

Defined in src\math\math.js:12

The factor to multiply by to convert Degrees into Radians. The value is π/180

mat3

Object

Defined in src\math\math.js:47

A 3x3 Matrix namespace, directory exposes PIXI.mat3. You can use this to create 3x3 Matrix classes.

mat3

Object

Defined in src\math\math.js:54

A 4x4 Matrix namespace, directory exposes PIXI.mat4. You can use this to create 4x4 Matrix classes.

Matrix

Matrix

Defined in src\math\math.js:40

A Matrix class, directory exposes PIXI.Matrix.

RAD_TO_DEG

Number

Defined in src\math\math.js:20

The factor to multiply by to convert Radians into Degrees. The value is 180/π

SEED

Number

Defined in src\math\math.js:28

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()