jsPsych/examples/save-trial-parameters.html
2021-02-20 14:57:29 -08:00

95 lines
4.0 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<script src="../jspsych.js"></script>
<script src="../plugins/jspsych-html-keyboard-response.js"></script>
<script src="../plugins/jspsych-html-button-response.js"></script>
<script src="../plugins/jspsych-canvas-keyboard-response.js"></script>
<link rel="stylesheet" href="../css/jspsych.css">
</head>
<body></body>
<script>
// Plugins save some trial parameters by default.
// If you want to save parameters to the trial data that aren't normally saved, you can do so with the save_trial_parameters.
// For example, the html-keyboard-response plugin doesn't save the prompt string or choices array by default,
// but this information will be saved in the example trial below.
var trial_1 = {
type: 'html-keyboard-response',
stimulus: '<p style="color: red; font-size: 48px; font-weight: bold;">GREEN</p>',
choices: ['y', 'n'],
prompt: '<p>Does the color match the word? (y or n)</p><p>The prompt and choices parameters will be saved to the data in this trial.</p>',
save_trial_parameters: {
choices: true,
prompt: true
}
}
// You can also uses 'false' in the save_trial_parameters option to prevent a parameter from being saved
// that is normally saved by default.
var trial_2 = {
type: 'html-keyboard-response',
stimulus: '<p style="color: red; font-size: 48px; font-weight: bold;">RED</p>',
choices: ['y', 'n'],
prompt: '<p>Does the color match the word? (y or n)</p><p>The stimulus will not be saved to the data in this trial.</p>',
save_trial_parameters: {
stimulus: false,
prompt: true
}
}
// If you use dynamic parameters and/or timeline variables,
// then the value that is returned from the function will be saved to the data.
var trial_3 = {
type: 'html-button-response',
stimulus: '<p style="color: orange; font-size: 48px; font-weight: bold;">BLUE</p>',
choices: function() {
// randomly shuffle the yes/no button order on each trial
var button_choices = ['Yes','No'];
return jsPsych.randomization.shuffle(button_choices);
},
post_trial_gap: function() {
// randomly select an ITI duration
return jsPsych.randomization.sampleWithoutReplacement([200,300,400,500],1)[0];
},
prompt: '<p>Does the color match the word?</p><p>Randomized button choice order and post trial gap duration.<br>'+
'These values will be saved to the data.</p>',
save_trial_parameters: {
// save the randomly-selected button order and ITI value to the trial data
choices: true,
post_trial_gap: true
},
on_finish: function(data) {
// determine which button was pressed, based on the response (button index: 0 or 1) and choices array (randomized order of button labels)
data.response_button_label = data.choices[data.response];
}
}
// If the parameter is a function and is NOT a dynamic parameter,
// (i.e. it is a parameter that is always expected to be a function, like the stimulus function in canvas-* plugins),
// then the function will be saved to the data as a string.
var trial_4 = {
type: 'canvas-keyboard-response',
stimulus: function (c) {
var ctx = c.getContext('2d');
ctx.beginPath();
ctx.arc(250, 250, 200, 0, 2 * Math.PI);
ctx.stroke();
},
choices: ['e', 'i'],
prompt: '<p>Is this a circle or a rectangle? Press "e" for circle and "i" for rectangle.</p>' +
'<p>The stimulus function will be saved to the data as a string.</p>',
save_trial_parameters: {
stimulus: true
}
}
jsPsych.init({
timeline: [trial_1, trial_2, trial_3, trial_4],
on_finish: function () {
jsPsych.data.displayData();
}
});
</script>
</html>