1
0

circumference-drawer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. export class CircumferenceDrawer extends SelectableDrawer {
  8. constructor() {
  9. super();
  10. this.states = ["center", "radius"];
  11. this.state = undefined;
  12. this.circumference = undefined;
  13. this.pointDrawer = new PointDrawer();
  14. this.center;
  15. this.radius;
  16. this.centerAggregator;
  17. this.konvaObject;
  18. }
  19. draw(e) {
  20. const points = Selector.getSelectedPoints();
  21. if (points == undefined || points.length == 0) {
  22. this.drawByStates(e);
  23. } else this.drawByPoints(points, undefined, e);
  24. }
  25. drawByPoints(points, aggregators, e) {
  26. if (aggregators == undefined)
  27. aggregators = Selector.getSelectedPointsAggregators();
  28. else {
  29. aggregators = [
  30. Objects.getByGenericObject(this.pointA)[0],
  31. Objects.getByGenericObject(this.pointB)[0]
  32. ];
  33. }
  34. this.centerAggregator = aggregators[0];
  35. this.radiusAggregator = aggregators[1];
  36. this.center = points[0];
  37. this.radius = points[1];
  38. this.createAndDraw(this.center, this.radius);
  39. this.reset();
  40. this.setStatus("");
  41. }
  42. drawByStates(e) {
  43. if (this.state == undefined) {
  44. this.state = this.states[0];
  45. this.setStatus("Selecione o centro da Circunferência");
  46. } else if (this.state == this.states[0]) {
  47. this.centerAggregator = this.pointDrawer.drawPoint();
  48. this.center = this.centerAggregator.genericObject;
  49. this.state = this.states[1];
  50. this.setStatus("Selecione o raio da Circunferência");
  51. } else if (this.state == this.states[1]) {
  52. this.radiusAggregator = this.pointDrawer.drawPoint();
  53. this.radius = this.radiusAggregator.genericObject;
  54. this.createAndDraw(this.center, this.radius);
  55. this.reset();
  56. this.state = this.states[0];
  57. }
  58. }
  59. createAndDraw(center, radius) {
  60. this.center = center;
  61. this.radius = radius;
  62. this.circumference = new CircumferenceModel(this.center, this.radius);
  63. this.konvaObject = this.drawcircumference(this.circumference);
  64. const aggregator = new DrawerAggregator(
  65. this,
  66. this.circumference,
  67. this.konvaObject,
  68. ELEMENTS_CLASS.CIRCUMFERENCE
  69. );
  70. super.addAggregator(aggregator);
  71. this.centerAggregator.addAggregator(aggregator);
  72. this.radiusAggregator.addAggregator(aggregator);
  73. SelectableDrawer.drawObject(this.konvaObject);
  74. this.konvaObject.zIndex(0);
  75. super.batchDraw();
  76. return aggregator;
  77. }
  78. drawcircumference(circumference) {
  79. // const circle = new Konva.Circle({
  80. // x: circumference.center.posX,
  81. // y: circumference.center.posY,
  82. // radius: circumference.getRadius(),
  83. // fill: "transparent",
  84. // stroke: "grey",
  85. // strokeWidth: 2,
  86. // strokeScaleEnabled: false,
  87. // transformEnabled: false,
  88. // draggable: false,
  89. // selectable: false,
  90. // index: 0,
  91. // class: ELEMENTS_CLASS.CIRCUMFERENCE,
  92. // style: { stroke: "grey", fill: "grey" },
  93. // fillEnable: false,
  94. // strokeHitEnabled: true,
  95. // hitStrokeWidth: true,
  96. // strokeEnabled: true
  97. // });
  98. const circle = new Konva.Arc({
  99. x: circumference.center.posX,
  100. y: circumference.center.posY,
  101. innerRadius: circumference.getRadius(),
  102. outerRadius: circumference.getRadius(),
  103. angle: 360,
  104. fill: "grey",
  105. stroke: "grey",
  106. strokeWidth: 2,
  107. strokeScaleEnabled: false,
  108. transformEnabled: false,
  109. draggable: false,
  110. selectable: false,
  111. index: 0,
  112. class: ELEMENTS_CLASS.CIRCUMFERENCE,
  113. style: { stroke: "grey", fill: "grey" }
  114. });
  115. SelectableDrawer.setSelectableIfToolChanged(circle);
  116. return circle;
  117. }
  118. update(aggregator, e) {
  119. if (!aggregator.visible) return;
  120. const center = aggregator.genericObject.center;
  121. aggregator.konvaObject.innerRadius(aggregator.genericObject.getRadius());
  122. aggregator.konvaObject.outerRadius(aggregator.genericObject.getRadius());
  123. aggregator.konvaObject.x(center.posX);
  124. aggregator.konvaObject.y(center.posY);
  125. super.batchDraw();
  126. }
  127. }