not sure if this would help, as it doesn’t use the constrain() function.
Ive created the start of a cnc program, which uses classes to separate each section. You have menus, menu controls, and buttons, the menu function tracks the mouse and click, so that it activates only if the mouse is clicked in its location. After that the cnc area could be amended to take mouse input and draw instead of left, right, down, up.
alternatively heres an old paint program i started on khan academy
https://www.khanacademy.org/computer-programming/paint/5326237854695424
Soz just porting it to p5.js now.
please note this is old work and the logical functions aren’t great.
and finally paint ported to p5.js.