Gyroscope API
Variable: Gyroscope
Type: [IGyroscope | undefined]
Interface: IGyroscope
The Gyroscope API provides access to the angular velocity data measured by the hardware sensor.
A Gyroscope is a sensor that measures a device's angular velocity along 3
orthogonal axes x
, y
and z
.
- The
x
axis is parallel to the device's screen, aligned with the top and bottom edges, in the left-to-right direction. - The
y
axis is parallel to the device's screen, aligned with the left and right edges, in the bottom-to-top direction. - The
z
axis is perpendicular to the device's screen, pointing up.
The sign of angular velocities follows the 'right hand rule'. A positive angular velocity along an axis corresponds to the curling direction of the fingers of a right hand, with the thumb pointing in the direction of the axis.
Read the Gyroscope Sensor Guide for further information.
import { Gyroscope } from "gyroscope";
if (Gyroscope) {
console.log("This device has a Gyroscope!");
const gyroscope = new Gyroscope({ frequency: 1 });
gyroscope.addEventListener("reading", () => {
console.log(
`Gyroscope Reading: \
timestamp=${gyroscope.timestamp}, \
[${gyroscope.x}, \
${gyroscope.y}, \
${gyroscope.z}]`
);
});
gyroscope.start();
} else {
console.log("This device does NOT have a Gyroscope!");
}
Properties
readonly activated
boolean
Flag that indicates if the sensor is activated or not. When a sensor
is created, the sensor is not activated, thus the initial value of
this property equals false
.
onactivate
((this: Sensor, event: Event) => any) or undefined
Event handler that is called when the sensor is activated.
onerror
((this: Sensor, event: SensorErrorEvent) => any) or undefined
Event handler that is called when an error occurs. When an error
occurs, the sensor is automatically stopped, and the activated
property equals false
.
onreading
((this: Sensor, event: Event) => any) or undefined
Event handler that is called whenever a new reading is available.
readonly readings
BatchedGyroscopeReading or undefined
New in SDK 2.0
Interface: BatchedGyroscopeReading
New in SDK 2.0
Properties
readonly timestamp
Uint32Array
readonly x
Float32Array
readonly y
Float32Array
readonly z
Float32Array
Interface: GyroscopeReading
Angular velocities measured by the Gyroscope sensor.
Properties
readonly timestamp
number or null
Timestamp of the reading in milliseconds.
NOTE: this is relative to an unspecified arbitrary
0
time, ornull
if no reading is available (when the sensor is not yet activated and there are no valid cached values that can be used).
readonly x
number or null
Angular velocity around the x
axis in rad/s
readonly y
number or null
Angular velocity around the y
axis in rad/s
readonly z
number or null
Angular velocity around the z
axis in rad/s