Wraps an integer number within a specified range, defaulting to degrees (0-360). Use wrap for floating-point wrapping.
This is useful for calculations involving degree angles and hue, which wrap from 0-360. Eg: to add 200 to 200, we don't want 400, but 40.
const v = wrapInteger(200+200, 0, 360); // 40 Copy
const v = wrapInteger(200+200, 0, 360); // 40
Or if we minus 100 from 10, we don't want -90 but 270
const v = wrapInteger(10-100, 0, 360); // 270 Copy
const v = wrapInteger(10-100, 0, 360); // 270
wrapInteger uses 0-360 as a default range, so both of these examples could just as well be:
wrapInteger
wrapInteger(200+200); // 40wrapInteger(10-100); // 270 Copy
wrapInteger(200+200); // 40wrapInteger(10-100); // 270
Non-zero starting points can be used. A range of 20-70:
const v = wrapInteger(-20, 20, 70); // 50 Copy
const v = wrapInteger(-20, 20, 70); // 50
Note that the minimum value is inclusive, while the maximum is exclusive. So with the default range of 0-360, 360 is never reached:
wrapInteger(360); // 0wrapInteger(361); // 1 Copy
wrapInteger(360); // 0wrapInteger(361); // 1
If you just want to lock values to a range without wrapping, consider clamp.
Value to wrap
Integer minimum of range (default: 0). Inclusive
Integer maximum of range (default: 360). Exlusive
Wraps an integer number within a specified range, defaulting to degrees (0-360). Use wrap for floating-point wrapping.
This is useful for calculations involving degree angles and hue, which wrap from 0-360. Eg: to add 200 to 200, we don't want 400, but 40.
Or if we minus 100 from 10, we don't want -90 but 270
wrapInteger
uses 0-360 as a default range, so both of these examples could just as well be:Non-zero starting points can be used. A range of 20-70:
Note that the minimum value is inclusive, while the maximum is exclusive. So with the default range of 0-360, 360 is never reached:
If you just want to lock values to a range without wrapping, consider clamp.