微信小程序实现手写签名

 更新时间:2022年07月08日 08:39:26   作者:KabunM  
这篇文章主要为大家详细介绍了微信小程序实现手写签名,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序实现手写签名的具体代码,供大家参考,具体内容如下

本示例具备的功能:

1、笔迹绘制

2、笔迹清空

以下是js代码:

var content = null;
var touchs = [];
var canvasw = 0;
var canvash = 0;
var that = null;
 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    
  },
  // 画布的触摸移动开始手势响应
  start: function(event) {
    // console.log("触摸开始" + event.changedTouches[0].y)
    // console.log("触摸开始" + event.changedTouches[0].x)
    //获取触摸开始的 x,y
    let point = {
      x: event.changedTouches[0].y,
      y: event.changedTouches[0].x
    }
    touchs.push(point)
  },
 
  // 画布的触摸移动手势响应
  move: function(e) {
    let point = {
      x: e.touches[0].y,
      y: e.touches[0].x
    }
    touchs.push(point)
    if (touchs.length >= 2) {
      this.draw(touchs)
    }
  },
 
  // 画布的触摸移动结束手势响应
  end: function(e) {
    // console.log("触摸结束" + e)
    //清空轨迹数组
    for (let i = 0; i < touchs.length; i++) {
      touchs.pop()
    }
 
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    //获得Canvas的上下文
    content = wx.createCanvasContext('firstCanvas')
    //设置线的颜色
    content.setStrokeStyle("#000")
    //设置线的宽度
    content.setLineWidth(5)
    //设置线两端端点样式更加圆润
    content.setLineCap('round')
    //设置两条线连接处更加圆润
    content.setLineJoin('round')
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
 
    // 获取画布尺寸
    var query = wx.createSelectorQuery()
    query.select('#canvas').boundingClientRect()
    query.exec(function(res) {
      canvasw = res[0].width;
      canvash = res[0].height
    })
  },
 
  //绘制
  draw: function(touchs) {
    let point1 = touchs[0]
    let point2 = touchs[1]
    touchs.shift()
    content.moveTo(point1.y, point1.x)
    content.lineTo(point2.y, point2.x)
    content.stroke()
    content.draw(true)
  },
  //清除操作
  clearClick: function() {
    //清除画布
    content.clearRect(0, 0, canvasw, canvash)
    content.draw(true)
  },
  //保存图片
  saveClick: function() {
    var that = this
    wx.canvasToTempFilePath({
      canvasId: 'firstCanvas',
      success: function(res) {
        //打印图片路径
        var path = res.tempFilePath
        //上传图片
        that.uploadSignPic(path)
        console.log(path)
 
 
      }
    })
  },
 
 
  /**
   * 上传签名图片
   */
  uploadSignPic: function(path) {
      //具体实现的业务逻辑
  }
 
})

以下是wxml代码:

<!-- 手写界面 -->
<view class='hand_writing_container'>
  <view class="tips_title">请在区域内进行签名</view>
  <view id="canvas" class="canvas">
    <canvas class='firstCanvas' 
    canvas-id="firstCanvas" 
    bindtouchmove='move' 
    bindtouchstart='start' 
    bindtouchend='end' 
    disable-scroll='true' >
    </canvas>
  </view>
  <view class="btn_container">
    <view class="btn clean" bindtap="clearClick">
      <image src="/image/clear.png"></image>
      <text>内容清除</text>
    </view>
    <view class="btn submit" bindtap="saveClick">
      <image src="/image/submit.png"></image>
      <text>确认提交</text>
    </view>
  </view>
</view>

以下是样式代码:

.hand_writing_container {
  width: 100%;
  padding: 5.503vh 8.1761vh 0;
  box-sizing: border-box;
}
 
.tips_title {
  height: 4.72vh;
  line-height: 4.72vh;
  margin-bottom: 3.459vh;
  font-size: 4.71698vh;
  font-family: Source Han Sans CN;
  font-weight: 500;
  color: rgba(45, 45, 45, 1);
}
 
.canvas {
  width: 100%;
  height: 66.194968vh;
  margin-bottom: 3.7735849vh;
  box-sizing: border-box;
  border: 1rpx dashed black;
}
 
.firstCanvas {
  background-color: white;
  width: 100%;
  height: 100%;
}
 
.btn_container {
  display: flex;
  align-items: center;
  padding: 0 45.44vh;
  box-sizing: border-box;
  justify-content: space-between;
}
 
.btn {
  width: 45.597484vh;
  height: 13.83647798vh;
  padding: 0 11vh;
  box-sizing: border-box;
  border-radius: 1.572327vh;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
 
.btn image {
  flex: 0 0 4.71698vh;
  width: 4.71698vh;
  height: 4.71698vh;
  display: block;
  margin-right: 1.88679vh;
}
 
.btn text {
  flex: 0 0 auto;
  height: 4.717vh;
  line-height: 4.717vh;
  font-size: 4.71698vh;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: rgba(255, 255, 255, 1);
}
 
.clean {
  background-color: #07c160;
}
 
.submit {
  background-color: #ff3d33;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 微信小程序实现监听页面滚动

    微信小程序实现监听页面滚动

    这篇文章主要为大家详细介绍了微信小程序实现监听页面滚动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • vite打包优化vite-plugin-compression的使用示例详解

    vite打包优化vite-plugin-compression的使用示例详解

    这篇文章主要介绍了vite打包优化vite-plugin-compression的使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • Javascript中Fetch请求Coze API并流式展示请求结果

    Javascript中Fetch请求Coze API并流式展示请求结果

    文章介绍了如何使用Fetch API处理CozeAPI返回的流式响应,并在前端进行实时展示,文章提供了处理流式响应的步骤和示例代码,包括使用fetch发起POST请求、读取响应体流、逐步处理数据块以及更新UI,感兴趣的朋友一起看看吧
    2025-02-02
  • JavaScript 常用函数

    JavaScript 常用函数

    javascript提供了许多函数供开发人员使用,下面给出一个简单介绍,更详细的信息请参考Visual InterDev提供的在线帮助。
    2009-12-12
  • web打印小结

    web打印小结

    本文主要介绍了一款比较强大的web打印工具lodop实现将winform客户端的打印,移到网页上由客户自行打印,打印要求是根据一定的格式实现套打的过程与方法。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • JS动态生成年份和月份实例代码

    JS动态生成年份和月份实例代码

    本文给大家分享两段代码来通过js动态生成年份和月份功能,非常不错,具有参考借鉴价值,需要的朋友参考下
    2017-02-02
  • cocos2dx骨骼动画Armature源码剖析(三)

    cocos2dx骨骼动画Armature源码剖析(三)

    本篇文章给大家分享cocos2dx骨骼动画Armature源码剖析(三),代码附有注释,介绍的非常详细,需要的朋友可以参考下
    2015-09-09
  • js网页滚动条滚动事件实例分析

    js网页滚动条滚动事件实例分析

    这篇文章主要介绍了js网页滚动条滚动事件的用法,实例分析了javascript中window.onscroll监控滚动条滚动事件的相关技巧,需要的朋友可以参考下
    2015-05-05
  • JavaScript Prototype对象

    JavaScript Prototype对象

    从JavaScript 1.1开始,它就有了一个内置对象叫Prototype。通过它,可以扩展JavaScript,在对象上编写自定义的属性和方法。
    2009-01-01
  • Javascript中的arguments对象

    Javascript中的arguments对象

    这篇文章主要介绍了Javascript中的arguments对象 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06

最新评论