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 🙏

© 2026 – Pkg Stats / Ryan Hefner

device-heading

v0.1.0

Published

TypeScript library to get and track device heading

Readme

device-heading

NPM Version Built with Bun CI License

TypeScript library to get and track device heading.

[!NOTE] Requires HTTPS or localhost to access motion sensors.

On iOS Safari, requestIOSPermission() must be called inside a user gesture handler (e.g., a button click).

Heading accuracy depends on the device's magnetometer and surrounding environment.

✨ Features

  • ✅ Simple API to get heading once or watch continuously
  • 📱 Works with iOS and Android (with built-in permission helper for iOS)
  • ⚡ Zero dependencies

Demo

🔗 Live Demo: https://ryohidaka.github.io/device-heading/

[!TIP] Access from a smartphone to see real-time compass heading updates as you move your device.

📦 Installation

npm i device-heading

🚀 Usage

Initialize

import { DeviceHeading } from "device-heading";

const compass = new DeviceHeading();

if (compass.isSupported()) {
	console.log("DeviceOrientationEvent API is supported on this device.");
} else {
	console.error("DeviceOrientationEvent API is not supported on this device.");
}

Get a single heading value

import { DeviceHeading } from "device-heading";

const compass = new DeviceHeading();

const heading = await compass.once();
console.log("Current heading:", heading);

Round heading values

import { DeviceHeading } from "device-heading";

// Round to 1 decimal place (e.g., 180.5)
const compass = new DeviceHeading({ precision: 1 });

const heading = await compass.once();
console.log("Current heading::", heading);

Observe heading changes in real time

import { DeviceHeading } from "device-heading";

const compass = new DeviceHeading();

compass.start((heading) => {
	console.log("Heading:", heading);
});

// Stop watching when done
setTimeout(() => {
	compass.stop();
	console.log("Stopped observing.");
}, 5000);

Check iOS permission

import { DeviceHeading } from "device-heading";

const compass = new DeviceHeading();

if (!(await compass.hasIOSPermission())) {
	// show permission request UI
}

Request iOS permission

On iOS Safari, the user must explicitly grant permission to access motion data. Call this inside a user gesture handler (e.g., a button click).

import { DeviceHeading } from "device-heading";

const compass = new DeviceHeading();

button.addEventListener("click", async () => {
	const granted = await compass.requestIOSPermission();

	if (granted) {
		console.log("Permission granted");
	} else {
		console.log("Permission denied");
	}
});

🧠 API Reference

new DeviceHeading(options?)

Creates a new compass instance.

| Option | Type | Default | Description | | ----------- | -------- | ----------- | ------------------------------------------------------------------------------ | | precision | number | undefined | Decimal places to round heading values to. If omitted, no rounding is applied. |

isSupported(): boolean

Returns true if the current environment supports DeviceOrientationEvent.

once(): Promise<number>

Returns the current heading once (in degrees, 0–360°).

start(callback: (heading: number) => void): void

Starts watching for heading updates. Calls callback with the latest heading on each change.

stop(): void

Stops watching for heading updates.

hasIOSPermission(): Promise<boolean>

Returns true if the DeviceOrientationEvent permission is granted on iOS 13+. On non-iOS devices, always returns true.

requestIOSPermission(): Promise<boolean>

Requests motion permission on iOS 13+. Must be called inside a user gesture handler (e.g., a button click). On non-iOS devices, returns true immediately.

Contributing

Please see CONTRIBUTING.md for contribution guidelines.

License

MIT