qianming.js 2.8 KB

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