This example draws a circle as the cursor’s position but keeps the circle within a rectangle. It does so by passing the mouse’s coordinates into the constrain() function.
Constrain by p5.js Contributors and the Processing Foundation is licensed under CC BY-NC-SA 4.0.