Triangle restricted drag

whats the best way to restrict a draggable object to a non-rectangular shape (like triangle)
I did it with hittest but i cant get it to go to the nearest point when the cursor is outside the shape…
Thanks