mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-10 19:20:55 +00:00
86 lines
2.6 KiB
HTML
86 lines
2.6 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="https://unpkg.com/jspsych@7.0.0"></script>
|
|
<script src="https://unpkg.com/@jspsych/plugin-canvas-button-response@1.0.0"></script>
|
|
<script src="https://unpkg.com/@jspsych/plugin-html-button-response@1.0.0"></script>
|
|
<link rel="stylesheet" href="https://unpkg.com/jspsych@7.0.0/css/jspsych.css">
|
|
<style>
|
|
.jspsych-btn {margin-bottom: 10px;}
|
|
</style>
|
|
</head>
|
|
<body></body>
|
|
<script>
|
|
|
|
var jsPsych = initJsPsych();
|
|
|
|
var start = {
|
|
type: jsPsychHtmlButtonResponse,
|
|
stimulus: '',
|
|
choices: ['Run demo']
|
|
};
|
|
|
|
var show_data = {
|
|
type: jsPsychHtmlButtonResponse,
|
|
stimulus: function() {
|
|
var all_trial_data = jsPsych.data.get().filter({trial_type: 'canvas-button-response'});
|
|
var last_two_data = all_trial_data.last(2).values(); // One block consists of two canvas-button-response trials
|
|
var trial_json = JSON.stringify(last_two_data, null, 2);
|
|
return `<p style="margin-bottom:0px;"><strong>Trial data:</strong></p>
|
|
<pre style="margin-top:0px;text-align:left;">${trial_json}</pre>`;
|
|
},
|
|
choices: ['Repeat demo']
|
|
};
|
|
|
|
|
|
// stimulus function that takes the canvas and additional parameters (radius, color)
|
|
// this can be called inside of an anonymous stimulus function, which takes the canvas (c) as its only argument
|
|
function filledCirc(canvas, radius, color) {
|
|
var ctx = canvas.getContext("2d");
|
|
ctx.beginPath();
|
|
ctx.arc(150, 150, radius, 0, 2 * Math.PI);
|
|
ctx.fillStyle = color;
|
|
ctx.fill();
|
|
}
|
|
|
|
var circle_1 = {
|
|
type: jsPsychCanvasButtonResponse,
|
|
stimulus: function(c) {
|
|
filledCirc(c, 100, 'blue');
|
|
},
|
|
canvas_size: [300, 300],
|
|
choices: ['Red', 'Green', 'Blue'],
|
|
prompt: '<p>What color is the circle?</p>',
|
|
data: {color: 'blue', radius: 100}
|
|
};
|
|
|
|
var circle_2 = {
|
|
type: jsPsychCanvasButtonResponse,
|
|
stimulus: function(c) {
|
|
filledCirc(c, 150, 'green');
|
|
},
|
|
canvas_size: [300, 300],
|
|
choices: ['Larger', 'Smaller'],
|
|
stimulus_duration: 1000,
|
|
prompt: '<p>Is this circle larger or smaller than the last one?</p>'+
|
|
'<p>Stimulus will be hidden after 1 second.</p>',
|
|
data: {color: 'green', radius: 150}
|
|
};
|
|
|
|
var canvas_data_loop = {
|
|
timeline: [circle_1, circle_2, show_data],
|
|
loop_function: function() {
|
|
return true;
|
|
}
|
|
};
|
|
|
|
if (typeof jsPsych !== "undefined") {
|
|
jsPsych.run([start, canvas_data_loop]);
|
|
} else {
|
|
document.body.innerHTML = '<div style="text-align:center; margin-top:50%; transform:translate(0,-50%);">You must be online to view the plugin demo.</div>';
|
|
}
|
|
|
|
</script>
|
|
|
|
</html>
|