您当前的位置: 首页 > 解决方案

小程序手写签名(wepy)

  • 作者: admin
  • 发布于 2018-08-23 13:44:18
  • 来源:  
  • 栏目:解决方案

导语: 对于手写签名组件组件晚上有很多种写法,我选择了一种进行了wepy的框架的改造。如果使用wepy框架做手写签名的话可以直接复制下面的代码。这里需要提醒的是:安卓手机和苹果

 对于手写签名组件组件晚上有很多种写法,我选择了一种进行了wepy的框架的改造。如果使用wepy框架做手写签名的话可以直接复制下面的代码。

这里需要提醒的是:安卓手机和苹果手机有适配性的问题,苹果手机在签名的以后手机将无法进行文档的上下左右的滑动,而安卓手机没有影响。解决这个问题的方法是给canvas给disable-scroll属性绑定一个变量 disable-scroll="true" 当进行滑动的时候讲disable-scroll属性设置成true,滑动完成时将属性还原为false.

 
  1. <template>
  2. <view class='content'>
  3. <canvas class='firstCanvas' canvas-id="firstCanvas" bindtouchmove='move' bindtouchstart='start' bindtouchend='end' bindtouchcancel='cancel' bindlongtap='tap' disable-scroll='true' binderror='error'>
  4. </canvas>
  5. <button bindtap='clearClick'>清除</button>
  6. <button bindtap='saveClick'>保存图片</button>
  7. <image id='signatureImg' src='{{signImage}}'></image>
  8. </view>
  9. </template>
  10. <script>
  11. import wepy from 'wepy';
  12. var content = null;
  13. var touchs = [];
  14. var canvasw = 0;
  15. var canvash = 0;
  16.  
  17. export default class Index extends wepy.page {
  18. config = {};
  19. components = {};
  20. data = {
  21. signImage: '',
  22. };
  23. events = {};
  24. methods = {
  25. start: function(event) {
  26. // console.log("触摸开始" + event.changedTouches[0].x)
  27. // console.log("触摸开始" + event.changedTouches[0].y)
  28. //获取触摸开始的 x,y
  29. let point = {
  30. x: event.changedTouches[0].x,
  31. y: event.changedTouches[0].y
  32. }
  33. touchs.push(point)
  34. },
  35. // 画布的触摸移动手势响应
  36. move: function(e) {
  37. let point = {
  38. x: e.touches[0].x,
  39. y: e.touches[0].y
  40. }
  41. touchs.push(point)
  42. if (touchs.length >= 2) {
  43. this.draw(touchs)
  44. }
  45. },
  46. // 画布的触摸取消响应
  47. cancel: function(e) {
  48. console.log("触摸取消" + e)
  49. },
  50. // 画布的长按手势响应
  51. tap: function(e) {
  52. console.log("长按手势" + e)
  53. },
  54. error: function(e) {
  55. console.log("画布触摸错误" + e)
  56. },
  57. // 画布的触摸移动结束手势响应
  58. end: function(e) {
  59. console.log("触摸结束" + e)
  60. //清空轨迹数组
  61. for (let i = 0; i < touchs.length; i++) {
  62. touchs.pop()
  63. }
  64. },
  65. };
  66. async onShow() {}
  67. async onLoad(options) {
  68. //获得Canvas的上下文
  69. content = wx.createCanvasContext('firstCanvas')
  70. //设置线的颜色
  71. content.setStrokeStyle("#00ff00")
  72. //设置线的宽度
  73. content.setLineWidth(5)
  74. //设置线两端端点样式更加圆润
  75. content.setLineCap('round')
  76. //设置两条线连接处更加圆润
  77. content.setLineJoin('round')
  78. }
  79. draw(touchs) {
  80. let point1 = touchs[0]
  81. let point2 = touchs[1]
  82. touchs.shift()
  83. content.moveTo(point1.x, point1.y)
  84. content.lineTo(point2.x, point2.y)
  85. content.stroke()
  86. content.draw(true)
  87. }
  88. clearClick() {
  89. //清除画布
  90. content.clearRect(0, 0, canvasw, canvash)
  91. content.draw(true)
  92. }
  93. saveClick() {
  94. var that = this
  95. wx.canvasToTempFilePath({
  96. canvasId: 'firstCanvas',
  97. success: function(res) {
  98. //打印图片路径
  99. console.log(res.tempFilePath)
  100. //设置保存的图片
  101. that.setData({
  102. signImage: res.tempFilePath
  103. })
  104. }
  105. })
  106. }
  107. }
  108. </script>
  109. <style>
  110. .content {
  111. width: 100%;
  112. height: 500px;
  113. background-color: red;
  114. }
  115. .firstCanvas {
  116. background-color: blue;
  117. width: 100%;
  118. height: 200px;
  119. }
  120. image {
  121. width: 100%;
  122. height: 200px;
  123. background-color: orange;
  124. }
  125. </style>



温馨提示:这篇文章没有解决您的问题?欢迎添加微信:18948083295,有微信小程序专业人员,保证有问必答。转载本站文章请注明转自http://www.okeydown.com/(微信小程序网)。

  • 微信扫描二维码关注官方微信
  • ▲长按图片识别二维码
关注我们

微信小程序官方微信

栏目最新
栏目推荐
返回顶部