mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-11 16:18:11 +00:00
add reconstruction plugin
This commit is contained in:
parent
6a634244e3
commit
05863b3e5d
113
plugins/jspsych-reconstruction.js
Normal file
113
plugins/jspsych-reconstruction.js
Normal file
@ -0,0 +1,113 @@
|
||||
/**
|
||||
* jspsych-reconstruction
|
||||
* a jspsych plugin for a reconstruction task where the subject recreates
|
||||
* a stimulus from memory
|
||||
*
|
||||
* Josh de Leeuw
|
||||
*
|
||||
* documentation: docs.jspsych.org
|
||||
*
|
||||
*/
|
||||
|
||||
(function($) {
|
||||
jsPsych['reconstruction'] = (function() {
|
||||
|
||||
var plugin = {};
|
||||
|
||||
plugin.create = function(params) {
|
||||
|
||||
params = jsPsych.pluginAPI.enforceArray(params, ['data']);
|
||||
|
||||
var n_trials = (typeof params.starting_value == 'undefined') ? 1 : params.starting_value.length
|
||||
|
||||
var trials = [];
|
||||
for (var i = 0; i < n_trials; i++) {
|
||||
trials.push({
|
||||
starting_value: (typeof params.starting_value == 'undefined') ? [0.5] : params.starting_value[i],
|
||||
stim_function: params.stim_function,
|
||||
step_size: params.step_size || 0.05,
|
||||
key_increase: params.key_increase || 'h',
|
||||
key_decrease: params.key_decrease || 'g'
|
||||
});
|
||||
}
|
||||
return trials;
|
||||
};
|
||||
|
||||
plugin.trial = function(display_element, trial) {
|
||||
|
||||
// if any trial variables are functions
|
||||
// this evaluates the function and replaces
|
||||
// it with the output of the function
|
||||
trial = jsPsych.pluginAPI.normalizeTrialVariables(trial, ['stim_function']);
|
||||
|
||||
// current param level
|
||||
var param = trial.starting_value;
|
||||
|
||||
// set-up key listeners
|
||||
var after_response = function(info){
|
||||
// get new param value
|
||||
if(info.key == key_increase) {
|
||||
param = param + step_size;
|
||||
} else if(info.key == key_decrease) {
|
||||
param = param - step_size;
|
||||
}
|
||||
param = Math.max(Math.min(1,param),0);
|
||||
|
||||
// refresh the display
|
||||
draw(param);
|
||||
}
|
||||
|
||||
// listen for responses
|
||||
var key_listener = jsPsych.pluginAPI.getKeyboardResponse(after_response, [trial.key_increase, trial.key_decrease], 'date', true);
|
||||
|
||||
// draw first iteration
|
||||
draw(param);
|
||||
|
||||
function draw(param){
|
||||
display_element.html('');
|
||||
|
||||
display_element.append($('<div id="jspsych-reconstruction-stim-container"></div>'));
|
||||
|
||||
$('#jspsych-reconstruction-stim-container').html(trial.stim_function(param));
|
||||
|
||||
// add submit button
|
||||
display_element.append($('<button>', {
|
||||
'id': 'jspsych-survey-text-next',
|
||||
'class': 'jspsych-survey-text'
|
||||
}));
|
||||
$("#jspsych-survey-text-next").html('Submit Answers');
|
||||
$("#jspsych-survey-text-next").click(
|
||||
|
||||
}
|
||||
|
||||
function endTrial(){
|
||||
// measure response time
|
||||
var endTime = (new Date()).getTime();
|
||||
var response_time = endTime - startTime;
|
||||
|
||||
// save data
|
||||
jsPsych.data.write($.extend({}, {
|
||||
"rt": response_time,
|
||||
"final_value": param,
|
||||
"start_value": trial.starting_value
|
||||
}, trial.data));
|
||||
|
||||
display_element.html('');
|
||||
|
||||
// next trial
|
||||
if (trial.timing_post_trial > 0) {
|
||||
setTimeout(function() {
|
||||
jsPsych.finishTrial();
|
||||
}, trial.timing_post_trial);
|
||||
} else {
|
||||
jsPsych.finishTrial();
|
||||
}
|
||||
}
|
||||
|
||||
var startTime = (new Date()).getTime();
|
||||
|
||||
};
|
||||
|
||||
return plugin;
|
||||
})();
|
||||
})(jQuery);
|
Loading…
Reference in New Issue
Block a user