screenshotsPrint

Modified from https://www.jianshu.com/p/8c43576bdbe3

目前为 2023-04-27 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/464944/1182064/screenshotsPrint.js

  1. /**
  2. * 默认画笔线宽
  3. * @type {number}
  4. */
  5. var defaultStrokeWidth = 1; //画矩形选取框的线宽
  6.  
  7. /**
  8. * 选取划线的canvasExt
  9. * @type {{drawRect: canvasExt.drawRect}}
  10. */
  11. var canvasExt = {
  12. /**
  13. * 画矩形
  14. * @param canvasId canvasId
  15. * @param penColor 画笔颜色
  16. * @param strokeWidth 线宽
  17. */
  18. drawRect: function (canvasId, penColor, strokeWidth) {
  19. var that = this;
  20.  
  21. that.penColor = penColor;
  22. that.penWidth = strokeWidth;
  23. var canvas = document.getElementById(canvasId);
  24. //canvas 的矩形框
  25. var canvasRect = canvas.getBoundingClientRect();
  26. //canvas 矩形框的左上角坐标
  27. var canvasLeft = canvasRect.left;
  28. var canvasTop = canvasRect.top;
  29.  
  30. // 要画的矩形的起点 xy
  31. var x = 0;
  32. var y = 0;
  33.  
  34. //鼠标点击按下事件,画图准备
  35. canvas.onmousedown = function (e) {
  36.  
  37. //设置画笔颜色和宽度
  38. var color = that.penColor;
  39. var penWidth = that.penWidth;
  40. // 确定起点
  41. x = e.clientX - canvasLeft;
  42. y = e.clientY - canvasTop;
  43. // 添加layer
  44. $("#" + canvasId).addLayer({
  45. type: 'rectangle',
  46. strokeStyle: color,
  47. strokeWidth: penWidth,
  48. name: 'areaLayer',
  49. fromCenter: false,
  50. x: x, y: y,
  51. width: 1,
  52. height: 1
  53. });
  54. // 绘制
  55. $("#" + canvasId).drawLayers();
  56. $("#" + canvasId).saveCanvas();
  57.  
  58. //鼠标移动事件,画图
  59. canvas.onmousemove = function (e) {
  60.  
  61. // 要画的矩形的宽高
  62. var width = e.clientX - canvasLeft - x;
  63. var height = e.clientY - canvasTop - y;
  64.  
  65. // 清除之前画的
  66. $("#" + canvasId).removeLayer('areaLayer');
  67.  
  68. $("#" + canvasId).addLayer({
  69. type: 'rectangle',
  70. strokeStyle: color,
  71. strokeWidth: penWidth,
  72. name: 'areaLayer',
  73. fromCenter: false,
  74. x: x, y: y,
  75. width: width,
  76. height: height
  77. });
  78.  
  79. $("#" + canvasId).drawLayers();
  80. }
  81. };
  82. //鼠标抬起
  83. canvas.onmouseup = function (e) {
  84.  
  85. var color = that.penColor;
  86. var penWidth = that.penWidth;
  87.  
  88. canvas.onmousemove = null;
  89.  
  90. var width = e.clientX - canvasLeft - x;
  91. var height = e.clientY - canvasTop - y;
  92.  
  93. $("#" + canvasId).removeLayer('areaLayer');
  94.  
  95. $("#" + canvasId).addLayer({
  96. type: 'rectangle',
  97. strokeStyle: color,
  98. strokeWidth: penWidth,
  99. name: 'areaLayer',
  100. fromCenter: false,
  101. x: x, y: y,
  102. width: width,
  103. height: height
  104. });
  105.  
  106. $("#" + canvasId).drawLayers();
  107. $("#" + canvasId).saveCanvas();
  108.  
  109. // 把body转成canvas
  110. html2canvas(document.body, {
  111. scale: 1,
  112. // allowTaint: true,
  113. useCORS: true, //跨域使用
  114. // foreignObjectRendering: true
  115. }).then(canvas => {
  116. var capture_x, capture_y
  117. if (width > 0) {
  118. //从左往右画
  119. capture_x = x + that.penWidth
  120. } else {
  121. //从右往左画
  122. capture_x = x + width + that.penWidth
  123. }
  124. if (height > 0) {
  125. //从上往下画
  126. capture_y = y + that.penWidth
  127. } else {
  128. //从下往上画
  129. capture_y = y + height + that.penWidth
  130. }
  131. printClip(canvas, capture_x, capture_y, Math.abs(width), Math.abs(height))
  132. });
  133. // 移除画的选取框
  134. $("#" + canvasId).removeLayer('areaLayer');
  135. // 隐藏用于华画取框的canvas
  136. $("#" + canvasId).hide()
  137. }
  138. }
  139. };
  140.  
  141. /**
  142. * 选取截屏
  143. * @param canvasId
  144. */
  145. function clipScreenshots(canvasId) {
  146. canvasExt.drawRect(canvasId, "red", defaultStrokeWidth);
  147. }
  148.  
  149. /**
  150. * 打印截取区域
  151. * @param canvas 截取的canvas
  152. * @param capture_x 截取的起点x
  153. * @param capture_y 截取的起点y
  154. * @param capture_width 截取的起点宽
  155. * @param capture_height 截取的起点高
  156. */
  157. function printClip(canvas, capture_x, capture_y, capture_width, capture_height) {
  158. // 创建一个用于截取的canvas
  159. var clipCanvas = document.createElement('canvas')
  160. clipCanvas.width = capture_width
  161. clipCanvas.height = capture_height
  162. // 截取
  163. clipCanvas.getContext('2d').drawImage(canvas, capture_x, capture_y, capture_width, capture_height, 0, 0, capture_width, capture_height)
  164. var clipImgBase64 = clipCanvas.toDataURL()
  165. // // 生成图片
  166. // var clipImg = new Image()
  167. // clipImg.src = clipImgBase64
  168. // var con = confirm('打印截图吗?取消则保存截图')
  169. // if (con) {
  170. // $(clipImg).print()
  171. // } else {
  172. // downloadIamge(clipImgBase64)
  173. // }
  174.  
  175. // alert二维码解析内容
  176. const qrcode = new QRCode.Decoder();
  177. qrcode
  178. .scan(clipImgBase64)
  179. .then(result => {
  180. console.log(result.data);
  181. alert(result.data)
  182. })
  183. .catch(error => {
  184. console.error(error);
  185. alert("出错:" + error)
  186. });
  187. }
  188.  
  189. /**
  190. * 下载保存图片
  191. * @param imgUrl 图片地址
  192. */
  193. function downloadIamge(imgUrl) {
  194. // 图片保存有很多方式,这里使用了一种投机的简单方法。
  195. // 生成一个a元素
  196. var a = document.createElement('a')
  197. // 创建一个单击事件
  198. var event = new MouseEvent('click')
  199. // 生成文件名称
  200. var timestamp = new Date().getTime();
  201. var name = imgUrl.substring(22, 30) + timestamp + '.png';
  202. a.download = name
  203. // 将生成的URL设置为a.href属性
  204. a.href = imgUrl;
  205. // 触发a的单击事件 开始下载
  206. a.dispatchEvent(event);
  207. }
  208.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址