Skip to content

math

square

Signature

js
math.square(value)

Description

Returns the square of a number.

Parameters

NameTypeRequiredDescription
valuenumberyesThe number to square

Returns

TypeDescription
numbervalue²

Examples

js
math.square(3)  // 9
math.square(-4) // 16

cube

Signature

js
math.cube(value)

Description

Returns the cube of a number.

Parameters

NameTypeRequiredDescription
valuenumberyesThe number to cube

Returns

TypeDescription
numbervalue³

Examples

js
math.cube(3)  // 27
math.cube(-2) // -8

clamp

Signature

js
math.clamp(value, min, max)

Description

Clamps a number between a minimum and maximum value.

Parameters

NameTypeRequiredDescription
valuenumberyesThe number to clamp
minnumberyesThe lower bound
maxnumberyesThe upper bound

Returns

TypeDescription
numbervalue clamped between min and max

Examples

js
math.clamp(5, 0, 10)  // 5
math.clamp(-5, 0, 10) // 0
math.clamp(15, 0, 10) // 10

round

Signature

js
math.round(value, precision = 2)

Description

Rounds a number to a given number of decimal places.

Parameters

NameTypeRequiredDescription
valuenumberyesThe number to truncate
precisionnumbernoNumber of decimal places. Defaults to 2

Returns

TypeDescription
numberThe rounded number

Examples

js
math.truncate(1.23456)       // 1.23
math.truncate(1.23456789, 4) // 1.2346
math.truncate(1.23456789, 7) // 1.2345679

percentage

Signature

js
math.percentage(value, total)

Description

Returns the percentage of value relative to total, truncated to 2 decimal places.

Parameters

NameTypeRequiredDescription
valuenumberyesThe partial value
totalnumberyesThe total value

Returns

TypeDescription
numberThe percentage, truncated to 2 decimal places

Examples

js
math.percentage(1, 4) // 25
math.percentage(1, 3) // 33.33

linear

Signature

js
math.linear(t, initial = 0, final = 1)

Description

Linearly interpolates between initial and final based on a normalized progress value t.

Parameters

NameTypeRequiredDescription
tnumberyesNormalized progress value in [0, 1]
initialnumbernoThe start value. Defaults to 0
finalnumbernoThe end value. Defaults to 1

Returns

TypeDescription
numberInterpolated value between initial and final

Examples

js
math.linear(0)        // 0
math.linear(0.5)      // 0.5
math.linear(1)        // 1
math.linear(0.5, 0, 100) // 50
math.linear(0.5, 100, 200) // 150

easeIn

Signature

js
math.easeIn(t, linearity = 0.5)

Description

Applies an ease-in curve to a normalized value t. The curve starts slow and accelerates. linearity controls the sharpness of the curve — lower values produce a sharper ease-in.

Parameters

NameTypeRequiredDescription
tnumberyesNormalized progress value in [0, 1]
linearitynumbernoControls the sharpness of the curve. Defaults to 0.5

Returns

TypeDescription
numberEased value in [0, 1]

Examples

js
math.easeIn(0)    // 0
math.easeIn(0.5)  // 0.25
math.easeIn(1)    // 1
math.easeIn(0.5, 0.25) // sharper curve

easeOut

Signature

js
math.easeOut(t, linearity = 0.5)

Description

Applies an ease-out curve to a normalized value t. The curve starts fast and decelerates. linearity controls the sharpness of the curve — lower values produce a sharper ease-out.

Parameters

NameTypeRequiredDescription
tnumberyesNormalized progress value in [0, 1]
linearitynumbernoControls the sharpness of the curve. Defaults to 0.5

Returns

TypeDescription
numberEased value in [0, 1]

Examples

js
math.easeOut(0)   // 0
math.easeOut(0.5) // 0.75
math.easeOut(1)   // 1

cubicBezier

Signature

js
math.cubicBezier(t, x1 = 0.42, y1 = 0, x2 = 0.58, y2 = 1)

Description

Evaluates a cubic Bézier curve at t. The default control points (0.42, 0, 0.58, 1) produce a standard ease-in-out curve, equivalent to the CSS ease-in-out timing function.

Parameters

NameTypeRequiredDescription
tnumberyesNormalized progress value in [0, 1]
x1numbernoX coordinate of the first control point. Defaults to 0.42
y1numbernoY coordinate of the first control point. Defaults to 0
x2numbernoX coordinate of the second control point. Defaults to 0.58
y2numbernoY coordinate of the second control point. Defaults to 1

Returns

TypeDescription
numberThe Y value of the curve at t

Examples

js
math.cubicBezier(0)   // 0
math.cubicBezier(0.5) // ~0.5 (symmetric curve)
math.cubicBezier(1)   // 1

// CSS ease equivalent
math.cubicBezier(0.5, 0.25, 0.1, 0.25, 1)

sum

Signature

js
math.sum(values)

Description

Returns the sum of an array of numbers.

Parameters

NameTypeRequiredDescription
valuesnumber[]yesArray of numbers

Returns

TypeDescription
numberThe sum of all values. Returns 0 for an empty array

Examples

js
math.sum([1, 2, 3, 4]) // 10
math.sum([])            // 0

average

Signature

js
math.average(values)

Description

Returns the arithmetic mean of an array of numbers.

Parameters

NameTypeRequiredDescription
valuesnumber[]yesNon-empty array of numbers

Returns

TypeDescription
numberThe arithmetic mean

Examples

js
math.average([1, 2, 3, 4]) // 2.5
math.average([5])           // 5

median

Signature

js
math.median(values)

Description

Returns the median of an array of numbers. For even-length arrays, returns the average of the two middle values.

Parameters

NameTypeRequiredDescription
valuesnumber[]yesNon-empty array of numbers

Returns

TypeDescription
numberThe median value

Examples

js
math.median([1, 2, 3, 4, 5]) // 3
math.median([1, 2, 3, 4])    // 2.5
math.median([5, 1, 3])       // 3