asimryu

drag and drop and draw line between two

Oct 19th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>
  8.     <script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  9.     <style>
  10.         #content{
  11.             position:relative;
  12.         }
  13.         .mydiv{
  14.             border:1px solid #368ABB;
  15.             background-color:#43A4DC;
  16.             position:absolute;
  17.             width:50px;
  18.             height: 50px;
  19.         }
  20.         .mydiv:after{
  21.             content:no-close-quote;
  22.             position:absolute;
  23.             top:50%;
  24.             left:50%;
  25.             background-color:black;
  26.             width:4px;
  27.             height:4px;
  28.             border-radius:50%;
  29.             margin-left:-2px;
  30.             margin-top:-2px;
  31.         }
  32.  
  33.         .drag {
  34.             left:20px;
  35.         }
  36.         .drop {
  37.             left:200px;
  38.         }
  39.         #div1, #div2{ top:20px; }
  40.         #div3, #div4{ top:100px; }
  41.         #div5, #div6{ top:180px; }
  42.  
  43.         .line{
  44.             position:absolute;
  45.             width:1px;
  46.             background-color:red;
  47.         }  
  48.  
  49.     </style>
  50. </head>
  51. <body>
  52.  
  53.     <div id="content">
  54.         <div id="div1" class="mydiv drag"></div>
  55.         <div id="div2" class="mydiv drop"></div>
  56.         <div id="div3" class="mydiv drag"></div>
  57.         <div id="div4" class="mydiv drop"></div>    
  58.         <div id="div5" class="mydiv drag"></div>
  59.         <div id="div6" class="mydiv drop"></div>        
  60.     </div>
  61.  
  62.     <script>
  63.         let from, to;
  64.         let fT=0, tT=0, fL=0, tL=0;
  65.         function drawLine(){
  66.             let line = document.createElement("div");
  67.             line.classList.add("line");
  68.             var CA   = Math.abs(tT - fT);
  69.             var CO   = Math.abs(tL - fL);
  70.             var H    = Math.sqrt(CA*CA + CO*CO);
  71.             var ANG  = 180 / Math.PI * Math.acos( CA/H );
  72.  
  73.             if(tT > fT){
  74.                 var top  = (tT-fT)/2 + fT;
  75.             }else{
  76.                 var top  = (fT-tT)/2 + tT;
  77.             }
  78.             if(tL > fL){
  79.                 var left = (tL-fL)/2 + fL;
  80.             }else{
  81.                 var left = (fL-tL)/2 + tL;
  82.             }
  83.  
  84.             if(( fT < tT && fL < tL) || ( tT < fT && tL < fL) || (fT > tT && fL > tL) || (tT > fT && tL > fL)){
  85.                ANG *= -1;
  86.             }
  87.             top-= H/2;
  88.  
  89.             line.style["-webkit-transform"] = 'rotate('+ ANG +'deg)';
  90.             line.style["-moz-transform"] = 'rotate('+ ANG +'deg)';
  91.             line.style["-ms-transform"] = 'rotate('+ ANG +'deg)';
  92.             line.style["-o-transform"] = 'rotate('+ ANG +'deg)';
  93.             line.style["-transform"] = 'rotate('+ ANG +'deg)';
  94.             line.style.top    = top+'px';
  95.             line.style.left   = left+'px';
  96.             line.style.height = H + 'px';
  97.             document.querySelector("#content").appendChild(line);
  98.         }
  99.        
  100.         $(".drag").draggable({
  101.             revert:false,
  102.             start: function(event, ui){
  103.                 from = this;
  104.                 $(from).draggable("option", "revert", false);
  105.                 fT = from.offsetTop  + from.offsetHeight/2;
  106.                 fL = from.offsetLeft + from.offsetWidth/2;
  107.             }
  108.         });
  109.         $( ".drop" ).droppable({
  110.             drop: async function( event, ui ) {
  111.                 to = this;
  112.                 tT = to.offsetTop    + to.offsetHeight/2;
  113.                 tL = to.offsetLeft   + to.offsetWidth/2;
  114.                 $(from).draggable("option", "revert", true);
  115.                 await drawLine();
  116.             }
  117.         });
  118.     </script>
  119. <!-- original code from "https://jsfiddle.net/rdamasceno/o3Lroapa/5/"     -->
  120. </body>
  121. </html>
Add Comment
Please, Sign In to add comment