constrain - nodef/extra-number GitHub Wiki

Constrain a number within a minimum and a maximum value.

Similar: constrain, normalize, remap, lerp.

function constrain(x, min, max)
// x:   a number
// min: minimum value
// max: maximum value
const xnumber = require('extra-number');


xnumber.constrain(20, 0, 50);
// → 20

xnumber.constrain(-10, 0, 100);
// → 0

xnumber.constrain(120, 0, 100);
// → 100

References