Register the mousemove event handler on window, then you will still get the events when the mouse moves out of the window/frame while dragging and it won't be that buggy.
Things are much nicer now and the problem is entirely avoided by using pointer events: https://developer.mozilla.org/en-US/docs/Web/API/Element/set...