jsPsych/docs/demos/jspsych-canvas-button-response-demo1.html

86 lines
2.7 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/gh/jspsych/jsPsych@6.3.1/jspsych.js"></script>
<script src="https://cdn.jsdelivr.net/gh/jspsych/jsPsych@6.3.1/plugins/jspsych-canvas-button-response.js"></script>
<script src="https://cdn.jsdelivr.net/gh/jspsych/jsPsych@6.3.1/plugins/jspsych-html-button-response.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jspsych/jsPsych@6.3.1/css/jspsych.css">
<style>
.jspsych-btn {margin-bottom: 10px;}
</style>
</head>
<body></body>
<script>
var jsPsych = initJsPsych();
var start = {
type: 'html-button-response',
stimulus: '',
choices: ['Run demo']
};
var show_data = {
type: 'html-button-response',
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: 'canvas-button-response',
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: 'canvas-button-response',
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>