A minimal javascript library for accessing the compass direction of a user relative to North.
<script src="https://unpkg.com/@joeyklee/compass.js@latest/dist/compass.js"></script>
Include the reference to the script:
<script src="https://unpkg.com/@joeyklee/compass.js@latest/dist/compass.js"></script>
Using Async/Await
window.addEventListener("DOMContentLoaded", async() => {
const compass = new Compass();
await compass.init();
const bearingToNorth = compass.getBearingToNorth();
console.log(bearingToNorth); // 0;
})
Using Callbacks
window.addEventListener("DOMContentLoaded", () => {
const compass = new Compass();
compass.init(compassLoaded);
function compassLoaded(){
const bearingToNorth = compass.getBearingToNorth();
console.log(bearingToNorth); // 0;
}
});
You can read the docs to get a better understanding of the Compass class
.
See: Docs
Here's a few examples to get you started:
To develop the Compass you can install the development dependencies and run the tests below.
$ npm install
$ npm run test
$ npm run docs
$ npm run examples
- Add webpack build
- Allow customization for device orientation access trigger
- Built by Joey Lee
- A note on new iphone devices
- device orientation requires user to confirm with a gesture
- A note on compass directions:
- 0 deg = north
- 90 deg = east
- 180 deg = south
- 270 deg = west