label.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function Label (x = 0, y = 0) {
  2. this.x = x
  3. this.y = y
  4. this._text=""
  5. this._fillStyle ="#ffffff"
  6. this._textAlign ="left"
  7. this._font ="Helvetica"
  8. this._fontSize="30px"
  9. this._baseLine ="middle"
  10. this._name=""
  11. this._globalAlpha = 1;
  12. this.visible = true
  13. }
  14. Label.prototype={
  15. set name(tex) {
  16. this._name = tex;
  17. },
  18. get name() {
  19. return this._name;
  20. },
  21. set text(tex) {
  22. this._text = tex;
  23. },
  24. get text() {
  25. return this._text;
  26. },
  27. set fillStyle(tex) {
  28. this._fillStyle = tex;
  29. },
  30. get fillStyle() {
  31. return this._fillStyle;
  32. },
  33. set textAlign(tex) {
  34. this._textAlign = tex;
  35. },
  36. get textAlign() {
  37. return this._textAlign;
  38. },
  39. set font(tex) {
  40. this._font = tex;
  41. },
  42. get font() {
  43. return this._font;
  44. },
  45. set fontSize(tex) {
  46. this._fontSize = tex;
  47. },
  48. get fontSize() {
  49. return this._fontSize;
  50. },
  51. set baseLine(tex) {
  52. this._baseLine = tex;
  53. },
  54. get baseLine() {
  55. return this._baseLine;
  56. },
  57. get globalAlpha() {
  58. return this._globalAlpha;
  59. },
  60. set globalAlpha(tex) {
  61. this._globalAlpha = tex;
  62. },
  63. }
  64. Label.prototype.position = function(x,y){
  65. this.x = x;
  66. this.y = y;
  67. }
  68. /**
  69. * 将文字绘制在canvas上
  70. */
  71. Label.prototype.drawToCanvas=function(ctx){
  72. if (!this.visible)
  73. return
  74. ctx.globalAlpha = this.globalAlpha;
  75. ctx.fillStyle = this.fillStyle;
  76. ctx.textAlign = this.textAlign;
  77. ctx.baseLine = this.baseLine;
  78. ctx.font = `${this.fontSize} ${this.font}`;
  79. ctx.fillText(this.text, this.x, this.y);
  80. }
  81. module.exports.Label = Label;