UNPKG

4.82 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 * @return {Animation} The Animation, so you can chain calls to rotate/flip methods.
115 */
116Animation.prototype.flipHorizontally = function() {
117 for (var i = 0; i < this.frames.length; i++) {
118 this.frames[i].img = buffer.flipBufferHorizontally(this.frames[i].img);
119 }
120 return this;
121};
122/**
123 * Flip all frames in the Animation vertically. Useful when you want to use a single set of images for up and down animations.
124 * @return {Animation} The Animation, so you can chain calls to rotate/flip methods.
125 */
126Animation.prototype.flipVertically = function() {
127 for (var i = 0; i < this.frames.length; i++) {
128 this.frames[i].img = buffer.flipBufferVertically(this.frames[i].img);
129 }
130 return this;
131};
132/**
133 * Rotate all frames in the Animation clockwise by 90 degrees.
134 * @return {Animation} The Animation, so you can chain calls to rotate/flip methods.
135 */
136Animation.prototype.rotateClockwise = function() {
137 var w = this.width;
138 this.width = this.height;
139 this.height = w;
140 for (var i = 0; i < this.frames.length; i++) {
141 this.frames[i].img = buffer.rotateClockwise(this.frames[i].img);
142 }
143 return this;
144};
145/**
146 * Rotate all frames in the Animation counter-clockwise by 90 degrees.
147 * @return {Animation} The Animation, so you can chain calls to rotate/flip methods.
148 */
149Animation.prototype.rotateCounterclockwise = function() {
150 var w = this.width;
151 this.width = this.height;
152 this.height = w;
153 for (var i = 0; i < this.frames.length; i++) {
154 this.frames[i].img = buffer.rotateCounterclockwise(this.frames[i].img);
155 }
156 return this;
157};
158
159module.exports = Animation;