1.1.0 • Published 1 year ago

use-device-orientation-hooks v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

use-device-orientation-hooks

npm version build License

ko-fi

Overview

A React hook to obtain device orientation using DeviceOrientationEvent.

Notes

Installation

You can install this library using npm:

npm install use-device-orientation-hooks

Usage

import { useDeviceOrientation } from "use-device-orientation-hooks";

function App() {
  const [deviceOrientation, toggle] = useDeviceOrientation();

  return (
    <div>
      <p>Absolute: {deviceOrientation.absolute}</p>
      <p>Alpha: {deviceOrientation.alpha}</p>
      <p>Beta: {deviceOrientation.beta}</p>
      <p>Gamma: {deviceOrientation.gamma}</p>
      <p>Webkit Compass Heading: {deviceOrientation.webkitCompassHeading}</p>
      <p>Webkit Compass Accuracy: {deviceOrientation.webkitCompassAccuracy}</p>

      <button onClick={toggle}>Toggle Orientation Detection</button>
    </div>
  );
}

API

useDeviceOrientation()

A React hook that returns an array with two elements:

  1. deviceOrientation: An object that represents the current device orientation. It has the following properties:
PropertyTypeDescription
absoluteBooleanIndicates whether the device is providing absolute orientation data.
alphaNumberThe rotation of the device around the Z axis, representing a clockwise angle in degrees.
betaNumberThe rotation of the device around the X axis, representing a forward or backward motion of the device in degrees.
gammaNumberThe rotation of the device around the Y axis, representing a left or right motion of the device in degrees.
webkitCompassHeadingNumber | nullThe direction that the device is facing according to the compass, in degrees.
webkitCompassAccuracyNumber | nullThe accuracy of the compass heading in degrees.
  1. toggle(): A function that can be used to start or stop the device orientation detection.

Link

License

This project is licensed under the MIT License - see the LICENSE file for details.

1.1.0

1 year ago

1.0.0

1 year ago