移动端签名

一. HTML

html 复制代码
<el-dialog :visible.sync="dialogVisible" width="100%">
    <div class="inDialog" id="inDialog">
        <canvas id="can"></canvas>
        <div class="btnBox">
            <el-button class="move90" @click="clearDraw" round>清除</el-button>
            <el-button class="move90" round>取消</el-button>
            <el-button class="move90" ref="saveCanvas" id="saveCanvas" type="primary" round>确定</el-button>
        </div>
    </div>
</el-dialog>

二. JS

data里定义变量

javascript 复制代码
canvas: "",
context: "",
javascript 复制代码
watch: {
    'dialogVisible': function () {
        if (this.dialogVisible) {
            this.$nextTick(() => {
                setTimeout(() => {
                    this.canvas = document.getElementById('can')
                    this.context = this.canvas.getContext('2d')
                    this.canvas.width = document.getElementById('inDialog').clientWidth
                    this.canvas.height = document.getElementById('inDialog').clientHeight
                    this.context.fillStyle = "#fff"
                    this.context.fillRect(0, 0, this.canvas.width, this.canvas.height)
                    this.context.strokeStyle = "#000"
                    this.context.lineWidth = 4
                    this.context.lineCap = 'round'

                    // 开始绘制
                    this.canvas.addEventListener('touchstart', (e) => {
                        this.context.beginPath()
                        this.context.moveTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY)
                    })
                    // 绘制中
                    this.canvas.addEventListener('touchmove', (e) => {
                        e.preventDefault()
                        this.context.lineTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY)
                        this.context.stroke()
                    })
                    // 结束绘制
                    this.canvas.addEventListener('touchend', () => {
                        this.context.closePath()
                        const imgBase64 = this.canvas.toDataURL()
                        this.signSrc = imgBase64
                        this.isSign = true
                    })
                }, 300);
            })
        }
    }
},
javascript 复制代码
// 清空画布
clearDraw() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
相关推荐
程序猿John1 小时前
ES6 新增特性 箭头函数
前端·javascript·es6
百锦再2 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
@大迁世界2 小时前
彻底改变我 React 开发方式的组件模式
前端·javascript·react.js·前端框架·ecmascript
William Dawson3 小时前
【Vue 3 + Element Plus 实现产品标签的动态添加、删除与回显】
前端·javascript·vue.js
拉不动的猪3 小时前
项目基础搭建时的一些基本注意点
前端·javascript·面试
蕉君桑3 小时前
vue2使用vue-echarts
前端·vue.js·echarts
runnerdancer4 小时前
React+Vite+Typescript项目脚手架模版
前端
Code额4 小时前
ECMAScript 6 新特性(二)
前端·javascript·ecmascript
群联云防护小杜4 小时前
基于AI的Web应用防火墙(AppWall)实战:漏洞拦截与威胁情报集成
前端·分布式·安全·ddos
_清浅5 小时前
JavaScript(JS进阶)
开发语言·前端·javascript·操作系统·html5