Line.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. /**
  4. * Line object draw a line from one point to another.
  5. *
  6. * @class
  7. * @extends {Object2D}
  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 pattern is defined as the size of dashes as pairs of space with no line and with line.
  28. *
  29. * E.g if the dash pattern 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};