npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webmo

v0.5.2

Published

A tiny library to capture device motion, rotation, and orientation events.

Downloads

10

Readme

WebMo

Bundle size npm version TypeScript License

A tiny library to capture device motion, rotation, and orientation events.

iOS 12.2+ Security

From iOS 12.2 onwards the Device Motion and Orientation APIs are disabled by default and can only be used by HTTPS domains. You need to enable access to the APIs in Settings > Safari > Privacy & Security.

This might change in a future release to instead use a popup similar to the Geolocation API permission access.

Usage

import * as webmo from 'webmo'

async function startCapture () {
  const hasMotionSupport = await webmo.motion.deviceHasMotionSupport()
  
  if (hasMotionSupport) {
    const mListener = new webmo.motion.MotionListener(data => {
      console.log('Received motion payload: ', data)
    })
  }
  
  const hasOrientationSupport = await webmo.orientation.deviceHasOrientationSupport()
  
  if (hasOrientationSupport) {
    const oListener = new webmo.orientation.OrientationListener(data => {
      console.log('Received orientation payload: ', data)
    })
  }
}

startCapture()

Behaviours

Behaviour like this can be made configurable through options in the future.

Alpha Orientation Value

This library normalises device alpha orientation, i.e the position the phone is in when the first orientation event is captured is used as alpha of 0. This is only necessary on Android devices, iOS devices default to 0 as the initial alpha heading.

Device Support Tests

The orientation.deviceHasOrientationSupport and motion.deviceHasMotionSupport functions are designed to detect a motion event. If none is detected then you might get a false negative, e.g user left their phone lying on a table so no motion change is detected. Make sure you account for this in your application, by telling the user to "hold the device in their hand" or similar.

Example

To try the example application do the following:

git clone https://github.com/evanshortiss/webmo.git
cd webmo
npm i
npm start

Now visit port 8080 using a device that supports motion and orientation, for example a modern iOS or Android phone, and you'll see live data from the sensors.

API

webmo.motion

All motion related functions fall under this namespace.

webmo.motion.mayHaveMotionSupport(): boolean

Returns true if DeviceMotionEvent is defined globally.

webmo.motion.deviceHasMotionSupport(timeout: number): Promise<boolean>

Tests for motion support by detecting motion events. Defaults to a 1000 millisecond timeout before resolving with false. Resolves with true if a motion event is detected before timeout is reached.

webmo.motion.MotionListener(callback, options)

A class that can be instantiated to listen for motion events. Accepts two arguments:

  • callback - Callback that receives sensor data
  • options - Object containing configuration

Valid options are:

  • threshold (Default: 0) - Threshold required to register motion. Can be used to filter out smaller motions.
  • rotationRateThreshold (Default: 0) - Threshold required to fire an event for rotation rate. Can be used to filter out smaller/sloewr rotations.
  • autoStart (Default: true) - If motion events should be listened for immediately.

Callback and data format:

const options = {
  threshold: 5
}
const listener = new webmo.MotionListener((data) => {
  // data is an object like so:
  // {
  //   acceleration: {
  //     x: number
  //     y: number
  //     z: number
  //   }
  //   rotationRate: {
  //     alpha: number
  //     beta: number
  //     gamma: number
  //   }
  //   timestamp: number
  // }
}, options)

webmo.motion.MotionListener.start()

Start listening for motion events. Only required if options.autoStart was set to false.

webmo.motion.MotionListener.stop()

Stop listening for motion events.

webmo.motion.MotionListener.setListener(callback: function|undefined)

Replace the listener function that was passed to the instance constructor.

webmo.motion.MotionListener.setThreshold(n: number)

Changes the threshold required for this instance to consider a motion or rotation event event worth emitting.

webmo.motion.MotionListener.isListening(): boolean

Returns true if the instance has bound event handlers for motion.

webmo.orientation

The orientation API is the same as webmo.motion, but returned data objects have the following structure:

const listener = new webmo.MotionListener((data) => {
  // data is an object like so:
  // {
  //   alpha: number
  //   beta: number
  //   gamma: number
  //   timestamp: number
  // }
})