You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
1.4 KiB

  1. define([
  2. 'js/rendering/renderer'
  3. ], function (
  4. renderer
  5. ) {
  6. return {
  7. type: 'textSprite',
  8. text: null,
  9. layerName: null,
  10. sprite: undefined,
  11. position: 'under',
  12. init: function (blueprint) {
  13. this.buildSprite();
  14. },
  15. buildSprite: async function () {
  16. const { layerName, text, container } = this;
  17. this.sprite = renderer.buildTextSprite({
  18. layerName,
  19. container,
  20. text
  21. });
  22. },
  23. getPosition: function () {
  24. const { obj: { x, y, height, offsetX, offsetY }, position, sprite } = this;
  25. const width = this.obj.transform.width * scaleMult;
  26. let rx = (x * scale);
  27. let ry = (y * scale);
  28. if (position === 'under' || position === 'above')
  29. rx += (width / 2) - (sprite.width / 2);
  30. if (position === 'under')
  31. ry += height;
  32. if (offsetX)
  33. rx += offsetX;
  34. if (offsetY)
  35. ry += offsetY;
  36. return { x: rx, y: ry };
  37. },
  38. update: function () {
  39. const { sprite, obj } = this;
  40. if (!sprite)
  41. return;
  42. const { x, y } = this.getPosition();
  43. Object.entries({
  44. x,
  45. y,
  46. visible: obj.isVisible
  47. }).forEach(([k, v]) => {
  48. if (sprite[k] !== v && v !== undefined)
  49. sprite[k] = v;
  50. });
  51. [
  52. 'text'
  53. ].forEach(p => {
  54. const value = this[p];
  55. if (sprite[p] !== value && value !== undefined)
  56. sprite[p] = value;
  57. });
  58. },
  59. destroy: function () {
  60. const { sprite } = this;
  61. if (!sprite)
  62. return;
  63. sprite.parent.removeChild(sprite);
  64. }
  65. };
  66. });