sign.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.setStrokeStyle('#000000');
  56. context.setLineWidth(4);
  57. context.setLineCap('round');
  58. context.setLineJoin('round');
  59. context.stroke();
  60. context.draw(false);
  61. },
  62. // 点击保存图片
  63. clickMe: function () {
  64. wx.showToast({
  65. title: '签名生成中...',
  66. icon: 'loading',
  67. duration: 2000
  68. });
  69. setTimeout(function () {
  70. context.draw(true, wx.canvasToTempFilePath({
  71. canvasId: 'canvas',
  72. fileType: 'png',
  73. //quality:0.5,
  74. success: function (res) {
  75. //console.log(res);
  76. let pages = getCurrentPages();
  77. let prevPage = pages[pages.length - 2]; //首页
  78. //console.log(pages);
  79. //console.log(prevPage);
  80. var signs = [res.tempFilePath];
  81. // 限制最多只能留下5张照片
  82. prevPage.setData({
  83. signs
  84. });
  85. wx.navigateBack();
  86. // //存入服务器
  87. // wx.uploadFile({
  88. // url: 'a.php', //接口地址
  89. // filePath: res.tempFilePath,
  90. // name: 'file',
  91. // formData: { //HTTP 请求中其他额外的 form data
  92. // 'user': 'test'
  93. // },
  94. // success: function (res) {
  95. // console.log(res);
  96. // },
  97. // fail: function (res) {
  98. // console.log(res);
  99. // },
  100. // complete: function (res) {
  101. // }
  102. // });
  103. // wx.saveImageToPhotosAlbum({
  104. // filePath: res.tempFilePath,
  105. // success(res) {
  106. // console.log(res)
  107. // wx.hideLoading();
  108. // wx.showToast({
  109. // title: '保存成功',
  110. // });
  111. // },
  112. // fail() {
  113. // wx.hideLoading()
  114. // }
  115. // });
  116. }
  117. }));
  118. }, 2000);
  119. },
  120. canvasEnd: function (event) {
  121. isButtonDown = false;
  122. },
  123. cleardraw: function () {
  124. //清除画布
  125. arrx = [];
  126. arry = [];
  127. arrz = [];
  128. context.draw(false);
  129. },
  130. goWorksheet(e) {
  131. wx.navigateBack();
  132. },
  133. onShow(e) {
  134. if (context) {
  135. this.cleardraw();
  136. }
  137. }
  138. })