circumference-drawer.js 5.4 KB

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