微信小程序手写签名,使用canvas 2d 丝滑连笔,不卡顿

最近产品让我写个手写签名,上网百度了一下,基本上快速连笔都很卡,于是自己参考资料重写了一下,体验效果拉满,话不多说上代码。

WXML代码

<canvas type="2d" id="canvas" disable-scroll="true"  bindtouchmove="handleMove" bindtouchstart="handleStart"  binderror="handleError" style="width:{{width}}px;height:{{height}}px;"></canvas>
<view class="btn">
  <button bindtap="clearClick">重签</button>
  <button bindtap="saveClick">确认</button>
</view>

WXSS

page {
  background-color: #e7e7e7;
}
.btn {
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 60px;
  position: fixed;
  bottom: 0px;
}
.btn button {
  color: #006ec7;
}
button {
  width: 200rpx;
  border-radius: 5rpx;
  box-shadow: 0px 0px 1px 1px #c5c5c5;
}
canvas {
  background-color: white;
}

 JS代码

Page({
  data: {
    canvas: '',
    ctx: '',
    pr: 0,
    width: 0,
    height: 0,
    preX: 0,
    preY: 0
  },
  handleStart(e) {
    this.data.ctx.beginPath()
    this.data.preX = e.changedTouches[0].x
    this.data.preY =  e.changedTouches[0].y
  },
  handleMove(e) {
    this.data.ctx.moveTo(this.data.preX, this.data.preY)
    this.data.ctx.lineTo(e.changedTouches[0].x, e.changedTouches[0].y) 
    this.data.ctx.stroke()
    this.data.preX = e.changedTouches[0].x
    this.data.preY = e.changedTouches[0].y
  },
  handleError(e) {
    console.log("画布触摸错误" + e);
  },

  /**
   * 初始化
   */
  createCanvas() {
    const pr = this.data.pr; // 像素比
    const query = wx.createSelectorQuery();
    query.select('#canvas').fields({
      node: true,
      size: true
    }).exec((res) => {
      console.log(this.data.height);
      const canvas = res[0].node;
      const ctx = canvas.getContext('2d');
      canvas.width = this.data.width * pr; // 画布宽度
      canvas.height = this.data.height * pr; // 画布高度
      ctx.scale(pr, pr)
      ctx.lineGap = 'round';
      ctx.lineJoin = 'round';
      ctx.lineWidth = 4; // 字体粗细
      ctx.font = '40px Arial'; // 字体大小,
      ctx.fillStyle = '#ecf0ef'; // 填充颜色
      this.setData({
        ctx,
        canvas
      })
    })
  },
  // 获取系统信息
  getSystemInfo() {
    let that = this;
    wx.getSystemInfo({
      success(res) {
        console.log(res,'sysinfo');
        that.setData({
          pr: res.pixelRatio,
          width: res.windowWidth,
          height: res.windowHeight - 60,
        })
      }
    })
  },
  clearClick: function () {
    //清除画布
    this.data.ctx.clearRect(0, 0, this.data.width, this.data.height);
  },
  //保存图片
  saveClick: function () {
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: this.data.width,
      height: this.data.height,
      destWidth:this.data.width*this.data.pr,
      destHeight: this.data.height*this.data.pr,
      canvasId: 'canvas',
      canvas: this.data.canvas,
      fileType: 'png',
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getSystemInfo()
    this.createCanvas()
    
  },
  onShow() {
    let that = this;
    wx.onWindowResize(function (res) {
      that.setData({
        width: res.size.windowWidth,
        height: res.size.windowHeight - 60,
      })
      that.createCanvas()
    })
  }
})

运行效果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值