circumference-drawer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { CircumferenceModel } from "../models/circumference-model";
  2. import { PointDrawer } from "../../point-component/drawers/point-drawer";
  3. import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
  4. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  5. import { selector as Selector } from "../../../core/application/selector";
  6. import { SelectableDrawer } from "../../../core/drawers/selectable-drawer";
  7. import { objects as Objects } from "../../../core/application/objects";
  8. export class CircumferenceDrawer extends SelectableDrawer {
  9. constructor() {
  10. super();
  11. this.states = ["center", "radius"];
  12. this.state = undefined;
  13. this.circumference = undefined;
  14. this.pointDrawer = new PointDrawer();
  15. this.center;
  16. this.radius;
  17. this.centerAggregator;
  18. this.konvaObject;
  19. this.setElementClass(ELEMENTS_CLASS.CIRCUMFERENCE);
  20. }
  21. draw(e) {
  22. if (e != undefined) {
  23. if (
  24. e.target != undefined &&
  25. e.target.attrs.class != undefined &&
  26. (e.target.attrs.class == ELEMENTS_CLASS.POINT ||
  27. e.target.attrs.class == ELEMENTS_CLASS.INTERSECTION_POINT)
  28. ) {
  29. this.drawByStates(e.target);
  30. return;
  31. } else if (e.attrs != undefined && e.attrs.genericObject != undefined) {
  32. this.resolveAggregators([e.attrs.genericObject.center, e.attrs.genericObject.radius], undefined, false);
  33. this.createByCircumference(e.attrs.genericObject);
  34. return;
  35. }
  36. }
  37. const points = Selector.getSelectedPoints();
  38. if (points == undefined || points.length == 0) {
  39. this.drawByStates();
  40. } else this.drawByPoints(points, undefined, e);
  41. }
  42. drawByPoints(points, aggregators, e) {
  43. aggregators = this.resolveAggregators(points, aggregators, true);
  44. this.centerAggregator = aggregators[0];
  45. this.radiusAggregator = aggregators[1];
  46. this.center = points[0];
  47. this.radius = points[1];
  48. this.createAndDraw(this.center, this.radius);
  49. this.reset();
  50. this.setStatus("");
  51. }
  52. drawByStates(konvaObject) {
  53. let aggregator = undefined;
  54. if (konvaObject != undefined) {
  55. aggregator = Objects.getByKonvaObject(konvaObject)[0];
  56. }
  57. if (this.state == undefined) {
  58. this.state = this.states[0];
  59. this.setStatus("Selecione o centro da Circunferência");
  60. } else if (this.state == this.states[0]) {
  61. this.centerAggregator =
  62. aggregator != undefined ? aggregator : this.pointDrawer.drawPoint();
  63. this.center = this.centerAggregator.genericObject;
  64. this.state = this.states[1];
  65. this.setStatus("Selecione o raio da Circunferência");
  66. } else if (this.state == this.states[1]) {
  67. this.radiusAggregator =
  68. aggregator != undefined ? aggregator : this.pointDrawer.drawPoint();
  69. this.radius = this.radiusAggregator.genericObject;
  70. this.createAndDraw(this.center, this.radius);
  71. this.reset();
  72. this.state = this.states[0];
  73. }
  74. }
  75. createAndDraw(center, radius) {
  76. this.center = center;
  77. this.radius = radius;
  78. this.circumference = new CircumferenceModel(this.center, this.radius);
  79. this.createByCircumference(this.circumference);
  80. }
  81. createByCircumference(circumference) {
  82. this.circumference = circumference;
  83. this.konvaObject = this.drawCircumference(this.circumference);
  84. const aggregator = new DrawerAggregator(
  85. this,
  86. this.circumference,
  87. this.konvaObject,
  88. ELEMENTS_CLASS.CIRCUMFERENCE
  89. );
  90. super.addAggregator(aggregator);
  91. this.centerAggregator.addAggregator(aggregator);
  92. this.radiusAggregator.addAggregator(aggregator);
  93. SelectableDrawer.drawObject(this.konvaObject);
  94. this.konvaObject.zIndex(0);
  95. super.batchDraw();
  96. return aggregator;
  97. }
  98. drawCircumference(circumference) {
  99. const circle = new Konva.Arc({
  100. x: circumference.center.posX,
  101. y: circumference.center.posY,
  102. innerRadius: circumference.getRadius(),
  103. outerRadius: circumference.getRadius(),
  104. angle: 360,
  105. fill: "grey",
  106. stroke: "grey",
  107. strokeWidth: 2,
  108. strokeScaleEnabled: false,
  109. transformEnabled: false,
  110. draggable: false,
  111. selectable: false,
  112. index: 0,
  113. class: ELEMENTS_CLASS.CIRCUMFERENCE,
  114. style: { stroke: "grey", fill: "grey" }
  115. });
  116. SelectableDrawer.setSelectableIfToolChanged(circle);
  117. return circle;
  118. }
  119. resolveAggregators(points, aggregators, selected) {
  120. if (this.center == undefined) {
  121. this.center = points[0];
  122. }
  123. if (this.radius == undefined) {
  124. this.radius = points[1];
  125. }
  126. if (aggregators == undefined && selected == true)
  127. aggregators = Selector.getSelectedPointsAggregators();
  128. else {
  129. aggregators = [
  130. Objects.getByGenericObject(points[0])[0],
  131. Objects.getByGenericObject(points[1])[0]
  132. ];
  133. }
  134. this.centerAggregator = aggregators[0];
  135. this.radiusAggregator = aggregators[1];
  136. return aggregators;
  137. }
  138. update(aggregator, e) {
  139. if (!aggregator.visible) return;
  140. const center = aggregator.genericObject.center;
  141. aggregator.konvaObject.innerRadius(aggregator.genericObject.getRadius());
  142. aggregator.konvaObject.outerRadius(aggregator.genericObject.getRadius());
  143. aggregator.konvaObject.x(center.posX);
  144. aggregator.konvaObject.y(center.posY);
  145. super.batchDraw();
  146. }
  147. }