SpriteAtlas.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. class SpriteAtlas {
  2. constructor(assetsPath, xmlFilename) {
  3. this.assetsPath = assetsPath;
  4. this.xmlFilename = xmlFilename;
  5. this.xmlPath = `./${assetsPath}/${xmlFilename}`;
  6. this.atlas = {};
  7. this.xmlData = {};
  8. this.spritesheetImage = new Image();
  9. this.cacheBusting = true;
  10. }
  11. load() {
  12. return new Promise((resolve, reject) => {
  13. var request = new XMLHttpRequest();
  14. request.addEventListener("load", (event) => this.xmlLoadComplete(event, resolve, reject));
  15. let url = this.xmlPath;
  16. if (this.cacheBusting) {
  17. url += "?v=" + new Date().getTime();
  18. }
  19. request.open("GET", url);
  20. request.setRequestHeader("Content-Type", "text/xml");
  21. request.send();
  22. });
  23. }
  24. xmlLoadComplete(event, resolve, reject) {
  25. var source = (new DOMParser()).parseFromString(event.target.responseText, "application/xml");
  26. this.xmlData = source;
  27. var textureAtlas = source.getElementsByTagName("TextureAtlas")[0];
  28. this.populateAtlas(source);
  29. var sheetsrc = `./${this.assetsPath}/${textureAtlas.getAttribute("imagePath")}`;
  30. if (this.cacheBusting) {
  31. sheetsrc += "?v=" + new Date().getTime();
  32. }
  33. this.spritesheetImage.src = sheetsrc;
  34. this.spritesheetImage.addEventListener('load', resolve);
  35. };
  36. populateAtlas(source) {
  37. var subtextures = source.getElementsByTagName("SubTexture");
  38. for (var i = 0, subtexture = subtextures[0]; subtexture = subtextures[i]; i++) {
  39. this.atlas[subtexture.getAttribute("name")] = {
  40. name: subtexture.getAttribute("name"),
  41. x: parseInt(subtexture.getAttribute("x")),
  42. y: parseInt(subtexture.getAttribute("y")),
  43. width: parseInt(subtexture.getAttribute("width")),
  44. height: parseInt(subtexture.getAttribute("height"))
  45. };
  46. }
  47. };
  48. getSprite(name) {
  49. let imageLookup = this.atlas[name];
  50. if (!!imageLookup) {
  51. return imageLookup;
  52. }
  53. throw new Error(`Sprite ${name} not found in atlas ${this.xmlPath}`);
  54. }
  55. draw(context, name, position) {
  56. let imageLookup = this.getSprite(name);
  57. context.drawImage(this.spritesheetImage,
  58. imageLookup.x,
  59. imageLookup.y,
  60. imageLookup.width,
  61. imageLookup.height,
  62. position.x,
  63. position.y,
  64. imageLookup.width,
  65. imageLookup.height);
  66. };
  67. drawCentered(context, name, position) {
  68. let imageLookup = this.getSprite(name);
  69. context.drawImage(this.spritesheetImage,
  70. imageLookup.x,
  71. imageLookup.y,
  72. imageLookup.width,
  73. imageLookup.height,
  74. position.x - imageLookup.width / 2,
  75. position.y - imageLookup.height / 2,
  76. imageLookup.width,
  77. imageLookup.height);
  78. };
  79. drawBottomCentered(context, name, position) {
  80. let imageLookup = this.getSprite(name);
  81. context.drawImage(this.spritesheetImage,
  82. imageLookup.x,
  83. imageLookup.y,
  84. imageLookup.width,
  85. imageLookup.height,
  86. position.x - imageLookup.width / 2,
  87. position.y + imageLookup.height / 2,
  88. imageLookup.width,
  89. imageLookup.height);
  90. };
  91. getWidth(name) {
  92. return this.getSprite(name).width;
  93. };
  94. getHeight(name) {
  95. return this.getSprite(name).height;
  96. };
  97. }