h5canvas实现签名(vue)

实现个人签名技术上没那么复杂,只是平时大家用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>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近公司要求用h5实现电子签名,在网上搜索后,发现一章文章很实用,用vue,canvas实现。 在移动端上需要在获...
    子语喵阅读 6,319评论 1 3
  • 标题很难引人入胜,先放个效果图好了 如果图片吸引不了你,那我觉得也就没啥看的了。 demo链接:https://w...
    kingZXY2009阅读 2,187评论 0 1
  • Mobile Web Favorites 参与贡献 移动前端开发收藏夹,欢迎使用Issues以及 Pull Req...
    柴东啊阅读 957评论 0 2
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,696评论 1 11
  • 今天,爸爸带着我们全家一起去泡温泉,经过漫长的两个小时坐车时间,我们到了。迫不及待的换好衣服后,我们来到了其中的一...
    7f1fbc8bc5f3阅读 184评论 0 0

友情链接更多精彩内容