circumference.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { app as App } from "../app";
  2. import { menu as Menu } from "../core/application/menu";
  3. export const circumference = (function() {
  4. let _tool = {};
  5. let _states = ["center", "radius"];
  6. let _state = undefined;
  7. let _coordinates = [0, 0, 0, 0];
  8. let _points = [0, 0, 0];
  9. function _draw() {
  10. if (_state == undefined) {
  11. _state = _states[0];
  12. App.setStatus("Selecione o centro da Circunferência");
  13. } else if (_state == _states[0]) {
  14. let pos = App.pos();
  15. _coordinates[0] = pos.x;
  16. _coordinates[1] = pos.y;
  17. _state = _states[1];
  18. App.setStatus("Selecione o raio da Circunferência");
  19. } else if (_state == _states[1]) {
  20. let pos = App.pos();
  21. _coordinates[2] = pos.x;
  22. _coordinates[3] = pos.y;
  23. let legA = _coordinates[2] - _coordinates[0];
  24. let legB = _coordinates[3] - _coordinates[1];
  25. let radius = Math.sqrt(Math.pow(legA, 2) + Math.pow(legB, 2));
  26. _points = [_coordinates[0], _coordinates[1], radius];
  27. let p = _points.slice();
  28. _drawcircumference(p[0], p[1], p[2]);
  29. }
  30. }
  31. function _drawcircumference(x, y, radius) {
  32. let layer = App.currentLayer();
  33. let group = new Konva.Group({
  34. draggable: true,
  35. resizeEnabled: false
  36. });
  37. let circle = new Konva.Circle({
  38. x: x,
  39. y: y,
  40. radius: radius,
  41. fill: "transparent",
  42. stroke: "black",
  43. strokeWidth: 1,
  44. strokeScaleEnabled: false,
  45. transformEnabled: true,
  46. draggable: false
  47. });
  48. let point = new Konva.Circle({
  49. x: x,
  50. y: y,
  51. radius: 5,
  52. fill: "#9bc364",
  53. stroke: "#9bc364",
  54. strokeWidth: 1,
  55. strokeScaleEnabled: false,
  56. draggable: false,
  57. resizeEnabled: false,
  58. transformEnabled: false
  59. });
  60. group.add(circle);
  61. group.add(point);
  62. layer.add(group);
  63. App.stage.draw();
  64. _clearState();
  65. App.clearSelectedTool();
  66. App.setStatus("");
  67. }
  68. function _bootstrap() {
  69. Menu.add(_tool);
  70. }
  71. function _click(id) {
  72. if (_state == _states[0]) {
  73. App.clearSelectedTool();
  74. _clearState();
  75. return;
  76. }
  77. App.setSelectedTool(_tool);
  78. _state = _states[0];
  79. App.setStatus("Selecione o primeiro ponto no canvas");
  80. }
  81. function _clearState() {
  82. _state = undefined;
  83. }
  84. _tool = {
  85. id: "circumference",
  86. title: "Circunferência",
  87. icon: "line",
  88. click: _click,
  89. draw: _draw,
  90. points: _points,
  91. coordinates: _coordinates,
  92. drawcircumference: _drawcircumference
  93. };
  94. _bootstrap();
  95. return {
  96. draw: _draw,
  97. click: _click
  98. };
  99. })();