line.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { app as App } from "../app";
  2. import { menu as Menu } from "../core/components/menu";
  3. import { selector as Selector } from "../core/components/selector";
  4. import { ELEMENTS_CLASS } from "../core/enums/elements-class-enum";
  5. export class Line {
  6. constructor() {
  7. this._states = ["primeiro_ponto", "segundo_ponto"];
  8. this._state = undefined;
  9. this._points = [0, 0, 0, 0];
  10. this._tool = {
  11. id: "line",
  12. title: "Reta",
  13. icon: "line",
  14. click: this.click,
  15. draw: this.draw,
  16. points: this.points,
  17. object: this
  18. };
  19. this.bootstrap();
  20. }
  21. draw(e) {
  22. let _this = e;
  23. if (e == undefined) {
  24. _this = this;
  25. }
  26. if (_this._state == undefined) {
  27. _this._state = _this._states[0];
  28. App.setStatus("Selecione o primeiro ponto no canvas");
  29. } else if (_this._state == _this._states[0]) {
  30. let pos = App.pos();
  31. _this._points[0] = pos.x;
  32. _this._points[1] = pos.y;
  33. _this._state = _this._states[1];
  34. App.setStatus("Selecione o segundo ponto no canvas");
  35. } else if (_this._state == _this._states[1]) {
  36. let pos = App.pos();
  37. _this._points[2] = pos.x;
  38. _this._points[3] = pos.y;
  39. let p = _this._points.slice();
  40. _this.drawPoint(p);
  41. }
  42. }
  43. click(e) {
  44. let _this = e;
  45. if (e == undefined) {
  46. _this = this;
  47. }
  48. if (_this._state == _this._states[0] || _this._state == _this._states[1]) {
  49. App.clearSelectedTool();
  50. _this.clearState();
  51. return;
  52. }
  53. let objects = Selector.getPoints();
  54. if (
  55. _this._state == undefined &&
  56. objects != undefined &&
  57. objects.length > 0
  58. ) {
  59. let layer = App.currentLayer();
  60. let p = [];
  61. let ln = _this.createLine(p);
  62. objects.forEach(object => {
  63. object.attrs.connections.push(ln);
  64. p.push(object.x());
  65. p.push(object.y());
  66. object.parent.on("dragmove", () => {
  67. let p = [];
  68. let objects = Selector.getPoints();
  69. objects.forEach(object => {
  70. p.push(object.x());
  71. p.push(object.y());
  72. });
  73. object.attrs.connections[0].points(p.slice());
  74. layer.batchDraw();
  75. });
  76. ln.points(p.slice());
  77. layer.batchDraw();
  78. });
  79. _this.drawPoint(p);
  80. App.clearSelectedTool();
  81. _this.clearState();
  82. return;
  83. }
  84. App.setSelectedTool(_this._tool);
  85. _this._state = _this._states[0];
  86. App.setStatus("Selecione o primeiro ponto no canvas");
  87. }
  88. clearState() {
  89. this._state = undefined;
  90. }
  91. drawPoint(p) {
  92. let ln = this.createLine(p);
  93. let layer = App.currentLayer();
  94. layer.add(ln);
  95. App.stage.draw();
  96. this.clearState();
  97. App.clearSelectedTool();
  98. App.setStatus("");
  99. }
  100. createLine(p) {
  101. return new Konva.Line({
  102. points: p,
  103. stroke: "grey",
  104. strokeWidth: 2,
  105. lineJoin: "round",
  106. draggable: true,
  107. strokeScaleEnabled: false,
  108. class: ELEMENTS_CLASS.LINE,
  109. connections: []
  110. });
  111. }
  112. bootstrap() {
  113. Menu.add(this._tool);
  114. }
  115. }
  116. export const line = new Line();