Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop along a diagonal line?

Tags:

Are there any examples of a drag-and-drop solution in which the elements being dragged can only move along a slanted line? For example, constrain an element's draggability so that it can only be moved along a 30º line, or 10º, etc.

Most examples I've been able to find only constrain the dragged element's area to either a vertical or horizontal line, or to within a larger parent div.

Possibly related: Drag along a diagonal line no further than 100px, or along a curve.

like image 951
Jason Hall Avatar asked Oct 30 '09 02:10

Jason Hall


1 Answers

complete example (FF only)

<div id="drag" style="position:absolute;width:20px;height:20px;background:red"></div>
<script>
var angle = 10;
window.onload = function() 
{
    var d = document.getElementById("drag");
    d.onmousedown = function() {
        document.onmouseup = function() {
            document.onmousemove = null;
        }
        document.onmousemove = function(e) {
            d.style.left = e.clientX;
            d.style.top = e.clientX * Math.tan(angle * Math.PI / 180);
        }
    }
}
</script>
like image 59
user187291 Avatar answered Oct 14 '22 21:10

user187291