drawCheckbox.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. $(function(){
  2. let canvas = $("#bg_canvas");
  3. if (canvas.length===0){
  4. let canvas = $("<canvas id='bg_canvas' style='position: absolute;z-index: 500;left: 0;top: 0;width: 100%;height: 100%'></canvas>");
  5. $("body").append(canvas);
  6. }
  7. var clientWidth = document.documentElement.clientWidth || document.body.clientWidth
  8. var clientHeight = document.documentElement.clientHeight || document.body.clientHeight
  9. // 更新canvas宽高
  10. $("#bg_canvas").attr("width", clientWidth);
  11. $("#bg_canvas").attr("height", clientHeight);
  12. $("#bg_canvas").hide();
  13. });
  14. /**
  15. * 默认画笔线宽
  16. * @type {number}
  17. */
  18. var defaultStrokeWidth = 1; //画矩形选取框的线宽
  19. var canvasExt = {
  20. /**
  21. * 画矩形
  22. * @param canvasId canvasId
  23. * @param penColor 画笔颜色
  24. * @param strokeWidth 线宽
  25. */
  26. drawRect: function (canvasId, penColor, strokeWidth) {
  27. var that = this;
  28. that.penColor = penColor;
  29. that.penWidth = strokeWidth;
  30. var canvas = document.getElementById(canvasId);
  31. //canvas 的矩形框
  32. var canvasRect = canvas.getBoundingClientRect();
  33. //canvas 矩形框的左上角坐标
  34. var canvasLeft = canvasRect.left;
  35. var canvasTop = canvasRect.top;
  36. // 要画的矩形的起点 xy
  37. var x = 0;
  38. var y = 0;
  39. //鼠标点击按下事件,画图准备
  40. canvas.onmousedown = function(e) {
  41. //设置画笔颜色和宽度
  42. var color = that.penColor;
  43. var penWidth = that.penWidth;
  44. // 确定起点
  45. x = e.clientX - canvasLeft;
  46. y = e.clientY - canvasTop;
  47. // 添加layer
  48. $("#"+canvasId).addLayer({
  49. type: 'rectangle',
  50. strokeStyle: color,
  51. strokeWidth: penWidth,
  52. name:'areaLayer',
  53. fromCenter: false,
  54. x: x, y: y,
  55. width: 1,
  56. height: 1
  57. });
  58. // 绘制
  59. $("#"+canvasId).drawLayers();
  60. $("#"+canvasId).saveCanvas();
  61. //鼠标移动事件,画图
  62. canvas.onmousemove = function(e){
  63. // 要画的矩形的宽高
  64. var width = e.clientX-canvasLeft - x;
  65. var height = e.clientY-canvasTop - y;
  66. // 清除之前画的
  67. $("#"+canvasId).removeLayer('areaLayer');
  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. $("#"+canvasId).drawLayers();
  79. }
  80. };
  81. //鼠标抬起
  82. canvas.onmouseup=function(e){
  83. var color = that.penColor;
  84. var penWidth = that.penWidth;
  85. canvas.onmousemove = null;
  86. var width = e.clientX - canvasLeft - x;
  87. var height = e.clientY- canvasTop - y;
  88. $("#"+canvasId).removeLayer('areaLayer');
  89. $("#"+canvasId).addLayer({
  90. type: 'rectangle',
  91. strokeStyle: color,
  92. strokeWidth: penWidth,
  93. name:'areaLayer',
  94. fromCenter: false,
  95. x: x, y: y,
  96. width: width,
  97. height: height
  98. });
  99. $("#"+canvasId).drawLayers();
  100. $("#"+canvasId).saveCanvas();
  101. // 把body转成canvas
  102. html2canvas(document.body, {
  103. scale: 1,
  104. // allowTaint: true,
  105. useCORS: true //跨域使用
  106. }).then(canvas => {
  107. var capture_x, capture_y
  108. if (width > 0) {
  109. //从左往右画
  110. capture_x = x + that.penWidth
  111. }else {
  112. //从右往左画
  113. capture_x = x + width + that.penWidth
  114. }
  115. if (height > 0) {
  116. //从上往下画
  117. capture_y = y + that.penWidth
  118. }else {
  119. //从下往上画
  120. capture_y = y + height + that.penWidth
  121. }
  122. clipInfo = {
  123. "x":capture_x,
  124. "y":capture_y,
  125. "w":Math.abs(width),
  126. "h":Math.abs(height),
  127. };
  128. afterExt();
  129. });
  130. // 移除画的选取框
  131. $("#"+canvasId).removeLayer('areaLayer');
  132. // 隐藏用于华画取框的canvas
  133. $("#"+canvasId).hide()
  134. }
  135. }
  136. };
  137. var afterExt = function (){};
  138. var clipInfo = {};
  139. /**
  140. * 选取截屏
  141. * @param canvasId
  142. */
  143. function clipScreenshots(){
  144. $("#bg_canvas").show()
  145. canvasExt.drawRect("bg_canvas", "red", defaultStrokeWidth);
  146. }