map-number
Version:
processing/p5.js map like function, including floating point numbers support
33 lines (25 loc) • 1.04 kB
JavaScript
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);
export { ceil, limit as clamp, compile, compile as create, floor, limit, map, round, transformed as transform, transformed, compile as wrap };
//# sourceMappingURL=map.mjs.map