Text.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {Object2D} from "../Object2D.js";
  2. import {RoundedBox} from "./RoundedBox";
  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 {string}
  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 {string}
  42. */
  43. this.fillStyle = "#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.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. export {Text};