line.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 drawLine() {
  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.add(layer);
  33. app.stage.draw();
  34. state = undefined;
  35. app.clearSelectedTool();
  36. app.setStatus("");
  37. }
  38. }
  39. function bootstrap() {
  40. app.tools.push(tool);
  41. }
  42. function onClick() {
  43. app.setSelectedTool(tool);
  44. state = states[0];
  45. app.setStatus("Selecione o primeiro ponto no canvas");
  46. }
  47. tool = {
  48. id: "reta",
  49. title: "Reta",
  50. icon: "line",
  51. click: onClick,
  52. draw: drawLine,
  53. points: points
  54. };
  55. bootstrap();
  56. return {
  57. drawLine: drawLine,
  58. onClick: onClick
  59. };
  60. })();