qm1.vue 3.3 KB

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