-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (52 loc) · 1.79 KB
/
index.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
54
55
56
57
58
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
console.log(ctx);
let playerState = 'idle'; // Initial state
const CANVAS_WIDTH = canvas.width = 600;
const CANVAS_HEIGHT = canvas.height = 600;
const spriteWidth = 575;
const spriteHeight = 523;
let frameX = 0;
let gameFrame = 0;
const staggerFrames = 6;
const spriteAnimations = {};
const animationStates = [
{ 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 }
];
animationStates.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;
});
const playerImage = new Image();
playerImage.src = 'shadow_dog.png';
document.getElementById('animations').addEventListener('change', function (e) {
playerState = e.target.value;
});
playerImage.onload = function () {
animate();
}
function animate() {
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
let position = Math.floor(gameFrame / staggerFrames) % spriteAnimations[playerState].loc.length;
frameX = spriteAnimations[playerState].loc[position].x;
let frameY = spriteAnimations[playerState].loc[position].y;
ctx.drawImage(playerImage, frameX, frameY, spriteWidth, spriteHeight, 0, 0, spriteWidth, spriteHeight);
gameFrame++;
requestAnimationFrame(animate);
}