sign.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // pages/sign/sign.js
  2. var context = null;
  3. var isButtonDown = false;
  4. var arrx = [];
  5. var arry = [];
  6. var arrz = [];
  7. var canvasw = 0;
  8. var canvash = 0;
  9. //注册页面
  10. Page({
  11. canvasIdErrorCallback: function (e) {
  12. console.error(e.detail.errMsg)
  13. },
  14. //开始
  15. canvasStart: function (event) {
  16. isButtonDown = true;
  17. arrz.push(0);
  18. arrx.push(event.changedTouches[0].x);
  19. arry.push(event.changedTouches[0].y);
  20. },
  21. data: {
  22. src: "",
  23. img: "",
  24. rpx: ''
  25. },
  26. onLoad: function (options) {
  27. var that = this
  28. // 使用 wx.createContext 获取绘图上下文 context
  29. context = wx.createCanvasContext('canvas');
  30. context.beginPath()
  31. context.setStrokeStyle('#000000');
  32. context.setLineWidth(4);
  33. context.setLineCap('round');
  34. context.setLineJoin('round');
  35. // context.drawImage('../../images/img111.png', 0, 0, canvasw, 500);
  36. context.draw(false);
  37. },
  38. //过程
  39. canvasMove: function (event) {
  40. var that = this
  41. if (isButtonDown) {
  42. arrz.push(1);
  43. //console.log(event)
  44. arrx.push(event.changedTouches[0].x);
  45. arry.push(event.changedTouches[0].y);
  46. };
  47. for (var i = 0; i < arrx.length; i++) {
  48. if (arrz[i] == 0) {
  49. context.moveTo(arrx[i], arry[i])
  50. } else {
  51. context.lineTo(arrx[i], arry[i])
  52. };
  53. };
  54. context.clearRect(0, 0, canvasw, canvash);
  55. context.setFillStyle("#FFF")
  56. context.fillRect(0,0,900,900)
  57. context.setStrokeStyle('#000000');
  58. context.setLineWidth(4);
  59. context.setLineCap('round');
  60. context.setLineJoin('round');
  61. context.stroke();
  62. context.draw(false);
  63. },
  64. // 点击保存图片
  65. clickMe: function () {
  66. wx.showToast({
  67. title: '签名生成中...',
  68. icon: 'loading',
  69. duration: 2000
  70. });
  71. setTimeout(function () {
  72. context.draw(true, wx.canvasToTempFilePath({
  73. canvasId: 'canvas',
  74. fileType: 'png',
  75. //quality:0.5,
  76. success: function (res) {
  77. //console.log(res);
  78. let pages = getCurrentPages();
  79. let prevPage = pages[pages.length - 2]; //首页
  80. //console.log(pages);
  81. //console.log(prevPage);
  82. var signs = [res.tempFilePath];
  83. // 限制最多只能留下5张照片
  84. prevPage.setData({
  85. signs
  86. });
  87. wx.navigateBack();
  88. // //存入服务器
  89. // wx.uploadFile({
  90. // url: 'a.php', //接口地址
  91. // filePath: res.tempFilePath,
  92. // name: 'file',
  93. // formData: { //HTTP 请求中其他额外的 form data
  94. // 'user': 'test'
  95. // },
  96. // success: function (res) {
  97. // console.log(res);
  98. // },
  99. // fail: function (res) {
  100. // console.log(res);
  101. // },
  102. // complete: function (res) {
  103. // }
  104. // });
  105. // wx.saveImageToPhotosAlbum({
  106. // filePath: res.tempFilePath,
  107. // success(res) {
  108. // console.log(res)
  109. // wx.hideLoading();
  110. // wx.showToast({
  111. // title: '保存成功',
  112. // });
  113. // },
  114. // fail() {
  115. // wx.hideLoading()
  116. // }
  117. // });
  118. }
  119. }));
  120. }, 2000);
  121. },
  122. canvasEnd: function (event) {
  123. isButtonDown = false;
  124. },
  125. cleardraw: function () {
  126. //清除画布
  127. arrx = [];
  128. arry = [];
  129. arrz = [];
  130. context.draw(false);
  131. },
  132. goWorksheet(e) {
  133. wx.navigateBack();
  134. },
  135. onShow(e) {
  136. if (context) {
  137. this.cleardraw();
  138. }
  139. }
  140. })