-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteSheet.js
More file actions
40 lines (31 loc) · 1017 Bytes
/
SpriteSheet.js
File metadata and controls
40 lines (31 loc) · 1017 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
function SpriteSheet(path, frameWidth, frameHeight, frameSpeed, endFrame) {
var currentFrame = 0; // the current frame to draw
var counter = 0; // keep track of frame rate
// Update the animation
this.update = function() {
// update to the next frame if it is time
if (counter == (frameSpeed - 1))
currentFrame = (currentFrame + 1) % endFrame;
// update the counter
counter = (counter + 1) % frameSpeed;
};
// Draw the current frame
this.draw = function(x, y) {
// get the row and col of the frame
var row = Math.floor(currentFrame / framesPerRow);
var col = Math.floor(currentFrame % framesPerRow);
ctx.drawImage(
image,
col * frameWidth, row * frameHeight,
frameWidth, frameHeight,
x, y,
frameWidth, frameHeight);
};
};
};
function animate() {
requestAnimFrame( animate );
ctx.clearRect(0, 0, 150, 150);
spritesheet.update();
spritesheet.draw(12.5, 12.5);
}