【问题标题】:Is there a map function in vanilla javascript like p5.js? [duplicate]vanilla javascript中是否有像p5.js这样的地图功能? [复制]
【发布时间】:2023-03-26 11:19:02
【问题描述】:

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

【问题讨论】:

标签: javascript p5.js


【解决方案1】:

不,开箱即用的 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;

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 2012-10-14
    • 2018-02-14
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2011-04-03
    • 2019-03-19
    • 1970-01-01
    相关资源
    最近更新 更多