app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*!
  2. * @pixi/app - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/app is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. 'use strict';
  9. Object.defineProperty(exports, '__esModule', { value: true });
  10. var core = require('@pixi/core');
  11. var display = require('@pixi/display');
  12. var utils = require('@pixi/utils');
  13. /**
  14. * Middleware for for Application's resize functionality
  15. * @private
  16. * @class
  17. */
  18. var ResizePlugin = /** @class */ (function () {
  19. function ResizePlugin() {
  20. }
  21. /**
  22. * Initialize the plugin with scope of application instance
  23. * @static
  24. * @private
  25. * @param {object} [options] - See application options
  26. */
  27. ResizePlugin.init = function (options) {
  28. var _this = this;
  29. Object.defineProperty(this, 'resizeTo',
  30. /**
  31. * The HTML element or window to automatically resize the
  32. * renderer's view element to match width and height.
  33. * @member {Window|HTMLElement}
  34. * @name resizeTo
  35. * @memberof PIXI.Application#
  36. */
  37. {
  38. set: function (dom) {
  39. globalThis.removeEventListener('resize', this.queueResize);
  40. this._resizeTo = dom;
  41. if (dom) {
  42. globalThis.addEventListener('resize', this.queueResize);
  43. this.resize();
  44. }
  45. },
  46. get: function () {
  47. return this._resizeTo;
  48. },
  49. });
  50. /**
  51. * Resize is throttled, so it's safe to call this multiple times per frame and it'll
  52. * only be called once.
  53. * @memberof PIXI.Application#
  54. * @method queueResize
  55. * @private
  56. */
  57. this.queueResize = function () {
  58. if (!_this._resizeTo) {
  59. return;
  60. }
  61. _this.cancelResize();
  62. // // Throttle resize events per raf
  63. _this._resizeId = requestAnimationFrame(function () { return _this.resize(); });
  64. };
  65. /**
  66. * Cancel the resize queue.
  67. * @memberof PIXI.Application#
  68. * @method cancelResize
  69. * @private
  70. */
  71. this.cancelResize = function () {
  72. if (_this._resizeId) {
  73. cancelAnimationFrame(_this._resizeId);
  74. _this._resizeId = null;
  75. }
  76. };
  77. /**
  78. * Execute an immediate resize on the renderer, this is not
  79. * throttled and can be expensive to call many times in a row.
  80. * Will resize only if `resizeTo` property is set.
  81. * @memberof PIXI.Application#
  82. * @method resize
  83. */
  84. this.resize = function () {
  85. if (!_this._resizeTo) {
  86. return;
  87. }
  88. // clear queue resize
  89. _this.cancelResize();
  90. var width;
  91. var height;
  92. // Resize to the window
  93. if (_this._resizeTo === globalThis.window) {
  94. width = globalThis.innerWidth;
  95. height = globalThis.innerHeight;
  96. }
  97. // Resize to other HTML entities
  98. else {
  99. var _a = _this._resizeTo, clientWidth = _a.clientWidth, clientHeight = _a.clientHeight;
  100. width = clientWidth;
  101. height = clientHeight;
  102. }
  103. _this.renderer.resize(width, height);
  104. };
  105. // On resize
  106. this._resizeId = null;
  107. this._resizeTo = null;
  108. this.resizeTo = options.resizeTo || null;
  109. };
  110. /**
  111. * Clean up the ticker, scoped to application
  112. * @static
  113. * @private
  114. */
  115. ResizePlugin.destroy = function () {
  116. globalThis.removeEventListener('resize', this.queueResize);
  117. this.cancelResize();
  118. this.cancelResize = null;
  119. this.queueResize = null;
  120. this.resizeTo = null;
  121. this.resize = null;
  122. };
  123. /** @ignore */
  124. ResizePlugin.extension = core.ExtensionType.Application;
  125. return ResizePlugin;
  126. }());
  127. /**
  128. * Convenience class to create a new PIXI application.
  129. *
  130. * This class automatically creates the renderer, ticker and root container.
  131. * @example
  132. * // Create the application
  133. * const app = new PIXI.Application();
  134. *
  135. * // Add the view to the DOM
  136. * document.body.appendChild(app.view);
  137. *
  138. * // ex, add display objects
  139. * app.stage.addChild(PIXI.Sprite.from('something.png'));
  140. * @class
  141. * @memberof PIXI
  142. */
  143. var Application = /** @class */ (function () {
  144. /**
  145. * @param {PIXI.IApplicationOptions} [options] - The optional application and renderer parameters.
  146. * @param {boolean} [options.antialias=false] -
  147. * **WebGL Only.** Whether to enable anti-aliasing. This may affect performance.
  148. * @param {boolean} [options.autoDensity=false] -
  149. * Whether the CSS dimensions of the renderer's view should be resized automatically.
  150. * @param {boolean} [options.autoStart=true] - Automatically starts the rendering after the construction.
  151. * **Note**: Setting this parameter to false does NOT stop the shared ticker even if you set
  152. * `options.sharedTicker` to `true` in case that it is already started. Stop it by your own.
  153. * @param {number} [options.backgroundAlpha=1] -
  154. * Transparency of the background color, value from `0` (fully transparent) to `1` (fully opaque).
  155. * @param {number} [options.backgroundColor=0x000000] -
  156. * The background color used to clear the canvas. It accepts hex numbers (e.g. `0xff0000`).
  157. * @param {boolean} [options.clearBeforeRender=true] - Whether to clear the canvas before new render passes.
  158. * @param {PIXI.IRenderingContext} [options.context] - **WebGL Only.** User-provided WebGL rendering context object.
  159. * @param {boolean} [options.forceCanvas=false] -
  160. * Force using {@link PIXI.CanvasRenderer}, even if WebGL is available. This option only is available when
  161. * using **pixi.js-legacy** or **@pixi/canvas-renderer** packages, otherwise it is ignored.
  162. * @param {number} [options.height=600] - The height of the renderer's view.
  163. * @param {string} [options.powerPreference] -
  164. * **WebGL Only.** A hint indicating what configuration of GPU is suitable for the WebGL context,
  165. * can be `'default'`, `'high-performance'` or `'low-power'`.
  166. * Setting to `'high-performance'` will prioritize rendering performance over power consumption,
  167. * while setting to `'low-power'` will prioritize power saving over rendering performance.
  168. * @param {boolean} [options.premultipliedAlpha=true] -
  169. * **WebGL Only.** Whether the compositor will assume the drawing buffer contains colors with premultiplied alpha.
  170. * @param {boolean} [options.preserveDrawingBuffer=false] -
  171. * **WebGL Only.** Whether to enable drawing buffer preservation. If enabled, the drawing buffer will preserve
  172. * its value until cleared or overwritten. Enable this if you need to call `toDataUrl` on the WebGL context.
  173. * @param {Window|HTMLElement} [options.resizeTo] - Element to automatically resize stage to.
  174. * @param {number} [options.resolution=PIXI.settings.RESOLUTION] -
  175. * The resolution / device pixel ratio of the renderer.
  176. * @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.Loader.shared, `false` to create new Loader.
  177. * @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.Ticker.shared, `false` to create new ticker.
  178. * If set to `false`, you cannot register a handler to occur before anything that runs on the shared ticker.
  179. * The system ticker will always run before both the shared ticker and the app ticker.
  180. * @param {boolean} [options.transparent] -
  181. * **Deprecated since 6.0.0, Use `backgroundAlpha` instead.** \
  182. * `true` sets `backgroundAlpha` to `0`, `false` sets `backgroundAlpha` to `1`.
  183. * @param {boolean|'notMultiplied'} [options.useContextAlpha=true] -
  184. * Pass-through value for canvas' context attribute `alpha`. This option is for cases where the
  185. * canvas needs to be opaque, possibly for performance reasons on some older devices.
  186. * If you want to set transparency, please use `backgroundAlpha`. \
  187. * **WebGL Only:** When set to `'notMultiplied'`, the canvas' context attribute `alpha` will be
  188. * set to `true` and `premultipliedAlpha` will be to `false`.
  189. * @param {HTMLCanvasElement} [options.view=null] -
  190. * The canvas to use as the view. If omitted, a new canvas will be created.
  191. * @param {number} [options.width=800] - The width of the renderer's view.
  192. */
  193. function Application(options) {
  194. var _this = this;
  195. /**
  196. * The root display container that's rendered.
  197. * @member {PIXI.Container}
  198. */
  199. this.stage = new display.Container();
  200. // The default options
  201. options = Object.assign({
  202. forceCanvas: false,
  203. }, options);
  204. this.renderer = core.autoDetectRenderer(options);
  205. // install plugins here
  206. Application._plugins.forEach(function (plugin) {
  207. plugin.init.call(_this, options);
  208. });
  209. }
  210. /**
  211. * Use the {@link PIXI.extensions.add} API to register plugins.
  212. * @deprecated since 6.5.0
  213. * @static
  214. * @param {PIXI.IApplicationPlugin} plugin - Plugin being installed
  215. */
  216. Application.registerPlugin = function (plugin) {
  217. utils.deprecation('6.5.0', 'Application.registerPlugin() is deprecated, use extensions.add()');
  218. core.extensions.add({
  219. type: core.ExtensionType.Application,
  220. ref: plugin,
  221. });
  222. };
  223. /** Render the current stage. */
  224. Application.prototype.render = function () {
  225. this.renderer.render(this.stage);
  226. };
  227. Object.defineProperty(Application.prototype, "view", {
  228. /**
  229. * Reference to the renderer's canvas element.
  230. * @member {HTMLCanvasElement}
  231. * @readonly
  232. */
  233. get: function () {
  234. return this.renderer.view;
  235. },
  236. enumerable: false,
  237. configurable: true
  238. });
  239. Object.defineProperty(Application.prototype, "screen", {
  240. /**
  241. * Reference to the renderer's screen rectangle. Its safe to use as `filterArea` or `hitArea` for the whole screen.
  242. * @member {PIXI.Rectangle}
  243. * @readonly
  244. */
  245. get: function () {
  246. return this.renderer.screen;
  247. },
  248. enumerable: false,
  249. configurable: true
  250. });
  251. /**
  252. * Destroy and don't use after this.
  253. * @param {boolean} [removeView=false] - Automatically remove canvas from DOM.
  254. * @param {object|boolean} [stageOptions] - Options parameter. A boolean will act as if all options
  255. * have been set to that value
  256. * @param {boolean} [stageOptions.children=false] - if set to true, all the children will have their destroy
  257. * method called as well. 'stageOptions' will be passed on to those calls.
  258. * @param {boolean} [stageOptions.texture=false] - Only used for child Sprites if stageOptions.children is set
  259. * to true. Should it destroy the texture of the child sprite
  260. * @param {boolean} [stageOptions.baseTexture=false] - Only used for child Sprites if stageOptions.children is set
  261. * to true. Should it destroy the base texture of the child sprite
  262. */
  263. Application.prototype.destroy = function (removeView, stageOptions) {
  264. var _this = this;
  265. // Destroy plugins in the opposite order
  266. // which they were constructed
  267. var plugins = Application._plugins.slice(0);
  268. plugins.reverse();
  269. plugins.forEach(function (plugin) {
  270. plugin.destroy.call(_this);
  271. });
  272. this.stage.destroy(stageOptions);
  273. this.stage = null;
  274. this.renderer.destroy(removeView);
  275. this.renderer = null;
  276. };
  277. /** Collection of installed plugins. */
  278. Application._plugins = [];
  279. return Application;
  280. }());
  281. core.extensions.handleByList(core.ExtensionType.Application, Application._plugins);
  282. core.extensions.add(ResizePlugin);
  283. exports.Application = Application;
  284. exports.ResizePlugin = ResizePlugin;
  285. //# sourceMappingURL=app.js.map