UNPKG

map-number

Version:

processing/p5.js map like function, including floating point numbers support

45 lines (36 loc) 1.19 kB
'use strict'; function map(num, inMin, inMax, outMin, outMax) { return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } function transformed(map, transform) { return (num, inMin, inMax, outMin, outMax) => transform(map(num, inMin, inMax, outMin, outMax)); } const ceil = transformed(map, Math.ceil); function compile(map, inMin, inMax, outMin, outMax) { return value => map(value, inMin, inMax, outMin, outMax); } const floor = transformed(map, Math.floor); function limit(num, inMin, inMax, outMin, outMax) { const result = map(num, inMin, inMax, outMin, outMax); let boundMin = outMin; let boundMax = outMax; if (boundMax < boundMin) { const temp = boundMin; boundMin = boundMax; boundMax = temp; } return result > boundMax ? boundMax : result < boundMin ? boundMin : result; } const round = transformed(map, Math.round); exports.ceil = ceil; exports.clamp = limit; exports.compile = compile; exports.create = compile; exports.floor = floor; exports.limit = limit; exports.map = map; exports.round = round; exports.transform = transformed; exports.transformed = transformed; exports.wrap = compile; //# sourceMappingURL=index.cjs.map