line.js 1.5 KB

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