Text.js 1.1 KB

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