Helper functions
Various helper functions and types that are used throughout this package. Some of these functions might be useful when you’re creating your own components, especially ones that use rotation.
Constants
TAU
Defined as 2π, useful when working with radians.
Functions
clamp
- Returns
value
if it is betweenminValue
andmaxValue
; - Returns
minValue
ifvalue
is a number smaller thanminValue
,null
, orundefined
; - Returns
maxValue
ifvalue
is larger thanmaxValue
normalisedAngle
angle
is an angle in radians, possibly negative, and returns a normalised angle in radians, where
0 ≤ angle
≥ 2π.
angleToValue
Converts angle
in radians to a value by linear mapping from the range minAngle
- maxAngle
to minValue
- maxValue
.
valueToAngle
Maps value
, and returns a corresponding angle in radians, by linear mapping from minValue
- maxValue
to minAngle
- maxAngle
.
angleToPoint
Takes angle
in radians between 0-2π, radius
as the radius of a circle with center origin
, and returns
the point {x, y}
that lies on the circle and a line from the origin at angle. Examples:
pointToAngle
Returns the angle in radians of a vector from origin
to point
. Example:
pointEquals
Returns true
if point
and otherPoint
have equal x
and y
coordinates, or if both points are null
.
isPointInRect
Returns true
if point
is within rectangle rect
(inclusive).
Returns false
if rect
is null
or undefined
.
rad2deg
Converts radians to degrees, which is useful for CSS transforms.
deg2rad
Converts degrees to radians.