Skip to content

Instantly share code, notes, and snippets.

@vectorsize
Last active December 20, 2022 14:30
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vectorsize/7031902 to your computer and use it in GitHub Desktop.
Save vectorsize/7031902 to your computer and use it in GitHub Desktop.
Quick linear scale inspired by d3.js scales, based on the processing.org map() function.takes in an object with a domain array and a rage array, gives you back a function that receives a value and returns the scaled value.
// based on https://github.com/processing/processing/blob/a6e0e227a948e7e2dc042c04504d6f5b8cf0c1a6/core/src/processing/core/PApplet.java#L5093
var scale = function(opts){
var istart = opts.domain[0],
istop = opts.domain[1],
ostart = opts.range[0],
ostop = opts.range[1];
return function scale(value) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment