line.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var line = (function() {
  2. let _tool = {};
  3. let _states = ["primeiro_ponto", "segundo_ponto"];
  4. let _state = undefined;
  5. let _points = [0, 0, 0, 0];
  6. function _draw() {
  7. if (_state == undefined) {
  8. _state = _states[0];
  9. app.setStatus("Selecione o primeiro ponto no canvas");
  10. } else if (_state == _states[0]) {
  11. let pos = app.pos();
  12. _points[0] = pos.x;
  13. _points[1] = pos.y;
  14. _state = _states[1];
  15. app.setStatus("Selecione o segundo ponto no canvas");
  16. } else if (_state == _states[1]) {
  17. let pos = app.pos();
  18. _points[2] = pos.x;
  19. _points[3] = pos.y;
  20. let p = _points.slice();
  21. let ln = new Konva.Line({
  22. points: p,
  23. stroke: "grey",
  24. strokeWidth: 2,
  25. lineJoin: "round",
  26. draggable: true,
  27. strokeScaleEnabled: false
  28. });
  29. let layer = app.currentLayer();
  30. layer.add(ln);
  31. app.stage.draw();
  32. _clearState();
  33. app.clearSelectedTool();
  34. app.setStatus("");
  35. }
  36. }
  37. function _bootstrap() {
  38. app.tools.push(_tool);
  39. }
  40. function _click() {
  41. if (_state == _states[0] || _state == _states[1]) {
  42. app.clearSelectedTool();
  43. _clearState();
  44. return;
  45. }
  46. app.setSelectedTool(_tool);
  47. _state = _states[0];
  48. app.setStatus("Selecione o primeiro ponto no canvas");
  49. }
  50. function _clearState() {
  51. _state = undefined;
  52. }
  53. _tool = {
  54. id: "line",
  55. title: "Reta",
  56. icon: "line",
  57. click: _click,
  58. draw: _draw,
  59. points: _points
  60. };
  61. _bootstrap();
  62. return {
  63. draw: _draw,
  64. click: _click
  65. };
  66. })();