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

88 lines
2.9 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: 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_three_data = all_trial_data.last(3).values(); // One block consists of three canvas-button-response trials
var trial_json = JSON.stringify(last_three_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']
};
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();
}
// To use the canvas stimulus function with timeline variables,
// the jsPsych.timelineVariable() function can be used inside your stimulus function.
// In addition, this code demonstrates how to check whether participants' answers were correct or not.
var circle_procedure = {
timeline: [{
type: jsPsychCanvasButtonResponse,
stimulus: function(c) {
filledCirc(c, jsPsych.timelineVariable('radius'), jsPsych.timelineVariable('color'));
},
canvas_size: [300, 300],
choices: ['Red', 'Green', 'Blue'],
prompt: '<p>What color is the circle?</p>',
data: {
radius: jsPsych.timelineVariable('radius'),
color: jsPsych.timelineVariable('color'),
correct_response: jsPsych.timelineVariable('correct_response')
},
on_finish: function(data){
data.correct = (data.response == data.correct_response);
}
}],
timeline_variables: [
{radius: 80, color: 'red', correct_response: 0},
{radius: 100, color: 'green', correct_response: 1},
{radius: 50, color: 'blue', correct_response: 2}
],
randomize_order: true
};
var canvas_data_loop = {
timeline: [circle_procedure, 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>