Skip to content

Instantly share code, notes, and snippets.

@du5rte
Last active Jul 25, 2018
Embed
What would you like to do?
Expand Ratios
// https://stackoverflow.com/questions/14224535/scaling-between-two-number-ranges
function withinRange(val, { min, max }) {
return (
val > max
? max
: val < min
? min
: val
);
}
function ratioToValue(ratio, { min, max }) {
const value = ratio * (max - min) + min;
return withinRange(value, { min, max });
}
function valueToRatio(value, { min=0, max=1 }={}) {
const ratio = (value - min) / (max - min);
return withinRange(ratio, { min: 0, max: 1 });
}
console.log(
valueToRatio(1, { min: 1, max: 3 })
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment