美文网首页
小程序canvas组件实现涂鸦板功能升级版带橡皮擦功能

小程序canvas组件实现涂鸦板功能升级版带橡皮擦功能

作者: 5cc9c8608284 | 来源:发表于2022-05-04 16:37 被阅读0次

canvasDemo.wxml

<view class="container">
<view class="canvas-area">
<canvas canvas-id="my-chavas" class="chavas"  disable-scroll bindtouchstart="handlestart" bindtouchmove="handlemove"></canvas>
</view>
<view class="canvas-tools">
<!-- 细笔 -->
    <view class="box box1" data-param="5" bindtap="bindpen"></view>
    <!-- 粗笔 -->
    <view  data-param="15"  class="box box2" bindtap="bindpen"></view>
    <!-- 红色笔 -->
    <view data-param="red"  class="box box3" bindtap="bindcolor"></view>
    <!-- 黄色笔 -->
    <view  data-param="orange" class="box box4" bindtap="bindcolor"></view>
    <!-- 橡皮擦 -->
    <view  class="box box5" data-param="#fed" bindtap="binderaser"></view>
</view>
</view>

canvasDemo.wxss

/* pages/canvasDemo/canvasDemo.wxss */
.container{
    width: 100%;
    height: 100%;
    position: relative;
    padding:0 10rpx ;
}
.canvas-area{
    width: 100%;
    height: 100%;
    background-color: #fed;
}
.chavas{
    width: 100%;
    height: 100%;
}
.canvas-tools{
    position: fixed;
    width: 100%;
    left: 0;
    bottom: 20rpx;
    display: flex;
    justify-content: space-around;
}
.box{
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
    background-color: purple;
}
.box1{
    background-color: #9cc;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAgCAYAAAAbifjMAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH3gUEFTU3owPJ+gAAArpJREFUSMeVlU9IVFEUxn/nvjdTaTQZYyaaZlEpIfhvkVSMSAmBFRZpYYugbcs2QbWpXUFKC3chLdpFUFHQpqxE2rQ0ahEp5BRm6TgqODP3tpg78nzznukHF+7lnfOd73zn8q7gw8jISG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bWI6OkRvY3VtZW50OjpQYWdlcwAxp/+7LwAAABl0RVh0VGh1bWI6OkltYWdlOjpIZWlnaHQAMTA2Mx0uiBoAAAAXdEVYdFRodW1iOjpJbWFnZTo6V2lkdGgANTM3XiCV0QAAABl0RVh0VGh1bWI6Ok1pbWV0eXBlAGltYWdlL3BuZz+yVk4AAAAXdEVYdFRodW1iOjpNVGltZQAxMzk5MjExNjM1VZFQ9AAAABJ0RVh0VGh1bWI6OlNpemUAMTguNEtCh1extQAAAF90RVh0VGh1bWI6OlVSSQBmaWxlOi8vL2hvbWUvd3d3cm9vdC9zaXRlL3d3dy5lYXN5aWNvbi5uZXQvY2RuLWltZy5lYXN5aWNvbi5jbi9zcmMvMTE2MDIvMTE2MDI0NC5wbmcC2t7tAAAAAElFTkSuQmCC);
    background-position:  center;
    background-repeat: no-repeat;
}
.box2{
    background-color: #09c;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAeCAMAAAAvtQ9FAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAACf1BMVEUAAAB8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiN8TiNINiWHUyJ+TyN9TyN9TyN9TyN9TyN+TyOPVyJBMyYoKCgpKChaPyVkQyVjQyVjQyVjQyVjQyVTPCYmJygoKCgfHx8eHh4fHx4gHx4gHx4gHx4gHx4gHx4eHh4eHh4fHx8TExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExITExIHBwcHBwcHBwcHBwd8TiMTExITExITExITExIHBwf////3ECwAAAAAznRSTlMAAAAARX4EAAAAAFLkqwIAAAAAVOv/vQkAAAA54v//4CEAAAARvP////1qAAAAAGj9/////9o1AAAAFMb/////2WgXAE70//////7BLI///////6O+/////+fY///84f///dz///HI/////9Wl/////6hy/f///G056////+YxDr7/////sggAcv////1hAAAg3P/////JEwBMTLj//v7//pxKS+j7+//////++vvi1f///////////83V///O1f//ztX//87V///O1f//zSidjOsAAAABYktHRNQJuwuFAAAAB3RJTUUH3gUEFTU21AT5bAAAAS9JREFUGNNjYGBgZGJmYWVjZwABDk4ubh5ePn4QW0BQSFhEVExcAsSRlJKWkZWTV1BUAnKUVVTV1DU0tbR1dBkY9PQNDI2MTUzNzC0sGaysbWzPnbOzd3B0cmZwcXU7d+6cu4enl7cPg6+f/zkQCAgMCmYICQ0Dc86FR0QyREWfg4KYWIa4eBgnIZEhKRnGSUllSEvPgLAzs7IZcnLzIJz8gkKGouISCKe0rJyhorIKzK6uqa1jqG9obAJxmlta2xjaOzq7QBLdPb19DP0TJk6aPHnylKnTps9gmDlr9py58+bNX7Bw0WKGJUuXLV+xcuWq1WvWrmNYv2Hjps1btmzdtn3HToZdu89DwZ69DPv2X4CCAwfxcA4dvggFR44yHDt+CQpOnGQ4dfoyFJw5CwBZdsIQZAP16AAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxNi0wOS0xN1QxNToyMDozOCswODowMAwMIWsAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTQtMDUtMDRUMjE6NTM6NTQrMDg6MDBivb2/AAAATXRFWHRzb2Z0d2FyZQBJbWFnZU1hZ2ljayA3LjAuMS02IFExNiB4ODZfNjQgMjAxNi0wOS0xNyBodHRwOi8vd3d3LmltYWdlbWFnaWNrLm9yZ93ZpU4AAAAYdEVYdFRodW1iOjpEb2N1bWVudDo6UGFnZXMAMaf/uy8AAAAZdEVYdFRodW1iOjpJbWFnZTo6SGVpZ2h0ADEwNjMdLogaAAAAF3RFWHRUaHVtYjo6SW1hZ2U6OldpZHRoADQyNaj3r4sAAAAZdEVYdFRodW1iOjpNaW1ldHlwZQBpbWFnZS9wbmc/slZOAAAAF3RFWHRUaHVtYjo6TVRpbWUAMTM5OTIxMTYzNCKWYGIAAAASdEVYdFRodW1iOjpTaXplADEyLjdLQs+hF00AAABfdEVYdFRodW1iOjpVUkkAZmlsZTovLy9ob21lL3d3d3Jvb3Qvc2l0ZS93d3cuZWFzeWljb24ubmV0L2Nkbi1pbWcuZWFzeWljb24uY24vc3JjLzExNjAyLzExNjAyNDMucG5nsPoC/QAAAABJRU5ErkJggg==) ;
    background-repeat: no-repeat;
    background-position: center;
}
.box3{
    background-color: red;
}
.box4{
    background-color: orange;
}
.box5{
    background-color: #ccc;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAABuvAAAbrwFeGpEcAAAAB3RJTUUH2wQdDR8AJlzyAwAAB79JREFUWMPtln9sU9cVx7/33fdsPzuOY+eHnSWOA+2qJhoTOBAyGDAkuiB1VK0iARpoLW2jpGs7VIEmoFo6GKOb1IgJKtSIDWmb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G5nP7JWTgAAABd0RVh0VGh1bWI6Ok1UaW1lADEzMDQwNTUwNjAVs8b7AAAAEnRFWHRUaHVtYjo6U2l6ZQAyMS4yS0IIXt3YAAAAXXRFWHRUaHVtYjo6VVJJAGZpbGU6Ly8vaG9tZS93d3dyb290L3NpdGUvd3d3LmVhc3lpY29uLm5ldC9jZG4taW1nLmVhc3lpY29uLmNuL3NyYy81Mzk2LzUzOTY3MC5wbmfCorxHAAAAAElFTkSuQmCC) ;
  background-repeat: no-repeat;
  background-position: center;
}

canvasDemo.js

// pages/canvasDemo/canvasDemo.js
Page({
startX:0,//起始x轴的坐标
startY:0,//起始y轴的坐标
isClear:false,//是否启用橡皮擦
    /**
     * 页面的初始数据
     */
    data: {
        pen:2,//画笔的粗细
        color:'orange'//画笔的颜色
    },
    binderaser(){
        this.isClear=!this.isClear
    },
    bindcolor(e){
        let color=e.currentTarget.dataset.param;
        this.setData({
            color 
        })
        this.isClear=false
    },
    bindpen(e){
        let pen=e.currentTarget.dataset.param;
        this.setData({
            pen
        });
        this.isClear=false;
        console.log(this.data.pen);
    },
    handlestart(e){
        console.log(e);
        this.startX=e.changedTouches[0].x;//获取起点坐标的x轴
        this.startY=e.changedTouches[0].y;//获取起点坐标的y轴
        // 创建画图上下文对象
        this.context=wx.createContext();
        // isClear为true表示其用橡皮擦功能
        if(this.isClear){
            this.context.setStrokeStyle('#fed');//设置线条样式,橡皮擦的原理把擦过的地方填充为画布颜色,使其一致
            this.context.setLineCap('round');//设置线条端点
            this.context.setLineJoin('round');//设置两线交叉处的样式
            this.context.setLineWidth(20);//设置线条宽度
            this.context.save();//保存当前坐标轴的缩放,旋转,平移信息
            this.context.beginPath();//开始一个路径
            this.context.arc(this.startX,this.startY,5,0,2*Math.PI,true);//添加一个弧形路径到当前路径,顺时针绘制,这里总共画360度 圆形
            this.context.fill()//对当前路径进行填充
            this.context.restore();//恢复之前保存过的坐标轴的缩放,旋转,平移信息
        }else{
            this.context.setStrokeStyle(this.data.color);//设置画笔的颜色
            this.context.setLineWidth(this.data.pen);//设置画笔的粗细
            this.context.setLineCap('round');//设置画笔圆角
            this.context.beginPath();//开始绘制
        }
    },
    handlemove(e){
        let startX1=e.changedTouches[0].x;
        let startY1=e.changedTouches[0].y;
        if(this.isClear){//采用橡皮擦模式
            this.context.save();//保存当前前面的所有操作
            this.context.moveTo(this.startX,this.startY);//把路径移动到画布中的指定点,但不创建线条
            this.context.lineTo(startX1,startY1);//添加一个新点,然后在画布中创建从该店到指定点的线条
            this.context.stroke();//对当前路径进行描边
            this.context.restore();//恢复之前保存过的坐标轴的缩放,旋转,平移信息
            this.startX=startX1;
            this.startY=startY1;//将下次绘制的起点移动到本次绘制的终点
        }else{
            this.context.moveTo(this.startX,this.startY);
            this.context.lineTo(startX1,startY1);
            this.context.stroke();
            this.startX=startX1;
            this.startY=startY1
        }
        wx.drawCanvas({
            canvasId:'my-chavas',
            reserve:true,
            actions:this.context.getActions()//获取绘图的动作数组
        })
    },
  
})

效果如下:


微信涂鸦板.png

结构样式和上一篇文章的一样,只是多加了一个橡皮擦的功能

相关文章

网友评论

      本文标题:小程序canvas组件实现涂鸦板功能升级版带橡皮擦功能

      本文链接:https://www.haomeiwen.com/subject/gwqoyrtx.html