Well, as mentioned in the comments, relatively easy when the dots are on the same line.
Everything becomes more complicated if you have one on top of the other - then you basically need: an angle to apply it to the CSS transform: rotate the property and the length between two points / elements to recalculate the width, since it is not a straight line, I I hate math :), so I used the knowledge of good people from the Internet: http://jsfiddle.net/codepo8/bAwUf/light/ ( codepo8 loans)
The two most important lines are:
var angle= Math.atan2(red2.offsetTop - red1.offsetTop, red2.offsetLeft - red1.offsetLeft) * 180 / Math.PI; var length = Math.sqrt((red2.offsetLeft-red1.offsetLeft) * (red2.offsetLeft-red1.offsetLeft) + (red2.offsetTop-red1.offsetTop) * (red2.offsetTop-red1.offsetTop));
And now your script should look like this:
var s = document.querySelector(".line"); red1=document.getElementById('RED1'); red2=document.getElementById('RED2'); function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); ev.target.appendChild(document.getElementById(data)); s.style.left=red1.offsetLeft+5+'px'; s.style.top=red1.offsetTop+5+'px'; s.style.visibility='visible'; var angle= Math.atan2(red2.offsetTop - red1.offsetTop, red2.offsetLeft - red1.offsetLeft) * 180 / Math.PI; var length = Math.sqrt((red2.offsetLeft-red1.offsetLeft) * (red2.offsetLeft-red1.offsetLeft) + (red2.offsetTop-red1.offsetTop) * (red2.offsetTop-red1.offsetTop)); s.style.width=Math.abs(length)+'px'; s.style.transform="rotate("+Math.round(angle)+"deg)"; s.style.transformOrigin ="0 0"; }
.div1 { width: 150px; height: 150px; padding: 0px; border: 1px solid #aaaaff; float: left; } .line { position:absolute; height:3px; background:red; width:100px; z-index:999; visibility:hidden; }
<div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <div class="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> <img id="RED1" src="http://i.imgur.com/By4xvE5.png" draggable="true" ondragstart="drag(event)" align="left"> <img id="RED2" src="http://i.imgur.com/By4xvE5.png" draggable="true" ondragstart="drag(event)" align="left"> <div class="line"> </div>
This is a good starting point, I think (there are few things left to make it perfect), but I think you will get it ...
DEMO (itβs better to see it here, the fragments are not large, for such demonstrations): https://jsfiddle.net/dxre19o6/
sinisake
source share