rpt_move_signature.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // 思路是点击签章的时候,原来的签章不显示,生成一个img用于拖动,实现拖动效果,再点击新生成的img就保存数据,干掉临时的img
  2. class MoveSignatureTool {
  3. constructor(ctx, canvas, pageObj, pageIdx, jpcOutput) {
  4. this.signatureList = [];
  5. this.ctx = ctx;
  6. // 被选中的签章
  7. this.activeImg = undefined;
  8. //鼠标位减图形位
  9. this.subPos = null;
  10. //图形是否被选择
  11. this.selected = false;
  12. // 上一个被选中的签章属性
  13. this.lastPosition = { x: 0, y: 0, width: 0, height: 0 };
  14. //canvas的dom对象
  15. this.canvas = canvas;
  16. this.pageObj = pageObj;
  17. this.pageIdx = pageIdx;
  18. this.jpcOutput = jpcOutput;
  19. this.signatureCanvas = document.getElementById("rptSignatureCanvas");
  20. }
  21. setSignature(obj, x, y, width, height, signature_name, signatureName, signType) {
  22. this.signatureList.push({
  23. obj,
  24. x,
  25. y,
  26. width,
  27. height,
  28. crossOrigin: 'Anonymous',
  29. signature_name,
  30. signatureName,
  31. signType
  32. })
  33. }
  34. //获取鼠标在canvas中的位置
  35. getMousePos(event, me) {
  36. //获取鼠标位置
  37. const { clientX, clientY } = event;
  38. //获取canvas 边界位置
  39. const { top, left } = me.canvas.getBoundingClientRect();
  40. //计算鼠标在canvas 中的位置
  41. const x = clientX - left;
  42. const y = clientY - top;
  43. return { x, y };
  44. }
  45. //判断点是否在图形中
  46. containPoint(data, mousePos) {
  47. //解构图形位置和尺寸
  48. const { x, y, width, height } = data;
  49. //解构鼠标位置
  50. const { x: mx, y: my } = mousePos;
  51. //计算鼠标和图形上、下、左、右边界的关系
  52. const l = mx > x;
  53. const r = mx < x + width;
  54. const t = my > y;
  55. const b = my < y + height;
  56. return l && r && t && b;
  57. }
  58. mousedownFn(event, me) {
  59. //鼠标位置
  60. const mousePos = me.getMousePos(event, me);
  61. // 找到相应的对象
  62. const target = me.signatureList.find((item) => me.containPoint(item, mousePos));
  63. if ($('#templateSignature').length === 0 && target) {
  64. //先把上一次的数据记录下来
  65. const { x, y, width, height } = target;
  66. Object.assign(me.lastPosition, { x, y, width, height });
  67. me.selected = true;
  68. me.activeImg = target;
  69. //鼠标位减图形位
  70. me.subPos = {
  71. x: mousePos.x - me.activeImg.x,
  72. y: mousePos.y - me.activeImg.y,
  73. };
  74. const orgTargetData = this.pageObj.items[0].signature_cells.find(item => item.signatureName === target.signatureName && item.signType === target.signType);
  75. orgTargetData.isMoving = true;
  76. this.render(me);
  77. const { top, left } = $("#rptCanvas").position();
  78. $("#rptCanvas").after(`<image id='templateSignature' data-ID='${orgTargetData.signature_name}' src='${orgTargetData.path}' style=" cursor: move;position: absolute;top: ${y + top + 10}px;left:${x + left + 10}px;width:${width}px;height:${height}px;" />`);
  79. $("#templateSignature").unbind('mousemove').on('mousemove', (e) => { this.templateSignatureMove(e, this) });
  80. $("#templateSignature").unbind('click').on('click', (e) => { this.templateSignatureClick(e, this) });
  81. } else {
  82. me.selected = false;
  83. }
  84. }
  85. saveSignature(me) {
  86. //鼠标抬起,取消拖拽
  87. me.selected = false;
  88. me.render(me);
  89. const params = {};
  90. params.id = CURRENT_ROLE_REL_ID;
  91. params.tender_id = TENDER_ID;
  92. params.stage_id = getStageId();
  93. params.rpt_id = zTreeOprObj.currentNode.refId;
  94. params.rel_content = ROLE_REL_LIST;
  95. const { x, y, width, height, signType, signatureName } = me.activeImg;
  96. const target = ROLE_REL_LIST.find(item => item.signature_name === signatureName);
  97. if (target) {
  98. if (!target.areaData) target.areaData = {};
  99. target.areaData[signType] = {
  100. Top: y,
  101. Bottom: y + height,
  102. Left: x,
  103. Right: x + width
  104. }
  105. }
  106. CommonAjax.postXsrfEx("/tender/report_api/updateRoleRelationship", params, 10000, true, getCookie('csrfToken_j'),
  107. async function (result) {
  108. // console.log(result);
  109. }
  110. )
  111. }
  112. render(me, isHideSignature = false) {
  113. let target;
  114. // 因为只有一页,所以就默认取是一个数据
  115. for (const signature of me.pageObj.items[0].signature_cells) {
  116. //找到要移动的签章
  117. if (signature.signature_name && me.activeImg && me.activeImg.signature_name.includes(signature.signature_name)) {
  118. target = signature;
  119. }
  120. }
  121. if (target) {
  122. Object.assign(target.area, {
  123. Top: me.activeImg.y,
  124. Bottom: me.activeImg.y + me.activeImg.height,
  125. Left: me.activeImg.x,
  126. Right: me.activeImg.x + me.activeImg.width,
  127. })
  128. me.jpcOutput.cleanCanvas(me.canvas);
  129. me.jpcOutput.drawPageBorder(me.pageObj, me.canvas, getScreenDPI())
  130. me.jpcOutput.drawToCanvas(me.pageObj, me.canvas, me.pageIdx, isHideSignature);
  131. }
  132. }
  133. domAddEventListener(canvas) {
  134. //点击事件
  135. $("#rptCanvas").unbind('mousedown').on('mousedown', (e) => { this.mousedownFn(e, this) });
  136. // $("#rptCanvas").unbind('mousemove').on('mousemove', (e) => { this.mouseMove(e, this) });
  137. }
  138. // 临时签章移动事件
  139. templateSignatureMove(event, me) {
  140. const mousePos = me.getMousePos(event, me);
  141. const { top, left } = $("#rptCanvas").position();
  142. event.currentTarget.style.top = `${mousePos.y - me.subPos.y + top}px`;
  143. event.currentTarget.style.left = `${mousePos.x - me.subPos.x + left}px`;
  144. }
  145. // 临时签章点击事件
  146. templateSignatureClick(event, me) {
  147. const canvasOuterTop = $("#rptCanvas").position().top;
  148. const canvasOuterLeft = $("#rptCanvas").position().left;
  149. const { top, left } = $(event.currentTarget).position();
  150. me.activeImg.y = top - canvasOuterTop - this.jpcOutput.offsetY;
  151. me.activeImg.x = left - canvasOuterLeft - this.jpcOutput.offsetX;
  152. const orgTargetData = this.pageObj.items[0].signature_cells.find(item => item.signature_name === $(event.currentTarget).data('id'));
  153. orgTargetData.isMoving = false;
  154. this.saveSignature(me);
  155. $("#templateSignature").remove();
  156. }
  157. }