canvas原生实现前端网页移动端签名
这篇文章给大家介绍的文章内容是关于canvas原生实现前端网页移动端签名,适用于vue.js react 等任何框架,有很好的参考价值,希望可以帮助到有需要的朋友。
let c = document.getElementById("canvas")let canvas = document.createElement("canvas")let availHeight = document.documentElement.clientHeightlet off = availHeight - 400canvas.height = '350'canvas.width = c.clientWidthc.appendChild(canvas)let dr = canvas.getContext('2d')dr.strokeStyle = 'blue'canvas.addEventListener('touchstart',(e)=>{ dr.beginPath() dr.moveTo(e.changedTouches["0"].pageX,e.changedTouches["0"].pageY-off)})canvas.addEventListener('touchmove',(e)=>{ dr.lineTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY-off) dr.stroke()})canvas.addEventListener('touchend',(e)=>{ dr.closePath()})
生成图片
let img = document.querySelector('canvas').toDataURL()
如需发送给后端,可以让后端支持base64或者blob、buffer