JSFiddle - React, Tailwind, and code Playground
HTML
<div id="arena">
<div id="draggable"/>
</div>
CSS
#arena {
position: relative;
width: 300px;
height: 300px;
border: 1px solid #ccc;
}
#draggable {
width: 20px;
height: 20px;
background: red;
position: absolute;
top: 50%;
left: 50%;
margin-left: -10px;
margin-top: -10px;
}
JavaScript
var halfDist = 150, // the mouse distance at which the mouse has moved twice as far as the draggable
centerX = 150,
centerY = 150;
$("#draggable").draggable({
drag: function(event, ui) {
var x, y, dist, ratio, factor;
x = ui.position.left - centerX;
y = ui.position.top - centerY;
dist = Math.sqrt(x*x + y*y);
ratio = dist / halfDist
factor = 1 / (ratio + 1);
x *= factor;
y *= factor;
ui.position.left = x + centerX;
ui.position.top = y + centerY;
}
});