2011-11-04 53 views
1

有一個「簽名板」我爲建設一個就業申請一個奇怪的問題...HTML5畫布繪製應用... X,Y是正確的,但圖紙是關閉

的問題是,當你在畫布的左側,畫出的線條和光標排列在一起...隨着您向右側移動X和絃被繪製並且光標的x和絃不對齊,兩次與火狐打交道時長,當您移動從左至右,我沒有乘在我的代碼「*」只減。

HTML 我沒有離開OTH呃腳本/ div最有可能會在那裏使用,所以你可以看到完整的代碼......我還包括那些即使他們現在應該沒有效果的JS。

<div id="container"> 
    <canvas id="imageView"> 
     <p> 
      Unfortunately, your browser is currently unsupported by our web 
      application. We are sorry for the inconvenience. Please use one of the 
      supported browsers listed below, or fill out a paper Signature release. 
     </p> 
     <p> 
      Supported browsers:<br /> 
      <a href="http://www.opera.com/browser/download/">Opera Browser</a> 
      <a href="http://www.mozilla.org/en-US/firefox/features/">Firefox</a> 
      <a href="http://www.apple.com/safari/download/">Safari</a> 
      <a href="http://www.konqueror.org/download/">Konqueror (Linux PC)</a> 
     </p> 
    </canvas><!-- 
    <div id="SigCover"></div> 
    <div id="SigCoverText"><span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Signature Saved</span></div> 
    <div class="ClearBoth"></div>--> 
</div> 
<form action=""> 
    <input type="button" value="Save Signature" onclick="SaveImage()" /> 
    <input type="button" value="Reset Signature" onclick="ResetSignature()" /> 
</form> 
<div id="ImageToSave"></div> 
<!--<script type="text/javascript" src="@Url.Content("~/Scripts/Signature/canvas2image.js")"></script>--> 
<script type="text/javascript" src="@Url.Content("~/Scripts/Signature/Signature.js")"></script><!-- 
<script type="text/javascript" src="@Url.Content("~/Scripts/Signature/SaveSignature.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/Signature/ResetSignature.js")"></script>--> 

簽名/繪圖JS ...

var points = new Array(); 
if (window.addEventListener) { 
    window.addEventListener('load', function() { 
     var canvas, context, tool; 

     function init() { 
      // Find the canvas element. 
      canvas = document.getElementById('imageView'); 
      if (!canvas) { 
       alert('Error: I cannot find the canvas element!'); 
       return; 
      } 

      if (!canvas.getContext) { 
       alert('Error: no canvas.getContext!'); 
       return; 
      } 

      // Get the 2D canvas context. 
      context = canvas.getContext('2d'); 
      if (!context) { 
       alert('Error: failed to getContext!'); 
       return; 
      } 

      // Pencil tool instance. 
      tool = new tool_pencil(); 

      // Attach the mousedown, mousemove and mouseup event listeners. 
      canvas.addEventListener('mousedown', ev_canvas, false); 
      canvas.addEventListener('mousemove', ev_canvas, false); 
      canvas.addEventListener('mouseup', ev_canvas, false); 
     } 

     // This painting tool works like a drawing pencil which tracks the mouse 
     // movements. 
     function tool_pencil() { 
      var tool = this; 
      this.started = false; 

      // This is called when you start holding down the mouse button. 
      // This starts the pencil drawing. 
      this.mousedown = function (ev) { 
       context.beginPath(); 
       context.moveTo(ev._x, ev._y); 
       tool.started = true; 
      }; 

      // This function is called every time you move the mouse. Obviously, it only 
      // draws if the tool.started state is set to true (when you are holding down 
      // the mouse button). 
      this.mousemove = function (ev) { 
       if (tool.started) { 
        context.lineTo(ev._x, ev._y); 
        context.stroke(); 
       } 
      }; 

      // This is called when you release the mouse button. 
      this.mouseup = function (ev) { 
       if (tool.started) { 
        tool.mousemove(ev); 
        tool.started = false; 
       } 
      }; 
     } 
     // The general-purpose event handler. This function just determines the mouse 
     // position relative to the canvas element. 
     function ev_canvas(ev) { 
      if (navigator.appName == 'Microsoft Internet Explorer' || navigator.vendor == 'Google Inc.' || navigator.vendor == 'Apple Computer, Inc.') { // IE or Chrome 
       ev._x = ev.offsetX; 
       ev._y = ev.offsetY; 
      } else if (ev.layerX || ev.layerX == 0) { // Firefox 
       ev._x = ev.layerX - this.offsetLeft; 
       ev._y = ev.layerY - this.offsetTop; 
      } else if (ev.offsetX || ev.offsetX == 0) { // Opera 
       ev._x = ev.offsetX; 
       ev._y = ev.offsetY; 
      } 
      // Call the event handler of the tool. 
      var func = tool[ev.type]; 
      if (func) { 
       func(ev); 
      } 
      points.push(ev); 
     } 

     init(); 

    }, false); 
} 

復位簽名JS ...

function ResetSignature() { 
    var canvasReset = document.getElementById('imageView'); 
    var contextReset = canvasReset.getContext('2d'); 

    contextReset.fillStyle = '#000000'; 
    contextReset.fillRect(0, 0, $('imageView').css('width'), $('imageView').css('height')); 
    canvasReset.width = canvasReset.width; 
    canvasReset.width = canvasReset.width; 

    alert(points.length); 
    points = new Array(); 
} 

保存簽名JS(使用Canvas2Image LIB)

function SaveImage() { 
    var CanvasToSave = document.getElementById('imageView'); 

    var oImg = Canvas2Image.saveAsPNG(CanvasToSave, true); 

    $('#ImageToSave').html(oImg); 

    $('#SigCover').css('z-index', 102); 
    $('#SigCover').css('left', 23); 
    $('#SigCover').css('width', 402); 
    $('#SigCover').css('height', 152); 
    $('#SigCoverText').css('z-index', 101); 
    $('#SigCoverText').css('left', 23); 
    $('#SigCoverText').css('width', 400); 
    $('#SigCoverText').css('height', 150); 
    alert(points.length); 
} 

最後的CSS

#imageView 
{ 
    background-color: #FFFFFF; 
    width: 400px; 
    height: 150px; 
    z-index: 100; 
}/* 

#SigCover 
{ 
    background-color: Gray; 
    opacity: .5; 
    width: 0px; 
    height: 0px; 
    position: absolute; 
    top: 57px; 
    left: -4000px; 
    z-index: -1; 
    float: left; 
} 

#SigCoverText 
{ 
    background-color: Gray; 
    opacity: .5; 
    width: 0px; 
    height: 0px; 
    position: absolute; 
    top: 57px; 
    left: -4000px; 
    z-index: -1; 
    float: left; 
} 

我只是找不到什麼原因造成的X choord方差成倍增長就像是......在Y choord在整個精細且沒有差異。把我的頭髮拉出來!

編輯:我包括圖像,以告訴你我在說什麼大(r)黑點是光標的大概位置頂圖像是非常現貨,底部圖像,你可以看到光標遠離它應該在的位置左側。

Image showing correct cursor position towards left of canvas

Image showing incorrect cursor position towards right of canvas

編輯2:根據要求這已投入的jsfiddle ... HERE

+0

我建議把所有這些代碼的jsfiddle – rochal

+0

做感謝提示那個...我總是忘記它,但是當別人包括它時就愛它。 – Jared

回答

3

你必須使用canvas.width和canvas.height大小畫布, 不是 CSS寬度/高度。

這裏你只是拉伸的畫布:

#imageView 
{ 
    background-color: #FFFFFF; 
    width: 400px; 
    height: 150px; 
    z-index: 100; 
} 

而且你不想這樣做。

這是固定的:

http://jsfiddle.net/KtuRA/5/

+1

+1最後一次與這個縮放問題作鬥爭,並沒有意識到我需要修改canvas元素的寬度和高度,而不是元素的CSS樣式。謝謝! – DavidAndroidDev

+0

非常感謝! – Jared

+0

@SimonSarris碰巧知道它是否會導致問題設置兩個值?我在js中使用css屬性,所以想保留兩個 – Jared

0

添加滾動頂部和左右滾動,因爲在滾動我們應該加上這些因素的時候。試試下面的代碼,它可能對你有幫助。

<html> 
    <script type="text/javascript"> 
    var canvas,ctx,flag=false,prevX=0,currX=0,prevY=0,currY=0,dot_flag=false; 
    var x="black",y=2; 
    function init() 
    { 
     canvas=document.getElementById('can'); 
     ctx=canvas.getContext("2d"); 
     w=canvas.width; 
     h=canvas.height; 

     canvas.addEventListener("mousemove",function(e){ findxy('move',e) },false); 
     canvas.addEventListener("mousedown",function(e){ findxy('down',e) },false); 
     canvas.addEventListener("mouseup",function(e){ findxy('up',e) },false); 
     canvas.addEventListener("mouseout",function(e){ findxy('out',e) },false); 
    } 
    function color(obj) 
    { 
     switch(obj.id) 
     { 
      case "green" : x="green";break; 
      case "blue" : x="blue";break; 
      case "red" : x="red";break; 
      case "yellow" : x="yellow";break; 
      case "orange" : x="orange";break; 
      case "black" : x="black";break; 
      case "white" : x="white";break; 
     } 
     if(x=="white")y=14; 
     else y=2; 

    } 
    function draw() 
    { 
     ctx.beginPath(); 
     ctx.moveTo(prevX,prevY); 
     ctx.lineTo(currX,currY); 
     ctx.strokeStyle=x; 
     ctx.lineWidth=y; 
     ctx.stroke(); 
     ctx.closePath(); 
    } 
    function erase() 
    { 
     var m=confirm("Want to clear"); 
     if(m) 
     { 
      ctx.clearRect(0,0,w,h); 
      document.getElementById("canvasimg").style.display="none"; 
     } 
    } 
    function save() 
    { 
     document.getElementById("canvasimg").style.border="2px solid"; 
     var dataURL = canvas.toDataURL(); 
     document.getElementById("canvasimg").src = dataURL; 
     document.getElementById("canvasimg").style.display="inline"; 
    } 
    function findxy(res,e) 
    { 

     if(res=='down') 
        { prevX=currX;prevY=currY; 
         currX=e.clientX-canvas.offsetLeft; 
         currY=e.clientY-canvas.offsetTop; 

         flag=true; 
         dot_flag=true; 
         if(dot_flag) 
         { 
         ctx.beginPath(); 
         ctx.fillStyle=x; 
         ctx.fillRect(currX,currY,2,2); 
         ctx.closePath(); 
         dot_flag=false; 
         } 
         } 
       if(res=='up'||res=="out") 
       { 
        flag=false; 
       } 
       if(res=='move') 
       { 

        if(flag) 
        { 
        prevX=currX; 
        prevY=currY; 
        currX=e.clientX-canvas.offsetLeft; 
        currY=e.clientY-canvas.offsetTop; 
       draw(); 
        } 
       } 
    } 

    </script> 

    <body onload="init()"> 
      <canvas id="can" width="400"height="400" style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas> 

      <div style="position:absolute;top:12%;left:43%;">Choose Color</div> 
      <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" id="green" onclick="color(this)"></div> 
      <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" id="blue" onclick="color(this)"></div> 
      <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" id="red" onclick="color(this)"></div> 
      <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" id="yellow" onclick="color(this)"></div> 
      <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" id="orange" onclick="color(this)"></div> 
      <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" id="black" onclick="color(this)"></div> 
      <div style="position:absolute;top:20%;left:43%;">Eraser</div> 
      <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" id="white" onclick="color(this)"></div> 

      <img id="canvasimg"style="position:absolute;top:10%;left:52%;" style="display:none;"> 
     <input type="button" value="save" id="btn" size="30" onclick="save()"style="position:absolute;top:55%;left:10%;"> 
     <input type="button" value="clear" id="clr" size="23" onclick="erase()"style="position:absolute;top:55%;left:15%;"> 
    </body> 
</html> 
1

我同意Simon Sarris的答案和一個額外的細節。如果您不想指定畫布高度和寬度並希望獲取父級的高度,那麼爲了避免您的繪圖偏離興趣點,請在init函數中添加以下代碼行

canvas.width = canvas.offsetWidth; 
canvas.height = canvas.offsetHeight; 

而且計算準確ev._x和ev._y,做閱讀這篇文章,它幫助時,文檔結構複雜

Tracking mouse position in canvas when no surrounding element exists