Last active
February 15, 2023 02:10
-
-
Save yashsway/8462443baccb3474af157518f06cb5e2 to your computer and use it in GitHub Desktop.
Clamping a value between a given min and max i.e. never more than the max and never less than min
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const clamp = (val, min = 0, max = 1) => { | |
// if provided min is somehow greater than max, exchange the values | |
if (min > max) { | |
[min, max] = [max, min]; | |
} | |
// get the maximum of two minimized values | |
return Math.max(min, Math.min(max, val)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment