vue used canvas to draw a signature

Not much to say, on the code:

<template>
        <div class="sign-canvas">
            <canvas
                    id="canvas"
                    width="400"
                    height="600"
                    @mousedown="canvasDown($event)"
                    @mousemove="canvasMove($event)"
                    @mouseup="canvasUp()"
                    @mouseleave="canvasLeave()"
                    ref="canvas"
            >
                Sorry, your browser does not support the canvas element
            </canvas>
            <div class="sign-btn">
                <div class="clear" @click="clear">
                    Clear
                </div>
                <div class="save" @click="save">
                    Storage
                </div>
            </div>
        </div>
    </template>
<script>
        export default {
            mounted() {
                this.show();
            },
            methods:{
                show(){
                    this.canvas = this.$refs.canvas;// 指定canvas
                    this.ctx = this.canvas.getContext ( "2d") // set the 2D rendering region
                    this.ctx.lineWidth = 5; // Sets the line width
                },
                canvasDown(e) {
                    this.canvasMoveUse = true;
                    const canvasX = e.clientX - e.target.offsetLeft + document.documentElement.scrollLeft
                    const canvasY = e.clientY - e.target.offsetTop + document.documentElement.scrollTop
                    this.ctx.beginPath () // move the starting point
                    this.ctx.moveTo(canvasX, canvasY)
                },
                canvasMove(e) {
                    // only movement is to draw a line
                    if (this.canvasMoveUse) {
                        const t = e.target;
                        let canvasX;
                        let canvasY;
                        canvasX = e.clientX - t.offsetLeft + document.documentElement.scrollLeft
                        canvasY = e.clientY - t.offsetTop + document.documentElement.scrollTop
                        this.ctx.lineTo(canvasX, canvasY)
                        this.ctx.stroke()
                    }
                },
                canvasUp() {
                    this.canvasMoveUse = false;
                },
                canvasLeave() {
                    this.canvasMoveUse = false;
                },
                clear(){
                    this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height)
                },
                save() {
                    const imgBase64 = this.$refs.canvas.toDataURL();
                    console.log(imgBase64);
                }
            }
        }
    </script>
    
    <style scoped>
        .sign-canvas{
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
            height: 100%;
            padding: 20px 30px;
        }
        .sign-canvas canvas{
            background-color: #e0e3e5;
        }
        .sign-btn {
            display: flex;
            margin:20px 0;
        }
        .sign-btn div {
            width: 175px;
            text-align: center;
            height: 70px;
            line-height: 70px;
            color: #FFFFFF;
        }
        .sign-btn div:active {
            background-color: #CCCCCC;
            color: #333333;
        }
        .sign-btn .clear {
            background-color: #FF8F58;
        }
        .sign-btn .save {
            background-color: #0599D7;
        }
    </style>
    Figure style:

 

 Renderings:

 

Guess you like

Origin www.cnblogs.com/szqtiger/p/11883825.html