Vue Canvas实现电子签名
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
最近再做移动端电子签名,Vue+Canvas实现,移动端、PC端均可,也可以从github下载 。
我在做这个功能的时候参考了 这个代码,但是在移动端光标与实际划线有偏移,我在我的代码中修正了这个问题。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < template > < section class = "signature" > < div class = "signatureBox" > < div class = "canvasBox" ref = "canvasHW" > < canvas ref = "canvasF" @ touchstart = 'touchStart' @ touchmove = 'touchMove' @ touchend = 'touchEnd' @ mousedown = "mouseDown" @ mousemove = "mouseMove" @ mouseup = "mouseUp" ></ canvas > < div class = "btnBox" > < div @ click = "overwrite" >重写</ div > < div @ click = "commit" >提交签名</ div > </ div > </ div > </ div > < img class = "imgCanvas" :src = "imgUrl" > </ section > </ template > |
JS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | <script> export default { data() { return { stageInfo: '' , imgUrl: '' , client: {}, points: [], canvasTxt: null , startX: 0, startY: 0, moveY: 0, moveX: 0, endY: 0, endX: 0, w: null , h: null , isDown: false , isViewAutograph: this .$route.query.isViews > 0, contractSuccess: this .$route.query.contractSuccess } }, mounted() { let canvas = this .$refs.canvasF canvas.height = this .$refs.canvasHW.offsetHeight - 500 canvas.width = this .$refs.canvasHW.offsetWidth - 50 this .canvasTxt = canvas.getContext( '2d' ) this .stageInfo = canvas.getBoundingClientRect() }, methods: { //mobile touchStart(ev) { ev = ev || event ev.preventDefault() if (ev.touches.length == 1) { let obj = { x: ev.targetTouches[0].clienX, y: ev.targetTouches[0].clientY, } this .startX = obj.x this .startY = obj.y this .canvasTxt.beginPath() this .canvasTxt.moveTo( this .startX, this .startY) this .canvasTxt.lineTo(obj.x, obj.y) this .canvasTxt.stroke() this .canvasTxt.closePath() this .points.push(obj) } }, touchMove(ev) { ev = ev || event ev.preventDefault() if (ev.touches.length == 1) { let obj = { x: ev.targetTouches[0].clientX - this .stageInfo.left, y: ev.targetTouches[0].clientY - this .stageInfo.top } this .moveY = obj.y this .moveX = obj.x this .canvasTxt.beginPath() this .canvasTxt.moveTo( this .startX, this .startY) this .canvasTxt.lineTo(obj.x, obj.y) this .canvasTxt.stroke() this .canvasTxt.closePath() this .startY = obj.y this .startX = obj.x this .points.push(obj) } }, touchEnd(ev) { ev = ev || event ev.preventDefault() if (ev.touches.length == 1) { let obj = { x: ev.targetTouches[0].clientX - this .stageInfo.left, y: ev.targetTouches[0].clientY - this .stageInfo.top } this .canvasTxt.beginPath() this .canvasTxt.moveTo( this .startX, this .startY) this .canvasTxt.lineTo(obj.x, obj.y) this .canvasTxt.stroke() this .canvasTxt.closePath() this .points.push(obj) } }, //pc mouseDown(ev) { ev = ev || event ev.preventDefault() if (1) { let obj = { x: ev.offsetX, y: ev.offsetY } this .startX = obj.x this .startY = obj.y this .canvasTxt.beginPath() this .canvasTxt.moveTo( this .startX, this .startY) this .canvasTxt.lineTo(obj.x, obj.y) this .canvasTxt.stroke() this .canvasTxt.closePath() this .points.push(obj) this .isDown = true } }, mouseMove(ev) { ev = ev || event ev.preventDefault() if ( this .isDown) { let obj = { x: ev.offsetX, y: ev.offsetY } this .moveY = obj.y this .moveX = obj.x this .canvasTxt.beginPath() this .canvasTxt.moveTo( this .startX, this .startY) this .canvasTxt.lineTo(obj.x, obj.y) this .canvasTxt.stroke() this .canvasTxt.closePath() this .startY = obj.y this .startX = obj.x this .points.push(obj) } }, mouseUp(ev) { ev = ev || event ev.preventDefault() if (1) { let obj = { x: ev.offsetX, y: ev.offsetY } this .canvasTxt.beginPath() this .canvasTxt.moveTo( this .startX, this .startY) this .canvasTxt.lineTo(obj.x, obj.y) this .canvasTxt.stroke() this .canvasTxt.closePath() this .points.push(obj) this .points.push({x: -1, y: -1}) this .isDown = false } }, //重写 overwrite() { this .canvasTxt.clearRect(0, 0, this .$refs.canvasF.width, this .$refs.canvasF.height) this .points = [] }, //提交签名 commit() { this .imgUrl= this .$refs.canvasF.toDataURL(); console.log( this .$refs.canvasF.toDataURL()) //签名img回传后台 } } } </script> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <style scoped> .signatureBox { width : 100% ; height : calc( 100% - 50px ); box-sizing: border-box; overflow : hidden ; background : #fff ; z-index : 100 ; display : flex; flex- direction : column; } .canvasBox { box-sizing: border-box; flex: 1 ; } canvas { border : 1px solid #7d7d7d ; } .btnBox { padding : 10px ; text-align : center ; } .btnBox button:first-of-type { background : transparent ; border-radius: 4px ; height : 40px ; width : 80px ; font-size : 14px ; } .btnBox button:last-of-type { background : #71b900 ; color : #fff ; border-radius: 4px ; height : 40px ; width : 80px ; font-size : 14px ; } </style> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
vue el-form一行里面放置多个el-form-item的实现
本文主要介绍了vue el-form一行里面放置多个el-form-item的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-08-08Vue 路由间跳转和新开窗口的方式(query、params)
这篇文章主要介绍了Vue 路由间跳转和新开窗口的方式,本文主要通过query方式和params方式介绍,需要的朋友可以参考下2019-12-12vue中@click和@click.native.prevent的区别
这篇文章主要介绍了vue中@click和@click.native.prevent的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-04-04vueJs函数readonly与shallowReadonly使用对比详解
这篇文章主要为大家介绍了vueJs函数readonly与shallowReadonly使用对比详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-03-03vue 2 实现自定义组件一到多个v-model双向数据绑定的方法(最新推荐)
有时候我们需要对一个组件绑定自定义 v-model,以更方便地实现双向数据,例如自定义表单输入控件,这篇文章主要介绍了vue 2 实现自定义组件一到多个v-model双向数据绑定的方法,需要的朋友可以参考下2024-07-07Element ui table表格内容超出隐藏显示省略号问题
这篇文章主要介绍了Element ui table表格内容超出隐藏显示省略号问题,具有很好的参考价值,希望对大家有所帮助,2023-11-11
最新评论