Line.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {ColorStyle} from "./style/ColorStyle";
  4. import {Style} from "./style/Style";
  5. /**
  6. * Line object draw a line from one point to another without any kind of interpolation.
  7. *
  8. * For drawing lines with interpolation check {BezierCurve}
  9. *
  10. * @class
  11. * @extends {Object2D}
  12. */
  13. function Line()
  14. {
  15. Object2D.call(this);
  16. /**
  17. * Initial point of the line.
  18. *
  19. * Can be equal to the position object of another object. Making it automatically follow that object.
  20. *
  21. * @type {Vector2}
  22. */
  23. this.from = new Vector2();
  24. /**
  25. * Final point of the line.
  26. *
  27. * Can be equal to the position object of another object. Making it automatically follow that object.
  28. *
  29. * @type {Vector2}
  30. */
  31. this.to = new Vector2();
  32. /**
  33. * Dash line pattern to be used, if empty draws a solid line.
  34. *
  35. * Dash pattern is defined as the size of dashes as pairs of space with no line and with line.
  36. *
  37. * E.g if the dash pattern is [1, 2] we get 1 point with line, 2 without line repeat infinitelly.
  38. *
  39. * @type {number[]}
  40. */
  41. this.dashPattern = [5, 5];
  42. /**
  43. * Style of the object line.
  44. *
  45. * @type {Style}
  46. */
  47. this.strokeStyle = new ColorStyle("#000000");
  48. /**
  49. * Line width of the line.
  50. *
  51. * @type {number}
  52. */
  53. this.lineWidth = 1;
  54. }
  55. Line.prototype = Object.create(Object2D.prototype);
  56. Line.prototype.constructor = Line;
  57. Line.prototype.type = "Line";
  58. Object2D.register(Line, "Line");
  59. Line.prototype.style = function(context, viewport, canvas)
  60. {
  61. context.lineWidth = this.lineWidth;
  62. context.strokeStyle = this.strokeStyle.get(context);
  63. context.setLineDash(this.dashPattern);
  64. };
  65. Line.prototype.draw = function(context, viewport, canvas)
  66. {
  67. context.beginPath();
  68. context.moveTo(this.from.x, this.from.y);
  69. context.lineTo(this.to.x, this.to.y);
  70. context.stroke();
  71. };
  72. Line.prototype.serialize = function(recursive)
  73. {
  74. var data = Object2D.prototype.serialize.call(this, recursive);
  75. data.from = this.from.toArray();
  76. data.to = this.to.toArray();
  77. data.dashPattern = this.dashPattern;
  78. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  79. data.lineWidth = this.lineWidth;
  80. return data;
  81. };
  82. Line.prototype.parse = function(data, root)
  83. {
  84. Object2D.prototype.parse.call(this, data, root);
  85. this.to.fromArray(data.to);
  86. this.from.fromArray(data.from);
  87. this.dashPattern = data.dashPattern;
  88. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  89. this.lineWidth = data.lineWidth;
  90. };
  91. export {Line};