2012-06-13 1 views
1

지금까지는 완벽하게 그릴 수있는 모바일 터치 연필 도구였습니다. 그러나 궁금한데 ... 어떻게하면 선 밖으로 그릴 수있는 기본 연필 도구 대신 완전히 직선을 그릴 수 있을까요? 여기 (터치) 연필 도구를 직선 도구로 바꾸는 방법?


지금까지 코드입니다 :

// "Draw Line" Button 
$(document).ready(function() { 
     initialize(); 
     }); 


     // works out the X, Y position of the click inside the canvas from the X, Y position on the page 
     function getPosition(mouseEvent, sigCanvas) { 
     var x, y; 
     if (mouseEvent.pageX != undefined && mouseEvent.pageY != undefined) { 
      x = mouseEvent.pageX; 
      y = mouseEvent.pageY; 
     } else { 
      x = mouseEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
      y = mouseEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
     } 

     return { X: x - sigCanvas.offsetLeft, Y: y - sigCanvas.offsetTop }; 
     } 

     function initialize() { 
     // get references to the canvas element as well as the 2D drawing context 
     var sigCanvas = document.getElementById("canvasSignature"); 
     var context = sigCanvas.getContext("2d"); 
     context.strokeStyle = 'Black'; 

     // This will be defined on a TOUCH device such as iPad or Android, etc. 
     var is_touch_device = 'ontouchstart' in document.documentElement; 

     if (is_touch_device) { 
      // create a drawer which tracks touch movements 
      var drawer = { 
       isDrawing: false, 
       touchstart: function (coors) { 
        context.beginPath(); 
        context.moveTo(coors.x, coors.y); 
        this.isDrawing = true; 
       }, 
       touchmove: function (coors) { 
        if (this.isDrawing) { 
        context.lineTo(coors.x, coors.y); 
        context.stroke(); 
        } 
       }, 
       touchend: function (coors) { 
        if (this.isDrawing) { 
        this.touchmove(coors); 
        this.isDrawing = false; 
        } 
       } 
      }; 

      // create a function to pass touch events and coordinates to drawer 
      function draw(event) { 

       // get the touch coordinates. Using the first touch in case of multi-touch 
       var coors = { 
        x: event.targetTouches[0].pageX, 
        y: event.targetTouches[0].pageY 
       }; 

       // Now we need to get the offset of the canvas location 
       var obj = sigCanvas; 

       if (obj.offsetParent) { 
        // Every time we find a new object, we add its offsetLeft and offsetTop to curleft and curtop. 
        do { 
        coors.x -= obj.offsetLeft; 
        coors.y -= obj.offsetTop; 
        } 
        // The while loop can be "while (obj = obj.offsetParent)" only, which does return null 
        // when null is passed back, but that creates a warning in some editors (i.e. VS2010). 
        while ((obj = obj.offsetParent) != null); 
       } 

       // pass the coordinates to the appropriate handler 
       drawer[event.type](coors); 
      } 


      // attach the touchstart, touchmove, touchend event listeners. 
      sigCanvas.addEventListener('touchstart', draw, false); 
      sigCanvas.addEventListener('touchmove', draw, false); 
      sigCanvas.addEventListener('touchend', draw, false); 

      // prevent elastic scrolling 
      sigCanvas.addEventListener('touchmove', function (event) { 
       event.preventDefault(); 
      }, false); 
     } 
     else { 

      // start drawing when the mousedown event fires, and attach handlers to 
      // draw a line to wherever the mouse moves to 
      $("#canvasSignature").mousedown(function (mouseEvent) { 
       var position = getPosition(mouseEvent, sigCanvas); 

       context.moveTo(position.X, position.Y); 
       context.beginPath(); 

       // attach event handlers 
       $(this).mousemove(function (mouseEvent) { 
        drawLine(mouseEvent, sigCanvas, context); 
       }).mouseup(function (mouseEvent) { 
        finishDrawing(mouseEvent, sigCanvas, context); 
       }).mouseout(function (mouseEvent) { 
        finishDrawing(mouseEvent, sigCanvas, context); 
       }); 
      }); 

     } 
     } 

     // draws a line to the x and y coordinates of the mouse event inside 
     // the specified element using the specified context 
     function drawLine(mouseEvent, sigCanvas, context) { 

     var position = getPosition(mouseEvent, sigCanvas); 

     context.lineTo(position.X, position.Y); 
     context.stroke(); 
     } 

     // draws a line from the last coordiantes in the path to the finishing 
     // coordinates and unbind any event handlers which need to be preceded 
     // by the mouse down event 
     function finishDrawing(mouseEvent, sigCanvas, context) { 
     // draw the line to the finishing coordinates 
     drawLine(mouseEvent, sigCanvas, context); 

     context.closePath(); 

     // unbind any events which could draw 
     $(sigCanvas).unbind("mousemove") 
        .unbind("mouseup") 
        .unbind("mouseout"); 
     } 

감사합니다, Wardenclyffe

답변

1

앵커 펜 아래 지점 첫 번째 터치에서하고 다음 터치는 처음부터 직선을 그릴 어디든지 새로운 터치를 만져라. 새 줄을 수락하려면 새 터치 위에 부동 체크 표시가 있어야합니다. 체크 표시가 선택되지 않은 상태에서 다른 터치가있는 경우 이전 줄을 제거하고 새 줄을 그리고 새 줄을 저장하기 위해 다른 체크 표시를하십시오. 선 그리기 도구를 취소하려면 원래 펜 다운 포인트 위에 (x)를 입력하십시오.

0

직선 도구와 연필 도구의 차이점은 연필이 가능한 한 많은 점을 그리고 그 (아마도 부드럽게 처리 된) 선분으로 연결된다는 것입니다. 직선 도구는 비교적 큰 선분으로 적은 수의 선을 연결합니다.

하나의 옵션은 손가락이 화면에 닿는 한 연필 도구가 포인트를 올리는 것입니다. 그리고 라인 도구는 탭에서 탭으로 끌어옵니다. 또는 탭하고 슬라이드 할 때 선 도구가 터치 점에서 드래그 점까지 선을 고무 밴드로 감은 후 터치가 들리기 전에 마지막 점을 유지할 수 있습니다.