/
experiment.js
276 lines (248 loc) · 11.7 KB
/
experiment.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
const resultList = [];
let result = {};
let isActive = false;
let praceticeRounds = 16;
let realRounds = 144;
function setButtonHTML() {
const angryButton = `<button id="angry" class="button" onclick="checkEmotion('ANGRY');">Angry</button>`;
const happyButton = `<button id="happy" class="button" onclick="checkEmotion('HAPPY');">Happy</button>`;
const sadButton = `<button id="sad" class="button" onclick="checkEmotion('SAD');">Sad</button>`;
const scaredButton = `<button id="scared" class="button" onclick="checkEmotion('SCARED');">Scared</button>`;
const array = [angryButton, happyButton, sadButton, scaredButton];
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
const numbers = ["one", "two", "three", "four"];
for (let i = 0; i < array.length; i++) {
array[i] = array[i].replace(`class="button"`, `class="button ${numbers[i]}"`);
}
return array.join(" ");
}
const buttonGroupInnerHTML = setButtonHTML();
function generateImage() {
const sadImages = [
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad1.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad10.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad2.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad3.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad4.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad5.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad6.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad7.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad8.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad9.jpeg'
];
const angryImages = [
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry1.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry10.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry2.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry3.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry4.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry5.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry6.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry7.jpeg",
"https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry8.jpeg",
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/angry9.jpeg'
];
const happyImages = [
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy1.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy10.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy2.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy3.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy4.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy5.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy6.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy7.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy8.jpeg',
'https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/happy9.jpeg'
];
const scaredImages = [
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared1.jpeg?alt=media&token=0da6347f-e36c-49f2-b1ea-4f0e7e7170ab',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared10.jpeg?alt=media&token=96b47c30-f5bc-460c-8ae1-c7146f5b7c61',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared2.jpeg?alt=media&token=da26bcf1-9c0a-4993-b65e-23ab3502add2',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared3.jpeg?alt=media&token=9afa9190-0263-40b0-b55b-2439cd908dca',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared4.jpeg?alt=media&token=53f6454d-c89e-4b51-877b-4ca0e59b8f1a',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared5.jpeg?alt=media&token=b4a4368d-fa6d-4cc3-8957-037983853f47',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared6.jpeg?alt=media&token=5674044b-fe69-4776-b005-8164ad0022a4',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared7.jpeg?alt=media&token=65bbb185-74ec-43cd-8ba4-997a99338fef',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared8.jpeg?alt=media&token=c89b2ffa-b345-4366-b63d-ac3a24b27597',
'https://firebasestorage.googleapis.com/v0/b/emotional-stroop.appspot.com/o/scared9.jpeg?alt=media&token=85e96257-6b9a-418f-98d1-5937e7617176'
];
const imageMap = {
0: sadImages,
1: angryImages,
2: happyImages,
3: scaredImages
}
const emotionList = [
"SAD",
"ANGRY",
"HAPPY",
"SCARED"
]
const min = 0;
const max = 3;
const rand = Math.floor(Math.random() * (max - min + 1) + min);
const imageList = imageMap[rand];
const randomImage = imageList[Math.floor(Math.random() * imageList.length)];
const randomImageEmotion = emotionList[rand];
const randomText = emotionList[Math.floor(Math.random() * emotionList.length)];
result["emotion"] = randomImageEmotion;
result["text"] = randomText;
const imageContainer = document.getElementById("imageContainer");
const img = document.createElement('img');
const text = document.createElement('p');
text.classList.add('centered')
text.innerText = randomText;
text.style.display = 'none';
text.setAttribute('id', 'text');
img.onload = randomTimer;
img.src = randomImage;
img.style.visibility = 'hidden';
img.setAttribute('id', 'img');
img.classList.add(randomImageEmotion)
imageContainer.className = '';
// add the text emotion ("sad", "happy"...) to imageContainer class.
imageContainer.classList.add(randomText);
imageContainer.appendChild(img);
imageContainer.appendChild(text)
}
function showImage() {
const image = document.getElementById('img');
image.style.visibility = 'visible';
const text = document.getElementById('text');
text.style.display = 'inline';
result["startTime"] = performance.now();
}
function checkEmotion(chosenEmotion) {
let nextPageName = "";
if (isActive) {
isActive = false;
result["endTime"] = performance.now();
result["chosenEmotion"] = chosenEmotion;
const emotionText = document.getElementById("imageContainer").className;
if (emotionText != chosenEmotion) {
result["correctness"] = "incorrect";
} else {
result["correctness"] = "correct";
}
if (praceticeRounds == 0 && realRounds != 0) {
resultList.push(result);
realRounds--
if (realRounds == 0) {
return renderEndPage();
}
nextPageName = "affective conflict resolution task"
} else {
praceticeRounds--
if (praceticeRounds == 0) {
return renderWarning();
}
nextPageName = "practice round"
}
result = {}
resetScreen(nextPageName);
if (praceticeRounds != 0) {
const title = document.getElementsByClassName("title")[0];
if (emotionText != chosenEmotion) {
title.innerHTML = `
<span id="incorrect">Incorrect</span>
`;
} else {
title.innerHTML = `
<span id="correct">Correct</span>
`;
}
setTimeout(() => {
title.innerHTML = `
practice round
`}, (800));
}
return generateImage();
}
}
function activatecheckEmotion() {
isActive = true;
}
function randomTimer() {
const min = 0.5;
const max = 1.5;
let rand = Math.floor(Math.random() * (max - min + 1) + min);
setTimeout(activatecheckEmotion, rand * 1000)
setTimeout(showImage, rand * 1000);
}
function resetScreen(nextPageName) {
const imageContainer = document.getElementById("imageContainer");
imageContainer.innerHTML = "";
const buttonGroup = document.getElementsByClassName("button-group")[0];
buttonGroup.innerHTML = buttonGroupInnerHTML;
const content = document.getElementById("content");
content.innerHTML = "";
const title = document.getElementsByClassName("title")[0];
if (nextPageName != "affective conflict resolution task") {
title.innerText = nextPageName;
}
}
function renderInstructions() {
const imageContainer = document.getElementById("imageContainer");
imageContainer.innerHTML = `
<img src='https://filedn.com/ltpGl4CBYf5Hf3ADcupJL7B/emostroop_img/sad1.jpeg' alt="">
<div class="centered instruction-image-text">HAPPY</div>
`;
const buttonGroup = document.getElementsByClassName("button-group")[0];
buttonGroup.innerHTML = `
<button class="button" onclick="startTrial('practice round');">Start Practice Rounds</button>
`;
}
function startTrial(nextPageName) {
resetScreen(nextPageName);
generateImage();
}
function renderWarning() {
const imageContainer = document.getElementById("imageContainer");
imageContainer.innerHTML = ``;
const buttonGroup = document.getElementsByClassName("button-group")[0];
buttonGroup.innerHTML = `<button class="button" onclick="startTrial('affective conflict resolution task');">Start</button>`;
const title = document.getElementsByClassName("title")[0];
title.innerHTML = `Warning`;
const content = document.getElementById("content");
content.innerHTML = `<h3>Practice round completed. clicking the button below will start the real trial!</h3>`;
}
function renderEndPage() {
const imageContainer = document.getElementById("imageContainer");
imageContainer.innerHTML = `Thank you for participating in our study.`;
const buttonGroup = document.getElementsByClassName("button-group")[0];
buttonGroup.innerHTML = `<button class="button" onclick="sendResults();">End trial</button>`;
const title = document.getElementsByClassName("title")[0];
title.innerHTML = `Task completed`;
}
async function sendResults(results) {
const imageContainer = document.getElementById("imageContainer");
imageContainer.innerHTML = `You can close this window.`;
const buttonGroup = document.getElementsByClassName("button-group")[0];
buttonGroup.innerHTML = ``;
const title = document.getElementsByClassName("title")[0];
title.innerHTML = `Results saved`;
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
fetch("/save", {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ data: results })
})
.then(handleErrors)
.then(response => console.log("Request complete! response: ", response))
.catch(error => console.log("We got an error: ", error));
}
renderInstructions();