Skip to content

pencil.js / math / constrain

Function: constrain() ​

ts
function constrain(
   value, 
   min, 
   max
): number;

Defined in: math.ts:14

Constrain a number between two limits.

Parameters ​

ParameterTypeDescription
valuenumberAny number
minnumberMinimal limit for the value
maxnumberMaximal limit for the value

Returns ​

number

Example ​

ts
constrain(999, 0, 50); // => 50
constrain(-999, 0, 50); // => 0

Released under the MIT License.