Source: animate/load.js

animate/load.js

  1. import * as loaders from '../loaders';
  2. /**
  3. * Load the stage class and preload any assets
  4. * @method PIXI.animate.load
  5. * @param {Object} options Options for loading.
  6. * @param {Function} options.stage Reference to the stage class
  7. * @param {Object} [options.stage.assets] Assets used to preload
  8. * @param {PIXI.Container} options.parent The Container to auto-add the stage to.
  9. * @param {String} [options.basePath] Base root directory
  10. * @param {boolean} [options.createInstance] enable or disable automatic instantiation of stage
  11. * @return {PIXI.loaders.Loader} instance of PIXI resource loader
  12. */
  13. /**
  14. * Load the stage class and preload any assets
  15. * ```
  16. * let renderer = new PIXI.autoDetectRenderer(1280, 720);
  17. * let stage = new PIXI.Container();
  18. * PIXI.animate.load(lib.MyStage, function(instance){
  19. * stage.addChild(instance);
  20. * });
  21. * function update() {
  22. * renderer.render(stage);
  23. * update();
  24. * }
  25. * update();
  26. * ```
  27. * @method PIXI.animate.load
  28. * @param {Function} StageRef Reference to the stage class.
  29. * @param {Object} [StageRef.assets] Assets used to preload.
  30. * @param {Function} complete The callback function when complete.
  31. * @return {PIXI.loaders.Loader} instance of PIXI resource loader
  32. */
  33. /**
  34. * Load the stage class and preload any assets
  35. * ```
  36. * let renderer = new PIXI.autoDetectRenderer(1280, 720);
  37. * let stage = new PIXI.Container();
  38. * PIXI.animate.load(lib.MyStage, stage);
  39. * function update() {
  40. * renderer.render(stage);
  41. * update();
  42. * }
  43. * update();
  44. * ```
  45. * @method PIXI.animate.load
  46. * @param {Function} StageRef Reference to the stage class.
  47. * @param {Object} [StageRef.assets] Assets used to preload.
  48. * @param {PIXI.Container} parent The Container to auto-add the stage to.
  49. * @param {String} [basePath] Base root directory
  50. * @return {PIXI.loaders.Loader} instance of PIXI resource loader
  51. */
  52. /**
  53. * Load the stage class and preload any assets
  54. * ```
  55. * let basePath = "file:/path/to/assets";
  56. * let renderer = new PIXI.autoDetectRenderer(1280, 720);
  57. *
  58. * let extensions = PIXI.compressedTextures.detectExtensions(renderer);
  59. * let loader = new PIXI.loaders.Loader();
  60. * // this is an example of setting up a pre loader plugin to handle compressed textures in this case
  61. * loader.pre(PIXI.compressedTextures.extensionChooser(extensions));
  62. *
  63. * // specify metadata this way if you want to provide a default loading strategy for all assets listed in the PIXI animation
  64. * let metadata = { default: { metadata: { imageMetadata: { choice: [".crn"] } } } };
  65. * // specify metadata this way if you want to provide a specific loading strategy for a certain asset listed inside the PIXI animation library
  66. * let metadata = { MyStage_atlas_1: { metadata: { imageMetadata: { choice: [".crn"] } } } };
  67. *
  68. * let stage = new PIXI.Container();
  69. * PIXI.animate.load(lib.MyStage, stage, ()=>{}, basePath, loader, metadata);
  70. * function update() {
  71. * renderer.render(stage);
  72. * update();
  73. * }
  74. * update();
  75. * ```
  76. * @method PIXI.animate.load
  77. * @param {Function} StageRef Reference to the stage class.
  78. * @param {Object} [StageRef.assets] Assets used to preload.
  79. * @param {PIXI.Container} parent The Container to auto-add the stage to.
  80. * @param {Function} [complete] The callback function when complete.
  81. * @param {String} [basePath] Base root directory
  82. * @param {PIXI.loaders.Loader} [loader] A Pixi loader object
  83. * @param {Object} [metadata] A metadata object for the asset being loaded
  84. * @return {PIXI.loaders.Loader} instance of PIXI resource loader
  85. */
  86. const load = function(options, parent, complete, basePath, loader, metadata) {
  87. // Support arguments (ref, complete, basePath)
  88. if (typeof parent === "function") {
  89. basePath = complete;
  90. complete = parent;
  91. parent = null;
  92. } else {
  93. if (typeof complete === "string") {
  94. basePath = complete;
  95. complete = null;
  96. }
  97. }
  98. if (typeof options === "function") {
  99. options = {
  100. stage: options,
  101. parent: parent,
  102. basePath: basePath || "",
  103. complete: complete
  104. };
  105. }
  106. options = Object.assign({
  107. stage: null,
  108. parent: null,
  109. basePath: '',
  110. complete: null,
  111. createInstance: true
  112. }, options || {});
  113. loader = loader || new loaders.Loader();
  114. function done() {
  115. let instance = (options.createInstance && typeof options.stage === "function") ? new options.stage() : null;
  116. if (options.parent) {
  117. options.parent.addChild(instance);
  118. }
  119. if (options.complete) {
  120. options.complete(instance, loader);
  121. }
  122. }
  123. // Check for assets to preload
  124. let assets = options.stage.assets || {};
  125. if (assets && Object.keys(assets).length) {
  126. // assetBaseDir can accept either with trailing slash or not
  127. let basePath = options.basePath;
  128. if (basePath) {
  129. basePath += "/";
  130. }
  131. for (let id in assets) {
  132. var data = null;
  133. if(metadata) {
  134. // if the metadata was supplied for this particular asset, use these options
  135. if(metadata[id]) {
  136. data = metadata[id];
  137. }
  138. // if the metadata supplied a default option
  139. else if (metadata.default){
  140. data = metadata.default;
  141. }
  142. }
  143. loader.add(id, basePath + assets[id], data);
  144. }
  145. loader.once('complete', done).load();
  146. } else {
  147. // tiny case where there's only text and no shapes/animations
  148. done();
  149. }
  150. return loader;
  151. };
  152. export default load;