-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
33 lines (24 loc) · 861 Bytes
/
script.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
const canvas = document.querySelector('#canvas_container')
const ctx = canvas.getContext('2d');
const spriteWidth = 319
const spriteHeight = 329
const CANVAS_WIDTH = canvas.width = 400
const CANVAS_HEIGHT = canvas.height = 400
const playerImg = new Image();
// console.log(playerImg);
playerImg.src = './img/sword.png';
let frameX = 0;
let frameY = 0;
let initFrame = 0;
const staggerFrame = 7;
// console.log(staggerFrame, initFrame);
function animate() {
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
let position = Math.floor(initFrame/staggerFrame) % 9;
frameX = spriteWidth* position;
// console.log(position);
ctx.drawImage(playerImg, frameX, frameY*spriteHeight, spriteWidth, spriteHeight, 0, 0, spriteWidth, spriteHeight);
initFrame++;
requestAnimationFrame(animate)
}
animate()