Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

view-sign.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Component({
  2. /**
  3. * 组件的初始数据
  4. */
  5. data: {
  6. tempFilePath: '',
  7. hideModal: false,
  8. hasDraw: false,
  9. canvasName: '#writeCanvas',
  10. ctx: '',
  11. canvasWidth: 0,
  12. canvasHeight: 0,
  13. startPoint: {
  14. x: 0,
  15. y: 0,
  16. },
  17. selectColor: 'black',
  18. lineColor: '#1A1A1A', // 颜色
  19. lineSize: 1, // 笔记倍数
  20. radius: 4, //画圆的半径
  21. },
  22. // 初始化画布
  23. ready() {
  24. this.setData({
  25. hideModal: false
  26. })
  27. let that = this
  28. let query = wx.createSelectorQuery().in(this);
  29. //获取自定义组件的SelectQuery对象
  30. this.canvasCtx = wx.createCanvasContext('signature', that)
  31. // 设置线的样式
  32. this.canvasCtx.setLineCap("round");
  33. this.canvasCtx.setLineJoin("round");
  34. // 初始化颜色
  35. this.canvasCtx.setStrokeStyle(that.data.selectColor);
  36. // 初始化粗细
  37. query.select('.modal-canvas').boundingClientRect(rect => {
  38. this.setData({
  39. canvasWidth: rect.width,
  40. canvasHeight: rect.height,
  41. });
  42. }).exec();
  43. },
  44. // 方法列表
  45. methods: {
  46. scaleStart(event) {
  47. if (event.type != 'touchstart') return false;
  48. let currentPoint = {
  49. x: event.touches[0].x,
  50. y: event.touches[0].y
  51. }
  52. // this.data.ctx.moveTo(currentPoint.x, currentPoint.y)
  53. this.drawCircle(currentPoint);
  54. this.setData({
  55. startPoint: currentPoint,
  56. hasDraw: true, //签字了
  57. });
  58. },
  59. mouseDown() {},
  60. scaleEnd(e) {
  61. this.setData({
  62. isStart: true
  63. })
  64. },
  65. scaleMove(event) {
  66. if (event.type != "touchmove") return false;
  67. let {
  68. startPoint
  69. } = this.data
  70. let currentPoint = {
  71. x: event.touches[0].x,
  72. y: event.touches[0].y
  73. }
  74. this.drawLine(startPoint, currentPoint)
  75. this.setData({
  76. startPoint: currentPoint
  77. })
  78. },
  79. drawCircle(point) { //这里负责点
  80. let ctx = this.canvasCtx;
  81. ctx.beginPath();
  82. ctx.setFillStyle(this.data.lineColor)
  83. //笔迹粗细由圆的大小决定
  84. ctx.arc(point.x, point.y, this.data.radius, 0, 2 * Math.PI);
  85. ctx.fill();
  86. ctx.closePath();
  87. ctx.draw(true)
  88. },
  89. drawLine(sourcePoint, targetPoint) {
  90. let ctx = this.canvasCtx;
  91. this.drawCircle(targetPoint);
  92. ctx.beginPath();
  93. ctx.setStrokeStyle(this.data.lineColor)
  94. ctx.setLineWidth(this.data.radius * 2)
  95. ctx.moveTo(sourcePoint.x, sourcePoint.y);
  96. ctx.lineTo(targetPoint.x, targetPoint.y);
  97. ctx.stroke();
  98. ctx.closePath();
  99. },
  100. clearCanvas() {
  101. //清空画布
  102. let ctx = this.canvasCtx;
  103. ctx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight);
  104. ctx.fillStyle = 'rgba(0, 0, 0, .1)';
  105. ctx.draw()
  106. this.setData({
  107. hasDraw: false //未签字
  108. })
  109. },
  110. save(cb) {
  111. let that = this
  112. let {
  113. hasDraw,
  114. } = this.data
  115. if (!hasDraw) {
  116. wx.showToast({
  117. title: '您还未签名!',
  118. icon: 'none',
  119. mask: true
  120. })
  121. return
  122. } else {
  123. let {
  124. canvasHeight,
  125. canvasWidth
  126. } = that.data;
  127. wx.canvasToTempFilePath({
  128. x: 0,
  129. y: 0,
  130. width: canvasWidth,
  131. height: canvasHeight,
  132. destWidth: 200,
  133. destHeight: 80,
  134. canvasId: 'signature',
  135. canvas: that.data.ctx,
  136. fileType: "png",
  137. success(res) {
  138. console.log(res)
  139. if (res.tempFilePath) {
  140. // 文件转base64
  141. wx.getFileSystemManager().readFile({
  142. filePath: res.tempFilePath,
  143. encoding: "binary",
  144. success: (val) => {
  145. cb && cb(val, res);
  146. that.triggerEvent("saveToImageEvent", val.data);
  147. },
  148. });
  149. }
  150. },
  151. fail(err) {
  152. console.log(err);
  153. }
  154. }, that)
  155. }
  156. },
  157. }
  158. })