vue实现用canvas写的手写签名组件

欢迎来到QQ技术交流群:126095418

signature.vue组件的内容

<template>
    <div class="signature">
        <div class="title">请在虚线方框内签名</div>
        <canvas
                ref="canvas"
                id="canvas">签名区域</canvas>
        <div class="canvas-btn">
            <button class="primary" @click="save('signature')">保存</button>
            <button class="danger" @click="clear">清除</button>
        </div>
    </div>
</template>

<script>
    let draw;
    let canvasWidth;
    let canvasHeight;
    let preHandler = function(e) {
        if(e.preventDefault){
            e.preventDefault();
        }else{
            e.returnValue = false;
        }
    };

    var meta = document.createElement('meta');
    meta.content = 'portrait';
    document.getElementsByTagName('head')[0].appendChild(meta);

    //获取当前屏幕的宽高
    function getWindowSize(){
        canvasWidth = document.documentElement.clientWidth - 40;
        if(!canvasWidth){
            return;
        }
        hengshuping();
    }

    //判断手机横竖屏状态:
    function hengshuping(){
        if(window.orientation==180||window.orientation==0){
            // alert("竖屏状态!")
            canvasHeight = document.documentElement.clientHeight - document.documentElement.clientHeight / 1.4;
        }
        if(window.orientation==90||window.orientation==-90){
            // alert("横屏状态!")
            canvasHeight = document.documentElement.clientHeight - 63;
        }
    }

    getWindowSize();

    window.onresize = function(){
        getWindowSize();
        draw = {};
        draw = new Draw("canvas");
        draw.init();
    };

    class Draw {
        constructor(el){
            if(!canvasWidth){return;}
            console.log(canvasWidth);
            this.el = el;
            this.canvas = document.getElementById(this.el);
            if(!this.canvas){return;}
            this.canvas.width = canvasWidth;
            this.canvas.height = canvasHeight;
            this.cxt = this.canvas.getContext("2d");
            this.stageInfo = canvas.getBoundingClientRect();
            this.path = {
                beginX: 0,
                beginY: 0,
                endX: 0,
                endY: 0
            };
        }
        init(btn){
            if(!this.canvas){
                return;
            }
            this.canvas.addEventListener("touchstart", event => {
                document.addEventListener("touchstart", preHandler, {passive: false});
                this.drawBegin(event);
            }, {passive: false});
            this.canvas.addEventListener("touchend", event => {
                document.addEventListener("touchend", preHandler, {passive: false});
                this.drawEnd();
            }, {passive: false});
            this.clear(btn);
        }
        drawBegin(e){
            window.getSelection()
                ? window.getSelection().removeAllRanges()
                : document.selection.empty();
            this.cxt.strokeStyle = "#000";
            this.cxt.beginPath();
            this.cxt.moveTo(
                e.changedTouches[0].clientX - this.stageInfo.left,
                e.changedTouches[0].clientY - this.stageInfo.top
            );
            this.path.beginX = e.changedTouches[0].clientX - this.stageInfo.left;
            this.path.beginY = e.changedTouches[0].clientY - this.stageInfo.top;
            canvas.addEventListener("touchmove", ()=>{
                this.drawing(event);
            }, {passive: false});
        }
        drawing(e){
            this.cxt.lineTo(
                e.changedTouches[0].clientX - this.stageInfo.left,
                e.changedTouches[0].clientY - this.stageInfo.top
            );
            this.path.endX = e.changedTouches[0].clientX - this.stageInfo.left;
            this.path.endY = e.changedTouches[0].clientY - this.stageInfo.top;
            this.cxt.stroke();
        }
        checkCanvas(){
            var blank = document.createElement('canvas');
            blank.width = canvas.width;
            blank.height = canvas.height;
            return canvas.toDataURL() == blank.toDataURL();
        }
        drawEnd(){
            document.removeEventListener("touchstart", preHandler, false);
            document.removeEventListener("touchend", preHandler, false);
            document.removeEventListener("touchmove", preHandler, false);
        }
        clear(){
            this.cxt.clearRect(0, 0, canvasWidth, canvasHeight);
        }
        save(fileName){
            let imgURL = canvas.toDataURL('image/png');
            return imgURL;
        }
    }
    export default {
        props: {
            show: {default:false},
        },

        mounted(){
            draw = new Draw("canvas");
            draw.init();
        },

        methods: {
            clear: function(){
                draw.clear();
            },
            save: function(fileName){
                let data = draw.save(fileName);
                if(draw.checkCanvas()){
                    alert('请签名');
                    return;
                }

                this.$emit('signature', data);
                this.clear();
            },
        },
        watch: {
            show(){
                if(this.show){
                    document.getElementsByClassName('signaturePanel')[0].style = 'z-index:1000';
                }else{
                    document.getElementsByClassName('signaturePanel')[0].style = 'z-index:-100';
                }
            }
        }
    }
</script>

<style lang="scss">
    .signature {
        position: relative;
        text-align: right;
        top: 10px;
        padding-right: 10px;
        .title {
            display: inline-block;
            width: 20px;
            vertical-align: top;
            text-align: center;
        }
    }
    #canvas {
        border: 1px dashed #ccc;
        background: #fff;
        display: inline-block;
    }
    .canvas-btn {
        margin-top: 10px;
        font-size: 0;
        position: fixed;
        width: 100%;
        button {
            padding: 10px;
            width: 50%;
        }
    }

    * {
        touch-action: pan-y;
    }
</style>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,关于vue实现签名手写签名的功能,可以使用HTML5的canvas标签实现。具体可以参考以下步骤: 1. 在vue组件中引入canvas标签,并设置该标签的宽高和边框等样式。如下: ``` <canvas id="canvas" width="400" height="200" style="border:1px solid #000000;"></canvas> ``` 2. 在vue组件的mounted生命周期中获取canvas对象,并设置canvas的绘制样式。如下: ``` mounted() { this.canvas = document.getElementById('canvas'); this.ctx = this.canvas.getContext('2d'); this.ctx.lineWidth = 2; // 设置笔画宽度 this.ctx.strokeStyle = '#000000'; // 设置笔画颜色 } ``` 3. 在提供签名功能的按钮事件中,绑定canvas的鼠标事件。如下: ``` <button @click="startDrawing">签名</button> ``` startDrawing方法中绑定事件: ``` startDrawing() { this.drawing = true; this.canvas.addEventListener('mousedown', this.onMouseDown); this.canvas.addEventListener('mouseup', this.onMouseUp); this.canvas.addEventListener('mousemove', this.onMouseMove); } ``` 4. 监听canvas的鼠标事件,实现手写签名。如下: ``` onMouseDown(e) { this.ctx.beginPath(); this.ctx.moveTo(e.offsetX, e.offsetY); this.mouseMoved = false; } onMouseMove(e) { if (this.drawing) { this.mouseMoved = true; this.ctx.lineTo(e.offsetX, e.offsetY); this.ctx.stroke(); } } onMouseUp(e) { if (!this.mouseMoved) { this.ctx.fillRect(e.offsetX - 1, e.offsetY - 1, 2, 2); } this.drawing = false; this.canvas.removeEventListener('mousedown', this.onMouseDown); this.canvas.removeEventListener('mouseup', this.onMouseUp); this.canvas.removeEventListener('mousemove', this.onMouseMove); } ``` 以上就是通过canvas实现vue签名手写签名的功能的具体步骤。希望能对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

快乐的逗号

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

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

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

打赏作者

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

抵扣说明:

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

余额充值