Line.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 without any kind of interpolation.
  5. *
  6. * For drawing lines with interpolation check {BezierCurve}
  7. *
  8. * @class
  9. * @extends {Object2D}
  10. */
  11. function Line()
  12. {
  13. Object2D.call(this);
  14. /**
  15. * Initial point of the line.
  16. *
  17. * Can be equal to the position object of another object. Making it automatically follow that object.
  18. */
  19. this.from = new Vector2();
  20. /**
  21. * Final point of the line.
  22. *
  23. * Can be equal to the position object of another object. Making it automatically follow that object.
  24. */
  25. this.to = new Vector2();
  26. /**
  27. * Dash line pattern to be used, if empty draws a solid line.
  28. *
  29. * Dash pattern is defined as the size of dashes as pairs of space with no line and with line.
  30. *
  31. * E.g if the dash pattern is [1, 2] we get 1 point with line, 2 without line repeat infinitelly.
  32. */
  33. this.dashPattern = [5, 5];
  34. /**
  35. * Style of the object line.
  36. */
  37. this.strokeStyle = "#000000";
  38. /**
  39. * Line width of the line.
  40. */
  41. this.lineWidth = 1;
  42. }
  43. Line.prototype = Object.create(Object2D.prototype);
  44. Line.prototype.style = function(context, viewport, canvas)
  45. {
  46. context.lineWidth = this.lineWidth;
  47. context.strokeStyle = this.strokeStyle;
  48. context.setLineDash(this.dashPattern);
  49. };
  50. Line.prototype.draw = function(context, viewport, canvas)
  51. {
  52. context.beginPath();
  53. context.moveTo(this.from.x, this.from.y);
  54. context.lineTo(this.to.x, this.to.y);
  55. context.stroke();
  56. };
  57. export {Line};