Skip to content

Commit

Permalink
Add html-keyboard-response-raf plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jodeleeuw committed Jan 12, 2024
1 parent 24130f2 commit 46cf7d4
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-trains-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych-contrib/plugin-html-keyboard-response-raf": major
---

Added html-keyboard-response-raf, a drop in replacement for the html-keyboard-response plugin but using requestAnimationFrame for timing.
35 changes: 35 additions & 0 deletions packages/plugin-html-keyboard-response-raf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# html-keyboard-response-raf

## Overview

This plugin implements the same functionality as the html-keyboard-response plugin, but uses requestAnimationFrame internally for timing

## Loading

### In browser

```js
<script src="https://unpkg.com/@jspsych-contrib/[email protected]">
```

### Via NPM

```
npm install @jspsych-contrib/plugin-html-keyboard-response-raf
```

```js
import jsPsychHtmlKeyboardResponseRaf from '@jspsych-contrib/plugin-html-keyboard-response-raf';
```

## Compatibility

jsPsych 7.0.0

## Documentation

See [documentation](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-html-keyboard-response-raf/docs/html-keyboard-response-raf.md)

## Author / Citation

Josh de Leeuw
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# html-keyboard-response-raf

This plugin implements the same functionality as the html-keyboard-response plugin, but uses requestAnimationFrame internally for timing

## Parameters

In addition to the [parameters available in all plugins](https://jspsych.org/latest/overview/plugins.md#parameters-available-in-all-plugins), this plugin accepts the following parameters. Parameters with a default value of undefined must be specified. Other parameters can be left unspecified if the default value is acceptable.

| Parameter | Type | Default Value | Description |
| ------------------- | ---------------- | ------------------ | ---------------------------------------- |
| stimulus | HTML string | *undefined* | The string to be displayed. |
| choices | array of strings | `"ALL_KEYS"` | This array contains the key(s) that the participant 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. The default value of `"ALL_KEYS"` means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean that no responses are allowed. |
| 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 participant 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 participant to make a response before ending the trial in milliseconds. If the participant fails to make a response before this timer is reached, the participant's response will be recorded as null for the trial and the trial will end. If the value of this parameter is null, then the trial will wait for a response indefinitely. |
| response_ends_trial | boolean | true | If true, then the trial will end whenever the participant 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 participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete. |
## Data Generated

In addition to the [default data collected by all plugins](https://jspsych.org/latest/overview/plugins.md#data-collected-by-all-plugins), this plugin collects the following data for each trial.

| Name | Type | Value |
| --------- | ------- | ---------------------------------------- |
| response | string | Indicates which key the participant pressed. |
| rt | numeric | The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. |
| stimulus | string | The HTML content that was displayed on the screen. |

## Install

Using the CDN-hosted JavaScript file:

```js
<script src="https://unpkg.com/@jspsych-contrib/plugin-html-keyboard-response-raf"></script>
```

Using the JavaScript file downloaded from a GitHub release dist archive:

```js
<script src="jspsych/plugin-html-keyboard-response-raf.js"></script>
```

Using NPM:

```
npm install @jspsych-contrib/plugin-html-keyboard-response-raf
```

```js
import HtmlKeyboardResponseRaf from '@jspsych-contrib/plugin-html-keyboard-response-raf';
```

## Examples

### Flicker a one-frame stimulus

```javascript
const trial = {
type: jsPsychHtmlKeyboardResponseRaf,
stimulus: 'Hello world!',
stimulus_duration: 16.6,
trial_duration: 50,
choices: ['a', 'b', 'c'],
}

const loop = {
timeline: [trial],
loop_function: () => true,
}
```
27 changes: 27 additions & 0 deletions packages/plugin-html-keyboard-response-raf/examples/example1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/jspsych"></script>
<script src="../dist/index.browser.js"></script>
<link href="https://unpkg.com/jspsych/css/jspsych.css" rel="stylesheet" type="text/css">
</head>
<body></body>
<script>
const jsPsych = initJsPsych();

const trial = {
type: jsPsychHtmlKeyboardResponseRaf,
stimulus: 'Hello world!',
stimulus_duration: 16.6,
trial_duration: 50,
choices: ['a', 'b', 'c'],
}

const loop = {
timeline: [trial],
loop_function: () => true,
}

jsPsych.run([loop]);
</script>
</html>
1 change: 1 addition & 0 deletions packages/plugin-html-keyboard-response-raf/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@jspsych/config/jest").makePackageConfig(__dirname);
44 changes: 44 additions & 0 deletions packages/plugin-html-keyboard-response-raf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@jspsych-contrib/plugin-html-keyboard-response-raf",
"version": "0.0.1",
"description": "This plugin uses the same functionality as the html-keyboard-response plugin, but uses requestAnimationFrame internally for timing",
"type": "module",
"main": "dist/index.cjs",
"exports": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"typings": "dist/index.d.ts",
"unpkg": "dist/index.browser.min.js",
"files": [
"src",
"dist"
],
"source": "src/index.ts",
"scripts": {
"test": "jest",
"test:watch": "npm test -- --watch",
"tsc": "tsc",
"build": "rollup --config",
"build:watch": "npm run build -- --watch"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jspsych/jspsych-contrib.git",
"directory": "packages/plugin-html-keyboard-response-raf"
},
"author": "Josh de Leeuw",
"license": "MIT",
"bugs": {
"url": "https://github.com/jspsych/jspsych-contrib/issues"
},
"homepage": "https://github.com/jspsych/jspsych-contrib/tree/main/packages/plugin-html-keyboard-response-raf",
"peerDependencies": {
"jspsych": ">=7.0.0"
},
"devDependencies": {
"@jspsych/config": "^2.0.0",
"@jspsych/test-utils": "^1.0.0",
"jspsych": "^7.0.0"
}
}
3 changes: 3 additions & 0 deletions packages/plugin-html-keyboard-response-raf/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { makeRollupConfig } from "@jspsych/config/rollup";

export default makeRollupConfig("jsPsychHtmlKeyboardResponseRaf");
19 changes: 19 additions & 0 deletions packages/plugin-html-keyboard-response-raf/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { startTimeline } from "@jspsych/test-utils";

import jsPsychHtmlKeyboardResponseRaf from ".";

jest.useFakeTimers();

describe("my plugin", () => {
it("should load", async () => {
const { expectFinished, getHTML, getData, displayElement, jsPsych } = await startTimeline([
{
type: jsPsychHtmlKeyboardResponseRaf,
parameter_name: 1,
parameter_name2: "img.png",
},
]);

await expectFinished();
});
});
Loading

0 comments on commit 46cf7d4

Please sign in to comment.