mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-11 16:18:11 +00:00
Create live demos for canvas-keyboard-response
This commit is contained in:
parent
bbbcd0ab3a
commit
c93e3f01e2
@ -0,0 +1,67 @@
|
|||||||
|
<!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-keyboard-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']
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function drawRect(c){
|
||||||
|
var ctx = c.getContext('2d');
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.rect(30, 30, 200, 50);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
var trial = {
|
||||||
|
type: 'canvas-keyboard-response',
|
||||||
|
canvas_size: [300, 300],
|
||||||
|
stimulus: drawRect,
|
||||||
|
choices: ['e','i'],
|
||||||
|
prompt: '<p>Is this a circle or a rectangle? Press "e" for circle and "i" for rectangle.</p>',
|
||||||
|
data: {shape: 'rectangle'}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var canvas_data_loop = {
|
||||||
|
timeline: [trial, 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>
|
@ -0,0 +1,67 @@
|
|||||||
|
<!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-keyboard-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']
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function drawCirc(c){
|
||||||
|
var ctx = c.getContext('2d');
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
var trial = {
|
||||||
|
type: 'canvas-keyboard-response',
|
||||||
|
canvas_size: [300, 300],
|
||||||
|
stimulus: drawCirc,
|
||||||
|
prompt: '<p>No key response is accepted</p><p>The stimulus disappears after 3 seconds.</p>',
|
||||||
|
choices: jsPsych.NO_KEYS,
|
||||||
|
trial_duration: 3000,
|
||||||
|
data: {shape: 'circle', radius: 50}
|
||||||
|
}
|
||||||
|
|
||||||
|
var canvas_data_loop = {
|
||||||
|
timeline: [trial, 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>
|
@ -29,40 +29,56 @@ Note: the canvas stimulus is *not* included in the trial data because it is a fu
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Draw rectangle and wait for response
|
???+ example "Draw rectangle and wait for response"
|
||||||
|
=== "Code"
|
||||||
|
```javascript
|
||||||
|
function drawRect(c){
|
||||||
|
var ctx = c.getContext('2d');
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.rect(30, 30, 200, 50);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
```javascript
|
var trial = {
|
||||||
function drawRect(c){
|
type: 'canvas-keyboard-response',
|
||||||
var ctx = c.getContext('2d');
|
canvas_size: [300, 300],
|
||||||
ctx.beginPath();
|
stimulus: drawRect,
|
||||||
ctx.rect(30, 30, 200, 50);
|
choices: ['e','i'],
|
||||||
ctx.stroke();
|
prompt: '<p>Is this a circle or a rectangle? Press "e" for circle and "i" for rectangle.</p>',
|
||||||
}
|
data: {shape: 'rectangle'}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
=== "Demo"
|
||||||
|
<div style="text-align:center;">
|
||||||
|
<iframe src="../plugins/demos/jspsych-canvas-keyboard-response-demo1.html" width="90%;" height="500px;" frameBorder="0"></iframe>
|
||||||
|
</div>
|
||||||
|
|
||||||
var trial = {
|
<a target="_blank" rel="noopener noreferrer" href="../plugins/demos/jspsych-canvas-keyboard-response-demo1.html">Open demo in new tab</a>
|
||||||
type: 'canvas-keyboard-response',
|
|
||||||
stimulus: drawRect,
|
???+ example "Draw circle, no response allowed"
|
||||||
choices: ['e','i'],
|
=== "Code"
|
||||||
prompt: '<p>Is this a circle or a rectangle? Press "e" for circle and "i" for rectangle.</p>',
|
```javascript
|
||||||
data: {shape: 'rectangle'}
|
function drawCirc(c){
|
||||||
}
|
var ctx = c.getContext('2d');
|
||||||
```
|
ctx.beginPath();
|
||||||
|
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
### Draw circle, no response allowed
|
var trial = {
|
||||||
|
type: 'canvas-keyboard-response',
|
||||||
|
canvas_size: [300, 300],
|
||||||
|
stimulus: drawCirc,
|
||||||
|
prompt: '<p>No key response is accepted</p><p>The stimulus disappears after 3 seconds.</p>',
|
||||||
|
choices: jsPsych.NO_KEYS,
|
||||||
|
trial_duration: 3000,
|
||||||
|
data: {shape: 'circle', radius: 50}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Demo"
|
||||||
|
<div style="text-align:center;">
|
||||||
|
<iframe src="../plugins/demos/jspsych-canvas-keyboard-response-demo2.html" width="90%;" height="500px;" frameBorder="0"></iframe>
|
||||||
|
</div>
|
||||||
|
|
||||||
```javascript
|
<a target="_blank" rel="noopener noreferrer" href="../plugins/demos/jspsych-canvas-keyboard-response-demo2.html">Open demo in new tab</a>
|
||||||
function drawCirc(c){
|
|
||||||
var ctx = c.getContext('2d');
|
|
||||||
ctx.beginPath();
|
|
||||||
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
|
|
||||||
ctx.stroke();
|
|
||||||
}
|
|
||||||
|
|
||||||
var trial = {
|
|
||||||
type: 'canvas-keyboard-response',
|
|
||||||
stimulus: drawCirc,
|
|
||||||
choices: jsPsych.NO_KEYS,
|
|
||||||
trial_duration: 1000,
|
|
||||||
data: {shape: 'circle', radius: 50}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
Loading…
Reference in New Issue
Block a user