jhylands

Create line in HTML outsourced code

Jan 14th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var isIE = navigator.userAgent.indexOf("MSIE") > -1;
  2.                 function createLine(x1, y1, x2, y2)
  3.         {
  4.                 if (x2 < x1)
  5.                 {
  6.                         var temp = x1;
  7.                                 x1 = x2;
  8.                                         x2 = temp;
  9.                                 temp = y1;
  10.                                         y1 = y2;
  11.                                 y2 = temp;
  12.                         }
  13.                         var line = document.createElement("div");
  14.                         line.className = "line";
  15.                         var length = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  16.                         line.style.width = length + "px";
  17.                                 if (isIE)
  18.                         {
  19.                                 line.style.top = (y2 > y1) ? y1 + "px" : y2 + "px";
  20.                                 line.style.left = x1 + "px";
  21.                                 var nCos = (x2-x1)/length;
  22.                                         var nSin = (y2-y1)/length;
  23.                                         line.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + nCos + ", M12=" + -1*nSin + ", M21=" + nSin + ", M22=" + nCos + ")";
  24.                                 }
  25.                                 else
  26.                                 {
  27.                                         var angle = Math.atan((y2-y1)/(x2-x1));
  28.                                         line.style.top = y1 + 0.5*length*Math.sin(angle) + "px";
  29.                                         line.style.left = x1 - 0.5*length*(1 - Math.cos(angle)) + "px";
  30.                                         line.style.MozTransform = line.style.WebkitTransform = line.style.OTransform= "rotate(" + angle + "rad)";
  31.                                 }
  32.                                 return line;
  33.                         }
Advertisement
Add Comment
Please, Sign In to add comment