Text.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {Object2D} from "../Object2D.js";
  2. /**
  3. * Text element, used to draw single line text into the canvas.
  4. *
  5. * For multi line text with support for line break check {MultiLineText} object.
  6. *
  7. * @class
  8. * @extends {Object2D}
  9. */
  10. function Text()
  11. {
  12. Object2D.call(this);
  13. /**
  14. * Text value displayed by this element.
  15. *
  16. * @type {string}
  17. */
  18. this.text = "";
  19. /**
  20. * Font of the text.
  21. *
  22. * @type {string}
  23. */
  24. this.font = "16px Arial";
  25. /**
  26. * Style of the object border line. If set null it is ignored.
  27. *
  28. * @type {string}
  29. */
  30. this.strokeStyle = null;
  31. /**
  32. * Line width, only used if a valid strokeStyle is defined.
  33. *
  34. * @type {number}
  35. */
  36. this.lineWidth = 1;
  37. /**
  38. * CSS background color of the box. If set null it is ignored.
  39. *
  40. * @type {string}
  41. */
  42. this.fillStyle = "#000000";
  43. /**
  44. * Text align property. Same values as used for canvas text applies
  45. *
  46. * Check documentation at https://developer.mozilla.org/en-US/docs/Web/CSS/text-align for mode details about this property.
  47. *
  48. * @type {string}
  49. */
  50. this.textAlign = "center";
  51. /**
  52. * Text baseline defines the vertical position of the text relative to the imaginary line Y position. Same values as used for canvas text applies
  53. *
  54. * Check documentation at https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline for mode details about this property.
  55. *
  56. * @type {string}
  57. */
  58. this.textBaseline = "middle";
  59. }
  60. Text.prototype = Object.create(Object2D.prototype);
  61. Text.prototype.constructor = Text;
  62. Text.prototype.type = "Text";
  63. Text.prototype.draw = function(context, viewport, canvas)
  64. {
  65. context.font = this.font;
  66. context.textAlign = this.textAlign;
  67. context.textBaseline = this.textBaseline ;
  68. if(this.fillStyle !== null)
  69. {
  70. context.fillStyle = this.fillStyle;
  71. context.fillText(this.text, 0, 0);
  72. }
  73. if(this.strokeStyle !== null)
  74. {
  75. context.strokeStyle = this.strokeStyle;
  76. context.strokeText(this.text, 0, 0);
  77. }
  78. };
  79. Text.prototype.serialize = function(recursive)
  80. {
  81. var data = Object2D.prototype.serialize.call(this, recursive);
  82. data.text = this.text;
  83. data.font = this.font;
  84. data.strokeStyle = this.strokeStyle;
  85. data.lineWidth = this.lineWidth;
  86. data.fillStyle = this.fillStyle;
  87. data.textAlign = this.textAlign;
  88. data.textBaseline = this.textBaseline;
  89. return data;
  90. };
  91. Text.prototype.parse = function(data)
  92. {
  93. Object2D.prototype.parse.call(this, data);
  94. this.text = data.text;
  95. this.font = data.font;
  96. this.strokeStyle = data.strokeStyle;
  97. this.lineWidth = data.lineWidth;
  98. this.fillStyle = data.fillStyle;
  99. this.textAlign = data.textAlign;
  100. this.textBaseline = data.textBaseline;
  101. };
  102. export {Text};