微信小程序实现手写签名的示例代码

 更新时间:2022年02月18日 12:00:00   作者:小小蚊子  
这篇文章主要和大家分享一个微信小程序的示例代码,可以实现手写签名的效果。文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下

在微信小程序上实现手写签名,获取canvascontext新版本和旧版本有点坑,新版本在获取canvas后如果页面有滑动,则签名坐标出现异常(在微信开发者工具上会出现2022-2-17),但是在真机上即使滑动也不会出现异常,为了防止出现问题,暂时使用旧版本获取canvascontext

1.效果图

2.相关代码

canvas代码

新版2d canvas

<canvas
  id="canvas"
  class="canvas"
  canvas-id="canvas"
  type="2d"
  :disable-scroll="true"
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
  @touchcancel="handleTouchCancel"
></canvas>

旧版canvas

<canvas
  class="canvas"
  canvas-id="canvas"
  :disable-scroll="true"
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
  @touchcancel="handleTouchCancel"
></canvas>

js相关

获取新版2d canvas对象

const query = uni.createSelectorQuery().in(this);
query.select('.canvas').node(res => {
  const {
      _width,
      _height
  } = res.node;
  
  /* 获取canvas wxml节点 */
  this.canvas = res.node;
  this.canvasWidth = _width;
  this.canvasHeight = _height;
  /* 获取canvas 2dcontext */
  this.canvasContext= this.canvas.getContext('2d');
  
  /* 缩放设置canvas画布大小,防止笔迹错位 */
  const ratio = wx.getSystemInfoSync().pixelRatio;
  this.canvas.width = this.canvasWidth * ratio;
  this.canvas.height = this.canvasHeight * ratio;
  this.canvasContext.scale(ratio, ratio);
  
  /* 设置线条颜色 */
  this.canvasContext.strokeStyle = '#2A2A2A';
  /* 设置线条粗细 */
  this.canvasContext.lineWidth = 4;
  /* 设置线条的结束端点样式 */
  this.canvasContext.lineCap = 'round';
}).exec()

缩放设置canvas画布大小,防止笔迹错位,这点和页面滑动没有关系,不设置也会导致坐标错位

const ratio = wx.getSystemInfoSync().pixelRatio;
this.canvas.width = this.canvasWidth * ratio;
this.canvas.height = this.canvasHeight * ratio;
this.canvasContext.scale(ratio, ratio);

旧版本获取canvas

this.canvasContext = uni.createCanvasContext('canvas', this);
/* 设置线条颜色 */
this.canvasContext.setStrokeStyle('#2A2A2A');
/* 设置线条粗细 */
this.canvasContext.setLineWidth(4);
/* 设置线条的结束端点样式 */
this.canvasContext.setLineCap('round');

签名js方法,新版本和旧版本只有一个draw的区别,新版本不需要使用draw方法

/* 触摸开始 */
handleTouchStart(e) {
  this.drawStartX = e.changedTouches[0].x;
  this.drawStartY = e.changedTouches[0].y;
    this.canvasContext.beginPath();
},
/* 触摸移动 */
handleTouchMove(e) {
    /* 记录当前位置 */
    const tempX = e.changedTouches[0].x;
    const tempY = e.changedTouches[0].y;

    /* 画线 */
    this.canvasContext.moveTo(this.drawStartX, this.drawStartY);
    this.canvasContext.lineTo(tempX, tempY);
    this.canvasContext.stroke();

    /* 旧版draw方法,新版本不需要draw */
    this.canvasContext.draw(true);

    /* 重新记录起始位置 */
    this.drawStartX = tempX;
    this.drawStartY = tempY;
},
/* 触摸结束 */
handleTouchEnd(e) {
    this.canvasContext.save();
},
/* 触摸取消 */
handleTouchCancel(e) {
    this.canvasContext.save();
},
/* 清空画布 */
clearCanvas() {
    this.canvasContext.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
},

canvas生成本地图片(我这里封装了组件,需要传入this防止this指向异常)

/* 生成签名图片 */
generateSignImage() {
    return new Promise((resolve, reject) => {
        uni.canvasToTempFilePath({
          x: 0,
          y: 0,
          // canvas: this.canvas, // 新版
          canvasId: 'canvas', // 旧版使用id
          width: this.canvasWidth,
          height: this.canvasHeight,
          destWidth: this.canvasWidth,
          destHeight: this.canvasHeight,
          fileType: 'png',
          quality: 1,
          success: res => {
              resolve(res.tempFilePath)
          },
          fail: err => {
              reject(err);
          }
        }, this)
    })
},

新版本的canvas主要是canvas wxml节点和canvas context中做了区分,旧版则只有一个canvas context就可以做全部的操作,在生成图片时,新版本是传入wxml对象,旧版本则是传入唯一canvasId,新版本canvas取消了draw方法

以上就是微信小程序实现手写签名的示例代码的详细内容,更多关于小程序手写签名的资料请关注脚本之家其它相关文章!

相关文章

  • layui之数据表格--与后台交互获取数据的方法

    layui之数据表格--与后台交互获取数据的方法

    今天小编就为大家分享一篇layui之数据表格--与后台交互获取数据的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • php对mongodb的扩展(小试牛刀)

    php对mongodb的扩展(小试牛刀)

    本文主要讲解php连接、操作mongodb,有需求的朋友可以参考下
    2012-11-11
  • js 键盘记录实现(兼容FireFox和IE)

    js 键盘记录实现(兼容FireFox和IE)

    用js实现键盘记录,要关注浏览器的三种按键事件类型,即keydown,keypress和keyup,它们分别对应onkeydown、onkeypress和onkeyup这三个事件句柄。一个典型的按键会产生所有这三种事件,依次是keydown,keypress,然后是按键释放时候的keyup。
    2010-02-02
  • D3.js实现饼图,环图,玫瑰图的绘制

    D3.js实现饼图,环图,玫瑰图的绘制

    这篇文章主要为大家介绍了如何利用D3.js中的d3.pie和d3.arc实现饼图、环图和玫瑰图的绘制,文中的实现方法讲解详细,感兴趣的可以尝试一下
    2022-11-11
  • 使用Webpack打包的流程分析

    使用Webpack打包的流程分析

    Webpack 是一个前端资源加载/打包工具。它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源,这篇文章主要介绍了使用Webpack打包的操作方法,需要的朋友可以参考下
    2022-12-12
  • 在 javascript 中如何快速获取数组指定位置的元素

    在 javascript 中如何快速获取数组指定位置的元素

    这篇文章主要介绍了在 javascript 中快速获取数组指定位置的元素,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • js绘制圆形和矩形的方法

    js绘制圆形和矩形的方法

    这篇文章主要介绍了js绘制圆形和矩形的方法,涉及javascript鼠标事件及页面元素样式的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • javascript 继承实现方法

    javascript 继承实现方法

    javascript的继承机制并不是明确规定的,而是通过模仿实现的,意味着继承不是由解释程序处理,开发者有权决定最适合的继承方式.
    2009-08-08
  • 基于JavaScript实现的折半查找算法示例

    基于JavaScript实现的折半查找算法示例

    这篇文章主要介绍了基于JavaScript实现的折半查找算法,结合实例形式分析了折半查找的原理、操作步骤及javascript实现折半查找的相关操作技巧与注意事项,需要的朋友可以参考下
    2017-04-04
  • 推荐阅读的js快速判断IE浏览器(兼容IE10与IE11)

    推荐阅读的js快速判断IE浏览器(兼容IE10与IE11)

    这篇文章主要介绍了推荐阅读的js快速判断IE浏览器(兼容IE10与IE11),因为ie11取消了很多低版本的判断,很多js都需要修改了,这里简单介绍下需要的朋友可以参考下
    2015-12-12

最新评论