/** * jspsych-image-button-response * Josh de Leeuw * * plugin for displaying a stimulus and getting a button response * * documentation: docs.jspsych.org * **/ jsPsych.plugins["image-button-response"] = (function() { var plugin = {}; jsPsych.pluginAPI.registerPreload('image-button-response', 'stimulus', 'image'); plugin.info = { name: 'image-button-response', description: '', parameters: { stimulus: { type: jsPsych.plugins.parameterType.IMAGE, pretty_name: 'Stimulus', default: undefined, description: 'The image to be displayed' }, stimulus_height: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Image height', default: null, description: 'Set the image height in pixels' }, stimulus_width: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Image width', default: null, description: 'Set the image width in pixels' }, maintain_aspect_ratio: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Maintain aspect ratio', default: true, description: 'Maintain the aspect ratio after setting width or height' }, choices: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Choices', default: undefined, array: true, description: 'The labels for the buttons.' }, button_html: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Button HTML', default: '', array: true, description: 'The html of the button. Can create own style.' }, prompt: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Prompt', default: null, description: 'Any content here will be displayed under the button.' }, stimulus_duration: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Stimulus duration', default: null, description: 'How long to hide the stimulus.' }, trial_duration: { type: jsPsych.plugins.parameterType.INT, pretty_name: 'Trial duration', default: null, description: 'How long to show the trial.' }, margin_vertical: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Margin vertical', default: '0px', description: 'The vertical margin of the button.' }, margin_horizontal: { type: jsPsych.plugins.parameterType.STRING, pretty_name: 'Margin horizontal', default: '8px', description: 'The horizontal margin of the button.' }, response_ends_trial: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Response ends trial', default: true, description: 'If true, then trial will end when user responds.' }, render_on_canvas: { type: jsPsych.plugins.parameterType.BOOL, pretty_name: 'Render on canvas', default: true, description: 'If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).'+ 'If false, the image will be shown via an img element.' } } } plugin.trial = function(display_element, trial) { var height, width; var html; if (trial.render_on_canvas) { // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML) if (display_element.hasChildNodes()) { // can't loop through child list because the list will be modified by .removeChild() while (display_element.firstChild) { display_element.removeChild(display_element.firstChild); } } // create canvas element and image var canvas = document.createElement("canvas"); canvas.id = "jspsych-image-button-response-stimulus"; canvas.style.margin = 0; canvas.style.padding = 0; var img = new Image(); img.src = trial.stimulus; // determine image height and width if (trial.stimulus_height !== null) { height = trial.stimulus_height; if (trial.stimulus_width == null && trial.maintain_aspect_ratio) { width = img.naturalWidth * (trial.stimulus_height/img.naturalHeight); } } else { height = img.naturalHeight; } if (trial.stimulus_width !== null) { width = trial.stimulus_width; if (trial.stimulus_height == null && trial.maintain_aspect_ratio) { height = img.naturalHeight * (trial.stimulus_width/img.naturalWidth); } } else if (!(trial.stimulus_height !== null & trial.maintain_aspect_ratio)) { // if stimulus width is null, only use the image's natural width if the width value wasn't set // in the if statement above, based on a specified height and maintain_aspect_ratio = true width = img.naturalWidth; } canvas.height = height; canvas.width = width; // create buttons var buttons = []; if (Array.isArray(trial.button_html)) { if (trial.button_html.length == trial.choices.length) { buttons = trial.button_html; } else { console.error('Error in image-button-response plugin. The length of the button_html array does not equal the length of the choices array'); } } else { for (var i = 0; i < trial.choices.length; i++) { buttons.push(trial.button_html); } } var btngroup_div = document.createElement('div'); btngroup_div.id = "jspsych-image-button-response-btngroup"; html = ''; for (var i = 0; i < trial.choices.length; i++) { var str = buttons[i].replace(/%choice%/g, trial.choices[i]); html += '
'; } btngroup_div.innerHTML = html; // add canvas to screen and draw image display_element.insertBefore(canvas, null); var ctx = canvas.getContext("2d"); ctx.drawImage(img,0,0,width,height); // add buttons to screen display_element.insertBefore(btngroup_div, canvas.nextElementSibling); // add prompt if there is one if (trial.prompt !== null) { display_element.insertAdjacentHTML('beforeend', trial.prompt); } } else { // display stimulus as an image element html = '