原生微信小程序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": {}
}

开发者工具内签名效果

相关推荐
weixin_1772972206920 分钟前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧
weixin_lynhgworld22 分钟前
剧本杀小程序系统开发:开启沉浸式社交娱乐新纪元
小程序·娱乐
~废弃回忆 �༄2 小时前
mobx-miniprogram小程序的数据传输
小程序
说私域5 小时前
“开源AI智能名片链动2+1模式S2B2C商城小程序”在直播公屏引流中的应用与效果
人工智能·小程序·开源
!win !6 小时前
uni-app项目支付宝端Input不受控
小程序·uni-app·支付宝小程序
毕设源码-钟学长8 小时前
【开题答辩全过程】以 “旧书驿站”微信小程序的设计与开发为例,包含答辩的问题和答案
微信小程序·小程序
毕设源码-赖学姐1 天前
【开题答辩全过程】以 “饭否”食材搭配指南小程序的设计与实现为例,包含答辩的问题和答案
小程序
nodcloud1 天前
点可云进销存商城如何部署在微信小程序
微信小程序·小程序
老华带你飞1 天前
畅阅读小程序|畅阅读系统|基于java的畅阅读系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·畅阅读系统小程序
老华带你飞2 天前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台