h5实现签名功能

 

<el-dialog :visible.sync="signatureVisible" :close-on-click-modal="false" width="1337px" class="dialogSignature" :before-close="close">
    <div class="tLeft reminderT" slot="title"></div>
    <div style="width:1250px;height:465px;" class="pRelative canvasBox" v-loading="canvasLoading">
        <canvas id="canvas3" width="1245px" height="465px" class="pAbsolute"></canvas>
    </div>
    <div class="buttonBox">
        <!-- w:359 h:89 -->
        <el-button class="dConfirm" @click="commitOk">签好了</el-button>
        <el-button class="dCancel" @click="clearCanvas">清除重签</el-button>
    </div>
</el-dialog>
methods:{
    dataURLtoBlob(dataurl) {
        var arr = dataurl.split(","),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], {
            type: mime,
        });
    },
    blobCl(data){
        return new Blob(data)
    },
    initCanvas(){
        this.$nextTick(()=>{
            var c = document.getElementById('canvas3');
            //获取canvs上下文
            var ctx = c.getContext("2d"); 
            ctx.fillStyle = '#FFFFFF'
            ctx.fillRect(0,0,1250,465)//设置canvas背景色
            ctx.fillStyle='#E3E3E3' //设置字体颜色
            ctx.textAlign='center';//水平居中
            ctx.textBaseline = "middle"; //字体设置垂直居中
            ctx.font = "139px Georgia"; //设置字体size 风格
            ctx.fillText('签字区',622.5,232.5)//填充字体,x,y
        })
    },
    async commitOk(){
        console.log('触发了');
        let canvas=document.getElementById('canvas3') 
        var dataURL = canvas.toDataURL('image/jpeg');   
        //转成的二进制文件流
        console.log(this.dataURLtoBlob(dataURL))
        this.canvasLoading=true;
        let res=await uploadxxx({multipartFile:this.dataURLtoBlob(dataURL) })
        this.canvasLoading=false;
        console.log('upload上传成功');
        console.log(res); 
    },
    //重置canvas画板
    clearCanvas(){
        this.initCanvas();
    },
    commonResize(dBox,yPy){ 
        let canvas=document.getElementById('canvas3')  
        //获取canvas上下文对象
        var ctx=canvas.getContext('2d')//2d:图象 
        ctx.lineWidth=3;//设置线宽
        // ctx.strokeStyle='purple'//设置线的颜色
        var flag=false;
        //监听鼠标按下的时候执行重新绘制
        canvas.onmousedown=function(e){ 
            flag=true;
            ctx.beginPath();
        }
        
        var bodyTag=document.querySelector('body')
        bodyTag.offsetTop-20
        var x,y;
        //鼠标移入绘图
        canvas.onmousemove=function(e){
            if(!flag)return;
            // x=e.clientX-offsetX*3
            console.log(yPy); 
            x=e.offsetX 
            // y=e.pageY-offsetY
            y=e.clientY-yPy
            // console.log('我是y');
            // console.log(y);
            ctx.lineTo(x,y)//画点 clientX,clientY获取鼠标所在位置的x,y轴坐标
            // ctx.lineTo(e.offsetX,e.offsetY)//画点 clientX,clientY获取鼠标所在位置的x,y轴坐标
            ctx.stroke();//绘制线条
        }
        //鼠标松开停止绘图
        canvas.onmouseup=function(e){
            flag=false; 
        }
    },
    beginDialog(){ 
        var dBox=document.querySelector('.dialogSignature .el-dialog')
        var dBody=document.querySelector('body')
        var dialogSignatureTag=document.querySelector('.dialogSignature')  
        this.initCanvas();
        //203是canvas距离顶部的距离
        this.commonResize(dBox,203);
        var _this=this; 
        document.querySelector('.dialogSignature').onscroll=function(e){    
             
            var jqYdY=203;//最终需要减去的移动x轴
                
            if(dialogSignatureTag.scrollTop>0){ 
                //为甚是减去scrollTop由于滚动条向下滚动,canvas顶部距离顶部会缩小
                jqYdY=203-dialogSignatureTag.scrollTop
            }else{

            }
            _this.commonResize(dBox,jqYdY);
        }
    },
    createCanvas(){

    },
    //关闭dialog弹窗,这个是传给父级的可以忽略,正常设置visibleDialog为false即可
    close(){
        this.$emit('update:signatureVisible')
    }   
},

 关键代码:

计算弹窗中的canvas距离顶部的距离,顶部距离又会受到,浏览器滚动的影响需要监听滚动事件

document.querySelector('.dialogSignature').onscroll=function(e){    
             
    var jqYdY=203;//最终需要减去的移动x轴
        
    if(dialogSignatureTag.scrollTop>0){ 
        jqYdY=203-dialogSignatureTag.scrollTop
    }else{

    }
    _this.commonResize(dBox,jqYdY);
}

x轴只需要获取鼠标移入点的位置offsetX的距离即可

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,我作为AI语言模型无法提供完整的代码。但是,我可以给你一些指导,帮助你实现手机H5手写签名采集功能。 首先,你需要使用HTML5的canvas元素来实现手写签名。可以通过以下代码来创建一个canvas元素: ```html <canvas id="canvas" width="500" height="300"></canvas> ``` 接下来,你需要使用JavaScript实现手写签名的采集功能。可以通过以下代码来实现: ```javascript var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var isDrawing = false; var lastX, lastY; canvas.addEventListener('mousedown', function(e) { isDrawing = true; lastX = e.clientX - canvas.offsetLeft; lastY = e.clientY - canvas.offsetTop; }); canvas.addEventListener('mousemove', function(e) { if (isDrawing) { var currentX = e.clientX - canvas.offsetLeft; var currentY = e.clientY - canvas.offsetTop; context.beginPath(); context.moveTo(lastX, lastY); context.lineTo(currentX, currentY); context.stroke(); lastX = currentX; lastY = currentY; } }); canvas.addEventListener('mouseup', function() { isDrawing = false; }); canvas.addEventListener('mouseleave', function() { isDrawing = false; }); ``` 代码中,我们使用了mousedown、mousemove、mouseup和mouseleave事件来实现手写签名的采集。当用户按下鼠标左键时,我们将isDrawing变量设为true,并记录下当前鼠标的位置。当用户移动鼠标时,我们将当前鼠标的位置与上一个鼠标位置连接起来,形成一条线段。当用户松开鼠标左键时,我们将isDrawing变量设为false。当用户鼠标移出canvas元素时,我们也将isDrawing变量设为false,以确保用户不会意外地继续绘制线段。 最后,你需要将手写签名保存为图片。可以通过以下代码来实现: ```javascript var dataURL = canvas.toDataURL(); ``` 代码中,我们使用toDataURL()方法将canvas元素转换为一个Base64编码的URL,这样就可以将手写签名保存为图片了。 希望这些指导能够帮助你实现手机H5手写签名采集功能

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路光.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值