Skip to content

clamp

Constrains a number to the range from min to max, inclusive.

Values inside the range come back unchanged, and values past either end come back as that end. Charts use it to keep font sizes, radii, and pixel positions within their bounds.

import {clamp} from "@d3plus/math";

clamp(5, 0, 10);
clamp(-3, 0, 10);
clamp(42, 0, 10);
clamp(0.25, 0, 1);

Parameters

import {clamp} from "@d3plus/math";
valuerequirednumber

The number to constrain.

minrequirednumber

The lower bound.

maxrequirednumber

The upper bound; when it is below min, min wins.