Skip to content

Instantly share code, notes, and snippets.

@tripulse
Created February 28, 2019 11:27
Show Gist options
  • Select an option

  • Save tripulse/eda1639c908be6fd315485c70866cc7a to your computer and use it in GitHub Desktop.

Select an option

Save tripulse/eda1639c908be6fd315485c70866cc7a to your computer and use it in GitHub Desktop.
The p5's map function in native JavaScript!
/**
* @description Maps a number in a range into another range.
* @param {Number} val - The value to map.
* @param {Number} v_min - The minimum value of the 'value'.
* @param {Number} v_max - The maximum value of the 'value'.
* @param {Number} m_min - The minimum value to map.
* @param {Number} m_max - The maximum value to map.
* @returns Number
*/
Math.map = function(val, v_min, v_max, m_min, m_max) {
// Make this range into (0..1)
val = (val - v_min) / (v_max - v_min);
// Convert the range to (m_min..m_max)
return m_min + val * (m_max - m_min);
}
@tripulse
Copy link
Copy Markdown
Author

AW,CAP

@tripulse
Copy link
Copy Markdown
Author

Great essential

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment