react-hook-accelerometer
v1.1.3
Published
React hook for Accelerometer Sensor access
Downloads
18
Readme
Installation
Using npm
:
npm install --save react-hook-accelerometer
Using yarn
:
yarn add react-hook-accelerometer
Usage
import React from "react";
import useAccelerometer from "react-hook-accelerometer";
const ComponentWithAccelerometer = () => {
const sensor = useAccelerometer();
return !sensor.error ? (
<ul>
<li>X: {sensor.x}</li>
<li>Y: {sensor.y}</li>
<li>Z: {sensor.z}</li>
</ul>
) : (
<p>No Accelerometer, sorry.</p>
);
};
Using SensorOptions
If you want to use this feature, simply provide useAccelerometer
with a SensorOptions
object:
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 for his original hook code for Geolocation.
License
LGPL-3.0