用Canvas自己写个小画板

2021-12-22  本文已影响0人  又菜又爱分享的小肖
        <canvas disable-scroll="true" canvas-id="drawline" class="board" @touchstart="handtouchstart" @touchmove="handtouchmove"
         @touchend="handtouchend">
        </canvas>
       data() {
            return {
                x: 0, // 渠道开始和移动位置
                y: 0,
                newx: 0,
                newy: 0
            }
        },
var ctx = uni.createCanvasContext('drawline')
            // 触摸开始
            handtouchstart(e) {
                let startX = e.changedTouches[0].x;
                let startY = e.changedTouches[0].y;
                this.x = startX;
                this.y = startY;
            },
            // 触摸移动
            handtouchmove(e) {
                let moveX = e.changedTouches[0].x;
                let moveY = e.changedTouches[0].y;
                this.newx = moveX;
                this.newy = moveY;
                ctx.setLineWidth(10); // 划线多粗
                ctx.setLineCap('round'); // 不中断
                ctx.setStrokeStyle('red')
                ctx.moveTo(this.x, this.y)
                ctx.lineTo(this.newx, this.newy)
                ctx.stroke()
                ctx.draw(true) // 保存绘画内容
                this.x = moveX
                this.y = moveY
                this.main(this.x, this.y);
            },
            // 结束触摸
            handtouchend(e) {
                ctx.draw() // 清空
            },
上一篇 下一篇

猜你喜欢

热点阅读