q-sign.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. },
  116. //生成图片
  117. saveCanvasAsImg() {
  118. if(!this.startX || !this.startY){
  119. uni.showToast({
  120. title:'请签字',
  121. icon:'none'
  122. })
  123. return
  124. }
  125. uni.canvasToTempFilePath({
  126. canvasId: 'protocol',
  127. fileType: 'png',
  128. destWidth: 595,
  129. destHeight: 842,
  130. quality: 0.001, //图片质量
  131. success:(res)=> {
  132. console.log(res.tempFilePath)
  133. uni.$emit('q-sign',{name:this.name,tempFilePath:res.tempFilePath})
  134. uni.navigateBack({
  135. delta:1
  136. })
  137. }
  138. });
  139. // uni.canvasToTempFilePath({
  140. // canvasId: 'handWriting',
  141. // fileType: 'png',
  142. // quality: 1, //图片质量
  143. // success:(res)=> {
  144. // // console.log(res.tempFilePath)
  145. // // uni.$emit('q-sign',{name:this.name,tempFilePath:res.tempFilePath})
  146. // // uni.navigateBack({
  147. // // delta:1
  148. // // })
  149. // }
  150. // });
  151. },
  152. //设置canvas背景色 不设置 导出的canvas的背景为透明
  153. //@params:字符串 color
  154. setCanvasBg(color) {
  155. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  156. //rect() 参数说明 矩形路径左上角的横坐标,左上角的纵坐标, 矩形路径的宽度, 矩形路径的高度
  157. //这里是 canvasHeight - 4 是因为下边盖住边框了,所以手动减了写
  158. this.ctx.rect(0, 0, this.canvasWidth, this.canvasHeight - 4);
  159. // ctx.setFillStyle('red')
  160. this.ctx.setFillStyle(color);
  161. this.ctx.fill(); //设置填充
  162. this.ctx.draw(); //开画
  163. },
  164. //设置canvas背景色 不设置 导出的canvas的背景为透明
  165. //@params:字符串 color
  166. setCanvasP(color) {
  167. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  168. //rect() 参数说明 矩形路径左上角的横坐标,左上角的纵坐标, 矩形路径的宽度, 矩形路径的高度
  169. //这里是 canvasHeight - 4 是因为下边盖住边框了,所以手动减了写
  170. this.ctx2.rect(0, 0, 595, 842);
  171. // ctx.setFillStyle('red')
  172. this.ctx2.setFillStyle('#fff');
  173. this.ctx2.fill(); //设置填充
  174. const text = this.protocolText;
  175. // 设置Canvas属性
  176. this.ctx2.font = '20px Microsoft YaHei'; // 设置字体大小以模拟打印效果
  177. this.ctx2.textAlign = 'left';
  178. this.ctx2.fillStyle = '#000000';
  179. this.ctx2.fillText("桂林理工大学测绘地理信息学院仪器借用承诺书",83, 50);
  180. this.ctx2.font = '15px Microsoft YaHei'; // 设置字体大小以模拟打印效果
  181. var strArr = [];
  182. for (let i = 0; i <text.length ; i++) {
  183. strArr.push(text.slice(i,i+1))
  184. }
  185. let line = '';
  186. let y = 120;
  187. strArr.forEach(char => {
  188. const testLine = line + char;
  189. const metrics = this.ctx2.measureText(testLine);
  190. const testWidth = metrics.width;
  191. // console.log(testLine,metrics.width,ctx.width)
  192. if (testWidth + 24 > 555) { // 减去左右边距
  193. if (line.trim() === '') {
  194. // 如果当前行没有内容,直接添加字符
  195. line += char;
  196. } else {
  197. // 否则,绘制当前行并开始新行
  198. this.ctx2.fillText(line, 33, y);
  199. line = char;
  200. y += 20; // 行间距
  201. }
  202. } else {
  203. line += char;
  204. }
  205. });
  206. if (line.trim() !== '') {
  207. this.ctx2.fillText(line, 33, y);
  208. }
  209. this.ctx2.draw(true, () => {
  210. console.log(123)
  211. uni.canvasToTempFilePath({
  212. x: 0,
  213. y: 0,
  214. width: 595,
  215. height: 842,
  216. destWidth: 595,
  217. destHeight: 842,
  218. canvasId: 'myCanvasP',
  219. success: function(res) {
  220. console.log(res.tempFilePath);
  221. // 假设uni.$emit是您项目中用于事件通信的方法
  222. uni.$emit('qing', { tempFilePath: res.tempFilePath });
  223. },
  224. error:function(res) {
  225. console.log(res);
  226. // 假设uni.$emit是您项目中用于事件通信的方法
  227. // uni.$emit('qing', { tempFilePath: res.tempFilePath });
  228. },
  229. },this);
  230. });
  231. }
  232. }
  233. };
  234. </script>
  235. <style>
  236. .root-view {
  237. background: #f6f6f6;
  238. height: calc(100vh - 44px);
  239. display: flex;
  240. /* align-items: center; */
  241. justify-content: center;
  242. padding: 27px;
  243. }
  244. .btn-box{
  245. display: flex;
  246. justify-content: flex-end;
  247. align-items: center;
  248. }
  249. .btn{
  250. width: 177rpx;
  251. height: 65rpx;
  252. border-radius: 20rpx;
  253. background: #fff;
  254. line-height: 65rpx;
  255. text-align: center;
  256. font-weight: 500;
  257. font-size: 24rpx;
  258. }
  259. .delBtn{
  260. border: 1rpx solid #3238ec;
  261. color: #333333;
  262. margin-right: 32rpx;
  263. }
  264. .saveBtn{
  265. background: #3238ec;
  266. color: #ffffff;
  267. }
  268. .handWriting {
  269. background: #fff;
  270. width: 680rpx;
  271. height: 783rpx;
  272. border-radius: 20rpx;
  273. }
  274. .handCenter{
  275. margin-bottom: 36rpx;
  276. }
  277. </style>