jhylands

Move a line

Jan 13th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <style type="text/css">
  2. .line
  3. {
  4. position: absolute;
  5. height: 0px;
  6. border-width: 2px 0px 0px 0px;
  7. border-style: solid;
  8. border-color: #99aadd;
  9. }
  10. </style>
  11. <script type="text/javascript">
  12. var isIE = navigator.userAgent.indexOf("MSIE") > -1;
  13. window.onload = init;
  14. function createLine(x1, y1, x2, y2)
  15. {
  16. if (x2 < x1)
  17. {
  18. var temp = x1;
  19. x1 = x2;
  20. x2 = temp;
  21. temp = y1;
  22. y1 = y2;
  23. y2 = temp;
  24. }
  25. var line = document.createElement("div");
  26. line.className = "line";
  27. var length = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  28. line.style.width = length + "px";
  29. if (isIE)
  30. {
  31. line.style.top = (y2 > y1) ? y1 + "px" : y2 + "px";
  32. line.style.left = x1 + "px";
  33. var nCos = (x2-x1)/length;
  34. var nSin = (y2-y1)/length;
  35. line.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + nCos + ", M12=" + -1*nSin + ", M21=" + nSin + ", M22=" + nCos + ")";
  36. }
  37. else
  38. {
  39. var angle = Math.atan((y2-y1)/(x2-x1));
  40. line.style.top = y1 + 0.5*length*Math.sin(angle) + "px";
  41. line.style.left = x1 - 0.5*length*(1 - Math.cos(angle)) + "px";
  42. line.style.MozTransform = line.style.WebkitTransform = line.style.OTransform= "rotate(" + angle + "rad)";
  43. }
  44. return line;
  45. }
  46.  
  47. function init()
  48. {
  49. var demoElement = document.getElementById("demo");
  50. demoElement.appendChild(createLine(0, 0, 100, 0));
  51. }
  52. function change()
  53. {
  54. var demoElement = document.getElementById("demo");
  55. demoElement.removeChild(demoElement.childNodes[0]);
  56. demoElement.appendChild(createLine(200, 200, 100, 100));
  57.  
  58. }
  59. </script>
  60. <div id="demo"></div>
  61. <input type ="button" onclick="change();" />
Advertisement
Add Comment
Please, Sign In to add comment