UNPKG

3.44 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 * Add a frame to the animation.
47 * @param {external:canvas|external:image} img The image to draw for the frame being added.
48 * @param {number} time How long, in milliseconds, this frame should be displayed in the animation.
49 */
50Animation.prototype.add = function(img, time) {
51 this.frames.push({img: img, time: time});
52 if (this.frames.length === 1) {
53 this.width = img.width;
54 this.height = img.height;
55 }
56};
57/**
58 * Advance the animation by a single frame.
59 */
60Animation.prototype.step = function() {
61 this.frame++;
62 if (this.frame >= this.frames.length) {
63 this.frame = this.repeatAt;
64 }
65};
66/**
67 * Advance the animation by a number of milliseconds.
68 * @param {number} elapsedMillis How many milliseconds to advance the animation by.
69 */
70Animation.prototype.move = function(elapsedMillis) {
71 this.elapsedMillis += elapsedMillis;
72 while (this.elapsedMillis > this.frames[this.frame].time) {
73 this.elapsedMillis -= this.frames[this.frame].time;
74 this.step();
75 }
76};
77/**
78 * Draw the current frame of the animation.
79 * @param {external:CanvasRenderingContext2D} context The drawing context.
80 * @param {number} x The x coordinate to draw the animation at.
81 * @param {number} y The y coordinate to draw the animation at.
82 */
83Animation.prototype.draw = function(context, x, y) {
84 var img = this.frames[this.frame].img;
85 context.drawImage(img, x, y);
86};
87/**
88 * 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.
89 */
90Animation.prototype.reset = function() {
91 this.frame = 0;
92 this.elapsedMillis = 0;
93};
94/**
95 * Flip all frames in the animation horizontally. Useful when you want to use a single set of images for left and right animations.
96 */
97Animation.prototype.flipHorizontally = function() {
98 for (var i = 0; i < this.frames.length; i++) {
99 this.frames[i].img = buffer.flipBufferHorizontally(this.frames[i].img);
100 }
101};
102/**
103 * Flip all frames in the animation vertically. Useful when you want to use a single set of images for up and down animations.
104 */
105Animation.prototype.flipVertically = function() {
106 for (var i = 0; i < this.frames.length; i++) {
107 this.frames[i].img = buffer.flipBufferVertically(this.frames[i].img);
108 }
109};
110
111module.exports = Animation;