1. 程式人生 > 其它 >vue+canvas實現簽名+橫屏簽名旋轉

vue+canvas實現簽名+橫屏簽名旋轉

1、HTML

<template>
    <div class="body" :style="{height:height + 'px'}" @touchmove.prevent>
        <div class="canvas" ref="canvas">

        </div>
        <div class="bottom">
            <div>
                <span class="saveCanvas" @click="saveCanvas">提交簽名</span>
                <span class="clearCanvas" @click="clearCanvas">重新簽名</span>
            </div>
            <p>請在區域內進行簽名,保證簽名清晰完整</p>
            <!-- <img :src="srcurl" /> -->
        </div>
    </div>
</template>

 2、JS

export default {

    data () {
        return {
            signSrc: '',//生成的圖片地址
            cxt: null,
            canvas: null,
            keyFrom: '',
            height: 600,
            srcurl: '',
        }
    },

    mounted () {
          this.createCanvas();
    },

    methods: {

        //點選儲存圖片
        saveCanvas () {
            this.srcurl = this.canvas.toDataURL('image/png');
            const loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            this.rotateBase64(this.canvas.toDataURL('image/png')).then((img) => {
                this.signSrc = img;
                this.uploadData(res => {
                    let data = {
                        FILE_CODE_IMAGE: res
                    };
                    api.uploadPutqrcode(data).then(re => {
                        loading.close();
                    })
                })
            });
        },

        //將base64圖片旋轉90度以後上傳
        rotateBase64 (data) {
            return new Promise((resolve) => {
                const imgView = new Image();
                imgView.src = data;
                const canvas = document.createElement('canvas');
                const context = canvas.getContext('2d');
                const cutCoor = { sx: 0, sy: 0, ex: 0, ey: 0 }; // 裁剪座標
                imgView.onload = () => {
                    const imgW = imgView.width;
                    const imgH = imgView.height;
                    const size = imgH;
                    canvas.width = size * 2;
                    canvas.height = size * 2;
                    cutCoor.sx = size;
                    cutCoor.sy = size - imgW;
                    cutCoor.ex = size + imgH;
                    cutCoor.ey = size + imgW;
                    context.translate(size, size);
                    context.rotate(Math.PI / 2 * 3);
                    context.drawImage(imgView, 0, 0);
                    const imgData = context.getImageData(cutCoor.sx, cutCoor.sy, cutCoor.ex, cutCoor.ey);
                    canvas.width = imgH;
                    canvas.height = imgW;
                    context.putImageData(imgData, 0, 0);
                    resolve(canvas.toDataURL('image/png'));
                };
            });
        },



        //圖片上傳
        uploadData (callback) {
            let form = new FormData();
            form.append('file_name', '簽名.png');
            form.append('file_size', 5000);
            form.append('file_ext', '.png');
            form.append('url', this.signSrc);
            let config = {
                headers: {
                    "Content-Type": "multipart/form-data",
                },
            };
            this.$axios.post(url, form, config).then((res) => {
                callback && callback(res.data.data.data)
            })
        },

        //清空畫板
        clearCanvas () {
            this.cxt.clearRect(0, 0, this.canvas.width, 600);
            this.cxt.fillRect(0, 0, this.canvas.width, 600);
        },

        //初始化canvas
        createCanvas () {
            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.height - 200;
            // this.cxt.fillStyle = "#ECF2FE";
            this.cxt.fillStyle = 'rgba(255, 255, 255, 0)'
            this.cxt.fillRect(0, 0, this.canvas.width, 600);
            this.cxt.strokeStyle = "#000000";
            this.cxt.lineWidth = 2;
            this.cxt.lineCap = "round";
            this.canvas.addEventListener("touchstart", function (e) {
                this.cxt.beginPath();
                this.cxt.moveTo(e.changedTouches[0].pageX + 2, e.changedTouches[0].pageY);
            }.bind(this), false);

            this.canvas.addEventListener("touchmove", function (e) {
                this.cxt.lineTo(e.changedTouches[0].pageX + 2, e.changedTouches[0].pageY);
                this.cxt.stroke();
            }.bind(this), false);

        },

    }
}