• vue中使用canvas绘制签名


    不多说,上代码:

    <template>
            <div class="sign-canvas">
                <canvas
                        id="canvas"
                        width="400"
                        height="600"
                        @mousedown="canvasDown($event)"
                        @mousemove="canvasMove($event)"
                        @mouseup="canvasUp()"
                        @mouseleave="canvasLeave()"
                        ref="canvas"
                >
                    抱歉,您的浏览器暂不支持canvas元素
                </canvas>
                <div class="sign-btn">
                    <div class="clear" @click="clear">
                        清空
                    </div>
                    <div class="save" @click="save">
                        保存
                    </div>
                </div>
            </div>
        </template>
    <script>
            export default {
                mounted() {
                    this.show();
                },
                methods:{
                    show(){
                        this.canvas = this.$refs.canvas;// 指定canvas
                        this.ctx = this.canvas.getContext("2d") // 设置2D渲染区域
                        this.ctx.lineWidth = 5; // 设置线的宽度
                    },
                    canvasDown(e) {
                        this.canvasMoveUse = true;
                        const canvasX = e.clientX - e.target.offsetLeft + document.documentElement.scrollLeft
                        const canvasY = e.clientY - e.target.offsetTop + document.documentElement.scrollTop
                        this.ctx.beginPath()  // 移动的起点
                        this.ctx.moveTo(canvasX, canvasY)
                    },
                    canvasMove(e) {
                        // 只在移动是进行绘制图线
                        if (this.canvasMoveUse) {
                            const t = e.target;
                            let canvasX;
                            let canvasY;
                            canvasX = e.clientX - t.offsetLeft + document.documentElement.scrollLeft
                            canvasY = e.clientY - t.offsetTop + document.documentElement.scrollTop
                            this.ctx.lineTo(canvasX, canvasY)
                            this.ctx.stroke()
                        }
                    },
                    canvasUp() {
                        this.canvasMoveUse = false;
                    },
                    canvasLeave() {
                        this.canvasMoveUse = false;
                    },
                    clear(){
                        this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height)
                    },
                    save() {
                        const imgBase64 = this.$refs.canvas.toDataURL();
                        console.log(imgBase64);
                    }
                }
            }
        </script>
        
        <style scoped>
            .sign-canvas{
                display: flex;
                flex-direction: column;
                align-items: center;
                 100%;
                height: 100%;
                padding: 20px 30px;
            }
            .sign-canvas canvas{
                background-color: #e0e3e5;
            }
            .sign-btn {
                display: flex;
                margin:20px 0;
            }
            .sign-btn div {
                 175px;
                text-align: center;
                height: 70px;
                line-height: 70px;
                color: #FFFFFF;
            }
            .sign-btn div:active {
                background-color: #CCCCCC;
                color: #333333;
            }
            .sign-btn .clear {
                background-color: #FF8F58;
            }
            .sign-btn .save {
                background-color: #0599D7;
            }
        </style>
        样式图:

     效果图:

  • 相关阅读:
    Hibernate Tomcat JNDI数据源配置(转)
    使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)
    丢弃重口味的xml配置--spring4用groovy配置bean(转)
    Java 对象的生命周期
    设计模式学习总结(23) 中介者模式
    WebSocket初探
    设计模式 之 建造者
    谈谈CListCtrl 扩展风格设置方法-SetExtendedStyle和ModifyStyleEx 比較
    linux signal 处理
    UVA 1546
  • 原文地址:https://www.cnblogs.com/szqtiger/p/11883825.html
Copyright © 2020-2023  润新知