首页 > 编程语言 >对于小程序canvas在某些情况下touchmove 不能连续触发导致的签名不连续替代方案(企微)

对于小程序canvas在某些情况下touchmove 不能连续触发导致的签名不连续替代方案(企微)

时间:2024-04-21 10:55:05浏览次数:16  
标签:canvas log timeStamp 企微 current touchmove previousPoint

1.问题

image
微信开放社区链接

尝试过新版canvas,在企业微信中签名依然是依然断触,有问题的手机是iphoe15,系统版本以及企微版本微信版本均与签名正常的手机一致,但是那个手机就是无法正常签字,在微信中无论新旧canvas均能正常签字

2.解决方案

既然canvas的touchmove触发有问题,那么就可以通过替代canvas的touchmove来实现,通过在canvas上覆盖一层dom,通过这层dom的touchmove来获取手指划过的轨迹即可,此文章中并没有小程序实际代码只是使用了h5验证可行性的代码

2.1 注意点
  • 要区别手指是否连续滑动,由于点击事件触发存在如下情况

区别手指是否连续滑动采用时间间隔判断
触发事件间隔小于80ms 主要用于判断是否松开手指再次滑动,正常手速来说80ms,人很难在画完一个线段后,松手再次画一个线段,如果无这个处理会出现滑动一个线段之后,再次点击另一个点会把线段和新点位连接起来

没有采取通过touchstart与touchend做一个判断是因为touchmove并不是固定一直在start与end事件中间触发
直接点击触发事件顺序

2.2 移动端浏览器体验地址
2.2 vue2代码
<template>
  <div class="DomCanvasSignature">
    <div :style="{ height: height + 'px', width: width + 'px' }" class="signatureWrapper" id="signatureWrapper"
      draggable="false" @mousedown="touchstart" @mouseup="touchend" @touchstart="touchstart" @touchend="touchend"
      @touchmove="touchmove" @mousemove="touchmove">
      <canvas canvas-id="999" :height="height" :width="width - 3" class="canvas" />
    </div>
  </div>
</template>
<script>
export default {
  name: 'DomCanvasSignature',
  data () {
    return {
      height: 302,
      width: 302,

      mycanvas: null,
      previousPoint: {
        x: 0,
        y: 0
      },
      isPcStart: false,
      removeLisner: () => { }
    }
  },


  methods: {
    initSize () {
      this.width = window.innerWidth
      this.height = window.innerHeight - 300
    },
    lisner () {
      this.initSize()
      window.addEventListener('resize', this.initSize)
      return () => {
        window.removeEventListener('resize', this.initSize)
      }
    },
    touchstart () {
      this.isPcStart = true
      console.log('====start') // zdz-log
    },
    touchend () {
      this.isPcStart = false
      console.log('====end') // zdz-log

    },
    touchmove (e) {
      console.log('move', e) // zdz-log
      // 阻止滚动
      e.preventDefault()
      if (e.type === 'mousemove' && !this.isPcStart) {
        return
      }
      // 合并处理 pc 与移动端
      const changeObj = e.changedTouches && e.changedTouches[0] || e
      const current = { x: changeObj.clientX, y: changeObj.clientY, timeStamp: e.timeStamp }

      // 1.获取元素
      // 2.获取上下文,绘制工具箱
      let ctx = this.mycanvas.getContext('2d')
      // 3.移动画笔
      const currentY = (current.y) - signatureWrapper.offsetTop
      // todo 改为touchstart 与end判断 无法实现 因为move 执行存在在 start end事件之后
      let diffLarge = false
      console.log(current.timeStamp - this.previousPoint.timeStamp) // zdz-log
      // 判断是否松手重新绘制
      if (this.previousPoint.timeStamp) {
        const timeDiff = current.timeStamp - this.previousPoint.timeStamp > 80
        if (timeDiff) {
          diffLarge = true
        }
      }

      const preY = diffLarge ? current.y - signatureWrapper.offsetTop : (this.previousPoint.y || current.y) - signatureWrapper.offsetTop
      const moveX = diffLarge ? current.x : this.previousPoint.x || current.x
      ctx.moveTo(moveX, preY < 0 ? 0 : preY)
      // 4.绘制直线(轨迹,绘制路径)
      ctx.lineTo(current.x, currentY < 0 ? 0 : currentY)
      // 5.描边
      ctx.stroke()

      this.previousPoint = current

    },


  },
  created () {
    this.removeLisner = this.lisner()
  },
  destroyed () {
    this.removeLisner()
  },
  mounted () {
    this.mycanvas = document.querySelector('canvas')
    this.signatureWrapper = document.getElementById('signatureWrapper')
  },

}
</script>

<style scoped>
.canvas {
  border: 1px solid red;
}

.signatureWrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid black;
  background-color: transparent;
}
</style>


标签:canvas,log,timeStamp,企微,current,touchmove,previousPoint
From: https://www.cnblogs.com/coderzdz/p/18148683

相关文章

  • 企业微信hook 最新版 、企微输入验证码,二次扫码方案、发名片收消息功能,企业微信hook源
    ​产品说明在PC端企业微信客户端上发送自定义名片、封装企业微信功能为DLL,可与其他语言调用实现功能。DLL可以监听企业微信的所有消息接收和群消息,根据需求实现机器人、群发、自动消息推送、聊天机器人、监管数据收集等功能企业微信hook,企业微信功能api,自定义开发功能清......
  • 鸿蒙HarmonyOS实战-ArkUI组件(Canvas)
    ......
  • html2canvas截取专题图(包含地图)
    html2canvas截取专题图(包含地图)问题:html2canvas截取地图时地图空白,报错:UnabletocloneWebGLcontextasithaspreserveDrawingBuffer=false一、情况介绍:​ 使用如下代码进行截图时,出现地图空白情况,报错:UnabletocloneWebGLcontextasithaspreserveDrawingBuffer=f......
  • canvas 捕获视频帧
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>捕获视频帧</title>......
  • WPF基础:在Canvas上绘制图形
    Canvas介绍Canvas是WPF(WindowsPresentationFoundation)中的一种面板控件,用于在XAML中布置子元素。它提供了绝对定位的能力,允许元素在自由的二维空间中放置。Canvas上的子元素可以通过指定绝对位置(Left和Top属性)来放置,也可以使用附加属性来指定相对于Canvas的位置。Canvas对于需......
  • Canvas图形编辑器-数据结构与History(undo/redo)
    Canvas图形编辑器-数据结构与History(undo/redo)这是作为社区老给我推Canvas,于是我也学习Canvas做了个简历编辑器的后续内容,主要是介绍了对数据结构的设计以及History能力的实现。在线编辑:https://windrunnermax.github.io/CanvasEditor开源地址:https://github.com/Wind......
  • 基于Canvas实现的简历编辑器
    基于Canvas实现的简历编辑器大概一个月前,我发现社区老是给我推荐Canvas相关的内容,比如很多小游戏、流程图编辑器、图片编辑器等等各种各样的项目,不知道是不是因为我某一天点击了相关内容触发了推荐机制,还是因为现在Canvas比较火大家都在卷,本着我可以用不上但是不能不会的原则,我......
  • 右击canvas画布时,显示自定义菜单
    <style> .clickRightMenu{ width:110px; background-color:#fff; font-size:12px; position:absolute; text-align:left; padding:2px0; border:1pxsolid#ccc; display:none; z-index:100; } .clickRightMe......
  • vue canvas绘制信令图,动态显示标题、宽度、高度
    需求:1、 根据后端返回的数据,动态绘制出信令图2、根据 dataStatus返回值:0和1,判断文字内容的颜色,0:#000,1:red3.、根据lineType 返回值:0和1, 判断箭头线的显示是实线、虚线4、根据返回的文字内容的换行符:“\r\n”自动换行(这步比较难,得计算高度)最后的效果图大......
  • 基于canvas 或 svg绘制并生成base64 用于cesium billboard 渲染 替代label
    原因cesium的label样式不太好修改canvas生成functionlabelContent(showData){constmyConvas=document.createElement("canvas");constscale=1;//获取2d的上线文开始设置相关属性myConvas.width=150myConvas.height=65;letcont......