- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
import { Texture } from '@pixi/core';
import { Sprite } from '@pixi/sprite';
import { Ticker, UPDATE_PRIORITY } from '@pixi/ticker';
import type { IDestroyOptions } from '@pixi/display';
/**
* An AnimatedSprite is a simple way to display an animation depicted by a list of textures.
*
* ```js
* let alienImages = ["image_sequence_01.png","image_sequence_02.png","image_sequence_03.png","image_sequence_04.png"];
* let textureArray = [];
*
* for (let i=0; i < 4; i++)
* {
* let texture = PIXI.Texture.from(alienImages[i]);
* textureArray.push(texture);
* };
*
* let animatedSprite = new PIXI.AnimatedSprite(textureArray);
* ```
*
* The more efficient and simpler way to create an animated sprite is using a {@link PIXI.Spritesheet}
* containing the animation definitions:
*
* ```js
* PIXI.Loader.shared.add("assets/spritesheet.json").load(setup);
*
* function setup() {
* let sheet = PIXI.Loader.shared.resources["assets/spritesheet.json"].spritesheet;
* animatedSprite = new PIXI.AnimatedSprite(sheet.animations["image_sequence"]);
* ...
* }
* ```
* @memberof PIXI
*/
export class AnimatedSprite extends Sprite
{
/**
* The speed that the AnimatedSprite will play at. Higher is faster, lower is slower.
* @default 1
*/
public animationSpeed: number;
/**
* Whether or not the animate sprite repeats after playing.
* @default true
*/
public loop: boolean;
/**
* Update anchor to [Texture's defaultAnchor]{@link PIXI.Texture#defaultAnchor} when frame changes.
*
* Useful with [sprite sheet animations]{@link PIXI.Spritesheet#animations} created with tools.
* Changing anchor for each frame allows to pin sprite origin to certain moving feature
* of the frame (e.g. left foot).
*
* Note: Enabling this will override any previously set `anchor` on each frame change.
* @default false
*/
public updateAnchor: boolean;
/**
* User-assigned function to call when an AnimatedSprite finishes playing.
* @example
* animation.onComplete = function () {
* // finished!
* };
*/
public onComplete?: () => void;
/**
* User-assigned function to call when an AnimatedSprite changes which texture is being rendered.
* @example
* animation.onFrameChange = function () {
* // updated!
* };
*/
public onFrameChange?: (currentFrame: number) => void;
/**
* User-assigned function to call when `loop` is true, and an AnimatedSprite is played and
* loops around to start again.
* @example
* animation.onLoop = function () {
* // looped!
* };
*/
public onLoop?: () => void;
private _playing: boolean;
private _textures: Texture[];
private _durations: number[];
/**
* `true` uses PIXI.Ticker.shared to auto update animation time.
* @default true
*/
private _autoUpdate: boolean;
/**
* `true` if the instance is currently connected to PIXI.Ticker.shared to auto update animation time.
* @default false
*/
private _isConnectedToTicker: boolean;
/** Elapsed time since animation has been started, used internally to display current texture. */
private _currentTime: number;
/** The texture index that was displayed last time. */
private _previousFrame: number;
/**
* @param textures - An array of {@link PIXI.Texture} or frame
* objects that make up the animation.
* @param {boolean} [autoUpdate=true] - Whether to use PIXI.Ticker.shared to auto update animation time.
*/
constructor(textures: Texture[] | FrameObject[], autoUpdate = true)
{
super(textures[0] instanceof Texture ? textures[0] : textures[0].texture);
this._textures = null;
this._durations = null;
this._autoUpdate = autoUpdate;
this._isConnectedToTicker = false;
this.animationSpeed = 1;
this.loop = true;
this.updateAnchor = false;
this.onComplete = null;
this.onFrameChange = null;
this.onLoop = null;
this._currentTime = 0;
this._playing = false;
this._previousFrame = null;
this.textures = textures;
}
/** Stops the AnimatedSprite. */
public stop(): void
{
if (!this._playing)
{
return;
}
this._playing = false;
if (this._autoUpdate && this._isConnectedToTicker)
{
Ticker.shared.remove(this.update, this);
this._isConnectedToTicker = false;
}
}
/** Plays the AnimatedSprite. */
public play(): void
{
if (this._playing)
{
return;
}
this._playing = true;
if (this._autoUpdate && !this._isConnectedToTicker)
{
Ticker.shared.add(this.update, this, UPDATE_PRIORITY.HIGH);
this._isConnectedToTicker = true;
}
}
/**
* Stops the AnimatedSprite and goes to a specific frame.
* @param frameNumber - Frame index to stop at.
*/
public gotoAndStop(frameNumber: number): void
{
this.stop();
const previousFrame = this.currentFrame;
this._currentTime = frameNumber;
if (previousFrame !== this.currentFrame)
{
this.updateTexture();
}
}
/**
* Goes to a specific frame and begins playing the AnimatedSprite.
* @param frameNumber - Frame index to start at.
*/
public gotoAndPlay(frameNumber: number): void
{
const previousFrame = this.currentFrame;
this._currentTime = frameNumber;
if (previousFrame !== this.currentFrame)
{
this.updateTexture();
}
this.play();
}
/**
* Updates the object transform for rendering.
* @param deltaTime - Time since last tick.
*/
update(deltaTime: number): void
{
if (!this._playing)
{
return;
}
const elapsed = this.animationSpeed * deltaTime;
const previousFrame = this.currentFrame;
if (this._durations !== null)
{
let lag = this._currentTime % 1 * this._durations[this.currentFrame];
lag += elapsed / 60 * 1000;
while (lag < 0)
{
this._currentTime--;
lag += this._durations[this.currentFrame];
}
const sign = Math.sign(this.animationSpeed * deltaTime);
this._currentTime = Math.floor(this._currentTime);
while (lag >= this._durations[this.currentFrame])
{
lag -= this._durations[this.currentFrame] * sign;
this._currentTime += sign;
}
this._currentTime += lag / this._durations[this.currentFrame];
}
else
{
this._currentTime += elapsed;
}
if (this._currentTime < 0 && !this.loop)
{
this.gotoAndStop(0);
if (this.onComplete)
{
this.onComplete();
}
}
else if (this._currentTime >= this._textures.length && !this.loop)
{
this.gotoAndStop(this._textures.length - 1);
if (this.onComplete)
{
this.onComplete();
}
}
else if (previousFrame !== this.currentFrame)
{
if (this.loop && this.onLoop)
{
if (this.animationSpeed > 0 && this.currentFrame < previousFrame)
{
this.onLoop();
}
else if (this.animationSpeed < 0 && this.currentFrame > previousFrame)
{
this.onLoop();
}
}
this.updateTexture();
}
}
/** Updates the displayed texture to match the current frame index. */
private updateTexture(): void
{
const currentFrame = this.currentFrame;
if (this._previousFrame === currentFrame)
{
return;
}
this._previousFrame = currentFrame;
this._texture = this._textures[currentFrame];
this._textureID = -1;
this._textureTrimmedID = -1;
this._cachedTint = 0xFFFFFF;
this.uvs = this._texture._uvs.uvsFloat32;
if (this.updateAnchor)
{
this._anchor.copyFrom(this._texture.defaultAnchor);
}
if (this.onFrameChange)
{
this.onFrameChange(this.currentFrame);
}
}
/**
* Stops the AnimatedSprite and destroys it.
* @param {object|boolean} [options] - Options parameter. A boolean will act as if all options
* have been set to that value.
* @param {boolean} [options.children=false] - If set to true, all the children will have their destroy
* method called as well. 'options' will be passed on to those calls.
* @param {boolean} [options.texture=false] - Should it destroy the current texture of the sprite as well.
* @param {boolean} [options.baseTexture=false] - Should it destroy the base texture of the sprite as well.
*/
public destroy(options?: IDestroyOptions | boolean): void
{
this.stop();
super.destroy(options);
this.onComplete = null;
this.onFrameChange = null;
this.onLoop = null;
}
/**
* A short hand way of creating an AnimatedSprite from an array of frame ids.
* @param frames - The array of frames ids the AnimatedSprite will use as its texture frames.
* @returns - The new animated sprite with the specified frames.
*/
public static fromFrames(frames: string[]): AnimatedSprite
{
const textures = [];
for (let i = 0; i < frames.length; ++i)
{
textures.push(Texture.from(frames[i]));
}
return new AnimatedSprite(textures);
}
/**
* A short hand way of creating an AnimatedSprite from an array of image ids.
* @param images - The array of image urls the AnimatedSprite will use as its texture frames.
* @returns The new animate sprite with the specified images as frames.
*/
public static fromImages(images: string[]): AnimatedSprite
{
const textures = [];
for (let i = 0; i < images.length; ++i)
{
textures.push(Texture.from(images[i]));
}
return new AnimatedSprite(textures);
}
/**
* The total number of frames in the AnimatedSprite. This is the same as number of textures
* assigned to the AnimatedSprite.
* @readonly
* @default 0
*/
get totalFrames(): number
{
return this._textures.length;
}
/** The array of textures used for this AnimatedSprite. */
get textures(): Texture[] | FrameObject[]
{
return this._textures;
}
set textures(value: Texture[] | FrameObject[])
{
if (value[0] instanceof Texture)
{
this._textures = value as Texture[];
this._durations = null;
}
else
{
this._textures = [];
this._durations = [];
for (let i = 0; i < value.length; i++)
{
this._textures.push((value[i] as FrameObject).texture);
this._durations.push((value[i] as FrameObject).time);
}
}
this._previousFrame = null;
this.gotoAndStop(0);
this.updateTexture();
}
/**
* The AnimatedSprites current frame index.
* @readonly
*/
get currentFrame(): number
{
let currentFrame = Math.floor(this._currentTime) % this._textures.length;
if (currentFrame < 0)
{
currentFrame += this._textures.length;
}
return currentFrame;
}
/**
* Indicates if the AnimatedSprite is currently playing.
* @readonly
*/
get playing(): boolean
{
return this._playing;
}
/** Whether to use PIXI.Ticker.shared to auto update animation time. */
get autoUpdate(): boolean
{
return this._autoUpdate;
}
set autoUpdate(value: boolean)
{
if (value !== this._autoUpdate)
{
this._autoUpdate = value;
if (!this._autoUpdate && this._isConnectedToTicker)
{
Ticker.shared.remove(this.update, this);
this._isConnectedToTicker = false;
}
else if (this._autoUpdate && !this._isConnectedToTicker && this._playing)
{
Ticker.shared.add(this.update, this);
this._isConnectedToTicker = true;
}
}
}
}
/** @memberof PIXI.AnimatedSprite */
export interface FrameObject
{
/** The {@link PIXI.Texture} of the frame. */
texture: Texture;
/** The duration of the frame, in milliseconds. */
time: number;
}