Skip to content

Instantly share code, notes, and snippets.

@alexh225
Forked from vectorsize/linearScale.js
Created February 7, 2020 15:21
Show Gist options
  • Select an option

  • Save alexh225/02bdcaa3e069972f5d07a0a2c0a22ef7 to your computer and use it in GitHub Desktop.

Select an option

Save alexh225/02bdcaa3e069972f5d07a0a2c0a22ef7 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 http://processing.org/reference/javadoc/core/processing/core/PApplet.html#map(float, float, float, float, float)
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