javascriptdictionaryprocessingremap

Remap or Map function in 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.

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?

More info here: http://processing.org/reference/map_.html

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


Solution

  • The function below 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);
    }