Line.js 1.4 KB

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