I've searched for an answer for this and couldn't find it... so hopefully someone can help me out. I'm trying to create a draggable div (using jquery ui) that is contained to a circle (and not a square). Right now, I've got the containment setting linked to it's parent div, but no matter what I try... it still allows the draggable element into the corners of the parent square div. I've tried setting the border-radius of the parent div so that it's circular and not square (but this might just be a css thing, and not effecting the actual div shape). Does anyone know how to constrain a draggable div to a circle? Thanks, Andy
var containment_radius = 50;
$(this.dragobject).draggable({
drag: function() {
var position = $(this.dragobject).position();
var result = limit(position.left, position.top, containment_radius, containment_radius);
if (!result.limit) {
$(this.dragobject).x = result.x + "px";
$(this.dragobject).y = result.y + "px";
}
console.log(result);
}
});
function limit(x, y, x1, y1) {
var dist = distance([x, y], [x1, y1]);
if (dist <= containment_radius) {
return {x: x, y: y};
} else {
return {limit: true};
}
}
function distance(dot1, dot2) {
var x1 = dot1[0],
y1 = dot1[1],
x2 = dot2[0],
y2 = dot2[1];
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}