diff --git a/plugins/jspsych-reconstruction.js b/plugins/jspsych-reconstruction.js new file mode 100644 index 00000000..c4d46e62 --- /dev/null +++ b/plugins/jspsych-reconstruction.js @@ -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($('
')); + + $('#jspsych-reconstruction-stim-container').html(trial.stim_function(param)); + + // add submit button + display_element.append($('