jsPsych/examples/jspsych-preload.html
Becky Gilbert db754538b7
Convert plugins to classes and update examples (#2078)
* modularize plugins: edit index.ts and example files (WIP)

* continue modularizing plugins: edit index.ts and example files (WIP)

* continue modularizing plugins: edit index.ts and example files (WIP)

* continue modularizing plugins and example files, make info static in plugin classes

* add preload flag to plugin info, fix a few parameterType values

* change preload flags in plugin info from audio/image/video strings to boolean

* convert more plugins and example files

* convert more plugins/examples, sync with webgazer branch plugins/examples (not working on this branch)

* remove preload flag from info, change to ParameterType, change to "ALL_KEYS"/"NO_KEYS", change KEY type to KEYS for array params

* remove descriptions from plugin info, fix some ALL_KEYS/NO_KEYS errors

* remove descriptions and add pretty names to plugin info

* remove/edit comments re ALL_KEYS/NO_KEYS and TS errors

* fix some preload plugin errors and convert example file (plugin still not working due to getAutoPreloadList)

* convert RDK plugin and example file

* convert preload plugin and example, add JsPsych getTimelineDescription method for auto-preloading (still not working for audio due to getAudioBuffer

* fix jsPsych getTimelineDescription method so that it does recursive search

* add image and video preloading to plugin examples

* Merge branch 'modularization' into modularization-plugins

* add tests for reconstruction

* fix timeline array getter method: add private timelineDescription var, set to timeline array in run(), rename getTimelineDescription to getTimeline

* changes in response to PR review: fix JsDoc comments, fix array: true in param info, change HTML string param types, add JsDoc author/file/see docs for all plugin classes, switch to @ts-expect-error. Thanks @bjoluc!

* fix JsDoc comments for plugin classes and preload trials parameter

* change preload type to boolean in ParameterInfo interface, and add `preload: false` to virtual-chinrest item_path image parameter

* All my tests except for preload plugin (will do that tomorrow)

* minor fixes to reconstruction tests

* Update preload plugin tests

* Finish test conversion

* switch to arrow functions to fix this keyword errors in plugins, add audio preloading into plugin example files, fix typos

* convert non-plugin example files (WIP, not tested)

* Fix allow_held_keys -> allow_held_key parameter in virtual-chinrest plugin

* Fix `keyboardListener` type in serial-reaction-time plugin

* type fixes for RDK, simplifying `correctOrNot()` function

* fixed ["ALL_KEYS"] => "ALL_KEYS" for iat plugins and tests

* Build jspsych packages before everything else

Dependent builds were previously failing due to missing type definitions

* Remove console.log from html-keyboard-response tests

I think I accidentially committed it.

* fix the delayed start to animation bug (#1885)

* round all RTs to nearest int (#2108)

* fixes and tests #1900 - IAT parameter problems

* finish converting/testing example files, add init settings, add audio preloading, fix errors

* fix progress-bar timeline to demo an example where auto-updating the progress bar works well

* Revert "round all RTs to nearest int (#2108)"

This reverts commit f53145d2e3.

* change how delayed timeline variables are implemented

* use static for generate_stimulus method so that it can be called on the vsl-grid-scene class

* fix external-html plugin and example (switch to arrow function for proper this context, fix incorrect parameter name)

* remove outdated TO DO comments

Co-authored-by: bjoluc <mail@bjoluc.de>
Co-authored-by: Josh de Leeuw <josh.deleeuw@gmail.com>
2021-09-08 09:44:53 -04:00

143 lines
5.7 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<script src="../packages/jspsych/dist/index.browser.js"></script>
<script src="../packages/plugin-preload/dist/index.browser.js"></script>
<script src="../packages/plugin-html-button-response/dist/index.browser.js"></script>
<script src="../packages/plugin-image-button-response/dist/index.browser.js"></script>
<script src="../packages/plugin-audio-button-response/dist/index.browser.js"></script>
<link rel="stylesheet" href="../packages/jspsych/css/jspsych.css">
</head>
<body></body>
<script>
var jsPsych = initJsPsych({
on_finish: function() {
jsPsych.data.displayData();
}
});
// all preload files will print information to the console about file loading success/failure
// through the on_error and on_success trial parameters
// automatically preload all stimuli files based on the timeline
var auto_preload_trial = {
type: jsPsychPreload,
auto_preload: true,
on_error: function(file) {
console.log('Error: ',file);
},
on_success: function(file) {
console.log('Loaded: ',file);
}
};
var start = {
type: jsPsychHtmlButtonResponse,
stimulus: '<p>The previous trial was an automatic preload trial.</p>'+
'<p>This trial loaded all stimuli files that can be automatically preloaded<br>based on the main jsPsych experiment timeline.</p>'+
'<p>In this example experiment, there are two trials in which the stimuli can be automatically preloaded.</p>'+
'<p>The stimuli for these trials have finished loading.</p>',
choices: ['Next']
};
var image_trial = {
type: jsPsychImageButtonResponse,
stimulus: 'img/happy_face_1.jpg',
choices: ['Happy', 'Sad'],
stimulus_width: 400
};
var audio_trial = {
type: jsPsychAudioButtonResponse,
stimulus: ['sound/speech_green.mp3'],
choices: ['Continue']
};
var start_fail_example_1 = {
type: jsPsychHtmlButtonResponse,
stimulus: '<p>The next trial is a manual preload trial.</p>'+
'<p>Manual preloading allows you to load specific files, for instance if you want to load files in batches,<br>'+
'or if the stimuli cannot be automatically preloaded (because you are using timeline variables or dynamic parameters).</p>'+
'<p>In the next preload example, the files in the <i>images</i> array do not exist, so the preload will fail.</p>'+
'<p>The <i>continue_after_error</i> parameter is set to <i>true</i>, so the experiment will continue despite the preload failure.</p>',
choices: ['Next']
};
// manually preload specific files
// continue with the experiment even if one or more files fails to load
var preload_fail_example_1 = {
type: jsPsychPreload,
images: ['BAD_IMAGE_NAME_1','BAD_IMAGE_NAME_2','BAD_IMAGE_NAME_3'],
audio: ['sound/speech_green.mp3','sound/speech_red.mp3'],
message: '<p>Please wait while the files load.</p>',
continue_after_error: true,
max_load_time: 4000,
on_error: function(file) {
console.log('Error: ',file);
},
on_success: function(file) {
console.log('Loaded: ',file);
}
};
var after_fail_example_1 = {
type: jsPsychHtmlButtonResponse,
stimulus: function() {
var message;
// if continue_after_error is true in your preload trial, then you can check the trial
// data to see if preloading was successful
var preload_trial_data = jsPsych.data.getLastTrialData().values()[0];
if (preload_trial_data.success) {
message = '<p>All files loaded successfully!</p>';
} else {
message = '<p>There was a problem loading one or more files.</p>'+
'<p>The files that failed to load are listed in the data for the previous trial.</p>';
}
return message;
},
choices: ['Next']
};
var start_fail_example_2 = {
type: jsPsychHtmlButtonResponse,
stimulus: '<p>The next trial is another manual preload trial.</p>'+
'<p>It has been set up with bad file names, so it will also fail.</p>'+
'<p>This time the <i>continue_after_error</i> parameter is set to <i>false</i>, so the experiment will stop with an error message.</p>'+
'<p>The next trial will also show details about the errors (<i>show_detailed_errors: true</i>).</p>',
choices: ['Next']
};
// manually preload specific files
// if loading fails, then stop with a detailed error message
var preload_fail_example_2 = {
type: jsPsychPreload,
images: ['img/happy_face_2.jpg','img/happy_face_3.jpg','img/sad_face_1.jpg','img/sad_face_2.jpg','img/sad_face_3.jpg','img/sad_face_4.jpg',
'img/1.gif','img/2.gif','img/3.gif','img/4.gif','img/5.gif','img/6.gif','img/7.gif','img/8.gif','img/9.gif','img/10.gif','img/11.gif','img/12.gif',
'BAD_IMAGE_NAME'],
audio: ['BAD_AUDIO_NAME'],
video: ['video/sample_video.mp4', 'BAD_VIDEO_NAME'], // videos will not be preloaded if HTML file is running locally (i.e. safe mode)
message: '<p>Please wait...</p>',
show_detailed_errors: true,
max_load_time: null,
post_trial_gap: 1000,
on_error: function(file) {
console.log('Error: ',file);
},
on_success: function(file) {
console.log('Loaded: ',file);
}
};
var after_fail_example_2 = {
type: jsPsychHtmlButtonResponse,
stimulus: '<p>All files loaded successfully!</p>',
choices: ['End']
};
jsPsych.run([auto_preload_trial, start, image_trial, audio_trial,
start_fail_example_1, preload_fail_example_1, after_fail_example_1,
start_fail_example_2, preload_fail_example_2, after_fail_example_2]);
</script>
</html>