移动端签名

一. 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);
},
相关推荐
摘星编程18 分钟前
React Native for OpenHarmony 实战:VirtualizedList 虚拟化列表
javascript·react native·react.js
谢尔登36 分钟前
Vue3 响应式系统——computed 和 watch
前端·架构
愚公移码38 分钟前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌
摘星编程1 小时前
React Native for OpenHarmony 实战:RefreshControl 下拉刷新组件
javascript·react native·react.js
欣然~2 小时前
法律案例 PDF 批量转 TXT 工具代码
linux·前端·python
一个小废渣2 小时前
Flutter Web端网络请求跨域错误解决方法
前端·flutter
鸣弦artha3 小时前
Flutter框架跨平台鸿蒙开发——Extension扩展方法
android·javascript·flutter
符文师3 小时前
css3 新特性
前端·css3
ct9783 小时前
WebGL开发
前端·gis·webgl
C_心欲无痕4 小时前
前端页面渲染方式:CSR、SSR、SSG
前端