-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimation.js
53 lines (37 loc) · 1.46 KB
/
animation.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
function Animation(props) {
var self = this,
count = props.frames.length,
realW = props.w * PIXEL_SIZE,
realH = props.h * PIXEL_SIZE,
canvas,
ctx,
tempCanvas,
tempCtx,
imgData;
canvas = createCanvas(realW * count, realH);
ctx = canvas.getContext('2d');
disableCanvasSmoothness(ctx);
ctx.scale(PIXEL_SIZE, PIXEL_SIZE);
ctx.drawImage(props.sprite, 0, 0);
self.frames = {};
while (count--) {
tempCanvas = createCanvas(realW, realH);
tempCtx = tempCanvas.getContext('2d');
imgData = ctx.getImageData(realW * count, 0, realW, realH);
// Put ImageData with a sprite into canvas context.
tempCtx.putImageData(imgData, 0, 0);
// Save the sprite as an image.
self.frames[props.frames[count]] = new Image();
self.frames[props.frames[count]].src = tempCanvas.toDataURL(IMAGE_TYPE);
// Draw previously saved image on the canvas, in reverse by the X-axis.
tempCtx.clearRect(0, 0, realW, realH);
tempCtx.scale(-1, 1);
tempCtx.drawImage(self.frames[props.frames[count]], -realW, 0);
// Save the reversed sprite as an image.
self.frames[props.frames[count] + 'R'] = new Image();
self.frames[props.frames[count] + 'R'].src = tempCanvas.toDataURL(IMAGE_TYPE);
}
}
Animation.prototype.getFrame = function (name, reversed) {
return this.frames[name + (reversed ? 'R' : '')];
};