微信小程序canvas的撤销功能

小程序越来越简单,提供给开发者的api也越来越多,微信这个平台真的很厉害~~

-------------------

canvas画板在涂鸦的时候,一不小心画错了一步,想撤销上一步,还是在微信小程序中去实现这个功能,顿时卡住了,还是去翻百度,翻csdn、简书,总结了几个文档按照自己的思路想了一个简单的方案;

在微信小程序的api支持下是这么做的:一个动作start->move->end(cancel)就结束了,小程序有个api

saveCurrentDrawWorks: function () {
   wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 0,
      height: 0,
      destWidth: 0,
      destHeight: 0,
      canvasId: self.data.Id,
      success: function (res) {
         var imgPath = res.tempFilePath;
         var allDrawWorksPath = self.data.allDrawWorksPath;
         allDrawWorksPath.push(imgPath);
         self.setData({
            allDrawWorksPath: allDrawWorksPath,
         })
      },
      fail: res => {
         console.log('获取画布图片失败', res);

      }
   })
},

在每次start的时候调用保存图片的这个方法,将当前画布的图片保存在本地的数组中;

点击撤销的时候:

drawRevoke: function () {
   var allDrawWorksPath = self.data.allDrawWorksPath;
   if (allDrawWorksPath == null || allDrawWorksPath.length == 0 || allDrawWorksPath == undefined) {
      return;
   }

   var privWorksPath = allDrawWorksPath.pop();
   self.setData({
      allDrawWorksPath: allDrawWorksPath,
   })


   drawPath.pop();
   self.setPaintSize(self.data.LineWidth);
   self.setPaintColor(self.data.StrokeStyle);
   drawContext.drawImage(privWorksPath, 0, 0, screenWidth, screenHeight);
   drawContext.draw();

   if (allDrawWorksPath.length == 0) {
     
   }
},

删除保存数组中的最后一个图片地址,并重画这个地址的图片(drawImage);

大功告成~虽然简单,但确实想了很久,,如果有帮到你,我会很开心

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值