Created
May 24, 2012 13:13
-
-
Save kujon/2781489 to your computer and use it in GitHub Desktop.
JavaScript: Clamp a number.
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
/** | |
* Clamps a number. Based on Zevan's idea: http://actionsnippet.com/?p=475 | |
* params: val, min, max | |
* Author: Jakub Korzeniowski | |
* Agency: Softhis | |
* http://www.softhis.com | |
*/ | |
(function(){Math.clamp=function(a,b,c){return Math.max(b,Math.min(c,a));}})(); |
webgl has this also
Make the code more readable by placing the clamped value between the min and the max values, like this:
Math.min(Math.max(min, val), max)
Why did you post this?
^ What a stupid question.
^ agreed he must have brain cancer or he has tusugma
there is missing at least 20 lines of license text : D Math.clamp = (n,a,z) => Math.max(a,Math.min(z,n));
. naz!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Haha ... yes, we must appease the almighty Linters.