移动端签名

一. 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);
},
相关推荐
fmc12110416 分钟前
Excel文档的读取(3)
java·前端·python
生椰拿铁You25 分钟前
CSS——盒子模型
前端·css
不知名的小Q38 分钟前
如何动态获取路由上的参数
前端·javascript·vue.js
大鸡腿最好吃1 小时前
react&webpack老项目开发环境增加vite
前端·react.js·webpack
四季予你661 小时前
手写call、apply、bind
前端·javascript·vue.js
椰椰椰耶1 小时前
【HTML】HTML页面和常见标签
前端·html
试着奔跑的菜鸟2 小时前
Vue:通过js控制css变量 - 一键修改全局样式
前端·javascript·css·vue.js
淡忘_cx2 小时前
vue本地打包并将构建文件推送到服务器对应目录下
服务器·前端·vue.js
清灵xmf2 小时前
CSS 布局技巧实现元素左右排列
前端·css
一条晒干的咸魚2 小时前
CSS传统布局方法(补充)——WEB开发系列37
前端·css·html·css3·网格布局·传统布局