Read the device’s gyroscope alongside its magnetic compass on a single channel. Every gyro reading carries the latest cached heading, so the same callback drives motion-driven UI, shake-to-action gestures, AR overlays, fitness apps, and compass or Qibla needles. Pass a magnitude threshold to filter out low-motion noise, or passDocumentation Index
Fetch the complete documentation index at: https://setup.despia.com/llms.txt
Use this file to discover all available pages before exploring further.
0 to receive every sample. No permission prompt, no setup.
Installation
- Bundle
- CDN
How it works
Callgyroscope://start to begin emitting readings and gyroscope://stop to end the session. While tracking is active, every gyro reading above the magnitude threshold is delivered to window.onGyroscopeChange in real time, and each reading carries the most recent magnetic heading. State persists across soft close and reopen, so if the user backgrounds the app and relaunches, tracking resumes with the same threshold and the callback keeps firing.
Parameters
gyroscope://start accepts the following query parameter.
| Parameter | Required | Description |
|---|---|---|
threshold | Yes | Magnitude threshold in degrees per second. A reading fires only when √(x² + y² + z²) is at or above this value. Use 0 to receive every sample, 90 for roughly a quarter-rotation per second, higher values for shake-style gestures. The threshold gates the gyro stream, and heading rides along on every emitted sample. |
gyroscope://stop takes no parameters.
Live updates with window.onGyroscopeChange
Definewindow.onGyroscopeChange before calling gyroscope://start so the first reading is not missed. Native invokes this for every event, success, error, and calibration prompt. Discriminate on data.status before reading any other field.
window.onGyroscopeChange with fresh readings as soon as your handler is reattached. No extra wiring needed.
Reading object
Every reading delivered towindow.onGyroscopeChange has the following shape on success.
| Field | Description | Usage |
|---|---|---|
status | "success" for valid readings, "error" if the sensor is unavailable, "calibration_required" when the magnetometer needs recalibrating | Branch on this before reading other fields |
x | Angular velocity around the X axis in radians per second. Pitch | Tilt detection, AR overlays |
y | Angular velocity around the Y axis in radians per second. Roll | Side-tilt detection |
z | Angular velocity around the Z axis in radians per second. Yaw | Spin and turn detection |
heading | Magnetic heading in degrees. 0 is north, 90 east, 180 south, 270 west. -1 when the compass has not yet produced a fix | Compass needles, Qibla pointers, navigation arrows |
headingAccuracy | Heading uncertainty in degrees, lower is better. -1 when unknown. Treat anything under roughly 15 as a good fix | Show or hide the needle, surface a calibration hint |
timestamp | Unix epoch in milliseconds when the reading was sampled | Debouncing, rate limiting downstream calls |
status and error. The current error code is "unavailable", returned on devices that do not expose a gyroscope or magnetometer.
status. Heading continues to stream while in this state, but headingAccuracy will be poor until the user moves the device through a figure-8 motion.
heading is magnetic north. For true north (used for accurate navigation and Qibla pointing), apply the local magnetic declination on the JavaScript side using the user’s own latitude and longitude. The native layer does not require location permission to deliver heading.Tracking state
Usedespia.gyroscopeActive to reflect the current tracking state in your UI. It is set to true when gyroscope://start is called and false when gyroscope://stop is called. The flag survives soft close and reopen, so your boot code can decide whether to resume or skip without guessing.
['gyroscopeActive'] as the second argument.
stop to confirm the sensor was released.
despia.gyroscopeActive does not flip to true if the device returns the unavailable error, since tracking never actually started. The flag stays false and your UI can fall back accordingly.
Idempotent calls are safe. Calling start while already started, or stop while already stopped, re-asserts the flag without disrupting the current session.
Resume after soft close
When the user backgrounds the app and reopens it, the runtime restores the gyroscope session automatically using the last threshold. The pattern below boots straight into the live readout if a session was already running, otherwise shows the start button.gyroscope://stop explicitly when the user finishes their session, not just when they navigate away from the screen.
Shake-to-action gesture
A common pattern is detecting a sharp shake and firing a one-shot action. Set a high threshold so the native layer filters out everyday motion and you only see real shakes in JavaScript.threshold=600 filters out everything but deliberate shakes, and the JavaScript-side debounce prevents a single shake from triggering the action twice.
Tilt-controlled UI
For continuous control like a tilt-to-pan or parallax effect, usethreshold=0 so every sample reaches your callback, and clamp the values yourself.
Compass needle
Drive a needle directly from the magnetic heading. Rotate the needle counter to the device heading so it always points to magnetic north relative to the screen. Usethreshold=0 so heading updates flow on every gyro tick.
True north and Qibla pointer
For navigation or Qibla apps, convert the magnetic heading to true heading using the local declination, then point the needle at any target bearing using the user’s own coordinates. No native location permission is involved, the page supplies the lat/lon it already has.Calibration prompts
iOS occasionally reports that the magnetometer needs recalibrating, typically after the device has been near a magnet or moved between very different magnetic environments. The runtime emits acalibration_required status. Heading continues to stream during this state but headingAccuracy stays poor until the user waves the device through a figure-8 motion.
headingAccuracy drops back into the good range.
Resources
NPM Package
despia-native