Last active
August 29, 2015 14:26
-
-
Save xdmorgan/04f7e160e0b9aaddd800 to your computer and use it in GitHub Desktop.
Got a framer prototype with this handy function in it, tried writing my own, judging by my use-case it mimics Framer's modulate perfectly, however, I should to run it through some unit tests see if it always does. [feedback welcome]
This file contains 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
// map a value from one range onto another, in this case scrolling | |
var st = _private.getScrollTop(), | |
m1 = _private.modulate( st, [0, 500], [0,1] ), | |
m2 = _private.modulate( st, [0, 400], [1,0] ), | |
m3 = _private.modulate( st, [0, 300], [0,3] ); | |
_private.modulate = function(val, range1, range2){ | |
var min1 = range1[0], max1 = range1[1], | |
min2 = range2[0], max2 = range2[1], | |
diff1 = 0, diff2 = 0, | |
pct = 0, output = 0, dir = 1; | |
if(min1 > max1){ | |
diff1 = min1 - max1; | |
}else if(min1 < max1){ | |
diff1 = max1 - min1; | |
} | |
if(min2 > max2){ | |
diff2 = min2 - max2; | |
dir = -1; | |
}else if(min2 < max2){ | |
diff2 = max2 - min2; | |
} | |
pct = val * ( 100 / diff1); | |
if(pct > 100){ pct = 100; } | |
else if(pct < 0 ){ pct = 0; } | |
output = ( pct / 100 ) * diff2; | |
output = range2[0] + ( output * dir ); | |
return output; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment