Present an image jsPsych 8.3.0
In this lesson, you will build on the fixation-cross example and learn how a jsPsych trial can preload an image, display it, and record a keyboard response. We will follow the sample script from top to bottom and explain how the pieces connect.
Show an image and record a response
Load an image first, show it, and wait for a keypress
Press any key to continue.
The sample uses the image-keyboard-response plugin. The image file is specified in the trial's stimulus setting. A separate preload trial loads the same file first so it is ready when the image trial begins.
You can also set auto_preload: true in a jsPsychPreload trial to have jsPsych look through the timeline for files to preload. This example lists the image explicitly in images: [...] so it is easy to see exactly which file is being loaded.
The sample uses the included ribbon.jpg. If you replace it with another image, put the new file in the media/ folder and update the path in both the preload settings and the image trial. The two paths should refer to the same file.
The sample HTML is kept beside dist/ and media/. Do not move it into this lesson's en/ folder; otherwise, the relative paths to the jsPsych files and image would need to change.
Read the script from top to bottom
-
Load the library, plugins, and stylesheet
<script src="dist/jspsych.js"></script> <script src="dist/plugin-preload.js"></script> <script src="dist/plugin-image-keyboard-response.js"></script> <link rel="stylesheet" href="dist/jspsych.css">jspsych.jsis the main library that initializes and runs the experiment.plugin-preload.jsprovides the preload trial.plugin-image-keyboard-response.jsprovides a trial that displays an image and listens for a keyboard response.jspsych.csssupplies the default appearance of jsPsych screens.These files are in the course's
dist/folder. The sample file sits beside that folder, sodist/plugin-preload.jsmeans “go into thedistfolder next to this HTML file.” The scripts are loaded in order: first the library, then the plugins that the experiment will use. -
Initialize the experiment and choose what happens at the end
const jsPsych = initJsPsych({ on_finish: () => { jsPsych.data.displayData(); } });initJsPsych({...})prepares a new jsPsych experiment. The settings inside the curly braces customize it. The resulting jsPsych object is stored in thejsPsychvariable so the script can use it later.on_finishis a callback: a function jsPsych runs after the whole experiment has finished. The arrow() =>begins this function. Here, it callsjsPsych.data.displayData()to show the collected data on the screen. This is useful for inspecting a small example; it is not the same as securely saving research data to a server.The braces following
on_finishare the function body. The semicolon afterdisplayData()ends that instruction, and the closing braces finish the function and the initialization settings. -
Define the preload trial
const preload = { type: jsPsychPreload, images: ['media/ribbon.jpg'] };preloadis a variable that holds the settings for a trial. Itstypeselects the preload plugin. Theimagessetting is an array: square brackets mark a list, and the quoted path tells jsPsych which image to load.The path is relative to
sample-image.html. Because the sample file andmedia/folder are siblings,media/ribbon.jpgmeans “open themediafolder beside this file and loadribbon.jpg.” Preloading does not show the image; it only prepares the file. -
Define the image-response trial
const imageTrial = { type: jsPsychImageKeyboardResponse, stimulus: 'media/ribbon.jpg', prompt: '<p>Press any key to continue.</p>' };imageTrialis the second trial definition. Itstypeselects the image keyboard-response plugin. Thestimulusis the image to display; here its path is the same one that was preloaded.promptis optional HTML shown with the image. The<p>tags make the instruction a paragraph. The text is surrounded by quotation marks because it is a JavaScript string. In the shared sample, the prompt is written in both Japanese and English. -
Use the default response behavior
This trial does not specify
choicesortrial_duration. With this plugin's defaults, the participant may press a key and the trial ends when a key is pressed. If you want to accept only particular keys, add achoicessetting. If you want the image to disappear after a fixed time, add atrial_durationin milliseconds. -
Run the trials in order
jsPsych.run([preload, imageTrial]);jsPsych.run(...)starts the experiment. The array lists the trials in the order they should run: firstpreload, thenimageTrial. This order ensures that the image is loaded before it is shown. After the image trial ends, the experiment finishes and theon_finishcallback displays the data.
Preload the files used in a task
For a single small image, the browser may load the file quickly. In a real experiment, images can be larger or there may be many of them. If a file is not ready when its trial starts, the participant may see a delay. Preloading gives the browser a chance to fetch resources before the experiment needs them.
const preload = {
type: jsPsychPreload,
images: ['media/ribbon.jpg']
};
List every image path in the images array. For example, multiple files can be listed with commas: images: ['media/one.jpg', 'media/two.jpg']. The files must exist at those paths.
The image path appears in two places in this example: once in images so it is preloaded, and once in stimulus so the image trial can display it. If you change the filename or folder, update both places. A typo in either path can prevent the image from loading.
What does auto_preload do?
When enabled, the preload plugin can inspect the experiment timeline and look for resources used by trials. In this introductory example, the explicit images list is easier to understand because it shows exactly which path is being loaded. For a larger experiment, automatic preloading may reduce the need to maintain a long list by hand.
What data does a keypress trial collect?
The image keyboard-response plugin records information about the trial, including the participant's key response and response time. Response time is typically measured in milliseconds from when the stimulus appears until the key is pressed.
When the timeline finishes, jsPsych.data.displayData() renders the collected data in the browser so you can inspect the example. This demonstration does not upload data to a server. Real studies need an appropriate, approved plan for participant consent, privacy, and secure data storage.
View the complete shared sample HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>画像提示 / Image presentation | jsPsych 8.3.0</title>
<script src="dist/jspsych.js"></script>
<script src="dist/plugin-preload.js"></script>
<script src="dist/plugin-image-keyboard-response.js"></script>
<link rel="stylesheet" href="dist/jspsych.css">
</head>
<body></body>
<script>
const jsPsych = initJsPsych({
on_finish: () => {
jsPsych.data.displayData();
}
});
const preload = {
type: jsPsychPreload,
images: ['media/ribbon.jpg']
};
const imageTrial = {
type: jsPsychImageKeyboardResponse,
stimulus: 'media/ribbon.jpg',
prompt: '<p>何かキーを押すと次に進みます。<br>Press any key to continue.</p>'
};
jsPsych.run([preload, imageTrial]);
</script>
</html>