Last active
May 4, 2016 19:57
-
-
Save Rich-Harris/5894545 to your computer and use it in GitHub Desktop.
A simple linear scale function generator, similar to D3's
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
(function ( global ) { | |
'use strict'; | |
var linearScale = function ( domain, range ) { | |
var d0 = domain[0], r0 = range[0], multiplier = ( range[1] - r0 ) / ( domain[1] - d0 ); | |
// special case | |
if ( r0 === range[1] ) { | |
return function () { | |
return r0; | |
}; | |
} | |
return function ( num ) { | |
return r0 + ( multiplier * ( num - d0 ) ); | |
}; | |
}; | |
// export as CommonJS... | |
if ( typeof module !== 'undefined' && module.exports ) { | |
module.exports = linearScale; | |
} | |
// ...or as AMD module | |
else if ( typeof define !== 'undefined' && define.amd ) { | |
define( function () { return linearScale; }); | |
} | |
// ...or as browser global | |
else { | |
global.linearScale = linearScale; | |
} | |
}( this )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment