在pilla.js这样的vanilla javascript中是否有地图功能?

时间:2018-02-15 08:37:35

标签: javascript p5.js

在p5.js中,有一个名为map()的函数,它将某个范围内的值映射到另一个范围内的另一个值。在vanilla javascript中是否有类似的方法?

1 个答案:

答案 0 :(得分:3)

不,开箱即用的JavaScript中没有类似的东西,但编写自己的东西很容易:

// linearly maps value from the range (a..b) to (c..d)
function mapRange (value, a, b, c, d) {
    // first map value from (a..b) to (0..1)
    value = (value - a) / (b - a);
    // then map it from (0..1) to (c..d) and return it
    return c + value * (d - c);
}

另外,P5.js是用JavaScript编写的,因此它的map()函数 是一个很好的JavaScript。 P5.js是开源的,可以找到map()函数here

var newval = (n - start1) / (stop1 - start1) * (stop2 - start2) + start2;
相关问题