移动端签名

一. 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);
},
相关推荐
偷光1 小时前
浏览器中的隐藏IDE: Elements (元素) 面板
开发语言·前端·ide·php
江拥羡橙6 小时前
Vue和React怎么选?全面比对
前端·vue.js·react.js
千码君20166 小时前
React Native:快速熟悉react 语法和企业级开发
javascript·react native·react.js·vite·hook
楼田莉子7 小时前
Qt开发学习——QtCreator深度介绍/程序运行/开发规范/对象树
开发语言·前端·c++·qt·学习
暮之沧蓝8 小时前
Vue总结
前端·javascript·vue.js
木易 士心8 小时前
Promise深度解析:前端异步编程的核心
前端·javascript
im_AMBER8 小时前
Web 开发 21
前端·学习
又是忙碌的一天8 小时前
前端学习day01
前端·学习·html
Joker Zxc8 小时前
【前端基础】20、CSS属性——transform、translate、transition
前端·css
excel8 小时前
深入解析 Vue 3 源码:computed 的底层实现原理
前端·javascript·vue.js