-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
85 lines (69 loc) · 2.57 KB
/
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
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
window.addEventListener('load', function(){
const canvas = document.getElementById('canvas1')
const ctx = canvas.getContext('2d')
canvas.width = 500;
canvas.height = 500
console.log(ctx)
class Mandrake {
constructor(canvasWidth, canvasHeight){
this.canvasWidth = canvasWidth
this.canvasHeight = canvasHeight;
this.image = document.getElementById('mandrake');
this.spriteWidth= 256;
this.spriteHeight= 256;
this.width = this.spriteWidth;
this.height = this.spriteHeight;
this.scale = 2;
this.x = canvasWidth /2 - this.width * this.scale / 2 ;
this.y = canvasHeight /2 - this.height * this.scale / 2 ;
this.minFrame = 0;
this.maxFrame = 355;
this.frame = 0
this.frameX = 3;
this.frameY = 3;
}
draw(context){
context.drawImage(this.image, this.frameX * this.spriteWidth, this.frameY * this.spriteHeight, this.spriteWidth, this.spriteHeight, this.x, this.y, this.width * this.scale, this.height * this.scale);
}
update(){
// if (this.frame < this.maxFrame) this.frame++;
// else this.frame = this.minFrame;
this.frame = this.frame < this.maxFrame ? this.frame +1 : this.minFrame;
this.frameX = this.frame % 18;
this.frameY = Math.floor(this.frame / 18 );
}
setAnimation(newMinFrame, newMaxFrame){
this.minFrame = newMinFrame;
this.maxFrame = newMaxFrame;
this.frame = this.newMinFrame;
}
}
const mandrake = new Mandrake(canvas.width, canvas.height)
function animate(){
ctx.clearRect(0, 0, canvas.width, canvas.height)
mandrake.draw(ctx);
mandrake.update();
requestAnimationFrame(animate);
}
animate()
const all= document.getElementById('all');
all.addEventListener('click', function(){
mandrake.setAnimation(0, 355);
})
const grow= document.getElementById('grow');
grow.addEventListener('click', function(){
mandrake.setAnimation(0, 75);
})
const wink= document.getElementById('wink');
wink.addEventListener('click', function(){
mandrake.setAnimation(76, 112);
})
const float= document.getElementById('float');
float.addEventListener('click', function(){
mandrake.setAnimation(113, 262);
})
const hide= document.getElementById('hide');
hide.addEventListener('click', function(){
mandrake.setAnimation(113, 262);
})
})