Javascript / jQuery - map a range of numbers to another range of numbers

后端 未结 8 1515

In other programming languages such as processing, there is a function which allows you to convert a number that falls within a range of numbers into a number within a diffe

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 18:16

    I've taken August Miller's idea and added limits to them (value can't go outside the range of in_min and in_max and if it does it returns out_min and out_max respectively) and minified it for those who want a copy paste function for their scripts:

    function map(n,i,o,r,t){return i>o?i>n?(n-i)*(t-r)/(o-i)+r:r:o>i?o>n?(n-i)*(t-r)/(o-i)+r:t:void 0}

    params are like map(value, in_min, in_max, out_in, out_max)

提交回复
热议问题