-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
sprite.js
261 lines (235 loc) · 6.68 KB
/
sprite.js
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
import BaseEvent from "@pencil.js/base-event";
import Image from "@pencil.js/image";
import { modulo } from "@pencil.js/math";
import minimatch from "minimatch";
/**
* @module Sprite
*/
/**
* Sprite class
* <br><img src="./media/examples/sprite.gif" alt="sprite demo"/>
* @class
* @extends {module:Image}
*/
export default class Sprite extends Image {
/**
* @typedef {Object} Frame
* @prop {Number} x - Horizontal position
* @prop {Number} y - Vertical position
* @prop {Number} w - Width
* @prop {Number} h - Height
*/
/**
* @typedef {Object} FrameData
* @prop {Frame} frame - Data about this frame in the sprite-sheet
* @prop {Frame} spriteSourceSize - Data about the original file
*/
/**
* Sprite constructor
* @param {PositionDefinition} positionDefinition -
* @param {String} url -
* @param {Array<FrameData>} frames -
* @param {SpriteOptions} [options] - Drawing options
*/
constructor (positionDefinition, url, frames, options) {
super(positionDefinition, url, options);
this.frames = frames;
this.frame = 0;
this.isPaused = false;
}
/**
* @inheritDoc
* @return {Sprite} Itself
*/
makePath (ctx) {
const { sourceSize } = this.frames[Math.floor(this.frame)];
this.width = sourceSize.w;
this.height = sourceSize.h;
super.makePath(ctx);
if (this.isLoaded) {
const frameNumber = Math.floor(this.frame);
if (!this.isPaused && (this.options.loop || this.frame < this.frames.length - 1)) {
this.setFrame(this.frame + this.options.speed);
}
const nextFrame = Math.floor(this.frame);
if (nextFrame !== frameNumber) {
this.fire(new BaseEvent(Sprite.events.frame, this));
if (!this.options.loop && nextFrame === this.frames.length - 1) {
this.fire(new BaseEvent(Sprite.events.end, this));
}
}
}
return this;
}
/**
* @inheritDoc
* @return {Sprite} Itself
*/
draw (ctx) {
const { frame, spriteSourceSize } = this.frames[Math.floor(this.frame)];
ctx.drawImage(
this.file,
frame.x, frame.y, frame.w, frame.h,
spriteSourceSize.x, spriteSourceSize.y, spriteSourceSize.w, spriteSourceSize.h,
);
return this;
}
/**
* Play the sprite animation
* @param {Number} [speed] - Choose a play speed
* @return {Sprite} Itself
*/
play (speed) {
this.isPaused = false;
if (speed !== undefined) {
this.options.speed = speed;
}
return this;
}
/**
* Put the sprite on pause
* @return {Sprite} Itself
*/
pause () {
this.isPaused = true;
return this;
}
/**
*
* @param {Number} frame - Number of the frame to set
* @return {Sprite} Itself
*/
setFrame (frame) {
this.frame = modulo(frame, this.frames.length);
return this;
}
/**
* @inheritDoc
*/
toJSON () {
const { frames, frame, isPaused } = this;
return {
...super.toJSON(),
frames,
frame,
isPaused,
};
}
/**
*
* @param {Object} definition -
* @return {Sprite}
*/
static from (definition) {
const { position, url, frames, frame, isPaused, options } = definition;
const sprite = new Sprite(position, url, frames, options);
sprite.setFrame(frame);
if (isPaused) {
sprite.pause();
}
return sprite;
}
/**
* Load and return a spritesheet json file
* @param {String} url - Url to the file
* @return {Spritesheet}
*/
static async sheet (url) {
const response = await window.fetch(url);
const json = await response.json();
json.meta.file = await this.load(json.meta.image);
// eslint-disable-next-line no-use-before-define
return new Spritesheet(json);
}
/**
* @typedef {Object} SpriteOptions
* @extends ComponentOptions
* @prop {Number} [speed=1] -
* @prop {Boolean} [loop=true] -
*/
/**
* @type {SpriteOptions}
*/
static get defaultOptions () {
return {
...super.defaultOptions,
speed: 1,
loop: true,
};
}
/**
* @typedef {Object} SpriteEvents
* @extends ContainerEvent
* @prop {String} start -
* @prop {String} frame -
* @prop {String} end -
*/
/**
* @type {SpriteEvents}
*/
static get events () {
return {
...super.events,
start: "sprite-start",
frame: "sprite-frame",
end: "sprite-end",
};
}
}
/**
* Spritesheet class
* @class
*/
class Spritesheet {
/**
* Spritesheet constructor
* @param {Object} json -
*/
constructor (json) {
this.json = json;
}
/**
* Getter for the image file
* @return {Image}
*/
get file () {
return this.json.meta.file;
}
/**
* Return all the frames corresponding to a selector
* @param {String|Function|RegExp} [selector="*"] - Match against the spritesheet images name using a glob pattern, a validation function or a regular expression
* @return {Array}
*/
get (selector = "*") {
const filter = ((matcher) => {
if (typeof matcher === "function") {
return matcher;
}
if (typeof matcher === "string") {
const glob = new minimatch.Minimatch(matcher, {
dot: true,
matchBase: true,
});
return string => matcher === string || glob.match(string);
}
if (matcher instanceof RegExp) {
return string => matcher.test(string);
}
return () => false;
})(selector);
const { frames } = this.json;
return Object.keys(frames)
.filter(filter)
.map(key => frames[key]);
}
/**
* Group images from the spritesheet into a single sprite
* @param {PositionDefinition} position - Position of the sprite
* @param {String|Function|RegExp} [selector="*"] - Match against the spritesheet images name using a glob pattern, a validation function or a regular expression
* @param {ImageOptions} [options] - Options of the sprite
* @return {Sprite}
*/
extract (position, selector, options) {
return new Sprite(position, this.file, this.get(selector), options);
}
}