processing/p5.js map like function, including floating point numbers support
npm install 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.
* Install
* CDN
* Usage
* API
* function map
* function floor
* function ceil
* function round
* function limit
* function compile
* function transformed
* Types
* type MapFunction
* type CompiledMapFunction
* type TransformFunction
* type MapNumberFunction
* type CompiledMapNumberFunction
``bash`
npm install map-number
`bash`
yarn add map-number
`bash`
pnpm add map-number
`html`
for production
`html`
> for production you may want to replace the "latest" version for a specific one.
`html`
for production
`html`
> for production you may want to replace the "latest" version for a specific one.
`javascript`
const { map } = require('map-number');
const result = map(Math.sin(angle), -1, 1, 100, 0);
using javascript modules...
`javascript`
import { map } from 'map-number';
const result = map(Math.sin(angle), -1, 1, 100, 0);
After the script tag has been added, mapNum will be available globally.
`javascript`
const result = mapNum.map(Math.sin(angle), -1, 1, 100, 0);
Maps a number within a given range to a different range, returning a floating point number. The result WILL NOT be limited (clamped) to the the given output range.
This is the core function and all other map function variants depend on it.
syntax*
`typescript`
function map(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
example*
`typescript`
map(4, 0, 10, 0, 100); // => returns 40
Maps a number within a given range to a different range, returning a number rounded down to the previous integer number. The result WILL NOT be limited (clamped) to the the given output range.
syntax
`typescript`
function floor(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
Maps a number within a given range to a different range, returning a number rounded up to the next integer number. The result WILL NOT be limited (clamped) to the the given output range.
syntax
`typescript`
function ceil(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
Maps a number within a given range to a different range, returning a number rounded to the closest integer number. The result WILL NOT be limited (clamped) to the the given output range.
syntax
`typescript`
function round(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
> If you need to round to a specific number of decimal places, you can use the transformed method and write your own round function.
alias: clamp
Maps a number within a given range to a different range, returning a floating point number. The result will be limited (clamped) to the given output range.
syntax
`typescript`
function limit(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
alias: wrap, create
Creates a single argument function implementing the given map, floor, ceil, round, limit, clamp or user created function. Useful when you need to map values multiple times within the same range, see example below.
syntax
`typescript`
function compile
map: MapFunction
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): CompiledMapFunction
See MapFunction and CompiledMapFunction.
example
`javascript
import { map, compile } from "map-number";
const myMap = compile(map, -1, 1, 100, 0);
myMap(-0.2);
myMap(0.33);
myMap(0.51);
// ... is equivalent to...
map(-0.2, -1, 1, 100, 0);
map(0.33, -1, 1, 100, 0);
map(0.51, -1, 1, 100, 0);
`
alias: transform
Creates a map function where the result of the given function is transformed to a different value. This method is used internally to create the floor, ceil and round methods.
syntax
`typescript`
function transformed
map: MapFunction
transform: TransformFunction
): MapFunction
See MapFunction and TransformFunction.
example
`javascript
import { transform, map } from "map-number";
const plusOne = transform(
map,
(value) => value + 1,
);
plusOne(0.4, 0, 1, 0, 100); // => 41 instead of 40
`
`typescript`
type MapFunction
`typescript`
type CompiledMapFunction
`typescript`
type TransformFunction = (value: I) => O;
`typescript`
type MapNumberFunction = MapFunction
See MapFunction
`typescript`
type CompiledMapNumberFunction = CompiledMapFunction
MIT © 2019-2024 Manuel Fernández