Skip to content

Instantly share code, notes, and snippets.

@enijar
Created February 5, 2020 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enijar/add594f27f0734943b6c55efb610d087 to your computer and use it in GitHub Desktop.
Save enijar/add594f27f0734943b6c55efb610d087 to your computer and use it in GitHub Desktop.
Linear mapping function

Takes a value from range (x1, y1) and maps that value to a new range (x2, y2).

const map = (value, x1, y1, x2, y2) => (value - x1) * (y2 - x2) / (y1 - x1) + x2;

const value = 5; // range (1, 10)
console.log(map(value, 1, 10, 0, 1)); // 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment