signatureBoard.vue 6.8 KB

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