Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Last active May 4, 2016 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rich-Harris/5894545 to your computer and use it in GitHub Desktop.
Save Rich-Harris/5894545 to your computer and use it in GitHub Desktop.
A simple linear scale function generator, similar to D3's
(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