q-sign.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="root-view">
  3. <view class="wrapper">
  4. <view class="handCenter">
  5. <canvas class="handWriting" :disable-scroll="true" @touchstart="uploadScaleStart"
  6. @touchmove="uploadScaleMove" canvas-id="handWriting"></canvas>
  7. <canvas canvas-id="protocol" style="width: 595px;height: 842px; position:fixed;left:100%;"></canvas>
  8. </view>
  9. <view class="btn-box">
  10. <view @click="retDraw" class="btn delBtn">重写</view>
  11. <view @click="saveCanvasAsImg" class="btn saveBtn">保存</view>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import { requestApi , requestFileApi } from '@/api/request.js';
  18. export default {
  19. data() {
  20. return {
  21. canvasName: 'handWriting',
  22. ctx: '',
  23. ctx2: '',
  24. startX: null,
  25. startY: null,
  26. startX2: null,
  27. startY2: null,
  28. canvasWidth: 0,
  29. canvasHeight: 0,
  30. selectColor: 'black',
  31. lineColor: '#1A1A1A', // 颜色
  32. lineSize: 5, // 笔记倍数
  33. name:'', //用来区分多个签字
  34. protocolText:'',
  35. };
  36. },
  37. onLoad({name = ''}) {
  38. // console.log('name',name);
  39. this.name = name
  40. this.ctx = uni.createCanvasContext("handWriting");
  41. this.ctx2 = uni.createCanvasContext("protocol", this);
  42. const getProtocol = requestApi(
  43. '/admin/borrow.BorrowApply/getProtocol?server=1','GET'
  44. ).then(ret => {
  45. console.log(ret)
  46. if(ret.code == 1){
  47. this.protocolText = ret.data.data1;
  48. this.setCanvasP();
  49. }
  50. })
  51. this.$nextTick(() => {
  52. uni.createSelectorQuery().select('.handCenter').boundingClientRect(rect => {
  53. this.canvasWidth = rect.width;
  54. this.canvasHeight = rect.height;
  55. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  56. this.setCanvasBg('#fff');
  57. })
  58. .exec();
  59. });
  60. },
  61. methods: {
  62. // 笔迹开始
  63. uploadScaleStart(e) {
  64. this.startX = e.changedTouches[0].x
  65. this.startY = e.changedTouches[0].y
  66. //设置画笔参数
  67. //画笔颜色
  68. this.ctx.setStrokeStyle(this.lineColor)
  69. //设置线条粗细
  70. this.ctx.setLineWidth(this.lineSize)
  71. //设置线条的结束端点样式
  72. this.ctx.setLineCap("round") //'butt'、'round'、'square'
  73. //开始画笔
  74. this.ctx.beginPath()
  75. this.startX2 = e.changedTouches[0].x
  76. this.startY2 = e.changedTouches[0].y + 400
  77. this.ctx2.setStrokeStyle(this.lineColor)
  78. //设置线条粗细
  79. this.ctx2.setLineWidth(this.lineSize)
  80. //设置线条的结束端点样式
  81. this.ctx2.setLineCap("round") //'butt'、'round'、'square'
  82. //开始画笔
  83. this.ctx2.beginPath()
  84. },
  85. // 笔迹移动
  86. uploadScaleMove(e) {
  87. //取点
  88. let temX = e.changedTouches[0].x
  89. let temY = e.changedTouches[0].y
  90. //画线条
  91. this.ctx.moveTo(this.startX, this.startY)
  92. this.ctx.lineTo(temX, temY)
  93. this.ctx.stroke()
  94. this.startX = temX
  95. this.startY = temY
  96. this.ctx.draw(true)
  97. let temX2 = e.changedTouches[0].x
  98. let temY2 = e.changedTouches[0].y + 400
  99. //画线条
  100. this.ctx2.moveTo(this.startX2, this.startY2)
  101. this.ctx2.lineTo(temX2, temY2)
  102. this.ctx2.stroke()
  103. this.startX2 = temX2
  104. this.startY2 = temY2
  105. this.ctx2.draw(true)
  106. },
  107. /**
  108. * 重写
  109. */
  110. retDraw() {
  111. this.ctx.clearRect(0, 0, 700, 730);
  112. this.ctx.draw();
  113. //设置canvas背景
  114. this.setCanvasBg('#fff');
  115. this.ctx2.clearRect(0, 0, 700, 730);
  116. this.ctx2.draw();
  117. this.setCanvasP()
  118. },
  119. //生成图片
  120. saveCanvasAsImg() {
  121. if(!this.startX || !this.startY){
  122. uni.showToast({
  123. title:'请签字',
  124. icon:'none'
  125. })
  126. return
  127. }
  128. uni.canvasToTempFilePath({
  129. canvasId: 'protocol',
  130. fileType: 'png',
  131. destWidth: 595,
  132. destHeight: 842,
  133. quality: 0.001, //图片质量
  134. success:(res)=> {
  135. console.log(res.tempFilePath)
  136. uni.$emit('q-sign',{name:this.name,tempFilePath:res.tempFilePath})
  137. uni.navigateBack({
  138. delta:1
  139. })
  140. }
  141. });
  142. // uni.canvasToTempFilePath({
  143. // canvasId: 'handWriting',
  144. // fileType: 'png',
  145. // quality: 1, //图片质量
  146. // success:(res)=> {
  147. // // console.log(res.tempFilePath)
  148. // // uni.$emit('q-sign',{name:this.name,tempFilePath:res.tempFilePath})
  149. // // uni.navigateBack({
  150. // // delta:1
  151. // // })
  152. // }
  153. // });
  154. },
  155. //设置canvas背景色 不设置 导出的canvas的背景为透明
  156. //@params:字符串 color
  157. setCanvasBg(color) {
  158. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  159. //rect() 参数说明 矩形路径左上角的横坐标,左上角的纵坐标, 矩形路径的宽度, 矩形路径的高度
  160. //这里是 canvasHeight - 4 是因为下边盖住边框了,所以手动减了写
  161. this.ctx.rect(0, 0, this.canvasWidth, this.canvasHeight - 4);
  162. // ctx.setFillStyle('red')
  163. this.ctx.setFillStyle(color);
  164. this.ctx.fill(); //设置填充
  165. this.ctx.draw(); //开画
  166. },
  167. //设置canvas背景色 不设置 导出的canvas的背景为透明
  168. //@params:字符串 color
  169. setCanvasP(color) {
  170. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  171. //rect() 参数说明 矩形路径左上角的横坐标,左上角的纵坐标, 矩形路径的宽度, 矩形路径的高度
  172. //这里是 canvasHeight - 4 是因为下边盖住边框了,所以手动减了写
  173. this.ctx2.rect(0, 0, 595, 842);
  174. // ctx.setFillStyle('red')
  175. this.ctx2.setFillStyle('#fff');
  176. this.ctx2.fill(); //设置填充
  177. const text = this.protocolText;
  178. // 设置Canvas属性
  179. this.ctx2.font = '20px Microsoft YaHei'; // 设置字体大小以模拟打印效果
  180. this.ctx2.textAlign = 'left';
  181. this.ctx2.fillStyle = '#000000';
  182. this.ctx2.fillText("桂林理工大学测绘地理信息学院仪器借用承诺书",83, 50);
  183. this.ctx2.font = '15px Microsoft YaHei'; // 设置字体大小以模拟打印效果
  184. var strArr = [];
  185. for (let i = 0; i <text.length ; i++) {
  186. strArr.push(text.slice(i,i+1))
  187. }
  188. let line = '';
  189. let y = 120;
  190. strArr.forEach(char => {
  191. const testLine = line + char;
  192. const metrics = this.ctx2.measureText(testLine);
  193. const testWidth = metrics.width;
  194. // console.log(testLine,metrics.width,ctx.width)
  195. if (testWidth + 24 > 555) { // 减去左右边距
  196. if (line.trim() === '') {
  197. // 如果当前行没有内容,直接添加字符
  198. line += char;
  199. } else {
  200. // 否则,绘制当前行并开始新行
  201. this.ctx2.fillText(line, 33, y);
  202. line = char;
  203. y += 20; // 行间距
  204. }
  205. } else {
  206. line += char;
  207. }
  208. });
  209. if (line.trim() !== '') {
  210. this.ctx2.fillText(line, 33, y);
  211. }
  212. this.ctx2.draw(true, () => {
  213. console.log(123)
  214. uni.canvasToTempFilePath({
  215. x: 0,
  216. y: 0,
  217. width: 595,
  218. height: 842,
  219. destWidth: 595,
  220. destHeight: 842,
  221. canvasId: 'myCanvasP',
  222. success: function(res) {
  223. console.log(res.tempFilePath);
  224. // 假设uni.$emit是您项目中用于事件通信的方法
  225. uni.$emit('qing', { tempFilePath: res.tempFilePath });
  226. },
  227. error:function(res) {
  228. console.log(res);
  229. // 假设uni.$emit是您项目中用于事件通信的方法
  230. // uni.$emit('qing', { tempFilePath: res.tempFilePath });
  231. },
  232. },this);
  233. });
  234. }
  235. }
  236. };
  237. </script>
  238. <style>
  239. .root-view {
  240. background: #f6f6f6;
  241. height: calc(100vh - 44px);
  242. display: flex;
  243. /* align-items: center; */
  244. justify-content: center;
  245. padding: 27px;
  246. }
  247. .btn-box{
  248. display: flex;
  249. justify-content: flex-end;
  250. align-items: center;
  251. }
  252. .btn{
  253. width: 177rpx;
  254. height: 65rpx;
  255. border-radius: 20rpx;
  256. background: #fff;
  257. line-height: 65rpx;
  258. text-align: center;
  259. font-weight: 500;
  260. font-size: 24rpx;
  261. }
  262. .delBtn{
  263. border: 1rpx solid #3238ec;
  264. color: #333333;
  265. margin-right: 32rpx;
  266. }
  267. .saveBtn{
  268. background: #3238ec;
  269. color: #ffffff;
  270. }
  271. .handWriting {
  272. background: #fff;
  273. width: 680rpx;
  274. height: 783rpx;
  275. border-radius: 20rpx;
  276. }
  277. .handCenter{
  278. margin-bottom: 36rpx;
  279. }
  280. </style>