Crafty.math

A set of utility functions for common (and not so common) operations.

Methods

Back to top

Crafty.math.abs()

public this Crafty.math.abs(Number n)
n

Some value.

[Returns]

Absolute value.

Returns the absolute value.

Back to top

Crafty.math.amountOf()

public Number Crafty.math.amountOf(Number checkValue, Number minValue, Number maxValue)
checkValue

Value that should checked with minimum and maximum.

minValue

Bottom of the range

maxValue

Top of the range

[Returns]

The position of the checked value in a coordinate system normalized such that minValue is 0 and maxValue is 1.

If checkValue is within the range, this will return a number between 0 and 1.

Back to top

Crafty.math.clamp()

public Number Crafty.math.clamp(Number value, Number min, Number max)
value

A value.

max

Maximum that value can be.

min

Minimum that value can be.

[Returns]

The value between minimum and maximum.

Restricts a value to be within a specified range.

Back to top

Crafty.math.degToRad()

Converts angle from degree to radian.

public Number degToRad(angleInDeg)
angleInDeg

The angle in degrees.

[Returns]

The angle in radians.

Back to top

Crafty.math.distance()

public Number Crafty.math.distance(Number x1, Number y1, Number x2, Number y2)
x1

First x coordinate.

y1

First y coordinate.

x2

Second x coordinate.

y2

Second y coordinate.

[Returns]

The distance between the two points.

Distance between two points.

Back to top

Crafty.math.lerp()

public Number Crafty.math.lerp(Number value1, Number value2, Number amount)
value1

One value.

value2

Another value.

amount

Amount of value2 to value1.

[Returns]

Linear interpolated value.

Linear interpolation. Passing amount with a value of 0 will cause value1 to be returned, a value of 1 will cause value2 to be returned.

Back to top

Crafty.math.negate()

public Number Crafty.math.negate(Number percent)
percent

The probability of returning -1

[Returns]

1 or -1.

Returns 1 or -1 randomly.

Back to top

Crafty.math.radToDeg()

public Number Crafty.math.radToDeg(Number angle)
angleInRad

The angle in radian.

[Returns]

The angle in degree.

Converts angle from radian to degree.

Back to top

Crafty.math.randomElementOfArray()

public Object Crafty.math.randomElementOfArray(Array array)
array

A specific array.

[Returns]

A random element of a specific array.

Returns a random element of a specific array.

Back to top

Crafty.math.randomInt()

public Number Crafty.math.randomInt(Number start, Number end)
start

Smallest int value that can be returned.

end

Biggest int value that can be returned.

[Returns]

A random int.

Returns a random int within a specific range.

Back to top

Crafty.math.randomNumber()

public Number Crafty.math.randomNumber(Number start, Number end)
start

Smallest number value that can be returned.

end

Biggest number value that can be returned.

[Returns]

A random number.

Returns a random number in within a specific range.

Back to top

Crafty.math.squaredDistance()

public Number Crafty.math.squaredDistance(Number x1, Number y1, Number x2, Number y2)
x1

First x coordinate.

y1

First y coordinate.

x2

Second x coordinate.

y2

Second y coordinate.

[Returns]

The squared distance between the two points.

Squared distance between two points.

Back to top

Crafty.math.withinRange()

public Boolean Crafty.math.withinRange(Number value, Number min, Number max)
value

The specific value.

min

Minimum value.

max

Maximum value.

[Returns]

Returns true if value is within a specific range.

Check if a value is within a specific range.