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

deltas

v0.1.1

Published

Library to configure and listen to device motion changes based on defined delta variations for each angle

Downloads

10

Readme

deltas Build Status

Library to configure and listen to device motion changes based on defined delta variations for each angle.

Why? Though you can listen to the device orientation event, you will receive long floating numbers for each angle that is affected when the user moves the device. Creating deltas listeners you can easily configure actions for specific variation points.

Use

Include the script inside the dist folder in your project:

<script src="dist/deltas.js"></script>

or install it running npm install --save deltas and import it:

import { Listener } from delta;

let listener = new Listener({
  delta: 15,
  action(delta, angle) {
    // do something...
  }
});

How it works

When setting a delta you're instructing the listener to fire an action each time an angle varies proportionally. This means that if you set a delta to 10, the action will be fired when the orientation angle is 10, 20, 30, etc.

Each action receives two arguments, the depth of the delta reached and the angle. If you set a delta to 10, the action will be called with 1 when the orientation angle is 10, with 2 when the orientation angle is 20, etc.

The second argument is the angle which can be alpha, beta or gamma. These three come from the spec fo the device orientation event.

alpha

The alpha swirls around the z axis. Imagine a phone is set on a flat surface, like a table, and you rotate without lifting it, that's the alpha angle and it goes from 0 to 360.

beta

The beta angle swirls around the x axis, which is the one that crosses the device from side to side and goes from -180 to 180, being zero when is to rest horizontally.

gamma

The gamma angle swirls around the y axis, which is the one that crosses the device from top to bottom and goes from -90 to 90, being zero when is to rest horizontally.

Orientation angles can be hard to figure, I recommend this MDN article about them: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Orientation_and_motion_data_explained

Set it up

With deltas listeners you can execute actions every time the angle varies an amount of degrees.

import { Listener } from delta;

let listener = new Listener({
  delta: 10,
  action(delta, angle) {
    // this will be called for each angle
  }
});

But you can also just listen to one specific angle.

let listener = new Listener({
  alpha: {
    delta: 10,
    action(delta, angle) {
      // this will be called only for alpha angle
    }
  }
});

You can pass a default delta and action, and add just a special action.

let listener = new Listener({
  delta: 10,
  action(delta, angle) {
    // this will get called for beta and gamma
  },
  alpha: {
    action(delta, angle) {
      // this will be called only for alpha angle
    }
  }
});

Or add just a special delta.

let listener = new Listener({
  delta: 10,
  action(delta, angle) {
    // this will get called for all angles
  },
  alpha: {
    delta: 25
  }
});

Or customize all!

let listener = new Listener({
  alpha: {
    delta: 10,
    action(delta, angle) {
      // this will be called only for alpha angle
    }
  },
  beta: {
    delta: 20,
    action(delta, angle) {
      // this will be called only for beta angle
    }
  },
  gamma: {
    delta: 30,
    action(delta, angle) {
      // this will be called only for gamma angle
    }
  }
});

Disclaimer

Deltas listeners were tested in latest Chrome mobile, latest Opera mini and Safari mobile 10 so further. The script is also written in ES2015 which works well on modern mobile browsers but would need transpiling for older versions.

If you create fun stuff with it, let me know!

MIT License