-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsani99.js
More file actions
60 lines (47 loc) · 1.9 KB
/
sani99.js
File metadata and controls
60 lines (47 loc) · 1.9 KB
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
// tutorial based on http://creativejs.com/2012/01/day-11-sprite-sheets/
// screen size variables
var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight;
var canvas = document.createElement('canvas');
var c = canvas.getContext('2d');
canvas.width = SCREEN_WIDTH;
canvas.height = SCREEN_HEIGHT;
var initX = 0;
var initY = 0;
var xpos = initX;
var ypos = initY;
var index=0;
var numFrames = 30;
var frameSizeX= 253;
var frameSizeY= 280;
// Add our drawing canvas
document.body.appendChild(canvas);
//load the image
image = new Image();
image.src = "cdsprite.png";
image.onload = function() {
//we're ready for the loop
setInterval(loop, 1000 / numFrames);
}
function loop() {
//clear the canvas!
c.clearRect(0,0, SCREEN_HEIGHT,SCREEN_WIDTH);
c.drawImage(image,xpos,ypos,frameSizeX,frameSizeY,0,0,frameSizeX, frameSizeY);
/*The long list means...:
1: image source
2 - 5: the rectangle in the source image to cut from
6 - 9: the rectangle of the canvas being to paste to
the increment changes the part of the source image bring cut from each time loop() is called
the rectangle bring pasted to here is not.
*/
//each time around we add the frame size to our xpos, moving along the source image like an old film strip
xpos += frameSizeX;
//increase the index so you know which frame of the animation comes next
index += 1;
//if the index is higher than the total number of frames, we're at the end and better start over
if (index >= numFrames) {
xpos = initX;
ypos = initY;
index=0;
}
}