1. Introduction
The Gyroscope API extends the Generic Sensor API [GENERIC-SENSOR] to provide information about the angular velocity around the device’s local X, Y and Z axis in terms of radian per seconds units.
2. Use Cases and Requirements
The use cases and requirements are addressed in the Motion Sensors Explainer document.
3. Examples
let sensor = new Gyroscope(); sensor.start(); sensor.onreading = () => { console.log("Angular velocity around the X-axis " + sensor.x); console.log("Angular velocity around the Y-axis " + sensor.y); console.log("Angular velocity around the Z-axis " + sensor.z); }; sensor.onerror = event => console.log(event.error.name, event.error.message);
4. Security and Privacy Considerations
Sensor readings provided by inertial sensors, such as gyroscope, could be used by adversaries to exploit various security threats, for example, keylogging, location tracking, fingerprinting, user identifying and even eavesdropping.
Research papers published by security community, for instance, [KEYSTROKEDEFENSE], indicate that by throttling the frequency, risks of successful attacks are not fully eliminated, while throttling may greatly affect usefulness of a web application with legitimate reasons to use the sensors.
The [TOUCHSIGNATURES] research paper proposes that implementations can provide visual indication when inertial sensors are in use and/or require explicit user consent to access sensor readings. These mitigation strategies complement the generic mitigations defined in the Generic Sensor API [GENERIC-SENSOR].
5. Model
The Gyroscope sensor type’s associated Sensor
subclass is the Gyroscope
class.
The Gyroscope has a default sensor, which is the device’s main gyroscope sensor.
The Gyroscope has an associated sensor permission name which is "gyroscope".
A latest reading of a Sensor
of Gyroscope sensor type includes three entries whose keys are "x", "y", "z" and whose values contain current angular
velocity about the corresponding axes.
The angular velocity is the rate at which the device rotates about a specified axis in a local coordinate system defined by the device. Its unit is the radian per second (rad/s) [SI].
The sign of the current angular velocity depends on the rotation direction and it must be according to the right-hand convention in a local coordinate system defined by the device, such that positive rotation around an axis is clockwise when viewed along the positive direction of the axis (see figure below).
5.1. Reference Frame
The local coordinate system represents the reference frame for the Gyroscope
readings. It can be either
the device coordinate system or the screen coordinate system.
6. API
6.1. The Gyroscope Interface
[Constructor
(optional GyroscopeSensorOptionssensorOptions
), SecureContext, Exposed=Window] interfaceGyroscope
: Sensor { readonly attribute double?x
; readonly attribute double?y
; readonly attribute double?z
; }; enumGyroscopeLocalCoordinateSystem
{"device"
,"screen"
}; dictionaryGyroscopeSensorOptions
: SensorOptions { GyroscopeLocalCoordinateSystemreferenceFrame
= "device"; };
To construct a Gyroscope
object the user agent must invoke
the construct a gyroscope object abstract operation.
Supported sensor options for Gyroscope
are
"frequency" and "referenceFrame".
6.1.1. Gyroscope.x
The x
attribute of the Gyroscope
interface represents the current angular velocity around X-axis.
In other words, this attribute returns the result of invoking get value from latest reading with
6.1.2. Gyroscope.y
The y
attribute of the Gyroscope
interface represents the current angular velocity around Y-axis.
In other words, this attribute returns the result of invoking get value from latest reading with
6.1.3. Gyroscope.z
The z
attribute of the Gyroscope
interface represents the current angular velocity around Z-axis.
In other words, this attribute returns the result of invoking get value from latest reading with
7. Abstract Operations
7.1. Construct a Gyroscope object
- input
-
options, a
GyroscopeSensorOptions
object. - output
-
A
Gyroscope
object.
-
Let allowed be the result of invoking check sensor policy-controlled features with Gyroscope.
-
If allowed is false, then:
-
Let gyroscope be the new
Gyroscope
object. -
Invoke initialize a sensor object with gyroscope and options.
-
If options.
referenceFrame
is "screen", then:-
Define local coordinate system for gyroscope as the screen coordinate system.
-
-
Otherwise, define local coordinate system for gyroscope as the device coordinate system.
-
Return gyroscope.
8. Acknowledgements
Tobie Langel for the work on Generic Sensor API.
9. Conformance
Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.
All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]
A conformant user agent must implement all the requirements listed in this specification that are applicable to user agents.
The IDL fragments in this specification must be interpreted as required for conforming IDL fragments, as described in the Web IDL specification. [WEBIDL]