画布签名生成图片

<!doctype html>
  <html lang="en">
  <head>
  <title>一款签名变成图片的插件</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;">
  <style>
  #c1 { background-color: pink; }
  </style>
  </head>
  <body>
  <canvas id="c1" width="400px" height="400px"> <!--宽高写在html里,写在css里有问题-->
  <span>该浏览器不支持canvas内容</span> <!--对于不支持canvas的浏览器显示-->
  </canvas>
  <input type="button" value="按钮" id="submit"/>
  <input type="button" value="清除" id="clear"/>
  <img id="qmimg"/>
  </body>
  <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
  <script>
  /*插件参考地址http://www.jq22.com/jquery-info1057*/
 
  /*移动端事件*/
  var canvas = document.getElementById('c1');
  var ctx = canvas.getContext('2d')
  canvas.addEventListener('touchstart', function (ev) {
  ev.preventDefault();
  ctx.beginPath();
  ctx.strokeStyle = 'red';
  ctx.lineWidth="5";
  var ev = ev.touches[0] || window.event.touches[0];
  ctx.moveTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
  canvas.addEventListener('touchmove',function (ev) {
  var ev = ev.touches[0] || window.event.touches[0];
  ctx.lineTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
  ctx.stroke();
  })
  // canvas.addEventListener('touchend',function (ev) {
  // canvas.onmousemove = null;
  // canvas.onmouseup = null;
  // })
  })
  document.getElementById('clear').addEventListener("click", function(){
  canvas.width = canvas.width;
  });
  document.getElementById('submit').addEventListener("click", function(){
  $('#qmimg').attr("src",canvas.toDataURL("image/png"));
  });
 
  /*pc端事件*/
  canvas.onmousedown = function (ev) {
  ctx.beginPath();
  ctx.strokeStyle = 'red';
  ctx.lineWidth="5";
  ctx.lineJoin = 'round';
  var ev = ev || window.event;
  ctx.moveTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
  canvas.onmousemove = function (ev) {
  ctx.lineTo(ev.clientX-canvas.offsetLeft,ev.clientY-canvas.offsetTop);
  ctx.stroke();
  }
  canvas.onmouseup = function() {
  canvas.onmousemove = null;
  canvas.onmouseup = null;
  };
  }
 
  document.getElementById('submit').addEventListener("click", function(){
  $('#qmimg').attr("src",canvas.toDataURL("image/png"));
  });
 
  </script>
  </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值