SpriteAtlas.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { AsyncDataReader } from './AsyncDataReader.js';
  2. export class SpriteAtlas {
  3. constructor() {}
  4. async load(assetsPath, xmlFilename) {
  5. this.atlas = {};
  6. this.spritesheetImage = new Image();
  7. this.assetsPath = assetsPath;
  8. this.xmlPath = assetsPath + "/" + xmlFilename
  9. let result = await new AsyncDataReader().readXML(this.xmlPath);
  10. return await this.xmlLoadComplete(result);
  11. }
  12. xmlLoadComplete(result) {
  13. return new Promise((resolve, reject) => {
  14. var source = result;
  15. var textureAtlas = source.getElementsByTagName("TextureAtlas")[0];
  16. this.populateAtlas(source);
  17. var sheetsrc = `./${this.assetsPath}/${textureAtlas.getAttribute("imagePath")}`;
  18. this.spritesheetImage.src = sheetsrc;
  19. this.spritesheetImage.addEventListener('load', resolve);
  20. });
  21. };
  22. populateAtlas(source) {
  23. var subtextures = source.getElementsByTagName("SubTexture");
  24. for (var i = 0, subtexture = subtextures[0]; subtexture = subtextures[i]; i++) {
  25. this.atlas[subtexture.getAttribute("name")] = {
  26. name: subtexture.getAttribute("name"),
  27. x: parseInt(subtexture.getAttribute("x")),
  28. y: parseInt(subtexture.getAttribute("y")),
  29. width: parseInt(subtexture.getAttribute("width")),
  30. height: parseInt(subtexture.getAttribute("height"))
  31. };
  32. }
  33. };
  34. getSpriteData(name) {
  35. let imageLookup = this.atlas[name];
  36. if (!!imageLookup) {
  37. return imageLookup;
  38. }
  39. throw new Error(`Sprite ${name} not found in atlas ${this.xmlPath}`);
  40. }
  41. draw(context, name, position) {
  42. let imageLookup = this.getSpriteData(name);
  43. context.drawImage(this.spritesheetImage,
  44. imageLookup.x,
  45. imageLookup.y,
  46. imageLookup.width,
  47. imageLookup.height,
  48. position.x,
  49. position.y,
  50. imageLookup.width,
  51. imageLookup.height);
  52. };
  53. drawCentered(context, name, position) {
  54. let imageLookup = this.getSpriteData(name);
  55. context.drawImage(this.spritesheetImage,
  56. imageLookup.x,
  57. imageLookup.y,
  58. imageLookup.width,
  59. imageLookup.height,
  60. position.x - imageLookup.width / 2,
  61. position.y - imageLookup.height / 2,
  62. imageLookup.width,
  63. imageLookup.height);
  64. };
  65. drawBottomCentered(context, name, position) {
  66. let imageLookup = this.getSpriteData(name);
  67. context.drawImage(this.spritesheetImage,
  68. imageLookup.x,
  69. imageLookup.y,
  70. imageLookup.width,
  71. imageLookup.height,
  72. position.x - imageLookup.width / 2,
  73. position.y + imageLookup.height / 2,
  74. imageLookup.width,
  75. imageLookup.height);
  76. };
  77. getWidth(name) {
  78. return this.getSpriteData(name).width;
  79. };
  80. getHeight(name) {
  81. return this.getSpriteData(name).height;
  82. };
  83. }