久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

基于canvas實現手寫簽名(vue)

瀏覽:63日期:2023-01-19 13:12:08

最近一直在研究canvas的東西,正好之前對手寫簽名這塊有點興趣。就自己基于vue寫了一個簡易的手寫簽名demo。

其中原理比較簡單,先生成一個canvas畫布,并對canvas進行touchstart和touchmove事件進行監聽。當監聽touchstart事件被觸發時,我們開始觸發canvas里的beginPath事件并且設置moveTo原始點。當監聽touchmove事件則去不斷去觸發lineTo事件,最后stroke()。

demo里還有清除簽名和保存簽名的功能,分別對應了clearRect()和toDataURL()方法。

具體的demo代碼如下:

<template> <div> <canvas height='150'> </canvas> <div class='btn'> <span @click='toClear()'>清除</span> <span @click='toSave()'>保存</span> </div> </div></template><script> export default { name: 'sign-name', data(){ return { ctx:null, canvas:null } }, mounted() { this.initPage() }, methods:{ initPage() { this.canvas = document.getElementById(’canvas’) if(this.canvas.getContext){ this.ctx = this.canvas.getContext(’2d’) let background = '#ffffff' this.ctx.lineCap = ’round’ this.ctx.fillStyle = background this.ctx.lineWidth = 2 this.ctx.fillRect(0,0,350,150) this.canvas.addEventListener('touchstart',(e)=>{console.log(123,e)this.ctx.beginPath()this.ctx.moveTo(e.changedTouches[0].pageX,e.changedTouches[0].pageY) }) this.canvas.addEventListener('touchmove',(e)=>{this.ctx.lineTo(e.changedTouches[0].pageX,e.changedTouches[0].pageY)this.ctx.stroke() }) } }, toClear() { this.ctx.clearRect(0,0,300,150) }, toSave() { let base64Img = this.canvas.toDataURL() console.log(123,base64Img) } } }</script><style lang='scss' scoped> .btn { height: px2Vw(55); position: fixed; bottom: 0; line-height: px2Vw(55); border-top: px2Vw(1) solid #f7f8f9; span { display: inline-block; width: px2Vw(185); text-align: center; } } canvas { position: fixed; border: 2px dashed #cccccc; float: right; }</style>

代碼運行后的效果圖如下:

基于canvas實現手寫簽名(vue)

基于canvas實現手寫簽名(vue)

這只是個簡易的demo,肯定會有很多未考慮到的地方。demo的下載地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 朝阳县| 崇信县| 万安县| 安多县| 嘉善县| 日土县| 肇州县| 额尔古纳市| 务川| 巍山| 萨迦县| 嘉善县| 庆阳市| 绥化市| 西青区| 崇礼县| 中方县| 新蔡县| 丽江市| 博客| 南皮县| 大渡口区| 阳泉市| 梅州市| 天长市| 温宿县| 铜川市| 平潭县| 云霄县| 萨嘎县| 松潘县| 杂多县| 东阳市| 闸北区| 察哈| 遂昌县| 普兰店市| 黎城县| 合作市| 康保县| 尼玛县|