qianming.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var content = null;
  2. var touchs = [];
  3. var canvasw = 0;
  4. var canvash = 0;
  5. var that = null;
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. logId : null
  12. },
  13. // 画布的触摸移动开始手势响应
  14. start: function (event) {
  15. // console.log("触摸开始" + event.changedTouches[0].y)
  16. // console.log("触摸开始" + event.changedTouches[0].x)
  17. //获取触摸开始的 x,y
  18. let point = {
  19. x: event.changedTouches[0].y,
  20. y: event.changedTouches[0].x
  21. }
  22. touchs.push(point)
  23. },
  24. // 画布的触摸移动手势响应
  25. move: function (e) {
  26. let point = {
  27. x: e.touches[0].y,
  28. y: e.touches[0].x
  29. }
  30. touchs.push(point)
  31. if (touchs.length >= 2) {
  32. this.draw(touchs)
  33. }
  34. },
  35. // 画布的触摸移动结束手势响应
  36. end: function (e) {
  37. // console.log("触摸结束" + e)
  38. //清空轨迹数组
  39. for (let i = 0; i < touchs.length; i++) {
  40. touchs.pop()
  41. }
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad: function (options) {
  47. this.data.logId = options.logId
  48. that = this
  49. //获得Canvas的上下文
  50. content = wx.createCanvasContext('firstCanvas')
  51. //设置线的颜色
  52. content.setStrokeStyle("#000")
  53. //设置线的宽度
  54. content.setLineWidth(5)
  55. //设置线两端端点样式更加圆润
  56. content.setLineCap('round')
  57. //设置两条线连接处更加圆润
  58. content.setLineJoin('round')
  59. },
  60. /**
  61. * 生命周期函数--监听页面初次渲染完成
  62. */
  63. onReady: function () {
  64. // 获取画布尺寸
  65. var query = wx.createSelectorQuery()
  66. query.select('#canvas').boundingClientRect()
  67. query.exec(function (res) {
  68. canvasw = res[0].width;
  69. canvash = res[0].height
  70. })
  71. },
  72. //绘制
  73. draw: function (touchs) {
  74. let point1 = touchs[0]
  75. let point2 = touchs[1]
  76. touchs.shift()
  77. content.moveTo(point1.y, point1.x)
  78. content.lineTo(point2.y, point2.x)
  79. content.stroke()
  80. content.draw(true)
  81. },
  82. //清除操作
  83. clearClick: function () {
  84. //清除画布
  85. content.clearRect(0, 0, canvasw, canvash)
  86. content.draw(true)
  87. },
  88. //保存图片
  89. saveClick: function () {
  90. var that = this
  91. wx.canvasToTempFilePath({
  92. canvasId: 'firstCanvas',
  93. success: function (res) {
  94. //打印图片路径
  95. var path = res.tempFilePath
  96. //上传图片
  97. that.uploadSignPic(path)
  98. console.log(path)
  99. }
  100. })
  101. },
  102. /**
  103. * 上传签名图片
  104. */
  105. uploadSignPic: function (path) {
  106. //具体实现的业务逻辑
  107. }
  108. })