sprite-animated.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*!
  2. * @pixi/sprite-animated - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/sprite-animated 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 sprite = require('@pixi/sprite');
  12. var ticker = require('@pixi/ticker');
  13. /*! *****************************************************************************
  14. Copyright (c) Microsoft Corporation.
  15. Permission to use, copy, modify, and/or distribute this software for any
  16. purpose with or without fee is hereby granted.
  17. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  18. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  19. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  20. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  21. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  23. PERFORMANCE OF THIS SOFTWARE.
  24. ***************************************************************************** */
  25. /* global Reflect, Promise */
  26. var extendStatics = function(d, b) {
  27. extendStatics = Object.setPrototypeOf ||
  28. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  29. function (d, b) { for (var p in b) { if (b.hasOwnProperty(p)) { d[p] = b[p]; } } };
  30. return extendStatics(d, b);
  31. };
  32. function __extends(d, b) {
  33. extendStatics(d, b);
  34. function __() { this.constructor = d; }
  35. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  36. }
  37. /**
  38. * An AnimatedSprite is a simple way to display an animation depicted by a list of textures.
  39. *
  40. * ```js
  41. * let alienImages = ["image_sequence_01.png","image_sequence_02.png","image_sequence_03.png","image_sequence_04.png"];
  42. * let textureArray = [];
  43. *
  44. * for (let i=0; i < 4; i++)
  45. * {
  46. * let texture = PIXI.Texture.from(alienImages[i]);
  47. * textureArray.push(texture);
  48. * };
  49. *
  50. * let animatedSprite = new PIXI.AnimatedSprite(textureArray);
  51. * ```
  52. *
  53. * The more efficient and simpler way to create an animated sprite is using a {@link PIXI.Spritesheet}
  54. * containing the animation definitions:
  55. *
  56. * ```js
  57. * PIXI.Loader.shared.add("assets/spritesheet.json").load(setup);
  58. *
  59. * function setup() {
  60. * let sheet = PIXI.Loader.shared.resources["assets/spritesheet.json"].spritesheet;
  61. * animatedSprite = new PIXI.AnimatedSprite(sheet.animations["image_sequence"]);
  62. * ...
  63. * }
  64. * ```
  65. * @memberof PIXI
  66. */
  67. var AnimatedSprite = /** @class */ (function (_super) {
  68. __extends(AnimatedSprite, _super);
  69. /**
  70. * @param textures - An array of {@link PIXI.Texture} or frame
  71. * objects that make up the animation.
  72. * @param {boolean} [autoUpdate=true] - Whether to use PIXI.Ticker.shared to auto update animation time.
  73. */
  74. function AnimatedSprite(textures, autoUpdate) {
  75. if (autoUpdate === void 0) { autoUpdate = true; }
  76. var _this = _super.call(this, textures[0] instanceof core.Texture ? textures[0] : textures[0].texture) || this;
  77. _this._textures = null;
  78. _this._durations = null;
  79. _this._autoUpdate = autoUpdate;
  80. _this._isConnectedToTicker = false;
  81. _this.animationSpeed = 1;
  82. _this.loop = true;
  83. _this.updateAnchor = false;
  84. _this.onComplete = null;
  85. _this.onFrameChange = null;
  86. _this.onLoop = null;
  87. _this._currentTime = 0;
  88. _this._playing = false;
  89. _this._previousFrame = null;
  90. _this.textures = textures;
  91. return _this;
  92. }
  93. /** Stops the AnimatedSprite. */
  94. AnimatedSprite.prototype.stop = function () {
  95. if (!this._playing) {
  96. return;
  97. }
  98. this._playing = false;
  99. if (this._autoUpdate && this._isConnectedToTicker) {
  100. ticker.Ticker.shared.remove(this.update, this);
  101. this._isConnectedToTicker = false;
  102. }
  103. };
  104. /** Plays the AnimatedSprite. */
  105. AnimatedSprite.prototype.play = function () {
  106. if (this._playing) {
  107. return;
  108. }
  109. this._playing = true;
  110. if (this._autoUpdate && !this._isConnectedToTicker) {
  111. ticker.Ticker.shared.add(this.update, this, ticker.UPDATE_PRIORITY.HIGH);
  112. this._isConnectedToTicker = true;
  113. }
  114. };
  115. /**
  116. * Stops the AnimatedSprite and goes to a specific frame.
  117. * @param frameNumber - Frame index to stop at.
  118. */
  119. AnimatedSprite.prototype.gotoAndStop = function (frameNumber) {
  120. this.stop();
  121. var previousFrame = this.currentFrame;
  122. this._currentTime = frameNumber;
  123. if (previousFrame !== this.currentFrame) {
  124. this.updateTexture();
  125. }
  126. };
  127. /**
  128. * Goes to a specific frame and begins playing the AnimatedSprite.
  129. * @param frameNumber - Frame index to start at.
  130. */
  131. AnimatedSprite.prototype.gotoAndPlay = function (frameNumber) {
  132. var previousFrame = this.currentFrame;
  133. this._currentTime = frameNumber;
  134. if (previousFrame !== this.currentFrame) {
  135. this.updateTexture();
  136. }
  137. this.play();
  138. };
  139. /**
  140. * Updates the object transform for rendering.
  141. * @param deltaTime - Time since last tick.
  142. */
  143. AnimatedSprite.prototype.update = function (deltaTime) {
  144. if (!this._playing) {
  145. return;
  146. }
  147. var elapsed = this.animationSpeed * deltaTime;
  148. var previousFrame = this.currentFrame;
  149. if (this._durations !== null) {
  150. var lag = this._currentTime % 1 * this._durations[this.currentFrame];
  151. lag += elapsed / 60 * 1000;
  152. while (lag < 0) {
  153. this._currentTime--;
  154. lag += this._durations[this.currentFrame];
  155. }
  156. var sign = Math.sign(this.animationSpeed * deltaTime);
  157. this._currentTime = Math.floor(this._currentTime);
  158. while (lag >= this._durations[this.currentFrame]) {
  159. lag -= this._durations[this.currentFrame] * sign;
  160. this._currentTime += sign;
  161. }
  162. this._currentTime += lag / this._durations[this.currentFrame];
  163. }
  164. else {
  165. this._currentTime += elapsed;
  166. }
  167. if (this._currentTime < 0 && !this.loop) {
  168. this.gotoAndStop(0);
  169. if (this.onComplete) {
  170. this.onComplete();
  171. }
  172. }
  173. else if (this._currentTime >= this._textures.length && !this.loop) {
  174. this.gotoAndStop(this._textures.length - 1);
  175. if (this.onComplete) {
  176. this.onComplete();
  177. }
  178. }
  179. else if (previousFrame !== this.currentFrame) {
  180. if (this.loop && this.onLoop) {
  181. if (this.animationSpeed > 0 && this.currentFrame < previousFrame) {
  182. this.onLoop();
  183. }
  184. else if (this.animationSpeed < 0 && this.currentFrame > previousFrame) {
  185. this.onLoop();
  186. }
  187. }
  188. this.updateTexture();
  189. }
  190. };
  191. /** Updates the displayed texture to match the current frame index. */
  192. AnimatedSprite.prototype.updateTexture = function () {
  193. var currentFrame = this.currentFrame;
  194. if (this._previousFrame === currentFrame) {
  195. return;
  196. }
  197. this._previousFrame = currentFrame;
  198. this._texture = this._textures[currentFrame];
  199. this._textureID = -1;
  200. this._textureTrimmedID = -1;
  201. this._cachedTint = 0xFFFFFF;
  202. this.uvs = this._texture._uvs.uvsFloat32;
  203. if (this.updateAnchor) {
  204. this._anchor.copyFrom(this._texture.defaultAnchor);
  205. }
  206. if (this.onFrameChange) {
  207. this.onFrameChange(this.currentFrame);
  208. }
  209. };
  210. /**
  211. * Stops the AnimatedSprite and destroys it.
  212. * @param {object|boolean} [options] - Options parameter. A boolean will act as if all options
  213. * have been set to that value.
  214. * @param {boolean} [options.children=false] - If set to true, all the children will have their destroy
  215. * method called as well. 'options' will be passed on to those calls.
  216. * @param {boolean} [options.texture=false] - Should it destroy the current texture of the sprite as well.
  217. * @param {boolean} [options.baseTexture=false] - Should it destroy the base texture of the sprite as well.
  218. */
  219. AnimatedSprite.prototype.destroy = function (options) {
  220. this.stop();
  221. _super.prototype.destroy.call(this, options);
  222. this.onComplete = null;
  223. this.onFrameChange = null;
  224. this.onLoop = null;
  225. };
  226. /**
  227. * A short hand way of creating an AnimatedSprite from an array of frame ids.
  228. * @param frames - The array of frames ids the AnimatedSprite will use as its texture frames.
  229. * @returns - The new animated sprite with the specified frames.
  230. */
  231. AnimatedSprite.fromFrames = function (frames) {
  232. var textures = [];
  233. for (var i = 0; i < frames.length; ++i) {
  234. textures.push(core.Texture.from(frames[i]));
  235. }
  236. return new AnimatedSprite(textures);
  237. };
  238. /**
  239. * A short hand way of creating an AnimatedSprite from an array of image ids.
  240. * @param images - The array of image urls the AnimatedSprite will use as its texture frames.
  241. * @returns The new animate sprite with the specified images as frames.
  242. */
  243. AnimatedSprite.fromImages = function (images) {
  244. var textures = [];
  245. for (var i = 0; i < images.length; ++i) {
  246. textures.push(core.Texture.from(images[i]));
  247. }
  248. return new AnimatedSprite(textures);
  249. };
  250. Object.defineProperty(AnimatedSprite.prototype, "totalFrames", {
  251. /**
  252. * The total number of frames in the AnimatedSprite. This is the same as number of textures
  253. * assigned to the AnimatedSprite.
  254. * @readonly
  255. * @default 0
  256. */
  257. get: function () {
  258. return this._textures.length;
  259. },
  260. enumerable: false,
  261. configurable: true
  262. });
  263. Object.defineProperty(AnimatedSprite.prototype, "textures", {
  264. /** The array of textures used for this AnimatedSprite. */
  265. get: function () {
  266. return this._textures;
  267. },
  268. set: function (value) {
  269. if (value[0] instanceof core.Texture) {
  270. this._textures = value;
  271. this._durations = null;
  272. }
  273. else {
  274. this._textures = [];
  275. this._durations = [];
  276. for (var i = 0; i < value.length; i++) {
  277. this._textures.push(value[i].texture);
  278. this._durations.push(value[i].time);
  279. }
  280. }
  281. this._previousFrame = null;
  282. this.gotoAndStop(0);
  283. this.updateTexture();
  284. },
  285. enumerable: false,
  286. configurable: true
  287. });
  288. Object.defineProperty(AnimatedSprite.prototype, "currentFrame", {
  289. /**
  290. * The AnimatedSprites current frame index.
  291. * @readonly
  292. */
  293. get: function () {
  294. var currentFrame = Math.floor(this._currentTime) % this._textures.length;
  295. if (currentFrame < 0) {
  296. currentFrame += this._textures.length;
  297. }
  298. return currentFrame;
  299. },
  300. enumerable: false,
  301. configurable: true
  302. });
  303. Object.defineProperty(AnimatedSprite.prototype, "playing", {
  304. /**
  305. * Indicates if the AnimatedSprite is currently playing.
  306. * @readonly
  307. */
  308. get: function () {
  309. return this._playing;
  310. },
  311. enumerable: false,
  312. configurable: true
  313. });
  314. Object.defineProperty(AnimatedSprite.prototype, "autoUpdate", {
  315. /** Whether to use PIXI.Ticker.shared to auto update animation time. */
  316. get: function () {
  317. return this._autoUpdate;
  318. },
  319. set: function (value) {
  320. if (value !== this._autoUpdate) {
  321. this._autoUpdate = value;
  322. if (!this._autoUpdate && this._isConnectedToTicker) {
  323. ticker.Ticker.shared.remove(this.update, this);
  324. this._isConnectedToTicker = false;
  325. }
  326. else if (this._autoUpdate && !this._isConnectedToTicker && this._playing) {
  327. ticker.Ticker.shared.add(this.update, this);
  328. this._isConnectedToTicker = true;
  329. }
  330. }
  331. },
  332. enumerable: false,
  333. configurable: true
  334. });
  335. return AnimatedSprite;
  336. }(sprite.Sprite));
  337. exports.AnimatedSprite = AnimatedSprite;
  338. //# sourceMappingURL=sprite-animated.js.map