Skip to content

Instantly share code, notes, and snippets.

@io-developer
Last active October 3, 2018 18:59
Show Gist options
  • Save io-developer/22514f352506f5cfb3b964456e076fd3 to your computer and use it in GitHub Desktop.
Save io-developer/22514f352506f5cfb3b964456e076fd3 to your computer and use it in GitHub Desktop.
Clamp looped numbers such as Radians, Degrees, etc.
function loopNum(x, lim) {
return x - lim * Math.floor(x / lim);
}
console.log([
loopNum(1, 360), // 1
loopNum(361, 360), // 1
loopNum(-1, 360), // 359
loopNum(-361, 360), // 359
loopNum(0.1, 3.1415), // 0.1
loopNum(-0.1, 3.1415), // 3.0415
loopNum(3.5, 3.1415), // 0.3585
loopNum(-3.5, 3.1415), // 2.783
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment