-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
335 lines (317 loc) · 9.19 KB
/
script.js
File metadata and controls
335 lines (317 loc) · 9.19 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//------------------------------------------------------------------------------- CONSTANTS
const shapesArray = [
`<circle class="circle" cx="50" cy="50" r="50" />`,
`<ellipse class="oval" cx="50" cy="50" rx="50" ry="30" />`,
`<path class="triangle" d="M50 0L100 100L0 100Z" />`,
`<path class="square" d="M0 0L100 0L100 100L0 100Z" />`,
`<path class="rectangle" d="M0 20L100 20L100 80L0 80Z" />`,
`<path class="rhombus" d="M50 0L100 50L50 100L0 50Z" />`,
`<path class="trapezoid" d="M25 0L75 0L100 100L0 100Z" />`,
`<path class="hexagon" d="M25 0L75 0L100 50L75 100L25 100L0 50Z" />`,
`<path class="star" d="M50 0L65 40L100 40L75 65L80 100L50 80L20 100L25 65L0 40L35 40Z" />`,
`<path class="octagon" d="M30 0L70 0L100 30L100 70L66.66 100L30 100L0 70L0 30Z" />`,
`<path class="pentagon" d="M50 0L100 40L75 100L25 100L0 40Z" />`,
`<path class="parallelogram" d="M50 0L100 0L50 100L0 100Z" />`,
`<circle class="circle" cx="50" cy="50" r="50" />`,
`<ellipse class="oval" cx="50" cy="50" rx="50" ry="30" />`,
`<path class="triangle" d="M50 0L100 100L0 100Z" />`,
`<path class="square" d="M0 0L100 0L100 100L0 100Z" />`,
`<path class="rectangle" d="M0 20L100 20L100 80L0 80Z" />`,
`<path class="rhombus" d="M50 0L100 50L50 100L0 50Z" />`,
`<path class="trapezoid" d="M25 0L75 0L100 100L0 100Z" />`,
`<path class="hexagon" d="M25 0L75 0L100 50L75 100L25 100L0 50Z" />`,
`<path class="star" d="M50 0L65 40L100 40L75 65L80 100L50 80L20 100L25 65L0 40L35 40Z" />`,
`<path class="octagon" d="M30 0L70 0L100 30L100 70L66.66 100L30 100L0 70L0 30Z" />`,
`<path class="pentagon" d="M50 0L100 40L75 100L25 100L0 40Z" />`,
`<path class="parallelogram" d="M50 0L100 0L50 100L0 100Z" />`,
];
const colorOptions = [
'firebrick',
'slateblue',
'magenta',
'mediumspringgreen',
'lime',
'lightseagreen',
'indigo',
'goldenrod',
'indianred',
'darkslategrey',
'darkolivegreen',
'darkmagenta',
'darksalmon',
'darkcyan',
'aquamarine',
'darkslateblue',
'deeppink',
'teal',
'springgreen',
'powderblue',
'plum',
'palegreen',
'navy',
'mediumpurple',
'lightsalmon',
'midnightblue',
'orchid',
];
const redArray = [
'crimson',
'darkred',
'brown',
'firebrick',
'indianred',
'maroon',
'lightcoral',
'orangered',
'red',
'tomato',
'crimson',
'darkred',
'brown',
'firebrick',
'indianred',
'maroon',
'lightcoral',
'orangered',
'red',
'tomato',
'crimson',
'darkred',
'brown',
'firebrick',
'indianred',
'maroon',
'lightcoral',
'orangered',
'red',
'tomato',
];
const greenBlueArray = [
'turquoise',
'teal',
'yellowgreen',
'springgreen',
'seagreen',
'paleturquoise',
'palegreen',
'olive',
'darkseagreen',
'olivedrab',
'mediumturquoise',
'mediumspringgreen',
'mediumaquamarine',
'mediumseagreen',
'lime',
'limegreen',
'lightseagreen',
'lightgreen',
'lawngreen',
'greenyellow',
'green',
'forestgreen',
'darkgreen',
'darkcyan',
'chartreuse',
];
const purplePinkArray = [
'blueviolet',
'darkmagenta',
'darkorchid',
'darkviolet',
'deeppink',
'darkslateblue',
'indigo',
'fuchsia',
'hotpink',
'magenta',
'mediumorchid',
'mediumslateblue',
'mediumvioletred',
'mediumpurple',
'purple',
'orchid',
'plum',
'rebeccapurple',
'slateblue',
'violet',
'palevioletred',
'blueviolet',
'darkmagenta',
'darkorchid',
'darkviolet',
'deeppink',
'darkslateblue',
'indigo',
'fuchsia',
'hotpink',
'magenta',
'mediumorchid',
'mediumslateblue',
'mediumvioletred',
'mediumpurple',
'purple',
'orchid',
'plum',
'rebeccapurple',
'slateblue',
'violet',
'palevioletred',
];
//------------------------------------------------------------------------- STATE VARIABLES
let shape1 = null;
let shape2 = null;
let matchedShapes = 0;
let totalShapeSets = shapesArray.length / 2;
//------------------------------------------------------------------------ CACHED ELEMENTS
const html = document.querySelector('html');
const lightModeButton = document.getElementById('mode');
const svgContainer = document.getElementById('svg-container');
const resetButton = document.getElementById('reset');
const unmatchedH2 = document.getElementById('unmatched');
const matchedH2 = document.getElementById('matched');
const redButton = document.getElementById('red');
const greensButton = document.getElementById('green');
const purpleButton = document.getElementById('purple');
const header = document.querySelector('header');
const buttonDiv = document.getElementById('buttonsDiv');
//------------------------------------------------------------------------- EVENT LISTENERS
lightModeButton.addEventListener('click', function () {
html.classList.toggle('light-mode');
if (lightModeButton.innerText === '💡') {
lightModeButton.innerText = '🌘';
} else {
lightModeButton.innerText = '💡';
}
});
resetButton.addEventListener('click', () => {
const svgs = document.querySelectorAll('svg');
initialize(svgs);
});
redButton.addEventListener('click', redShapes);
greensButton.addEventListener('click', greenShapes);
purpleButton.addEventListener('click', purpleShapes);
//------------------------------------------------------------------------------ FUNCTIONS
window.addEventListener('DOMContentLoaded', () => {
for (let i = 0; i < shapesArray.length; i++) {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('height', '100');
svg.setAttribute('width', '100');
svg.setAttribute('viewBox', '0 0 100 100');
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
svgContainer.appendChild(svg);
}
const svgs = document.querySelectorAll('svg');
initialize(svgs);
});
function initialize(svgs) {
const shapes = shuffleShapes();
const colors = shuffleColors(colorOptions);
matchedShapes = 0;
totalShapeSets = shapesArray.length / 2;
matchedH2.innerHTML = `Matched Shape Sets: <span style="color: red">${matchedShapes}</span>`;
unmatchedH2.innerHTML = `Unmatched Shape Sets: <span style="color: red">${totalShapeSets}</span>`;
svgs.forEach((svg, idx) => {
svg.classList = '';
svg.innerHTML = shapes[idx];
svg.addEventListener('click', handleSelection);
const shapeEls = svg.querySelector('path, ellipse, circle');
if (shapeEls) {
shapeEls.setAttribute('fill', colors[idx]);
shapeEls.setAttribute('id', idx);
}
});
}
function shuffleShapes() {
const shapeArr = shapesArray.slice();
for (let i = shapeArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shapeArr[i], shapeArr[j]] = [shapeArr[j], shapeArr[i]];
}
return shapeArr;
}
function shuffleColors(colorArray) {
const colorArr = colorArray.slice();
for (let i = colorArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[colorArr[i], colorArr[j]] = [colorArr[j], colorArr[i]];
}
return colorArr;
}
function handleSelection(e) {
const selection = e.target;
if (!shape1) {
shape1 = selection;
shape1.parentNode.classList.add('scale', 'no-click');
console.log('select1', shape1);
} else if (!shape2) {
shape2 = selection;
shape2.parentNode.classList.add('scale', 'no-click');
console.log('select2', shape2);
checkMatch(shape1, shape2);
}
}
function checkMatch(shape1, shape2) {
if (shape1.classList.toString() === shape2.classList.toString()) {
shape1.classList.add('animate__animated', 'animate__fadeOutUpBig');
shape2.classList.add('animate__animated', 'animate__fadeOutUpBig');
matchedShapes++;
totalShapeSets--;
matchedH2.innerHTML = `Matched Shape Sets: <span style="color: red">${matchedShapes}</span>`;
unmatchedH2.innerHTML = `Unmatched Shape Sets: <span style="color: red">${totalShapeSets}</span>`;
clearShape();
} else {
shape1.classList.add('animate__animated', 'animate__jello');
shape2.classList.add('animate__animated', 'animate__jello');
setTimeout(() => {
shape1.classList.remove('animate__animated', 'animate__jello');
shape2.classList.remove('animate__animated', 'animate__jello');
}, 2000);
clearSVG();
clearShape();
}
}
function clearShape() {
shape1 = null;
shape2 = null;
}
function clearSVG() {
shape1.parentNode.classList.remove('scale', 'no-click');
shape2.parentNode.classList.remove('scale', 'no-click');
}
function checkWin() {
if (totalShapeSets === 0) {
winGame();
} else {
return;
}
}
function redShapes() {
const reds = shuffleColors(redArray);
const svgs = document.querySelectorAll('svg');
svgs.forEach((svg, idx) => {
const shapes = svg.querySelector('path, ellipse, circle');
if (shapes) {
shapes.setAttribute('fill', reds[idx]);
}
});
}
function greenShapes() {
const greens = shuffleColors(greenBlueArray);
const svgs = document.querySelectorAll('svg');
svgs.forEach((svg, idx) => {
const shapes = svg.querySelector('path, ellipse, circle');
if (shapes) {
shapes.setAttribute('fill', greens[idx]);
}
});
}
function purpleShapes() {
const purples = shuffleColors(purplePinkArray);
const svgs = document.querySelectorAll('svg');
svgs.forEach((svg, idx) => {
const shapes = svg.querySelector('path, ellipse, circle');
if (shapes) {
shapes.setAttribute('fill', purples[idx]);
}
});
}