-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
341 lines (301 loc) · 10.5 KB
/
index.html
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
336
337
338
339
340
341
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>StaircaseJS example</title>
<script
src="https://code.jquery.com/jquery-2.2.4.js"
integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="
crossorigin="anonymous"></script>
<script src="Staircase.js"></script>
</head>
<body>
<div id="wrapper">
<div id='test-body'>
<section id="StimulusArea">
<div id="Canvases" style="width: 750px; margin:auto;">
<p class="tutorial-help" style="text-align: center; font-size: 1.3em;">Use the arrow keys to indicate which circle has more dots inside</p>
<canvas id="Canvas0" class="left" width="310" height="310"></canvas>
<canvas id="Canvas1" class="right" width="310" height="310"></canvas>
</div>
</section>
<section id="BlockFeedback">
<div id="Feedback" style="text-align: center;">
<p id='FeedbackScore'></p>
</div>
<div id="Graph" style="width: 80%; display: block; margin: auto"></div>
<br>
<div id='FeedbackButton' class="button">Start Again</div>
</section>
</div>
</div>
<script>
/*
StaircaseJS example
* Matt Jaquiery - 2017
TODO:
*/
//------------------------------------------------------------------------------
// Runtime variables
var currentTrialNumber;
var validTrials;
var currentTrial;
var trials = [];
var score;
var stair;
var trialActive = false;
var maxDots = 200;
$(window).load(setupInstructions);
function setupInstructions() {
$(document).on('keyup', function (evt) {
keyPress(evt.keyCode);
});
stair = new Staircase({
ratio: {
firstVal: Math.round(maxDots*0.8),
down: 2, // down is the number of correct answers required before we increase the difficulty
up: 1, // up is the number of incorrect answers before we decrease the difficulty
stepSizeDown: 3, // how much we in/decrease by
stepSizeUp: 3 * 0.5488, // Converge to 80.35% correct ('downUpRatio' and 'down' affect this)
limits: [(maxDots/2)+1, maxDots], // don't allow equal ratio
direction: -1, // -1 indicates that easier = greater values; 1 would indicate easier = lower values
reversalLimit: 5, // How many reversals to do before stopping
verbosity: 1, // Enable logging for debugging
}
});
stair.init(); // Activate the staircase
startTest();
}
function startTest() {
$('#BlockFeedback').hide();
currentTrialNumber = 0;
validTrials = 0;
score = 0;
startTrial();
}
/*
Each trial consists of painting stimuli and obtaining a response from the participant
*/
function startTrial() {
console.log('Starting trial '+currentTrialNumber);
$('#StimulusArea').show();
currentTrial = {};
playerChoice = -1;
drawStimuli();
}
function drawStimuli() {
// Trial info
var d = getDensity();
currentTrial.targetDensity0 = d[0];
currentTrial.targetDensity1 = d[1];
// Prepare canvas
for(var i=0;i<2;i++) {
var ctx = $('#Canvas'+i)[0].getContext("2d");
ctx.clearRect(0,0,ctx.canvas.clientWidth,ctx.canvas.clientHeight);
ctx.beginPath();
var radius = (ctx.canvas.clientWidth-10)/2;
ctx.arc(ctx.canvas.clientWidth/2,ctx.canvas.clientHeight/2,radius,0,2*Math.PI);
ctx.lineWidth = 3;
ctx.stroke();
// Draw stimuli
if (i==0)
currentTrial.density0 = drawStimulus("Canvas"+i,d[i]);
else
currentTrial.density1 = drawStimulus("Canvas"+i,d[i]);
}
decisionPhase();
}
function getDensity() {
var p; // ratio of circles to one another
var d; // densities
p = Math.round(stair.getLast('ratio'));
d = [p,maxDots-p];
// try again if we're exactly equal
if (d[0]==d[1])
d = getDensity();
// return randomised order
return (Math.random()<0.5)? d : [d[1],d[0]];
}
function drawStimulus(canvasID,density) {
var ctx = $('#'+canvasID)[0].getContext("2d");
var centre = {x: ctx.canvas.clientWidth/2, y: ctx.canvas.clientHeight/2};
var circles = [];
var radius = 4;
var stimDrawTime = Date.now();
for(var i=0;i<density;i++) {
// Find somewhere to put the next circle
var position = false;
var loop = 0;
while(position === false && loop<100) {
position = {x:radius+Math.round(Math.random()*(ctx.canvas.clientWidth-(2*radius))), y:radius+Math.round(Math.random()*(ctx.canvas.clientWidth-(2*radius)))};
// Check we're within the major circle
if (Math.sqrt(Math.pow(position.x-centre.x,2)+Math.pow(position.y-centre.y,2)) > ((ctx.canvas.clientWidth-(10))/2)-(radius*2)) {
position = false;
} else {
// Check we don't clash with preexisting circles
for(var n=0;n<circles.length;n++) {
if(Math.abs(position.x-circles[n].x)<=((radius+1)*2) && Math.abs(position.y-circles[n].y)<=((radius+1)*2)) {
position = false;
break;
}
}
}
loop++;
}
if (position !== false) {
// Place the circle
circles[circles.length] = position;
ctx.beginPath();
ctx.arc(position.x,position.y,radius,0,2*Math.PI);
ctx.fillStyle = 'black';
ctx.fill();
ctx.stroke();
}
}
console.log(canvasID+"> Drew "+circles.length+" circles in "+(Date.now()-stimDrawTime)+"ms");
return circles.length;
}
function decisionPhase() {
trialActive = true;
}
function decisionClick(id) {
if(!trialActive)
return;
trialActive = false;
currentTrial.decisionChoice = id;
currentTrial.errorCode = 0;
nextTrial();
}
function nextTrial() {
$('#StimulusArea').hide();
processTrialResult();
if(currentTrial.errorCode == 0)
validTrials++;
currentTrialNumber++;
if(!stair.reversalLimitReached('ratio'))
startTrial();
else
showBlockFeedback();
}
function processTrialResult() {
var oldScore = score;
if (((currentTrial.decisionChoice == 0 && currentTrial.density0 > currentTrial.density1) || (currentTrial.decisionChoice == 1 && currentTrial.density0 < currentTrial.density1)) && currentTrial.errorCode == 0) {
score++;
}
if(currentTrial.errorCode==0) {
stair.next(oldScore!=score);
trials[trials.length] = currentTrial;
}
}
function showBlockFeedback() {
$('#Test').hide();
$('#BlockFeedback').show();
getFeedbackText();
makeGraph();
$('#FeedbackButton').on('click',function (evt) {
window.location = window.location.href;
});
}
function keyPress(k) {
switch (k) {
case 37: // Leftarrow
decisionClick(0);
break;
case 39: // Rightarrow
decisionClick(1);
break;
}
return;
}
function getFeedbackText() {
$('#FeedbackScore').html("You scored "+score+"/"+(currentTrialNumber+1)+" ("+(Math.round(score/(currentTrialNumber+1)*1000)/10)+"%)<br>Your end difficulty was "+stair.getFinalVal('ratio'));
}
function makeGraph() {
var data = [];
var cols = ['Trial Number','Difficulty','Calibrated Difficulty'];
var finalDiff = stair.getFinalVal('ratio');
for(var i=0;i<trials.length;i++) {
data[i] = [i,(trials[i].density0 < trials[i].density1)? trials[i].density1 : trials[i].density0, finalDiff];
}
data.unshift(cols);
drawChart(data, 'Graph');
}
</script>
<style>
area {
cursor: pointer;
}
.game-area {
width: 700px;
margin: auto;
}
.prompt {
font-size: 2em;
text-align: center;
}
#FeedbackText {
clear: both;
font-size: 1.5em;
}
.decision-img {
cursor: pointer;
}
#ConfidenceTable {
width: 100%;
border-spacing: 30px;
border-collapse: separate;
}
td.confidence {
width: 25%;
padding: 5px;
text-align: center;
border: 2px solid black;
border-radius: 30px;
}
td.confidence:hover {
cursor: pointer;
}
.left {
margin-left: 18px;
}
.right {
float: right;
margin-right: 18px;
}
.button {
font-size: 1.5em;
font-weight: bold;
width: 10em;
height: 1.5em;
margin: auto;
margin-bottom: 0.2em;
text-align: center;
padding-top: 0.2em;
border: 0px solid black;
border-radius: 0.4em;
box-shadow: 0px 0px 5px 1px black;
background-color: #f0f0f0;
cursor: pointer;
}
.button:hover {
box-shadow: 0px 0px 15px 1px black;
background-color: #e0e0e0;
}
</style>
<!-- Google Charts stuff -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
function drawChart(array,divID) {
var data = google.visualization.arrayToDataTable(array);
var options = {
legend: { position: 'bottom' },
height: 500,
width: Math.floor(document.getElementById(divID).getBoundingClientRect().width),
};
var chart = new google.visualization.LineChart(document.getElementById(divID));
chart.draw(data, options);
}
</script>
</body>
</html>