Socket
Book a DemoInstallSign in
Socket

@ircam/devicemotion

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ircam/devicemotion

Low level library dedicated at normalizing devicemotion across browsers

latest
npmnpm
Version
1.3.1
Version published
Maintainers
3
Created
Source

@ircam/devicemotion

Minimal wrapper around the devicemotion API (cf. https://www.w3.org/TR/orientation-event/#devicemotion to normalize API, axis and units accross browsers.

Warning: Be aware that most, if not all, recent browsers will require your application to run through a https connection to grant access to motion sensors.

Install

npm install @ircam/devicemotion --save

Example

import devicemotion from '@ircam/devicemotion';

const permission = await devicemotion.requestPermission();

if (permission === 'granted') {
  devicemotion.addEventListener(e => console.log(e));
}

API

async devicemotion.requestPermission() -> Promise('granted' | 'refused')

Request permission to access motion sensors, this method should be called on the startup of your application, most browsers will prompt your users so that they authorize the sensors to be accessed by the application

devicemotion.addEventListener(callback: Function) -> void

The callback will be called with the data formatted as specificied in https://www.w3.org/TR/orientation-event/#devicemotion

devicemotion.addEventListener(e => {
  console.log(e);

  /*
  e = { 
    interval // ms
    accelerationIncludingGravity = { x, y, z } // m/s2
    rotationRate = { alpha, beta, gamma } // deg/s
  }
  */
});

Note: The event object is internally reused by the library, therefore it's the responsibility of the application to copy the data if needed.

devicemotion.removeEventListener(callback: Function) -> void

License

BSD-3-Clause

FAQs

Package last updated on 29 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts