A minimal javascript library for accessing the compass direction of a user relative to North.
npm install @joeyklee/compass.js``html`
Include the reference to the script:
`html`
Using Async/Await
`js
window.addEventListener("DOMContentLoaded", async() => {
const compass = new Compass();
await compass.init();
const bearingToNorth = compass.getBearingToNorth();
console.log(bearingToNorth); // 0;
})
`
Using Callbacks
`js
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
!compass.js demo on mapbox gl map
Here's a few examples to get you started:
* p5 example
* Vanillajs example
* Mapboxgl example;
To develop the Compass you can install the development dependencies and run the tests below.
`sh`
$ npm install
`sh`
$ npm run test
``
$ npm run docs
```
$ npm run examples