原生微信小程序canvas签名功能

半个月前百度搜出来的,没存书签现在不知道是哪篇文章了,再搜也没搜出来那篇文章,还好当时把代码复制到本地跑了一下,现在再发csdn存一下。

sign.js

javascript 复制代码
Page({
  data: {
    ctx: null,
    width: null,
    height: null,
    drawCount: 0,
    drawState: "init"
  },
  onShow: function () {
    this.initCanvas()
  },
  initCanvas() {
    let {
      width,
      height
    } = this.data
    width = wx.getSystemInfoSync().windowWidth
    height = wx.getSystemInfoSync().windowHeight
    console.log(wx.getSystemInfoSync())
    this.data.ctx = wx.createCanvasContext('canvas')
    this.setData({
      width,
      height
    })
    this.clearCanvas()
  },
  clearCanvas() {
    this.data.drawCount = 0
    this.data.drawState = "ing"
    this.data.ctx.setTextBaseline('top')
    this.data.ctx.setTextAlign('center')
    this.data.ctx.setFontSize(20)
    this.data.ctx.setFillStyle('#616165');
    this.data.ctx.fillText("请灰色区域内完成签名", this.data.width / 2, 30)
    this.data.ctx.draw(false)
  },
  catchtouchstart(e) {
    if (this.data.drawCount == 0) {
      this.data.ctx.draw(false)
    }
    this.data.drawCount++
    this.data.ctx.moveTo(e.changedTouches[0].clientX, e.changedTouches[0].clientY)
  },
  catchtouchmove(e) {
    if (this.data.drawState == "stop") return
    this.data.drawState = "ing"
    if (e.touches.length > 1) {
      return
    }
    this.data.ctx.setStrokeStyle('#000000');
    this.data.ctx.setLineWidth(3);
    this.data.ctx.setShadow(0, 0, 0.5, '#000000')
    this.data.ctx.setLineCap('round');
    this.data.ctx.setLineJoin('round');
    this.data.ctx.lineTo(e.changedTouches[0].clientX, e.changedTouches[0].clientY)
    this.data.ctx.stroke()
    this.data.ctx.draw(true)
    this.data.ctx.moveTo(e.changedTouches[0].clientX, e.changedTouches[0].clientY)
  },
  canvasToImg() {
    if (this.data.drawState == "init") return
    this.data.drawState = "stop"
    wx.canvasToTempFilePath({
      canvasId: 'canvas',
      success: res => {
        console.log(res.tempFilePath)
        // ...
        // 上传服务器
        wx.navigateTo({
          url: '/pages/showImg/showImg?src=' + res.tempFilePath,
        })
      }
    })
  }
})

sign.wxml

html 复制代码
<canvas canvas-id="canvas" style="width:{{width+'px'}};height:{{height+'px'}}" catchtouchstart="catchtouchstart" catchtouchmove="catchtouchmove" catchtouchend="catchtouchend"></canvas>
<view class="btn-reset" catchtap="clearCanvas">重新绘制</view>
<view class="btn-ok" catchtap="canvasToImg">确认</view>

sign.wxss

css 复制代码
page{
  position: relative;
  background-color: #f2f2f2;
  width: 100%;
  height: 100%;
}
canvas{
  width: 100%;
  height: 100vh;
}
.btn-reset{
  width: 100rpx;
  position: absolute;
  bottom: 10rpx;
  right: 160rpx;
  padding: 8rpx;
  text-align: center;
  border: 1rpx solid #4965B3;
  color: #4965B3;
  font-size: 18rpx;
  border-radius: 8rpx;
  box-sizing: border-box;
}
.btn-ok{
  width: 100rpx;
  position: absolute;
  bottom: 10rpx;
  right: 30rpx;
  padding: 8rpx;
  text-align: center;
  background-color: #4965B3;
  border: 1rpx solid #4965B3;
  color: #fff;
  font-size: 18rpx;
  border-radius: 8rpx;
  box-sizing: border-box;
}

sign.json

javascript 复制代码
{
  "usingComponents": {}
}

开发者工具内签名效果

相关推荐
说私域5 小时前
基于定制开发开源AI智能名片S2B2C商城小程序的会员存量池构建策略研究
人工智能·小程序·开源·零售
黄同学real9 小时前
微信小程序 XSS 防护知识整理
微信小程序·小程序·xss
车轮滚滚__11 小时前
uniapp 小程序 安卓苹果 短视频解决方案
ios·小程序·uni-app·安卓·html5
小羊Linux客栈20 小时前
Python小程序:上班该做点摸鱼的事情
开发语言·python·小程序·游戏程序
Nueuis20 小时前
微信小程序分页和下拉刷新
服务器·前端·微信小程序
276695829221 小时前
得物 小程序 6宫格 分析
java·python·小程序·得物·得物小程序·得物六宫格·六宫格验证码
MYmayue1 天前
微信小程序封装选择年月日时分秒组件
微信小程序·小程序
Nightne1 天前
总结小程序的坑
微信小程序
niech_cn1 天前
仿微信上传头像,实现拍摄、相册选择、手动缩放、裁剪、蒙版、撤回、还原、上传微信本地文件功能
微信·小程序