-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (147 loc) · 3.21 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const assets = {
hats: [
{
name: "cowboy",
x: 54,
y: 45,
width: 211,
height: 148
},
{
name: "tophat",
x: 107,
y: 34,
width: 178,
height: 164
},
{
name: "helmet",
x: 55,
y: 63,
width: 196,
height: 143
},
{
name: "cap",
x: -2,
y: 30,
width: 290,
height: 195
}
],
face: [
{
name: "glasses",
x: 37.5,
y: 135.75,
width: 187,
height: 89
},
{
name: "sunglasses",
x: 28,
y: 118,
width: 216,
height: 129
}
],
extras: [
{
name: "laptop",
x: 147,
y: 301,
width: 169,
height: 112
},
{
name: "coffee",
x: 183,
y: 257,
width: 160,
height: 160
}
]
}
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
const duck = new Image();
duck.src = "assets/duck.png";
let hatIndex = assets.hats.length > 0 ? Math.random() * assets.hats.length | 0 : -1
let faceIndex = assets.face.length > 0 ? Math.random() * assets.face.length | 0 : -1
let extraIndex = assets.extras.length > 0 ? Math.random() * assets.extras.length | 0 : -1
const hatImage = new Image(),
extrasImage = new Image(),
faceImage = new Image();
const drawDuck = () => {
return new Promise((resolve, _) => {
context.drawImage(duck, 0, 100, canvas.width, canvas.height - 100);
resolve();
})
}
const drawExtra = () => {
return new Promise((resolve, _) => {
if(extraIndex === -1) resolve();
const extra = assets.extras[extraIndex];
extrasImage.src = `assets/extras/${extra.name}.png`;
extrasImage.onload = () => {
context.drawImage(extrasImage, extra.x, extra.y, extra.width, extra.height);
resolve();
}
})
}
const drawFace = () => {
return new Promise((resolve, _) => {
if(faceIndex === -1) resolve();
const face = assets.face[faceIndex];
faceImage.src = `assets/face/${face.name}.png`;
faceImage.onload = () => {
context.drawImage(faceImage, face.x, face.y, face.width, face.height);
resolve();
}
})
}
const drawHat = () => {
return new Promise((resolve, _) => {
if(hatIndex === -1) resolve();
const hat = assets.hats[hatIndex];
hatImage.src = `assets/hats/${hat.name}.png`;
hatImage.onload = () => {
context.drawImage(hatImage, hat.x, hat.y, hat.width, hat.height);
resolve();
}
})
}
const draw = async () => {
//context.clearRect(0, 0, canvas.width, canvas.height);
// set canvas background color to #f7f7f7
context.fillStyle = "#f7f7f7";
context.fillRect(0, 0, canvas.width, canvas.height);
await drawDuck()
await drawExtra()
await drawFace()
await drawHat()
}
duck.onload = () => {
draw()
}
document.addEventListener("keydown", (event) => {
//if key is left arrow increment hatIndex by 1, if hatIndex is greater than the length of the hats array set hatIndex to 0
if(event.key === "ArrowLeft") {
hatIndex++
if(hatIndex > assets.hats.length - 1) {
hatIndex = 0;
}
}
//if key is right arrow decrement hatIndex by 1, if hatIndex is less than 0 set hatIndex to the length of the hats array minus 1
else if(event.key === "ArrowRight") {
hatIndex--
if(hatIndex < 0) {
hatIndex = assets.hats.length - 1;
}
}else {
return;
}
//set pickedHat to the hat object at the index of hatIndex
pickedHat = assets.hats[hatIndex]
draw()
})