Skip to content

Instantly share code, notes, and snippets.

@RichardMarks
Created March 28, 2019 18:39
Show Gist options
  • Save RichardMarks/324f6db83f070930d3cf9ce190b8cca2 to your computer and use it in GitHub Desktop.
Save RichardMarks/324f6db83f070930d3cf9ce190b8cca2 to your computer and use it in GitHub Desktop.
Scale Value to Range
const scaleValueToRange = ({
value = 0,
fromMinimum = 0,
fromMaximum = 1000,
toMinimum = -100,
toMaximum = 100
}) => {
const scaledValue = (( value - fromMinimum) / (fromMaximum - fromMinimum)) * (toMaximum - toMinimum) + toMinimum
return {
fromMinimum,
fromMaximum,
toMinimum,
toMaximum,
equation: `(( ${value} - ${fromMinimum}) / (${fromMaximum} - ${fromMinimum})) * (${toMaximum} - ${toMinimum}) + ${toMinimum} = ${scaledValue}`,
scaledValue
}
}
const value = 0
const result = scaleValueToRange({ value, fromMaximum: 1, toMinimum: -1.0, toMaximum: 1.0 })
console.log({ value, ...result })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment