Skip to content

Instantly share code, notes, and snippets.

@carambula
Created October 31, 2013 08:58
Show Gist options
  • Select an option

  • Save carambula/7246404 to your computer and use it in GitHub Desktop.

Select an option

Save carambula/7246404 to your computer and use it in GitHub Desktop.
Convert a number within a range to a different range. For instance, 3 on a 1-10 scale to a 1-100 scale. Handy for pegging something like alpha (0-1 range) to an x coordinate (0-width of space)
function convertRange(OldMin, OldMax, OldValue, NewMin, NewMax){
var OldRange = OldMax - OldMin;
var NewRange = NewMax - NewMin;
return (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
}
newOpacity = convertRange(0, 10, 3, 0, 100)
console.log (newOpacity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment