qianming.js 2.8 KB

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