Merge pull request #1937 from kurokida/docs-demos

Add a live demo for the canvas-button-response plugin docs page
This commit is contained in:
Becky Gilbert 2021-07-02 11:56:34 -07:00 committed by GitHub
commit bbbcd0ab3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 368 additions and 30 deletions

View File

@ -0,0 +1,85 @@
<!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 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.init({
timeline: [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>

View File

@ -0,0 +1,76 @@
<!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 start = {
type: 'html-button-response',
stimulus: '',
choices: ['Run demo']
};
var show_data = {
type: 'html-button-response',
stimulus: function() {
var trial_data = jsPsych.data.getLastTrialData().values();
var trial_json = JSON.stringify(trial_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']
};
// write the canvas stimulus drawing function without using a named function
// the anonymous function must take the canvas as an argument
var lines = {
type: 'canvas-button-response',
stimulus: function(c) {
var ctx = c.getContext("2d");
// first line
ctx.beginPath();
ctx.moveTo(200, 10);
ctx.lineTo(200, 250);
ctx.lineWidth = 10;
ctx.strokeStyle = 'MediumBlue';
ctx.stroke();
// second line
ctx.beginPath();
ctx.moveTo(20, 100);
ctx.lineTo(100, 250);
ctx.lineWidth = 10;
ctx.strokeStyle = 'MediumPurple';
ctx.stroke();
},
canvas_size: [300, 300],
choices: ['Blue line', 'Purple line'],
prompt: '<p>Which line is longer?</p>',
data: {line1_color: 'blue', line1_length: 290, line2_color: "purple", line2_length: 170}
};
var canvas_data_loop = {
timeline: [lines, show_data],
loop_function: function() {
return true;
}
};
if (typeof jsPsych !== "undefined") {
jsPsych.init({
timeline: [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>

View File

@ -0,0 +1,86 @@
<!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 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_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: 'canvas-button-response',
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 = jsPsych.pluginAPI.compareKeys(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.init({
timeline: [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>

View File

@ -43,8 +43,8 @@ response | array | An array, where each element is an object representing a resp
=== "Demo"
<div style="text-align:center;">
<iframe src="../demos/jspsych-animation-demo.html" width="90%;" height="500px;" frameBorder="0"></iframe>
<iframe src="../plugins/demos/jspsych-animation-demo.html" width="90%;" height="500px;" frameBorder="0"></iframe>
</div>
<a target="_blank" rel="noopener noreferrer" href="../demos/jspsych-animation-demo.html">Open demo in new tab</a>
<a target="_blank" rel="noopener noreferrer" href="../plugins/demos/jspsych-animation-demo.html">Open demo in new tab</a>

View File

@ -32,35 +32,126 @@ Note: the canvas stimulus is *not* included in the trial data because it is a fu
## Examples
### Drawing circles based on parameters
???+ example "Drawing circles based on parameters"
=== "Code"
```javascript
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();
}
```javascript
function filledCirc(canvas, radius, color){
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(250, 250, 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_1 = {
type: 'canvas-button-response',
stimulus: function (c) {
filledCirc(c, 100, 'blue');
},
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 circle_2 = {
type: 'canvas-button-response',
stimulus: function (c) {
filledCirc(c, 150, 'green');
},
choices: ['Larger', 'Smaller'],
prompt: '<p>Is this circle larger or smaller than the last one?</p>',
data: {color: 'green', radius: 150}
};
=== "Demo"
<div style="text-align:center;">
<iframe src="../plugins/demos/jspsych-canvas-button-response-demo1.html" width="90%;" height="500px;" frameBorder="0"></iframe>
</div>
```
<a target="_blank" rel="noopener noreferrer" href="../plugins/demos/jspsych-canvas-button-response-demo1.html">Open demo in new tab</a>
???+ example "Using an anonymous stimulus function"
=== "Code"
```javascript
var lines = {
type: 'canvas-button-response',
stimulus: function(c) {
var ctx = c.getContext("2d");
// first line
ctx.beginPath();
ctx.moveTo(200, 10);
ctx.lineTo(200, 250);
ctx.lineWidth = 10;
ctx.strokeStyle = 'MediumBlue';
ctx.stroke();
// second line
ctx.beginPath();
ctx.moveTo(20, 100);
ctx.lineTo(100, 250);
ctx.lineWidth = 10;
ctx.strokeStyle = 'MediumPurple';
ctx.stroke();
},
canvas_size: [300, 300],
choices: ['Blue line', 'Purple line'],
prompt: '<p>Which line is longer?</p>',
data: {line1_color: 'blue', line1_length: 290, line2_color: "purple", line2_length: 170}
};
```
=== "Demo"
<div style="text-align:center;">
<iframe src="../plugins/demos/jspsych-canvas-button-response-demo2.html" width="90%;" height="500px;" frameBorder="0"></iframe>
</div>
<a target="_blank" rel="noopener noreferrer" href="../plugins/demos/jspsych-canvas-button-response-demo2.html">Open demo in new tab</a>
???+ example "Using the canvas stimulus function with timeline variables, and recording the correctness of responses"
=== "Code"
```javascript
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_procedure = {
timeline: [{
type: 'canvas-button-response',
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 = jsPsych.pluginAPI.compareKeys(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
};
```
=== "Demo"
<div style="text-align:center;">
<iframe src="../plugins/demos/jspsych-canvas-button-response-demo3.html" width="90%;" height="500px;" frameBorder="0"></iframe>
</div>
<a target="_blank" rel="noopener noreferrer" href="../plugins/demos/jspsych-canvas-button-response-demo3.html">Open demo in new tab</a>