Text.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. import {Object2D} from "../Object2D.js";
  3. /**
  4. * Text element, used to draw text into the canvas.
  5. *
  6. * @class
  7. * @extends {Object2D}
  8. */
  9. function Text()
  10. {
  11. Object2D.call(this);
  12. /**
  13. * Text value.
  14. */
  15. this.text = "";
  16. /**
  17. * Font of the text.
  18. */
  19. this.font = "16px Arial";
  20. /**
  21. * Style of the object border line.
  22. *
  23. * If set null it is ignored.
  24. */
  25. this.strokeStyle = null;
  26. /**
  27. * Line width, only used if a valid strokeStyle is defined.
  28. */
  29. this.lineWidth = 1;
  30. /**
  31. * Background color of the box.
  32. *
  33. * If set null it is ignored.
  34. */
  35. this.fillStyle = "#000000";
  36. /**
  37. * Text align property.
  38. */
  39. this.textAlign = "center";
  40. }
  41. Text.prototype = Object.create(Object2D.prototype);
  42. Text.prototype.draw = function(context, viewport, canvas)
  43. {
  44. context.font = this.font;
  45. context.textAlign = this.textAlign;
  46. context.textBaseline = "middle";
  47. if(this.fillStyle !== null)
  48. {
  49. context.fillStyle = this.fillStyle;
  50. context.fillText(this.text, 0, 0);
  51. }
  52. if(this.strokeStyle !== null)
  53. {
  54. context.strokeStyle = this.strokeStyle;
  55. context.strokeText(this.text, 0, 0);
  56. }
  57. };
  58. export {Text};