assistRulerCtrl.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { SelectCtrl } from ".";
  2. type Ruler = {
  3. horz?: number, verz?: number
  4. }
  5. export class AssistRulerCtrl {
  6. ctrl: SelectCtrl
  7. rulers :Ruler[] = []
  8. _currDragItem?:Ruler;
  9. constructor(ctrl: SelectCtrl) {
  10. this.ctrl = ctrl;
  11. // const c = localStorage.getItem("ruler"+ this.ctrl.getProjectId());
  12. // if (c) {
  13. // this.rulers = JSON.parse(c);
  14. // }
  15. this.draw();
  16. }
  17. dragTest(e:MouseEvent) {
  18. const pageViewPort = this.ctrl.pageEl as HTMLElement;
  19. const pageBox = pageViewPort.getBoundingClientRect();
  20. let x = e.clientX - pageBox.left;
  21. let y = e.clientY - pageBox.top;
  22. this._currDragItem = undefined;
  23. let n = this.rulers.length;
  24. while(n--) {
  25. const item = this.rulers[n]
  26. if (item.horz != undefined && Math.abs(item.horz - y*2) < 4 ) {
  27. this._currDragItem = item;
  28. break;
  29. } else if (item.verz != undefined && Math.abs(item.verz - x*2) < 4) {
  30. this._currDragItem = item;
  31. break;
  32. }
  33. }
  34. console.log("test=>", this._currDragItem);
  35. if (this._currCursor) {
  36. this._currCursor = document.body.style.cursor;
  37. }
  38. return !!this._currDragItem;
  39. }
  40. _currAddingRuler?:{horz?: number, verz?: number}; //={horz: 0, verz: 0, cid:""}
  41. _currCursor = "";
  42. onDragMove(e:MouseEvent) {
  43. const it = this._currDragItem
  44. if (!it) return;
  45. const pageViewPort = this.ctrl.pageEl as HTMLElement;
  46. const pageBox = pageViewPort.getBoundingClientRect();
  47. const cx = e.clientX - pageBox.left;
  48. const cy = e.clientY - pageBox.top;
  49. if ( Math.floor(cx) < -40 || Math.floor(cy) < -40 || (e.clientX - pageBox.right) > 40 || (e.clientY - pageBox.bottom) > 40 ) {
  50. return;
  51. }
  52. if (it.verz != undefined) {
  53. it.verz = cx *2;
  54. document.body.style.cursor = "ew-resize";
  55. } else {
  56. it.horz = cy *2;
  57. document.body.style.cursor = "ns-resize";
  58. }
  59. console.log( it );
  60. this.draw();
  61. }
  62. onDragUp(e:MouseEvent) {
  63. this._currDragItem = undefined;
  64. document.body.style.cursor = this._currCursor;
  65. }
  66. rulerLineMouseMove(e:MouseEvent) {
  67. this.draw();
  68. const viewPort = this.ctrl.viewport as HTMLElement;
  69. const box = viewPort.getBoundingClientRect();
  70. if (e.clientX < box.x || e.clientX > box.right || e.clientY < box.top || e.clientY > box.bottom) return;
  71. const ctx = this.ctrl._selCtx;
  72. ctx.beginPath();
  73. const typeIndex = ["rulerLeft", "rulerRight", "rulerTop", "rulerBottom"].indexOf(this.ctrl._mouseDownFlag)
  74. const pageViewPort = this.ctrl.pageEl as HTMLElement;
  75. const pageBox = pageViewPort.getBoundingClientRect();
  76. // if ( !this._currAddingRuler ) this._currAddingRuler = {cid: this.ctrl.getCurrCard().id};
  77. if ( typeIndex == 0 || typeIndex == 1) {
  78. let mx = e.clientX;
  79. if ( Math.abs( mx - pageBox.left ) < 2 ) {//吸附效果
  80. mx = pageBox.left;
  81. }
  82. let x = mx
  83. let y = this.ctrl._selCanvaseSize.h
  84. ctx.moveTo(x *2, 0)
  85. ctx.lineTo(x *2, y)
  86. if (typeIndex == 0) {
  87. ctx.fillText(((mx-pageBox.left) *2).toFixed(0), x*2, e.clientY*2)
  88. } else {
  89. ctx.fillText(((pageBox.right - e.clientX) *2).toFixed(0), x*2, e.clientY*2)
  90. }
  91. this._currAddingRuler = {verz: (mx - pageBox.left)*2}
  92. } else {
  93. const x1 = box.left*2, x2 = box.right*2;
  94. const y = e.clientY*2
  95. ctx.moveTo(x1, y)
  96. ctx.lineTo(x2, y)
  97. const curBox = this.ctrl.getCurrCardBox();
  98. if (typeIndex ==2 ) {
  99. ctx.fillText(((e.clientY - curBox.top) *2).toFixed(0),(x1 + x2 ) / 2, y)
  100. } else {
  101. ctx.fillText((( curBox.bottom - e.clientY) *2).toFixed(0),(x1 + x2 ) / 2, y)
  102. }
  103. this._currAddingRuler = {horz: y - pageBox.top *2}
  104. }
  105. ctx.stroke();
  106. ctx.closePath();
  107. }
  108. rulerLineMouseUp(e:MouseEvent, isClick:boolean) {
  109. e.preventDefault();
  110. e.stopPropagation();
  111. if (!this._currAddingRuler) {
  112. return;
  113. }
  114. const viewPort = this.ctrl.pageEl as HTMLElement;
  115. const box = viewPort.getBoundingClientRect();
  116. if ((e.clientX - box.left) < -20 ||
  117. (e.clientY - box.top) < -20 ||
  118. (e.clientY - box.bottom) > 20 ||
  119. (e.clientX - box.right) > 20
  120. ) {
  121. this._currAddingRuler = undefined;
  122. return;
  123. }
  124. this.rulers.push({...this._currAddingRuler})
  125. console.log("rulerLineMouseUp=>", e.clientY);
  126. this._currAddingRuler = undefined;
  127. //localStorage.setItem("ruler"+ this.ctrl.getProjectId(), JSON.stringify(this.rulers));
  128. }
  129. draw() {
  130. const ctx = this.ctrl._selCtx;
  131. ctx.lineWidth = 2;
  132. ctx.strokeStyle = "#E88B00";
  133. ctx.fillStyle = "#E88B00";
  134. ctx.font = "36px Arial";
  135. ctx.setLineDash([5, 5]);
  136. ctx.clearRect(0, 0, this.ctrl._selCanvaseSize.w, this.ctrl._selCanvaseSize.h)
  137. let n = this.rulers.length;
  138. const viewPort = this.ctrl.viewport as HTMLElement;
  139. const pageEL = this.ctrl.pageEl as HTMLElement;
  140. const box = viewPort.getBoundingClientRect();
  141. const pageELBox = pageEL.getBoundingClientRect();
  142. while(n--) {
  143. const item = this.rulers[n];
  144. if (item.horz != undefined) { //水平线
  145. ctx.beginPath();
  146. ctx.moveTo(box.x *2, item.horz + pageELBox.top *2)
  147. ctx.lineTo(box.right *2, item.horz + pageELBox.top *2)
  148. ctx.stroke();
  149. ctx.closePath();
  150. } else if( item.verz != undefined) {
  151. ctx.beginPath();
  152. const x = item.verz + pageELBox.left *2
  153. ctx.moveTo(x, 0)
  154. ctx.lineTo(x, this.ctrl._selCanvaseSize.h);
  155. ctx.stroke();
  156. ctx.closePath();
  157. }
  158. }
  159. }
  160. }