美文网首页
h5canvas实现签名(vue)

h5canvas实现签名(vue)

作者: O蚂蚁O | 来源:发表于2019-03-22 18:25 被阅读0次

实现个人签名技术上没那么复杂,只是平时大家用canvas少,实现该需求主要用到的技术点:

1、h5新标签canvas

2、touchstart事件,touchmove事件,touchend事件,canvas转base64图片的方法toDataURL

废话不多说直接上代码

<template>
    <div>
        <div id="canvas" ref="canvas">
            <p id="clearCanvas" ref="clearCanvas">清除</p>
            <p id="saveCanvas" ref="saveCanvas">保存</p>
        </div>
        <div class="mySign" v-show="isSign">
            <img :src="signSrc" alt="">
        </div>
    </div>
</template>

<script>
export default {

    data() {
        return {
            isSign: false,
            signSrc:''
        }
    },
    created() {

    },
    mounted() {
       this.lineCanvas({
            el: this.$refs.canvas,//绘制canvas的父级div
            clearEl:  this.$refs.clearCanvas,//清除按钮
            saveEl:  this.$refs.saveCanvas,//保存按钮
        });
    },
    methods: {
        lineCanvas(obj) {
            this.linewidth = 2;
            this.color = "#000000";
            this.background = "#ffffff";
            for (var i in obj) {
                this[i] = obj[i];
            };
            this.canvas = document.createElement("canvas");
            this.el.appendChild(this.canvas);
            this.cxt = this.canvas.getContext("2d");
            this.canvas.width = this.el.clientWidth;
            this.canvas.height = this.el.clientHeight;
            this.cxt.fillStyle = this.background;
            this.cxt.fillRect(0, 0, this.canvas.width, this.canvas.width);
            this.cxt.strokeStyle = this.color;
            this.cxt.lineWidth = this.linewidth;
            this.cxt.lineCap = "round";
            //开始绘制
            this.canvas.addEventListener("touchstart", function(e) {
                this.cxt.beginPath();
                this.cxt.moveTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
            }.bind(this), false);
            //绘制中
            this.canvas.addEventListener("touchmove", function(e) {
                this.cxt.lineTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
                this.cxt.stroke();
            }.bind(this), false);
            //结束绘制
            this.canvas.addEventListener("touchend", function() {
                this.cxt.closePath();
                 let imgBase64 = this.canvas.toDataURL();
                //console.log(imgBase64);
                this.signSrc= imgBase64;
                this.isSign= true;
            }.bind(this), false);
            //清除画布
            this.clearEl.addEventListener("click", function() {
                this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
            }.bind(this), false);
            //保存图片,直接转base64
            this.saveEl.addEventListener("click", function() {
                let imgBase64 = this.canvas.toDataURL();
                //console.log(imgBase64);
                this.signSrc= imgBase64;
                this.isSign= true;
            }.bind(this), false);
        }
    }
}
</script>

<style scoped lang="less">
    #canvas{
    width: 100%;
    height: 300px;
    position: relative;
        canvas{
            display: block;
        }
        #clearCanvas{
            width: 50%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            position: absolute;
            bottom: 0;
            left: 0;
            border: 1px solid #DEDEDE;
            z-index: 1;
        }
        #saveCanvas{
            width: 50%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            position: absolute;
            bottom: 0;
            right: 0;
            border: 1px solid #DEDEDE;
            z-index: 1;
        }
    }
    .mySign{
        width: 100%;
        height: 300px;
        img{
            width: 100%;
            height: 300px;
        }
    }
    .test{
        width: 100%;
        height:200px;
        font-size:14px;
        font-weight:600;
        text-align:center;
        
    }
    
</style>

相关文章

  • h5canvas实现签名(vue)

    实现个人签名技术上没那么复杂,只是平时大家用canvas少,实现该需求主要用到的技术点: 1、h5新标签canva...

  • vue 实现手写电子签名

    1.兼容 PC 和 Mobile; 2.画布自适应屏幕大小变化(窗口缩放、屏幕旋转时画布无需重置,自动校正坐标偏移...

  • Vue实现在线签名

    1.首先,安装 vue-esign 2.首先,在main.js中引入 3.在 组件 中写入 4.在父组件中使用即可...

  • Vue、Springboot实现接口签名

    1、实现思路   接口签名目的是为了,确保请求参数不会被篡改,请求的数据是否已超时,数据是否重复提交等。 客户端提...

  • vue 电子签名

    最近公司要求用h5实现电子签名,在网上搜索后,发现一章文章很实用,用vue,canvas实现。 在移动端上需要在获...

  • vue电子签名

    最近公司要求用h5实现电子签名,在网上搜索后,发现一章文章很实用,用vue,canvas实现。在移动端上需要在获取...

  • 仿vue实现邮件在线签名生成器

    基于vue双向数据绑定的思想,实现一个在线邮件签名生成器。 直接上演示代码: 邮件...

  • Vue Canvas 实现电子签名 手写板

    最近再做移动端电子签名,Vue+Canvas实现,移动端、PC端均可,也可以从github下载 。我在做这个功能的...

  • vue 实现电子版签名

    https://github.com/JaimeCheng/vue-esign[https://github.co...

  • vue 签名

网友评论

      本文标题:h5canvas实现签名(vue)

      本文链接:https://www.haomeiwen.com/subject/rsnrvqtx.html