Advertisement
martyndev

Draw Line With Html Canvas Element

Mar 1st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
  5.  
  6. <meta charset="utf-8">
  7. <title>JS Bin</title>
  8.  
  9. <script>
  10.   // @martyndev
  11. // Global vars
  12. var _X     = "0";
  13. var _Y     = "0";
  14. var _XList = new Array(0);
  15. var _YList = new Array(0);
  16.  
  17. function setSpace()
  18. {
  19.   if(_XList[_XList.length - 1] === -1) { return; }
  20.   _XList.push(-1);
  21.   _YList.push(-1);
  22. }
  23.  
  24. function updateArray(x, y)
  25. {
  26.   _XList.push(x);
  27.   _YList.push(y);
  28.   _X = x;
  29.   _Y = y;
  30. }
  31.  
  32. function checkRoute()
  33. {
  34.   var xRoute = "";
  35.   var yRoute = "";
  36.  
  37.   for(var i=0; i<_XList.length; i++)
  38.   {
  39.     xRoute = xRoute + ", "+ _XList[i];
  40.   }
  41.  
  42.   for(i=0; i<_YList.length; i++)
  43.   {
  44.     yRoute = yRoute + ", "+ _YList[i];
  45.   }
  46.  
  47.  
  48.   $("#route").html("XRoute: " + xRoute + "<br /><br /> yRoute: " + yRoute);
  49. }
  50.  
  51. function draw_line(fromX, FromY, ToX, ToY)
  52. {
  53.   var objContent = document.getElementById("content");  
  54.   var ctx        = objContent.getContext("2d");
  55.  
  56.   ctx.beginPath();
  57.   ctx.lineWidth   = 2;
  58.   ctx.strokeStyle = '#ff0000';  
  59.   ctx.moveTo(fromX, FromY);
  60.   ctx.lineTo(ToX, ToY);  
  61.   ctx.stroke();  
  62. }
  63.  
  64. function draw_signature(e)
  65. {
  66.   //var objContent = document.getElementById("content");
  67.   var objContent = $("#content")[0];
  68.   var xClicked   = e.pageX - objContent.offsetLeft;
  69.   var yClicked   = e.pageY - objContent.offsetTop;  
  70.   var coords     = "X: "+xClicked+", Y: "+yClicked;
  71.  
  72.   $("#coord").text(coords);
  73.  
  74.   updateArray(xClicked, yClicked);
  75. }
  76.  
  77. function update_coords()
  78. {  
  79.   var coords = "X: " + _X + ", Y: " + _Y + "";
  80.  
  81.   $("#coord").text(coords);
  82.  
  83.   for(var i=0; i<_XList.length-1; i++)
  84.   {
  85.     if(_XList[i+1] === -1) { i++; continue; }
  86.     draw_line(_XList[i], _YList[i], _XList[i+1], _YList[i+1]);
  87.   }
  88.  
  89.   window.setTimeout(update_coords, 50);
  90. }
  91.  
  92. function mainEvent(e){  
  93.   // if it's not clicked, do not draw it (return)
  94.   if(e.which === 0){ return; }
  95.   draw_signature(e);
  96. }
  97. </script>
  98.  
  99. </head>
  100.  
  101. <body onLoad="update_coords()">
  102.   <span id="coord">X: 0, Y: 0 (OffSetLeft: 0, OffSetTop: 0)</span>
  103.   <br>
  104.  
  105.   <canvas id="content" width="310px" height="200px" style="border:1px solid green" onmousemove="mainEvent(event)" onmouseup="setSpace()">
  106.   </canvas>
  107.  
  108.   <br />
  109.  
  110.   <input type="button" value="Check Route" onClick="checkRoute()"></input>
  111.   <br>
  112.  
  113.   <span id="route"> </span>
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement