-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSprite.js
More file actions
43 lines (36 loc) · 755 Bytes
/
Sprite.js
File metadata and controls
43 lines (36 loc) · 755 Bytes
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
class Sprite {
constructor(src, width, height, columns, rows) {
const image = new Image();
image.src = src;
const frameWidth = width / columns;
const frameHeight = height / rows;
const frames = [];
let i;
let j;
for (j = 0; j < rows; j++) {
frames.push([]);
for (i = 0; i < columns; i++) {
frames[j][i] = [i * frameWidth, j * frameHeight];
}
}
this.sprite = {
image,
width,
height,
columns,
rows,
center: frameWidth / 2,
count: columns,
frames,
frameWidth,
frameHeight,
}
this.get = this.get.bind(this);
}
get(row = 0) {
return {
...this.sprite,
frames: this.sprite.frames[row],
};
}
}