signatureBoard.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <view>
  3. <view class="sign-content">
  4. <view>
  5. <view class="reset" @click="backClick" style="position:fixed;bottom: 466rpx;background:#fff;">返回</view>
  6. <view class="reset" @click="clearClick" style="position:fixed;bottom: 266rpx;background:#fff;">重写</view>
  7. <view class="reset" @click="saveClick"
  8. style="position:fixed;bottom: 66rpx;background: #39b54a;color:#fff;">确定</view>
  9. </view>
  10. <canvas class="sign" canvas-id="sign" @touchmove="move" @touchstart="start" @touchend="end"
  11. @touchcancel="cancel" @longtap="tap" disable-scroll="true" @error="error"></canvas>
  12. <view>
  13. <view
  14. style="transform: rotate(90deg);text-align: center;position:fixed;width:130rpx;right:-14rpx;top:46%;">
  15. 签名板</view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. let content = null;
  22. let touchs = [];
  23. export default {
  24. data() {
  25. return {
  26. isMove: false,
  27. createCanvas: false,
  28. outW: 0,
  29. outH: 0,
  30. windowsH: 0,
  31. windowsW: 0
  32. };
  33. },
  34. onLoad: function(options) {
  35. //获得Canvas的上下文
  36. content = uni.createCanvasContext('sign');
  37. this.windowsH = uni.getSystemInfoSync().windowHeight;
  38. this.windowsW = uni.getSystemInfoSync().windowWidth;
  39. //设置线的颜色
  40. content.setStrokeStyle('#000000');
  41. //设置线的宽度
  42. content.setLineWidth(5);
  43. //设置线两端端点样式更加圆润
  44. content.setLineCap('round');
  45. //设置两条线连接处更加圆润
  46. content.setLineJoin('round');
  47. // content.setFillStyle('white')
  48. // content.fillRect(0, 0, 750, 700)
  49. // content.draw()
  50. },
  51. methods: {
  52. backClick: function() {
  53. uni.navigateBack({
  54. delta: 1
  55. });
  56. },
  57. // 画布的触摸移动开始手势响应
  58. start: function(event) {
  59. // console.log("触摸开始" + event.changedTouches[0].x);
  60. // console.log("触摸开始" + event.changedTouches[0].y);
  61. //获取触摸开始的 x,y
  62. let point = {
  63. x: event.changedTouches[0].x,
  64. y: event.changedTouches[0].y
  65. };
  66. touchs.push(point);
  67. },
  68. // 画布的触摸移动手势响应
  69. move: function(e) {
  70. let point = {
  71. x: e.touches[0].x,
  72. y: e.touches[0].y
  73. };
  74. touchs.push(point);
  75. if (touchs.length >= 2) {
  76. this.isMove = true;
  77. this.draw(touchs);
  78. }
  79. },
  80. // 画布的触摸移动结束手势响应
  81. end: function(e) {
  82. console.log('触摸结束' + e);
  83. //清空轨迹数组
  84. for (let i = 0; i < touchs.length; i++) {
  85. touchs.pop();
  86. }
  87. },
  88. // 画布的触摸取消响应
  89. cancel: function(e) {
  90. console.log('触摸取消' + e);
  91. },
  92. // 画布的长按手势响应
  93. tap: function(e) {
  94. console.log('长按手势' + e);
  95. },
  96. error: function(e) {
  97. console.log('画布触摸错误' + e);
  98. },
  99. //绘制
  100. draw: function(touchs) {
  101. let point1 = touchs[0];
  102. let point2 = touchs[1];
  103. touchs.shift();
  104. content.moveTo(point1.x, point1.y);
  105. content.lineTo(point2.x, point2.y);
  106. content.stroke();
  107. content.draw(true);
  108. },
  109. //清除操作
  110. clearClick: function() {
  111. //清除画布
  112. content.clearRect(0, 0, this.windowsW, this.windowsH);
  113. content.draw(true);
  114. this.isMove = false;
  115. },
  116. //保存图片
  117. saveClick: function() {
  118. if (this.isMove == false) {
  119. uni.showToast({
  120. icon: 'none',
  121. title: '请绘制签名'
  122. })
  123. return
  124. }
  125. var that = this;
  126. uni.showLoading({
  127. title: '请稍等'
  128. });
  129. uni.canvasToTempFilePath({
  130. canvasId: 'sign',
  131. success: function(res) {
  132. uni.getImageInfo({
  133. src: res.tempFilePath,
  134. fail() {
  135. uni.hideLoading();
  136. uni.showToast({
  137. title: '获取图片信息失败'
  138. });
  139. },
  140. success: function(image) {
  141. //要先设置在获取 ,加载问题
  142. image.height = parseInt(image.height);
  143. image.width = parseInt(image.width);
  144. that.outW = image.width;
  145. that.outH = image.height;
  146. //修复大屏手机无法生成图片问题
  147. let maxWidth = that.windowsW - uni.upx2px(240);
  148. let base = that.outH / that.outW;
  149. if (that.outH > maxWidth) {
  150. that.outH = maxWidth;
  151. that.outW = Math.floor(that.outH / base);
  152. }
  153. content.rotate(-Math.PI / 2);
  154. content.translate(-that.outW, 0);
  155. content.drawImage(image.path, 0, 0, that.outW, that.outH);
  156. content.translate(that.outW, 0);
  157. content.rotate(Math.PI / 2);
  158. content.draw(false, () => {
  159. uni.canvasToTempFilePath({
  160. canvasId: 'sign',
  161. fileType: 'jpg',
  162. width: that.outH,
  163. height: that.outW,
  164. sdestWidth: image.height,
  165. sdestHeight: image.width,
  166. fail(res) {
  167. uni.hideLoading();
  168. uni.showToast({
  169. title: '签名失败',
  170. icon: 'none'
  171. });
  172. },
  173. success: function(resss) {
  174. uni.hideLoading();
  175. that.clearClick()
  176. uni.$emit('sign', resss
  177. .tempFilePath);
  178. uni.navigateBack();
  179. // let imgArr = [];
  180. // imgArr.push(resss.tempFilePath);
  181. // uni.previewImage({
  182. // urls: imgArr,
  183. // success(response) {
  184. // console.log(response);
  185. // }
  186. // });
  187. }
  188. });
  189. });
  190. }
  191. });
  192. },
  193. fail() {
  194. uni.hideLoading();
  195. }
  196. });
  197. }
  198. }
  199. };
  200. </script>
  201. <style lang="scss" scoped>
  202. .sign-content {
  203. display: flex;
  204. height: 100vh;
  205. background: #f1f1f1;
  206. /* #ifdef H5 */
  207. // height: calc(100vh - 44px);
  208. /* #endif */
  209. padding: 20rpx 0;
  210. box-sizing: border-box;
  211. .reset {
  212. color: #333;
  213. background-color: rgb(248, 248, 248);
  214. border: 1px solid #ddd;
  215. transform: rotate(90deg);
  216. margin-top: 20rpx;
  217. padding: 20rpx 40rpx;
  218. font-size: 30rpx;
  219. border-radius: 28rpx;
  220. border: none;
  221. }
  222. .tips {
  223. width: 600rpx;
  224. color: red;
  225. transform: rotate(90deg);
  226. height: 10px;
  227. position: fixed;
  228. left: -233rpx;
  229. top: 326rpx;
  230. /* #ifdef H5 */
  231. top: calc(326rpx + 88rpx);
  232. /* #endif */
  233. font-size: 34rpx;
  234. }
  235. .sign {
  236. flex: 1;
  237. height: 100%;
  238. margin-right: 100rpx;
  239. margin-left: 120rpx;
  240. border: 1px dashed #ddd;
  241. background-color: #fff;
  242. }
  243. }
  244. .g-btns {
  245. text-align: center;
  246. margin-top: 1rem;
  247. transform: rotate(90deg);
  248. -ms-transform: rotate(90deg);
  249. /* IE 9 */
  250. -moz-transform: rotate(90deg);
  251. /* Firefox */
  252. -webkit-transform: rotate(90deg);
  253. /* Safari 和 Chrome */
  254. -o-transform: rotate(90deg);
  255. position: absolute;
  256. top: 12rem;
  257. left: -6rem;
  258. }
  259. .g-btns {
  260. width: 7.5rem;
  261. height: 2.25rem;
  262. font-size: 0.9rem;
  263. font-weight: bold;
  264. border: none;
  265. border-radius: 1rem;
  266. }
  267. .u-reset {
  268. background: #ddd;
  269. color: #666;
  270. margin-right: 0.5rem;
  271. }
  272. .u-submit {
  273. background: #fc4949;
  274. color: #fff;
  275. margin-left: 0.5rem;
  276. }
  277. </style>