UNPKG

3.79 kBJavaScriptView Raw
1"use strict";
2
3var buffer = require("./buffer");
4
5/**
6 * An animated picture made of multiple images. Animations are constructed for you by {@link AnimationLoader}.
7 * @constructor
8 */
9function Animation() {
10 /**
11 * The individual frames making up the animation.
12 * @member {Array}
13 * @private
14 */
15 this.frames = [];
16 /**
17 * The currently displayed frame of the animation.
18 * @member {number}
19 */
20 this.frame = 0;
21 /**
22 * How long, in milliseconds, the current frame has been displayed for.
23 * @member {number}
24 * @private
25 */
26 this.elapsedMillis = 0;
27 /**
28 * The frame at which to restart the animation after the last frame plays. It defaults to 0, so animations loop. If you want to disable looping, set this to the last frame. Otherwise, you can set it to one of the middle frames to have non-repeating introductory frames, while having looping later frames.
29 * @member {number}
30 */
31 this.repeatAt = 0;
32 /**
33 * The width of the first frame of the animation.
34 * @member {number}
35 * @readonly
36 */
37 this.width = 0;
38 /**
39 * The height of the first frame of the animation.
40 * @member {number}
41 * @readonly
42 */
43 this.height = 0;
44}
45/**
46 * Make a copy of this Animation.
47 * @returns {Animation}
48 */
49Animation.prototype.copy = function() {
50 var anim = new Animation();
51 anim.frames = this.frames;
52 anim.frame = this.frame;
53 anim.elapsedMillis = this.elapsedMillis;
54 anim.repeatAt = this.repeatAt;
55 anim.width = this.width;
56 anim.height = this.height;
57 return anim;
58};
59
60/**
61 * Add a frame to the animation.
62 * @param {external:canvas|external:image} img The image to draw for the frame being added.
63 * @param {number} time How long, in milliseconds, this frame should be displayed in the animation.
64 */
65Animation.prototype.add = function(img, time) {
66 this.frames.push({
67 img: img,
68 time: time
69 });
70 if (this.frames.length === 1) {
71 this.width = img.width;
72 this.height = img.height;
73 }
74};
75/**
76 * Advance the animation by a single frame.
77 */
78Animation.prototype.step = function() {
79 this.frame++;
80 if (this.frame >= this.frames.length) {
81 this.frame = this.repeatAt;
82 }
83};
84/**
85 * Advance the animation by a number of milliseconds.
86 * @param {number} elapsedMillis How many milliseconds to advance the animation by.
87 */
88Animation.prototype.move = function(elapsedMillis) {
89 this.elapsedMillis += elapsedMillis;
90 while (this.elapsedMillis > this.frames[this.frame].time) {
91 this.elapsedMillis -= this.frames[this.frame].time;
92 this.step();
93 }
94};
95/**
96 * Draw the current frame of the animation.
97 * @param {external:CanvasRenderingContext2D} context The drawing context.
98 * @param {number} x The x coordinate to draw the animation at.
99 * @param {number} y The y coordinate to draw the animation at.
100 */
101Animation.prototype.draw = function(context, x, y) {
102 var img = this.frames[this.frame].img;
103 context.drawImage(img, x, y);
104};
105/**
106 * Reset the animation to the first frame. This can be useful when you have one piece of code calling {@link Animation#move} each frame, but you want the animation to appear stopped.
107 */
108Animation.prototype.reset = function() {
109 this.frame = 0;
110 this.elapsedMillis = 0;
111};
112/**
113 * Flip all frames in the animation horizontally. Useful when you want to use a single set of images for left and right animations.
114 */
115Animation.prototype.flipHorizontally = function() {
116 for (var i = 0; i < this.frames.length; i++) {
117 this.frames[i].img = buffer.flipBufferHorizontally(this.frames[i].img);
118 }
119};
120/**
121 * Flip all frames in the animation vertically. Useful when you want to use a single set of images for up and down animations.
122 */
123Animation.prototype.flipVertically = function() {
124 for (var i = 0; i < this.frames.length; i++) {
125 this.frames[i].img = buffer.flipBufferVertically(this.frames[i].img);
126 }
127};
128
129module.exports = Animation;