-
Hello, This code is producing a result which is very close to what I want: var slideshow = {
type: "html-keyboard-response",
stimulus: '<table> <tr> <td><img src="stimuli/hose.png"></td> <td><img src="stimuli/ei.png"></td> <td><img src="stimuli/papagei.png"></td> <td><img src="stimuli/fabrik.png"></td></tr> <tr> <td><h1><center>Hose</center></h1></td> <td><h1><center>Ei</center></h1></td> <td><h1><center>Papagei</center></h1></td> <td><h1><center>Fabrik</center></h1></td> </tr></table>',
trial_duration: 5000,
response_ends_trial: false,
data: jsPsych.timelineVariable("data"),
}; However, I have a larger array and would like to randomly select four pictures, present them next to each other for 5 seconds before taking the next four pics from the array. How can this be achieved? Note, that the names of the objects should be written below the pictures. The names are given in the stimulus path and ideally they would automatically be extracted from the file name. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Hi @AnneVogt ,
This probably means you'll need to use a dynamic parameter. You can create a variable that holds all the pictures, shuffle it at the start of the experiment using var stimuli = ['stimuli/host.png', ... ];
stimuli = jsPsych.randomization.shuffle(stimuli); stimulus: function(){
var first_stim = stimuli.pop();
var second_stim = stimuli.pop();
// etc ...
var html = `<table> <tr> <td><img src="${first_stim}"></td> ... `
return html;
}
Inside the function to generate the stimulus you could extract the filename from the variable. Here's a StackOverflow question with some relevant answers. Then you could add the name to the stimulus much like adding the path to the image. |
Beta Was this translation helpful? Give feedback.
-
I made a sample program which is available at I used an excellent hosting service: cognition.run. Here is the code: function getPresentedName(file_path){
const end_position = file_path.indexOf('.png');
return file_path.substr(0, end_position);
}
const trial = {
type: "html-keyboard-response",
stimulus: function(){
return `<table><tr>
<td><img src="${jsPsych.timelineVariable('image1', true)}"></td>
<td><img src="${jsPsych.timelineVariable('image2', true)}"></td>
<td><img src="${jsPsych.timelineVariable('image3', true)}"></td>
<td><img src="${jsPsych.timelineVariable('image4', true)}"></td>
</tr>
<tr>
<td><h1><center>${getPresentedName(jsPsych.timelineVariable('image1', true))}</center></h1></td>
<td><h1><center>${getPresentedName(jsPsych.timelineVariable('image2', true))}</center></h1></td>
<td><h1><center>${getPresentedName(jsPsych.timelineVariable('image3', true))}</center></h1></td>
<td><h1><center>${getPresentedName(jsPsych.timelineVariable('image4', true))}</center></h1></td>
</tr></table>`},
trial_duration: 5000,
response_ends_trial: false,
//data: jsPsych.timelineVariable("data"),
};
const image_names = [
'battery.png',
'clip.png',
'pen.png',
'pin.png',
'pliers.png',
'postit.png',
'scissors.png',
'tape.png'
]
const shuffled_image_names = jsPsych.randomization.shuffle(image_names);
const multi_images = {
timeline: [trial],
timeline_variables: [
{image1: shuffled_image_names[0], image2: shuffled_image_names[1], image3: shuffled_image_names[2], image4: shuffled_image_names[3]},
{image1: shuffled_image_names[4], image2: shuffled_image_names[5], image3: shuffled_image_names[6], image4: shuffled_image_names[7]}
]
}
jsPsych.init({
timeline: [multi_images],
}); |
Beta Was this translation helpful? Give feedback.
-
Thanks for your replies. With your help I got pretty close to where I want - at least when logging my steps in the console. However, the image and image names are not displayed properly. This is my code
The console output is giving me the correct file path, e.g. stimuli/hose.png However, I get the following error message: (I am running the experiment on a local Jatos installation.) Furthermore, the word which is written on the screen is ${first_name2} and not hose
|
Beta Was this translation helpful? Give feedback.
-
There seems to be a general problem with drawing items from an array, as I get the same error plus the additional message You must specify a value for the stimulus parameter in the image-keyboard-response plugin. in this case: var exercise = {
type: "image-keyboard-response",
stimulus: jsPsych.timelineVariable("stim"),
choices: ["q", "p"],
trial_duration: 2000,
response_ends_trial: false,
data: jsPsych.timelineVariable("data"),
on_finish: function(data){
if(data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode(data.target_response)){
data.correct = true;
} else {
data.correct = false;
}
},
};
var exercise_procedure_A = {
timeline: [fixation, exercise],
timeline_variables: example_pics_A,
randomize_order: true
};
var example_pics_A = [
{stim: "schere_u.png", block: 'example', stim_num: '81', data: { target_response: "q"}},
{stim: "pinsel_u.png", block: 'example', stim_num: '82', data: { target_response: "p"}}
]; Furthermore, I receive the error: Uncaught TypeError: character is undefined I copy-pasted the code from an experiment which is running. Therefore, I am at loss where to look for the potential error. I really appreciate your help! |
Beta Was this translation helpful? Give feedback.
-
Hi there, Here the code snippet, I use: var images2 = jsPsych.randomization.shuffle(images);
//presentation of stimuli (with target names)
var slideshow = {`
var images2 = jsPsych.randomization.shuffle(images);
//presentation of stimuli (with target names)
var slideshow = {
type: "html-keyboard-response",
stimulus: function(){
var first_stim = images2.pop();
var second_stim = images2.pop();
var third_stim = images2.pop();
//extracting filenames
//splitting url and getting filename with file extension
var first_name1=first_stim.split('/').pop();
//removing extension and keeping just the filename
var first_name2=first_name1.split('.').shift();
var second_name1=second_stim.split('/').pop();
var second_name2=second_name1.split('.').shift();
var third_name1=third_stim.split('/').pop();
var third_name2=third_name1.split('.').shift();
var html = `<table id=t01> <tr> <td><img src="${first_stim}"></td> <td></td><td><img src="${second_stim}"></td> <td></td> <td><img src="${third_stim}"></td> </tr> <tr><td><h1><center><option>${first_name2}</option></center></</h1></td> <td></td> <td><h1><center><option>${second_name2}</option></center></h1></td> <td></td> <td><h1><center><option>${third_name2}</option></center></h1></td> <td></td> </tr></table>`;
return html;
},
trial_duration: 7000,
response_ends_trial: false,
//data: jsPsych.timelineVariable("data"),
}; |
Beta Was this translation helpful? Give feedback.
I made a sample program which is available at
https://jm91t3ybwr.cognition.run/
I used an excellent hosting service: cognition.run.
Here is the code: