ive kinda been able to do it with the distance function but since its only detecting distance from one point to another, the box cant fold back on itself if the cursor goes in the opposite direction of the origin point
ive also found a discussion on this topic from 10 years ago but the links are dead :/