Ensure toScale stays within defined range

This commit is contained in:
Adrian Zankich 2014-03-11 18:33:39 -07:00
parent bb9838e3e6
commit 1ee0b8b87a
1 changed files with 10 additions and 1 deletions

View File

@ -65,7 +65,16 @@ Number.prototype.fromScale = function(start, end) {
//
// Returns an integer representing the scaled value
Number.prototype.toScale = function(start, end) {
return this * (Math.max(start, end) - Math.min(start, end)) + Math.min(start, end);
var i = this * (Math.max(start, end) - Math.min(start, end)) + Math.min(start, end);
if (i < Math.min(start, end)) {
return Math.min(start, end)
}
else if (i > Math.max(start,end)){
return Math.max(start, end)
}
else {
return i
}
};
var Utils = {