Line.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. import {Object2D} from "../Object2D.js";
  3. import {Vector2} from "../math/Vector2.js";
  4. /**
  5. * Line object draw a line from one point to another.
  6. *
  7. * @class
  8. */
  9. function Line()
  10. {
  11. Object2D.call(this);
  12. /**
  13. * Initial point of the line.
  14. *
  15. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  16. */
  17. this.from = new Vector2();
  18. /**
  19. * Final point of the line.
  20. *
  21. * Can be equal to the position object of another object. (Making it automatically follow that object.)
  22. */
  23. this.to = new Vector2();
  24. /**
  25. * Dash line pattern to be used, if empty draws a solid line.
  26. *
  27. * Dash parttern is defined as the size of dashes as pairs of space with no line and with line.
  28. *
  29. * E.g if the daspattern is [1, 2] we get 1 point with line, 2 without line repeat infinitelly.
  30. */
  31. this.dashPattern = [5, 5];
  32. /**
  33. * Style of the object line.
  34. */
  35. this.strokeStyle = "#000000";
  36. /**
  37. * Line width of the line.
  38. */
  39. this.lineWidth = 1;
  40. }
  41. Line.prototype = Object.create(Object2D.prototype);
  42. Line.prototype.draw = function(context, viewport, canvas)
  43. {
  44. context.lineWidth = this.lineWidth;
  45. context.strokeStyle = this.strokeStyle;
  46. context.setLineDash(this.dashPattern);
  47. context.beginPath();
  48. context.moveTo(this.from.x, this.from.y);
  49. context.lineTo(this.to.x, this.to.y);
  50. context.stroke();
  51. };
  52. export {Line};