本文介绍了在Javascript中重映射或映射功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我的大部分编程经验都在中,最近我一直想分支对某些JavaScript有更深入的了解,因为它们有些相关。

Of late, most of my programming experience has been in Processing, and more recently I have been wanting to branch out a little deeper in to some JavaScript, since they are slightly related.

我只是想知道,因为我最近的搜索没有发现,如果JavaScript与Processing的map函数有类似的功能,其中值和范围是获取和重新映射到新的范围?

I was just wondering, as my recent searching has turned up nothing, if JavaScript had a similar function to Processing's "map" function, in which a value and it's range is taken and remapped to a new range?

更多信息:

PS:是的,我也知道www.processingjs.org存在,但是我只是想知道JS是否有这样的功能本身。

PS: Yes, I also know that www.processingjs.org exists, but I was just wondering if JS had such a function natively.

推荐答案

AFAIK下面的功能产生与原始处理功能相同的行为:

The function below AFAIK produces the same behaviour as the original Processing function:

function map_range(value, low1, high1, low2, high2) {
    return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}

边界值肯定是正确的,并产生我期望的对于超出的值也是如此 map_range(-1,0,1,0,100)返回 -100

It's certainly correct for "in bounds" values, and produces what I would expect for "out of bounds" values too, e.g. map_range(-1, 0, 1, 0, 100) returns -100.

这篇关于在Javascript中重映射或映射功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:05