-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspsych-similarity.js
204 lines (165 loc) · 6.1 KB
/
jspsych-similarity.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
/**
* jspsych-similarity.js
* Josh de Leeuw
*
* This plugin create a trial where two images are shown sequentially, and the subject rates their similarity using a slider controlled with the mouse.
*
* documentation: docs.jspsych.org
*
*/
jsPsych.plugins.similarity = (function() {
var plugin = {};
jsPsych.pluginAPI.registerPreload('similarity', 'stimuli', 'image');
plugin.trial = function(display_element, trial) {
// default parameters
trial.labels = (typeof trial.labels === 'undefined') ? ["Not at all similar", "Identical"] : trial.labels;
trial.intervals = trial.intervals || 100;
trial.show_ticks = (typeof trial.show_ticks === 'undefined') ? false : trial.show_ticks;
trial.show_response = trial.show_response || "SECOND_STIMULUS";
trial.timing_first_stim = trial.timing_first_stim || 1000; // default 1000ms
trial.timing_second_stim = trial.timing_second_stim || -1; // -1 = inf time; positive numbers = msec to display second image.
trial.timing_image_gap = trial.timing_image_gap || 1000; // default 1000ms
trial.is_html = (typeof trial.is_html === 'undefined') ? false : trial.is_html;
trial.prompt = (typeof trial.prompt === 'undefined') ? '' : trial.prompt;
// if any trial variables are functions
// this evaluates the function and replaces
// it with the output of the function
trial = jsPsych.pluginAPI.evaluateFunctionParameters(trial);
// this array holds handlers from setTimeout calls
// that need to be cleared if the trial ends early
var setTimeoutHandlers = [];
// show the images
if (!trial.is_html) {
display_element.append($('<img>', {
"src": trial.stimuli[0],
"id": 'jspsych-sim-stim'
}));
} else {
display_element.append($('<div>', {
"html": trial.stimuli[0],
"id": 'jspsych-sim-stim'
}));
}
if (trial.show_response == "FIRST_STIMULUS") {
show_response_slider(display_element, trial);
}
setTimeoutHandlers.push(setTimeout(function() {
showBlankScreen();
}, trial.timing_first_stim));
function showBlankScreen() {
$('#jspsych-sim-stim').css('visibility', 'hidden');
setTimeoutHandlers.push(setTimeout(function() {
showSecondStim();
}, trial.timing_image_gap));
}
function showSecondStim() {
if (!trial.is_html) {
$('#jspsych-sim-stim').attr('src', trial.stimuli[1]);
} else {
$('#jspsych-sim-stim').html(trial.stimuli[1]);
}
$('#jspsych-sim-stim').css('visibility', 'visible');
if (trial.show_response == "SECOND_STIMULUS") {
show_response_slider(display_element, trial);
}
if (trial.timing_second_stim > 0) {
setTimeoutHandlers.push(setTimeout(function() {
$("#jspsych-sim-stim").css('visibility', 'hidden');
if (trial.show_response == "POST_STIMULUS") {
show_response_slider(display_element, trial);
}
}, trial.timing_second_stim));
}
}
function show_response_slider(display_element, trial) {
var startTime = (new Date()).getTime();
// create slider
display_element.append($('<div>', {
"id": 'slider',
"class": 'sim'
}));
$("#slider").slider({
value: Math.ceil(trial.intervals / 2),
min: 1,
max: trial.intervals,
step: 1,
});
// show tick marks
if (trial.show_ticks) {
for (var j = 1; j < trial.intervals - 1; j++) {
$('#slider').append('<div class="slidertickmark"></div>');
}
$('#slider .slidertickmark').each(function(index) {
var left = (index + 1) * (100 / (trial.intervals - 1));
$(this).css({
'position': 'absolute',
'left': left + '%',
'width': '1px',
'height': '100%',
'background-color': '#222222'
});
});
}
// create labels for slider
display_element.append($('<ul>', {
"id": "sliderlabels",
"class": 'sliderlabels',
"css": {
"width": "100%",
"height": "3em",
"margin": "10px 0px 0px 0px",
"padding": "0px",
"display": "block",
"position": "relative"
}
}));
for (var j = 0; j < trial.labels.length; j++) {
$("#sliderlabels").append('<li>' + trial.labels[j] + '</li>');
}
// position labels to match slider intervals
var slider_width = $("#slider").width();
var num_items = trial.labels.length;
var item_width = slider_width / num_items;
var spacing_interval = slider_width / (num_items - 1);
$("#sliderlabels li").each(function(index) {
$(this).css({
'display': 'inline-block',
'width': item_width + 'px',
'margin': '0px',
'padding': '0px',
'text-align': 'center',
'position': 'absolute',
'left': (spacing_interval * index) - (item_width / 2)
});
});
// create button
display_element.append($('<button>', {
'id': 'next',
'class': 'sim',
'html': 'Submit Answer'
}));
// if prompt is set, show prompt
if (trial.prompt !== "") {
display_element.append(trial.prompt);
}
$("#next").click(function() {
var endTime = (new Date()).getTime();
var response_time = endTime - startTime;
// kill any remaining setTimeout handlers
for (var i = 0; i < setTimeoutHandlers.length; i++) {
clearTimeout(setTimeoutHandlers[i]);
}
var score = $("#slider").slider("value");
var trial_data = {
"sim_score": score,
"rt": response_time,
"stimulus": JSON.stringify([trial.stimuli[0], trial.stimuli[1]])
};
// goto next trial in block
display_element.html('');
jsPsych.finishTrial(trial_data);
});
}
};
return plugin;
})();