This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (269 loc) · 7.76 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* COM1008 Assignement 2
* Author: Craig de Gouveia (170164160)
* Modified: 06/12/2017
*/
//Initialise canvas and controls
const canvas = document.getElementById("canvas")
const context = canvas.getContext("2d")
const WIDTH = canvas.width
const HEIGHT = canvas.height
const resetBtn = document.getElementById("reset")
const kawaiiBtn = document.getElementById("kawaii")
const sadBtn = document.getElementById("sad")
const oniBtn = document.getElementById("oni")
//Create an array containing the colour palette for use in rendering the image
const colours = [
"rgb(15,55,105)",
"rgb(255,255,255)",
"rgba(255,255,255,0)",
`hsl(${360 * Math.random()}, 50%, 50%)`,
"rgb(255,187,128)",
"rgb(30,190,254)",
"rgba(255,38,0,1)",
"rgb(145,145,145)",
`hsl(${360 * Math.random()}, 50%, 50%)`,
]
/**
*
* @param {string[][] name Takes an array of arrays with a single string
* @returns {string[][]} Returns an array of arrays with strings split per character
*/
const dataMap = data => data.map(f => f[0].split(""))
/**
*
* @param {int} x coordinate of cell
* @param {int} y coordinate of cell
* @param {int} width of cell
* @param {int} height of cell
* @param {string} c string representation of a colour, such as rgb(0,0,0), of cell
*/
const drawCell = (x, y, width, height, c) => {
context.fillStyle = c
context.fillRect(x, y, width, height)
}
/**
*
* @param {int} index of colour from palette array
* @returns string representation of colour
*/
const gridColour = index => colours[index]
/**
*
* Maps over a 2d array and plots each value in the canvas as a grid reference
* @param {*} context to execute against
* @param {*} grid 2d array of pixels for canvas
* @param {*} ox offset value for x coordinate
* @param {*} oy offset value for y coordinate
*/
const drawPart = (context, grid, ox, oy) => (
grid.map(
(row, i) => row.map(
(col, j) => drawCell(j * 2 + ox, i * 2 + oy, 4, 4, gridColour(grid[i][j]))
)
)
)
/**
*
* Maps over a 2d array and clears any solid pixels to ensure clean rendering and animations
* @param {*} context to execute against
* @param {*} grid 2d array of pixels for canvas
* @param {*} ox offset value for x coordinate
* @param {*} oy offset value for y coordinate
*/
const clearPart = (context, grid, ox, oy) => (
grid.map(
(row, i) => row.map(
(col, j) => (grid[i][j] == 0 || 1) && context.clearRect(j * 2 + ox, i * 2 + oy, 2, 2)
)
)
)
//Initialise global variables for drawing a face
let hasBeard = false
let gotShades = false
let currentFace = neutral
/**
* Draw the beard part in a desired location on any face
*/
const drawBeard = () => {
drawPart(context, dataMap(beard), 45, 168)
hasBeard = true
}
/**
* Draw the shades part in a desired location on any face
*/
const drawShades = () => {
drawPart(context, dataMap(shades), 69, 135)
}
/**
*
* Cleanly draws a face in the desired location. Checks for any additional features and renders them.
* @param {string[][]} face 2d array representing an expression for the face
*/
const drawFace = (face) => {
drawPart(context, dataMap(background), 0, 0)
drawPart(context, dataMap(face), 58, 80)
gotShades && drawShades()
hasBeard && drawBeard()
currentFace = face
}
/**
* Clears the entire canvas
*/
const clearAll = () => context.clearRect(0, 0, WIDTH, HEIGHT)
//Initialise global variables for animation
let requestId
let isRunning
let beardY = 190
let tearY = 165
let shadesY = -100
let beardStrokes = 0
let eyePokes = 0
let loopCount = 0
/**
*
* This ensures that an animation does not get triggered multiple times.
* Thus preventing performance issues and other animation bugs.
* @callback animation
* @param {animation} cb executes the defined animation function callback
*/
const startAnimation = (animation) => {
if (animation && !isRunning) {
isRunning = true;
animation()
} else if (!animation && isRunning) {
isRunning = false;
}
}
/**
* Stops the currently running animation
*/
const stopAnimation = () => cancelAnimationFrame(requestId)
/**
* Loops the Y coordinate of the tear for 200 frames
*/
const tearLoop = () => {
if (loopCount >= 200) {
stopAnimation()
isRunning = false;
loopCount = 0
}
tearY >= 197 && (tearY = 165)
tearY += 1
loopCount++
}
/**
* Updates the Y coordinate for the shades and stops when the eyes are reached
*/
const dropShades = () => {
if (shadesY >= 135) {
stopAnimation()
shadesY = -100
gotShades = true
isRunning = false
}
shadesY += 5
}
/**
* Updates the y coordinate of the additional beard until it reaches max length
*/
const growBeard = () => {
if (beardY >= 240) {
stopAnimation()
beardY = 190
beardStrokes = 0
isRunning = false
}
beardY += 1
}
/**
* Renders the tear when a user clicks on the eye
*/
const tearAnimation = () => {
isRunning && (requestId = requestAnimationFrame(tearAnimation))
drawPart(context, dataMap(currentFace), 58, 80)
hasBeard ? drawBeard() : drawPart(context, dataMap(chin), 44, 184)
gotShades && drawShades()
drawPart(context, dataMap(tear), 162, tearY)
tearLoop()
}
/**
* Renders the beard growth when a user clicks an already bearded chin
*/
const beardAnimation = () => {
isRunning && (requestId = requestAnimationFrame(beardAnimation))
drawPart(context, dataMap(extraBeard), 45, beardY)
growBeard()
}
/**
* Renders the shades dropping down from off screen when a user clicks on the forehead
*/
const shadesAnimation = () => {
isRunning && (requestId = requestAnimationFrame(shadesAnimation))
drawPart(context, dataMap(background), 0, 0)
drawPart(context, dataMap(currentFace), 58, 80)
drawPart(context, dataMap(shades), 69, shadesY)
hasBeard && drawBeard()
dropShades()
}
/**
*
* This handles the logic for what happens when a user clicks on a particular part of the canvas
* @param {*} evt mouse event passed in to function
*/
const mouse = (evt) => {
let boundingRect = canvas.getBoundingClientRect();
let offsetX = boundingRect.left;
let offsetY = boundingRect.top;
if ((evt.clientX >= 150 + offsetX && evt.clientX <= 200 + offsetX) &&
(evt.clientY >= 130 + offsetY && evt.clientY <= 170 + offsetY)) {
startAnimation(tearAnimation)
eyePokes < 1 ? eyePokes += 1 : (currentFace = sad) && drawFace()
}
if ((evt.clientX >= 60 + offsetX && evt.clientX <= 215 + offsetX) &&
(evt.clientY >= 190 + offsetY && evt.clientY <= 240 + offsetY)) {
drawBeard()
beardStrokes < 1 ? beardStrokes += 1 : startAnimation(beardAnimation)
}
if ((evt.clientX >= 60 + offsetX && evt.clientX <= 215 + offsetX) && (evt.clientY >= 40 + offsetY && evt.clientY <= 100 + offsetY)) {
!gotShades && startAnimation(shadesAnimation)
}
}
canvas.addEventListener("click", mouse)
/**
* Controls for the buttons on the page to change between expressions.
* In most cases some variables are reinitialised or redrawn.
*/
kawaiiBtn.addEventListener("click", function () {
stopAnimation()
clearAll()
drawFace(kawaii)
eyePokes = 0
isRunning = false
}, false)
sadBtn.addEventListener("click", function () {
stopAnimation()
clearAll()
drawFace(sad)
eyePokes = 0
isRunning = false
}, false)
oniBtn.addEventListener("click", function () {
stopAnimation()
clearAll()
drawFace(oni)
eyePokes = 0
isRunning = false
}, false)
resetBtn.addEventListener("click", function () {
hasBeard = false
gotShades = false
beardStrokes = 0
eyePokes = 0
isRunning = false
stopAnimation()
clearAll()
drawFace(neutral)
}, false)
//Initial face state when entering canvas
drawFace(neutral)