Skip to content

Commit b162ca4

Browse files
committed
Improved Math.clamp and docs.
1 parent 952b5c0 commit b162ca4

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

src/math/Math.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -896,16 +896,29 @@ Phaser.Math = {
896896
},
897897

898898
/**
899-
* Force a value within the boundaries by clamping `x` to the range `[a, b]`.
899+
* Force a value within the boundaries by clamping it to the range `min`, `max`.
900900
*
901901
* @method Phaser.Math#clamp
902-
* @param {number} x
903-
* @param {number} a
904-
* @param {number} b
905-
* @return {number}
902+
* @param {float} v - The value to be clamped.
903+
* @param {float} min - The minimum bounds.
904+
* @param {float} max - The maximum bounds.
905+
* @return {number} The clamped value.
906906
*/
907-
clamp: function (x, a, b) {
908-
return ( x < a ) ? a : ( ( x > b ) ? b : x );
907+
clamp: function (v, min, max) {
908+
909+
if (v < min)
910+
{
911+
return min;
912+
}
913+
else if (max < v)
914+
{
915+
return max;
916+
}
917+
else
918+
{
919+
return v;
920+
}
921+
909922
},
910923

911924
/**

0 commit comments

Comments
 (0)