Skip to main content
Module

x/911/src/math.ts>clamp

Opinionated collection of helper tools. Assembled with Deno in mind.
Go to Latest
function clamp
import { clamp } from "https://deno.land/x/911@0.1.1/src/math.ts";

Clamp the value of number n to a range of min and max.

Examples

Example 1

import { clamp } from "https://deno.land/x/911@0.1.1/src/math.ts";

clamp(0, 1, 3) // => 1
clamp(1, 1, 3) // => 1
clamp(2, 1, 3) // => 2
clamp(3, 1, 3) // => 3

Example 2

import { clamp } from "https://deno.land/x/911@0.1.1/src/math.ts";

clamp(0, -1, 1) // => -1
clamp(1, -1, 1) // => 1
clamp(2, -1, 1) // => 1
clamp(3, -1, 1) // => 1

Parameters

n: number

The number to clamp.

min: number

The lower bound of the clamp range.

max: number

The upper bound of the clamp range.

Returns

number

New value with the range constraint applied.