Text.js 2.9 KB

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