An open API service indexing awesome lists of open source software.

https://github.com/masaok/react-hook-accelerometer

React Hook into Accelerometer Sensor (includes the acceleration of gravity)
https://github.com/masaok/react-hook-accelerometer

accelerometer capstone hook react

Last synced: 2 months ago
JSON representation

React Hook into Accelerometer Sensor (includes the acceleration of gravity)

Awesome Lists containing this project

README

          

The Accelerometer sensor includes the force of gravity, unlike the Linear Acceleration sensor.

## Installation

Using `npm`:

```sh
npm install --save react-hook-accelerometer
```

Using `yarn`:

```sh
yarn add react-hook-accelerometer
```

## Usage

```jsx
import React from "react";
import useAccelerometer from "react-hook-accelerometer";

const ComponentWithAccelerometer = () => {
const sensor = useAccelerometer();

return !sensor.error ? (


  • X: {sensor.x}

  • Y: {sensor.y}

  • Z: {sensor.z}


) : (

No Accelerometer, sorry.


);
};
```

### Using `SensorOptions`

If you want to use this feature, simply provide `useAccelerometer` with a `SensorOptions` object:

```jsx
const sensor = useAccelerometer({
frequency: 60, // cycles per second
});
```

## Notes

Access to data from the Accelerometer API needs user permission.

## Caveats

Accelerometer API is available only in secure contexts (only using HTTPS).

## Credits

Credit to [Bence A. Tóth](https://github.com/bence-toth) for his original hook code for [Geolocation](https://www.npmjs.com/package/react-hook-geolocation).

## License

LGPL-3.0