map-number
processing/p5.js map like function, including floating point numbers support
:warning: this map
function has nothing to do with Array.prototype.map
method.
Usage
Node.js
npm install map-number
const { map } = require("map-number");
const y = map(Math.sin(angle), -1, 1, 100, 0);
Browser
<script src="https://cdn.jsdelivr.net/npm/map-number/dist/map.umd.min.js"></script>
const y = mapNum.map(Math.sin(angle), -1, 1, 100, 0);
API
map
maps a number in a range to a different range, returning a floting point number, including number outside the given output range.
all other map function variants depend on this one.
syntax
function map(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
floor
maps a number in a range to a different range, returning a number truncated to the inmediate previous integer number, including number outside the given output range.
syntax
function floor(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
round
maps a number in a range to a different range, returning a number rounded to the closest integer number, including number outside the given output range.
syntax
function round(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
limit
maps a number in a range to a different range, returning a floting point number, limiting the result to the given output range.
syntax
function limit(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
create
creates a single argument function implementing the given map
, floor
, round
or limit
function, useful when you need to map values multiple times within the same range, see example
syntax
function create(func: MapFunction, inMin: number, inMax: number, outMin: number, outMax: number): (num: number) => number;
example
const myMap = create(map, -1, 1, 100, 0);
myMap(Math.sin(angle));
map(Math.sin(angle), -1, 1, 100, 0);
wrap
an alias for create method
License
MIT :copyright: Manuel Fernández