Skip to content

Instantly share code, notes, and snippets.

@illtellyoulater
illtellyoulater / range-scaling.js
Last active January 26, 2023 16:14 — forked from fpillet/scale.js
Javascript value scaling between two ranges, the correct way
function convertRange( value, r1, r2 ) {
return ( value - r1[ 0 ] ) * ( r2[ 1 ] - r2[ 0 ] ) / ( r1[ 1 ] - r1[ 0 ] ) + r2[ 0 ];
}
// example
convertRange(26, [0, 100], [-2, 2]);
// = -0.96
// another example
convertRange(26, [0, 100.1], [-2, 2]);