canvas-sketch-util
Version:
Utilities for sketching in Canvas, WebGL and generative art
18 lines (17 loc) • 457 B
JavaScript
module.exports = wrap;
function wrap (value, from, to) {
if (typeof from !== 'number' || typeof to !== 'number') {
throw new TypeError('Must specify "to" and "from" arguments as numbers');
}
// algorithm from http://stackoverflow.com/a/5852628/599884
if (from > to) {
var t = from;
from = to;
to = t;
}
var cycle = to - from;
if (cycle === 0) {
return to;
}
return value - cycle * Math.floor((value - from) / cycle);
}