-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.js
More file actions
93 lines (83 loc) · 2.08 KB
/
p1.js
File metadata and controls
93 lines (83 loc) · 2.08 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
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
let playerstate="idle";
const dropdown=document.getElementById("animations");
dropdown.addEventListener('change',function(e){
playerstate=e.target.value;
})
const canvas=document.getElementById('canvas1');
const ctx=canvas.getContext("2d");
const canvas_width=canvas.width=600;
const canvas_height=canvas.height=600;
const playerImage= new Image();
playerImage.src="shadow_dog.png"
const spritewidth=575;//dimension of 1 image
const spriteheight=523;
let gameframe=0;
const staggerframes=5;
const spriteanimations=[];
const animationstate=[
{
name:'idle',
frames:7,
},
{
name:'jump',
frames:7,
},
{
name:'fall',
frames:7,
},
{
name:'run',
frames:9,
},
{
name:'dizzy',
frames:11,
},
{
name:'sit',
frames:5,
},
{
name:'roll',
frames:7,
},
{
name:'bite',
frames:7,
},
{
name:'ko',
frames:12,
},
{
name:'gethit',
frames:4,
}
];
animationstate.forEach((state,index)=>{
let frames={
loc: [],
}
for(let j=0;j< state.frames;j++){
let positionX=j*spritewidth;
let positionY=index*spriteheight;
frames.loc.push({x:positionX,y:positionY});
}
spriteanimations[state.name]=frames;
});
console.log(spriteanimations);
//this is method 1
function animate(){
ctx.clearRect(0,0,canvas_width,canvas_height);
//ctx.fillRect(50,50,100,100);
//ctx.drawImage(image,sx,sy,sw,sh,dx,dy,dw ,dh) s:source d:destination
let position=Math.floor(gameframe/staggerframes)%spriteanimations[playerstate].loc.length;//{math.floor is used to get whole answer only like 0.2 will be 0}
let frameX=position*spritewidth;
let frameY=spriteanimations[playerstate].loc[position].y;
ctx.drawImage(playerImage,frameX,frameY,spritewidth,spriteheight,0,0,spritewidth,spriteheight);
gameframe++;
requestAnimationFrame(animate);
};
animate();