Skip to content

Instantly share code, notes, and snippets.

@diegovgsilva95
Created September 26, 2022 20:37
Show Gist options
  • Select an option

  • Save diegovgsilva95/ec78bcb87b6763b4c71c5a87a66d857a to your computer and use it in GitHub Desktop.

Select an option

Save diegovgsilva95/ec78bcb87b6763b4c71c5a87a66d857a to your computer and use it in GitHub Desktop.
Map number
export function mapNumber(v, fn, fx, tn, tx, constrain = true){
if(fx == fn)
return v < fn ? tn : tx;
v = (v - fn) / (fx - fn);
if(constrain)
v = Math.min(1, Math.max(0, v));
return ((v * (tx - tn)) + tn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment