qm.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view class="center">
  3. <canvas class="canvas" canvas-id="mycanvas" @touchstart.stop="touchstart" @touchmove.stop="touchmove" @touchend.stop="touchend">
  4. <text class="txt-font" v-if="showtext">请在此处书写签名</text>
  5. </canvas>
  6. <view class="btn flex">
  7. <view class="button" @click.stop="clear"><text class="text">重写</text></view>
  8. <view class="button" @click.stop="submit"><text class="text">确认</text></view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. type: '',
  17. showtext: true,
  18. ctx: '',//绘画图像
  19. points: [], //路径点集合
  20. signature: ''
  21. }
  22. },
  23. onLoad(option) {
  24. this.type = option.type;
  25. this.ctx = uni.createCanvasContext("mycanvas",this);
  26. //设置画笔样式
  27. this.ctx.lineWidth = 1;
  28. this.ctx.lineCap = "round"
  29. this.ctx.lineJoin = "round"
  30. },
  31. methods: {
  32. //触摸开始,获取到起点
  33. touchstart: function(e) {
  34. this.showtext = false;
  35. // #ifdef H5
  36. let startX = e.changedTouches[0].x;
  37. let startY = e.changedTouches[0].y;
  38. // #endif
  39. // #ifdef MP
  40. let startX = e.changedTouches[0].clientX;
  41. let startY = e.changedTouches[0].clientY;
  42. // #endif
  43. let startPoint = { X: startX, Y: startY };
  44. this.points.push(startPoint);
  45. //每次触摸开始,开启新的路径
  46. this.ctx.beginPath();
  47. },
  48. //触摸移动,获取到路径点
  49. touchmove: function(e) {
  50. // #ifdef H5
  51. let moveX = e.changedTouches[0].x;
  52. let moveY = e.changedTouches[0].y;
  53. // #endif
  54. // #ifdef MP
  55. let moveX = e.changedTouches[0].clientX;
  56. let moveY = e.changedTouches[0].clientY;
  57. // #endif
  58. let movePoint = {X: moveX, Y: moveY};
  59. this.points.push(movePoint); //存点
  60. let len = this.points.length;
  61. if(len >= 2) {
  62. this.draw(); //绘制路径
  63. }
  64. },
  65. // 触摸结束,将未绘制的点清空防止对后续路径产生干扰
  66. touchend: function() {
  67. this.points = [];
  68. },
  69. //绘制笔迹
  70. // 1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
  71. // 2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
  72. // 3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
  73. draw: function() {
  74. let that = this;
  75. let point1 ={
  76. Y:that.points[0].Y,
  77. X:that.points[0].X,
  78. };
  79. let point2 = {
  80. Y:that.points[1].Y,
  81. X:that.points[1].X,
  82. }; ;
  83. // console.log(that.points);
  84. that.points.shift();
  85. that.ctx.moveTo(Math.floor(point1.X), Math.floor(point1.Y));
  86. that.ctx.lineTo(Math.floor(point2.X), Math.floor(point2.Y));
  87. // console.log('开始渲染地址', point1, point2);
  88. that.ctx.setStrokeStyle('#333333');
  89. that.ctx.setLineWidth(20);
  90. that.ctx.stroke();
  91. that.ctx.draw(true);
  92. },
  93. clear: function() {
  94. let obj = this;
  95. uni.getSystemInfo({
  96. success: function(res) {
  97. let canvasw = res.windowWidth;
  98. let canvash = res.windowHeight;
  99. obj.ctx.clearRect(0, 0, canvasw, canvash);
  100. obj.ctx.draw(true);
  101. }
  102. })
  103. this.showtext = true;
  104. },
  105. submit(){
  106. let obj = this;
  107. console.log(this.type);
  108. uni.canvasToTempFilePath({
  109. canvasId: 'mycanvas',
  110. success: function(res) {
  111. console.log(res,'------');
  112. uni.setStorageSync('mycanvas',res.tempFilePath);
  113. uni.navigateTo({
  114. url: '/pages/contract/Qrcontract?type=' + obj.type
  115. })
  116. }
  117. })
  118. }
  119. },
  120. }
  121. </script>
  122. <style lang="scss">
  123. page{
  124. height: 100%;
  125. }
  126. .canvas {
  127. text-align: center;
  128. line-height: 700rpx;
  129. width: 100%;
  130. height: 700rpx;
  131. border: 1rpx solid #f0f0f0;
  132. }
  133. .btn {
  134. position: absolute;
  135. left: 0;
  136. right: 0;
  137. bottom: 0;
  138. width: 100%;
  139. justify-content: space-between;
  140. padding: 53rpx;
  141. margin-bottom: 0;
  142. .button {
  143. width: 100rpx;
  144. height: 40rpx;
  145. display: flex;
  146. align-items: center;
  147. justify-content: space-around;
  148. border: 1rpx solid #FE4242;
  149. background: #FFFFFF;
  150. border-radius: 37rpx;
  151. line-height: 40rpx;
  152. text-align: center;
  153. .text {
  154. line-height: 40rpx;
  155. font-size: 24rpx;
  156. font-family: PingFang SC;
  157. font-weight: 500;
  158. color: #FF0000;
  159. }
  160. }
  161. }
  162. </style>