Last active
June 17, 2024 02:30
-
-
Save xposedbones/75ebaef3c10060a3ee3b246166caab56 to your computer and use it in GitHub Desktop.
Javascript Map range of number to another 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
Number.prototype.map = function (in_min, in_max, out_min, out_max) { | |
return (this - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; | |
} |
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
export function clamp(input: number, min: number, max: number): number { | |
return input < min ? min : input > max ? max : input; | |
} | |
export function map(current: number, in_min: number, in_max: number, out_min: number, out_max: number): number { | |
const mapped: number = ((current - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min; | |
return clamp(mapped, out_min, out_max); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Linear Interpolation