Draw line freehand and close

I’m writing a program that lets the user draw a freehand line following the mouse. I want the line to close as the user crosses the line at the point where it crosses. Any thought on how to implement this?