Constrain movement to path

How would I allow the user to move (drag) a shape ie circle along a path but not off the path.

If the path were a polyline, the shape could move back and forth and along the line but not completely off the line (but the shape isn’t constrained by a point, rather it can be moved across the line until the farthest edge just touches the line)

Sorry if I haven’t described it clearly enough, I have tried to search for this but haven’t been able to find anything.