Skip to content

Commit

Permalink
Merge pull request #110 from yeatmanlab/enh-add-button-to-swipe
Browse files Browse the repository at this point in the history
Enh add button to swipe
  • Loading branch information
jodeleeuw authored Jun 6, 2024
2 parents a19245d + f692f01 commit 1febc65
Show file tree
Hide file tree
Showing 8 changed files with 1,925 additions and 1,455 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-squids-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych-contrib/plugin-html-swipe-response": minor
---

Added button response modality.
3,121 changes: 1,683 additions & 1,438 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/plugin-html-swipe-response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Overview

This plugin displays HTML content and records responses generated by both swipe gestures and keyboard response. This is ideal for two-alternative forced choice assessments that will be administered on both mobile and desktop platforms. The stimulus can be displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically if the subject has failed to respond within a fixed length of time.
This plugin displays HTML content and records responses generated by swipe gestures, keyboard, and button responses. This is ideal for two-alternative forced choice assessments that will be administered on both mobile and desktop platforms. The stimulus can be displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically if the subject has failed to respond within a fixed length of time.

## Loading

Expand All @@ -20,7 +20,7 @@ See [documentation](docs/jspsych-html-swipe-response.md)

## Author / Citation

Created by [Adam Richie-Halford](https://github.com/richford)
[Adam Richie-Halford](https://github.com/richford), [Kruttika Bhat](https://github.com/KruttikaBhat)

Citation:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# jspsych-html-swipe-response plugin

This plugin displays HTML content and records responses generated by both swipe gestures and keyboard response. This plugin will be useful for two-alternative forced choice (2AFC) assessments that will be administered on both desktop and mobile devices. The stimulus can be animated to move off-screen before the trial ends. The stimulus can be displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically if the subject has failed to respond within a fixed length of time.
This plugin displays HTML content and records responses generated by swipe gestures, keyboard, and button responses. This plugin will be useful for two-alternative forced choice (2AFC) assessments that will be administered on both desktop and mobile devices. The stimulus can be animated to move off-screen before the trial ends. The stimulus can be displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically if the subject has failed to respond within a fixed length of time.

## Parameters

Expand All @@ -9,10 +9,14 @@ In addition to the [parameters available in all plugins](https://www.jspsych.org
| Parameter | Type | Default Value | Description |
| ----------|------|---------------|------------ |
| stimulus | HTML string | `undefined` | The HTML content to be displayed. |
| keyboard_choices | array of strings | `"NO_KEYS"` | This array contains the key(s) that the subject is allowed to press in order to respond to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/) for more examples. Any key presses that are not listed in the array will be ignored. Specifying `"ALL_KEYS"` means that all keys will be accepted as valid responses. The default value of `"NO_KEYS"` will mean that no responses are allowed. | |
| button_choices | array of strings | [] | Labels for the buttons. Each different string in the array will generate a different button. |
| keyboard_choices | array of strings | `"NO_KEYS"` | This array contains the key(s) that the subject is allowed to press in order to respond to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/) for more examples. Any key presses that are not listed in the array will be ignored. Specifying `"ALL_KEYS"` means that all keys will be accepted as valid responses. The default value of `"NO_KEYS"` will mean that no responses are allowed. |
| button_html | HTML string | `'<button class="jspsych-btn">%choice%</button>'` | A template of HTML for generating the button elements. You can override this to create customized buttons of various kinds. The string `%choice%` will be changed to the corresponding element of the `choices` array. You may also specify an array of strings, if you need different HTML to render for each button. If you do specify an array, the `choices` array and this array must have the same length. The HTML from position 0 in the `button_html` array will be used to create the button for element 0 in the `choices` array, and so on. |
| prompt | string | null | This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can be used to provide a reminder about the action the subject is supposed to take (e.g., which key to press). |
| stimulus_duration | numeric | null | How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will remain visible until the trial ends. |
| trial_duration | numeric | null | How long to wait for the subject to make a response before ending the trial in milliseconds. If the subject fails to make a response before this timer is reached, the subject's response will be recorded as null for the trial and the trial will end. If the value of this parameter is null, the trial will wait for a response indefinitely. |
| margin_vertical | string | '0px' | Vertical margin of the button(s). |
| margin_horizontal | string | '8px' | Horizontal margin of the button(s). |
| response_ends_trial | boolean | true | If true, then the trial will end whenever the subject makes a response (assuming they make their response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for `trial_duration` is reached. You can set this parameter to `false` to force the subject to view a stimulus for a fixed amount of time, even if they respond before the time is complete. |
| swipe_threshold | numeric | 20 | How far away from the center should the subject have to swipe for a left/right response to be recorded. If the subject releases their finger before this threshold, the stimulus will return to the neutral center position. If the subject exceeds this threshold, the swipe response will be recorded. |
| swipe_offscreen_coordinate | numeric | 1000 | The offscreen coordinate for the swipe animation. |
Expand All @@ -26,8 +30,9 @@ In addition to the [default data collected by all plugins](../overview/plugins.m
| Name | Type | Value |
| --------- | ------- | ---------------------------------------- |
| keyboard_response | string | Indicates which key the subject pressed. If the subject responded using button clicks, then this field will be `null`. |
| button_response | numeric | Indicates which button the subject pressed. The first button in the `choices` array is 0, the second is 1, and so on. If the subject responded using the keyboard, then this field will be `null`. |
| swipe_response | string | Indicates which direction the subject swiped. This will be either `"left"` or `"right"`. If the subject responded using the keyboard, then this field will be `null`. |
| response_source | string | Indicates the source of the response. This will either be `"swipe"` or `"keyboard"`. |
| response_source | string | Indicates the source of the response. This will either be `"swipe"`, `"keyboard"`, or `"button"`. |
| rt | numeric | The response time in milliseconds for the subject to make a response. The time is measured from when the stimulus first appears on the screen until the subject's response. |
| stimulus | string | The HTML content that was displayed on the screen. |

Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-html-swipe-response/examples/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
stimulus: `
<h3>Directions</h3>
<p>
Swipe right or press the right arrow key if the displayed image is a hot dog.
Swipe left or press the left arrow key otherwise.
Swipe right, press the Yes button, or press the right arrow key if the displayed image is a hot dog.
Swipe left, press the No button, or press the left arrow key otherwise.
</p>
<p>Press any key to continue</p>
`
Expand All @@ -66,9 +66,10 @@ <h3>Directions</h3>
stimulus: jsPsych.timelineVariable('target'),
prompt: `
<p>Is this a hot dog?</p>
<p>If yes, swipe right or press the right arrow key.</p>
<p>If no, swipe left or press the left arrow key.</p>
<p>If yes, swipe right, press the right arrow key, or click the Yes button.</p>
<p>If no, swipe left, press the left arrow key, or click the No button.</p>
`,
button_choices: ["No", "Yes"],
},
],
timeline_variables: allTargets,
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-html-swipe-response/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"jspsych": "^7.0.0"
},
"dependencies": {
"canvas": "^2.11.2",
"interactjs": "^1.10.17"
}
}
104 changes: 101 additions & 3 deletions packages/plugin-html-swipe-response/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,65 @@ describe("plugin-html-swipe-response", () => {
await expectFinished();
});

test("prompt should append html below stimulus", async () => {
test("display button labels", async () => {
const { getHTML } = await startTimeline([
{
type: htmlSwipeResponse,
stimulus: "this is html",
button_choices: ["button-choice1", "button-choice2"],
},
]);

expect(getHTML()).toContain('<button class="jspsych-btn">button-choice1</button>');
expect(getHTML()).toContain('<button class="jspsych-btn">button-choice2</button>');
});

test("display button html", async () => {
const { getHTML } = await startTimeline([
{
type: htmlSwipeResponse,
stimulus: "this is html",
button_choices: ["buttonChoice"],
button_html: '<button class="jspsych-custom-button">%choice%</button>',
},
]);

expect(getHTML()).toContain('<button class="jspsych-custom-button">buttonChoice</button>');
});

test("display should clear after button click", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlSwipeResponse,
stimulus: "this is html",
button_choices: ["button-choice"],
swipe_animation_duration: 0,
},
]);

expect(getHTML()).toContain(
'<div id="jspsych-html-swipe-response-stimulus">this is html</div>'
);

clickTarget(document.querySelector("#jspsych-html-swipe-response-button-0"));

await expectFinished();
});

test("prompt should append html below button", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlSwipeResponse,
stimulus: "this is html",
button_choices: ["button-choice-0", "button-choice-1"],
keyboard_choices: ["f", "j"],
prompt: '<div id="foo">this is a prompt</div>',
swipe_animation_duration: 0,
},
]);

expect(getHTML()).toContain(
'<div id="jspsych-html-swipe-response-stimulus">this is html</div><div id="foo">this is a prompt</div>'
'<button class="jspsych-btn">button-choice-1</button></div></div><div id="foo">this is a prompt</div>'
);

pressKey("f");
Expand Down Expand Up @@ -95,7 +141,9 @@ describe("plugin-html-swipe-response", () => {
},
]);

expect(getHTML()).toBe('<div id="jspsych-html-swipe-response-stimulus">this is html</div>');
expect(getHTML()).toBe(
'<div id="jspsych-html-swipe-response-stimulus">this is html</div><div id="jspsych-html-swipe-response-btngroup"></div>'
);
jest.advanceTimersByTime(500);
await expectFinished();
});
Expand All @@ -119,6 +167,26 @@ describe("plugin-html-swipe-response", () => {
await expectFinished();
});

test("should end trial when button is clicked", async () => {
const { getHTML, expectFinished } = await startTimeline([
{
type: htmlSwipeResponse,
stimulus: "this is html",
button_choices: ["button-choice-0", "button-choice-1"],
keyboard_choices: ["f", "j"],
swipe_animation_duration: 0,
response_ends_trial: true,
},
]);

expect(getHTML()).toContain(
'<div id="jspsych-html-swipe-response-stimulus">this is html</div>'
);

clickTarget(document.querySelector("#jspsych-html-swipe-response-button-0"));
await expectFinished();
});

test("class should say responded when key is pressed", async () => {
const { getHTML, expectRunning } = await startTimeline([
{
Expand All @@ -143,6 +211,26 @@ describe("plugin-html-swipe-response", () => {
await expectRunning();
});

test("class should have responded when button is clicked", async () => {
const { getHTML } = await startTimeline([
{
type: htmlSwipeResponse,
stimulus: "this is html",
button_choices: ["button-choice-0", "button-choice-1"],
response_ends_trial: false,
},
]);

expect(getHTML()).toContain(
'<div id="jspsych-html-swipe-response-stimulus">this is html</div>'
);

clickTarget(document.querySelector("#jspsych-html-swipe-response-button-0"));
expect(document.querySelector("#jspsych-html-swipe-response-stimulus").className).toBe(
" responded"
);
});

test("should wait for swipe animation if requested", async () => {
const { expectFinished, expectRunning } = await startTimeline([
{
Expand Down Expand Up @@ -170,18 +258,23 @@ describe("html-swipe-response simulation", () => {
type: htmlSwipeResponse,
stimulus: "foo",
swipe_animation_duration: 0,
button_choices: ["a", "b"],
},
];
const { expectFinished, getData } = await simulateTimeline(timeline);
await expectFinished();
const swipeResponse = getData().values()[0].swipe_response;
const buttonResponse = getData().values()[0].button_response;
const keyboardResponse = getData().values()[0].keyboard_response;
const responseSource = getData().values()[0].response_source;
expect(getData().values()[0].rt).toBeGreaterThan(0);
if (responseSource == "keyboard") {
expect(typeof keyboardResponse).toBe("string");
} else if (responseSource == "swipe") {
expect(typeof swipeResponse).toBe("string");
} else if (responseSource == "button") {
expect(buttonResponse).toBeGreaterThanOrEqual(0);
expect(buttonResponse).toBeLessThanOrEqual(1);
}
});

Expand All @@ -191,6 +284,7 @@ describe("html-swipe-response simulation", () => {
type: htmlSwipeResponse,
stimulus: "foo",
swipe_animation_duration: 0,
button_choices: ["a", "b"],
},
];

Expand All @@ -207,13 +301,17 @@ describe("html-swipe-response simulation", () => {
await expectFinished();

const swipeResponse = getData().values()[0].swipe_response;
const buttonResponse = getData().values()[0].button_response;
const keyboardResponse = getData().values()[0].keyboard_response;
const responseSource = getData().values()[0].response_source;
expect(getData().values()[0].rt).toBeGreaterThan(0);
if (responseSource == "keyboard") {
expect(typeof keyboardResponse).toBe("string");
} else if (responseSource == "swipe") {
expect(typeof swipeResponse).toBe("string");
} else if (responseSource == "button") {
expect(buttonResponse).toBeGreaterThanOrEqual(0);
expect(buttonResponse).toBeLessThanOrEqual(1);
}
});
});
Loading

0 comments on commit 1febc65

Please sign in to comment.