circumference.js 2.4 KB

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