Created
March 28, 2019 18:39
-
-
Save RichardMarks/324f6db83f070930d3cf9ce190b8cca2 to your computer and use it in GitHub Desktop.
Scale Value to Range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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