pc端結合canvas實現簡單簽名功能

需求:業務員作提交時要簽名。。。javascript

代碼很少簡單易懂,直接看代碼css

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <style type="text/css">
   body{
    background: #ccc;
   }
   #oc{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin:200px auto;
    background: white;
   }
   .span{
    display: inline-block;
    width: 100px;
    height: 50px;
    background: #fff;
    line-height: 50px;
    text-align: center;
    margin: 193px 0 0 200px;
    cursor: pointer;
   }
  </style>
 </head>
 <body>
  <canvas id="oc" width="500" height="500"></canvas>
  <span class="span" onclick="reset()">重置</span>
 </body>
 <script type="text/javascript">

  // 獲取canvas節點
  let testNode = document.getElementById('oc');

  window.onload = function(){
   // 判斷是否支持
   if(testNode.getContext){
    var ctx = testNode.getContext("2d")
    testNode.onmousedown = function(ev){
     var ev = ev || event;
     ctx.beginPath();
     ctx.moveTo(ev.clientX - this.offsetLeft,ev.clientY - this.offsetTop);
     
     if(testNode.setCapture){
      testNode.setCapture(); // 對鼠標進行捕獲
     }
     document.onmousemove = function(ev){
      var ev = ev || event;
      ctx.lineTo(ev.clientX - testNode.offsetLeft,ev.clientY - testNode.offsetTop); // 繪製線條
      ctx.stroke() // 繪製路勁
      // console.log(ev.clientX - testNode.offsetLeft,ev.clientY - testNode.offsetTop)
     }
     document.onmouseup = function(){
      document.onmousemove = document.onmouseup=null;
      if(document.releaseCapture){
       document.releaseCapture(); // 釋放對鼠標的捕獲
      }
     }
     // 禁止事件的默認行爲  處理拖拽在主流瀏覽器內的兼容問題
     return false;
    }
   }
  }

  // 重置
  function reset() {
   testNode.getContext("2d").clearRect(0, 0, testNode.width, testNode.height); // 清空畫布
  }
 </script>
</html>

效果:html

結語: 以上就是今天要分享的內容了,效果簡單勿噴java

相關文章
相關標籤/搜索