# Gyroscope

## Editor’s Draft, 1 January 1970

This version:
  https://w3c.github.io/gyroscope/
Latest published version:
  https://www.w3.org/TR/gyroscope/
Previous Versions:
  https://www.w3.org/TR/2019/WD-gyroscope-20190307/
Version History:
  https://github.com/w3c/gyroscope/commits/master/index.bs
Feedback:
  public-device-apis@w3.org with subject line “`[gyroscope] … message topic …`” (archives)
Editor:
  Anssi Kostiainen (Intel Corporation)
Former Editor:
  Mikhail Pozdnyakov (Intel Corporation)
Bug Reports:
  via the w3c/gyroscope repository on GitHub
Test Suite:
  web-platform-tests on GitHub

Copyright © 1970 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.

## Abstract

This specification defines a concrete sensor interface to monitor the rate of rotation around the device’s local three primary axes.

## Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

If you wish to make comments regarding this document, please send them to public-device-apis@w3.org (subscribe, archives). When sending e-mail, please put the text “gyroscope” in the subject, preferably like this: “[gyroscope] …summary of comment…”. All comments are welcome.

This document was produced by the Devices and Sensors Working Group.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 March 2019 W3C Process Document.

## 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

    [SecureContext, Exposed=Window]
    interface Gyroscope : Sensor {
      constructor(optional GyroscopeSensorOptions sensorOptions = {});
      readonly attribute double? x;
      readonly attribute double? y;
      readonly attribute double? z;
    };

    enum GyroscopeLocalCoordinateSystem { "device", "screen" };

    dictionary GyroscopeSensorOptions : SensorOptions {
      GyroscopeLocalCoordinateSystem referenceFrame = "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 this and "x" as arguments.

#### 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 this and "y" as arguments.

#### 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 this and "z" as arguments.

## 7. Abstract Operations

### 7.1. Construct a Gyroscope object

input

  options, a `GyroscopeSensorOptions` object.

output

  A `Gyroscope` object.

1. Let allowed be the result of invoking check sensor policy-controlled features with Gyroscope.

2. If allowed is false, then:

   1. Throw a `SecurityError` `DOMException`.

3. Let gyroscope be the new `Gyroscope` object.

4. Invoke initialize a sensor object with gyroscope and options.

5. If options.`referenceFrame` is "screen", then:

   1. Define local coordinate system for gyroscope as the screen coordinate system.

6. Otherwise, define local coordinate system for gyroscope as the device coordinate system.

7. Return gyroscope.

## 8. Automation

This section extends the automation section defined in the Generic Sensor API [GENERIC-SENSOR] to provide mocking information about the rate of rotation around the device’s local three primary axes for the purposes of testing a user agent’s implementation of `Gyroscope` API.

### 8.1. Mock Sensor Type

The `Gyroscope` class has an associated mock sensor type which is "gyroscope", its mock sensor reading values dictionary is defined as follows:

    dictionary GyroscopeReadingValues {
      required double? x;
      required double? y;
      required double? z;
    };

## 9. Acknowledgements

Tobie Langel for the work on Generic Sensor API.

## 10. 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]

## Index

### Terms defined by this specification

- angular velocity, in §5
- conformant user agent, in §10
- Construct a Gyroscope object, in §7
- constructor(), in §6.1
- constructor(sensorOptions), in §6.1
- "device", in §6.1
- Gyroscope
  - (interface), in §6.1
  - definition of, in §5
- Gyroscope(), in §6.1
- GyroscopeLocalCoordinateSystem, in §6.1
- GyroscopeReadingValues, in §8.1
- Gyroscope(sensorOptions), in §6.1
- GyroscopeSensorOptions, in §6.1
- referenceFrame, in §6.1
- "screen", in §6.1
- x
  - attribute for Gyroscope, in §6.1
  - dict-member for GyroscopeReadingValues, in §8.1
- y
  - attribute for Gyroscope, in §6.1
  - dict-member for GyroscopeReadingValues, in §8.1
- z
  - attribute for Gyroscope, in §6.1
  - dict-member for GyroscopeReadingValues, in §8.1

https://w3c.github.io/accelerometer/#device-coordinate-system Referenced in:

- 5.1. Reference Frame
- 7.1. Construct a Gyroscope object

https://w3c.github.io/accelerometer/#screen-coordinate-system Referenced in:

- 5.1. Reference Frame
- 7.1. Construct a Gyroscope object

https://w3c.github.io/sensors/#dom-mocksensortype-gyroscope Referenced in:

- 8.1. Mock Sensor Type

https://w3c.github.io/sensors/#sensor Referenced in:

- 5. Model (2)
- 6.1. The Gyroscope Interface

https://w3c.github.io/sensors/#dictdef-sensoroptions Referenced in:

- 6.1. The Gyroscope Interface

https://w3c.github.io/sensors/#automation Referenced in:

- 8. Automation

https://w3c.github.io/sensors/#check-sensor-policy-controlled-features Referenced in:

- 7.1. Construct a Gyroscope object

https://w3c.github.io/sensors/#default-sensor Referenced in:

- 5. Model

https://w3c.github.io/sensors/#eavesdropping Referenced in:

- 4. Security and Privacy Considerations

https://w3c.github.io/sensors/#device-fingerprinting Referenced in:

- 4. Security and Privacy Considerations

https://w3c.github.io/sensors/#mitigation-strategies Referenced in:

- 4. Security and Privacy Considerations

https://w3c.github.io/sensors/#get-value-from-latest-reading Referenced in:

- 6.1.1. Gyroscope.x
- 6.1.2. Gyroscope.y
- 6.1.3. Gyroscope.z

https://w3c.github.io/sensors/#initialize-a-sensor-object Referenced in:

- 7.1. Construct a Gyroscope object

https://w3c.github.io/sensors/#keystroke-monitoring Referenced in:

- 4. Security and Privacy Considerations

https://w3c.github.io/sensors/#latest-reading Referenced in:

- 5. Model

https://w3c.github.io/sensors/#local-coordinate-system Referenced in:

- 5. Model (2)
- 5.1. Reference Frame
- 7.1. Construct a Gyroscope object (2)

https://w3c.github.io/sensors/#location-tracking Referenced in:

- 4. Security and Privacy Considerations

https://w3c.github.io/sensors/#mock-sensor-reading-values Referenced in:

- 8.1. Mock Sensor Type

https://w3c.github.io/sensors/#mock-sensor-type Referenced in:

- 8.1. Mock Sensor Type

https://w3c.github.io/sensors/#sensor-permission-names Referenced in:

- 5. Model

https://w3c.github.io/sensors/#sensor-reading Referenced in:

- 4. Security and Privacy Considerations (2)
- 5.1. Reference Frame

https://w3c.github.io/sensors/#sensor-type Referenced in:

- 5. Model (2)

https://w3c.github.io/sensors/#supported-sensor-options Referenced in:

- 6.1. The Gyroscope Interface

https://w3c.github.io/sensors/#user-identifying Referenced in:

- 4. Security and Privacy Considerations

https://infra.spec.whatwg.org/#map-entry Referenced in:

- 5. Model

https://infra.spec.whatwg.org/#map-key Referenced in:

- 5. Model

https://infra.spec.whatwg.org/#map-value Referenced in:

- 5. Model

https://w3c.github.io/permissions/#dom-permissionname-gyroscope Referenced in:

- 5. Model

https://heycam.github.io/webidl/#idl-DOMException Referenced in:

- 7.1. Construct a Gyroscope object

https://heycam.github.io/webidl/#Exposed Referenced in:

- 6.1. The Gyroscope Interface

https://heycam.github.io/webidl/#SecureContext Referenced in:

- 6.1. The Gyroscope Interface

https://heycam.github.io/webidl/#securityerror Referenced in:

- 7.1. Construct a Gyroscope object

https://heycam.github.io/webidl/#idl-double Referenced in:

- 6.1. The Gyroscope Interface (2) (3)
- 8.1. Mock Sensor Type (2) (3)

https://heycam.github.io/webidl/#dfn-throw Referenced in:

- 7.1. Construct a Gyroscope object

### Terms defined by reference

- [accelerometer] defines the following terms:
  - device coordinate system
  - screen coordinate system
- [GENERIC-SENSOR] defines the following terms:
  - "gyroscope"
  - Sensor
  - SensorOptions
  - automation
  - check sensor policy-controlled features
  - default sensor
  - eavesdropping
  - fingerprinting
  - generic mitigations
  - get value from latest reading
  - initialize a sensor object
  - keylogging
  - latest reading
  - local coordinate system
  - location tracking
  - mock sensor reading values
  - mock sensor type
  - sensor permission name
  - sensor reading
  - sensor type
  - supported sensor options
  - user identifying
- [INFRA] defines the following terms:
  - entry
  - key
  - value
- [permissions] defines the following terms:
  - "gyroscope"
- [WEBIDL] defines the following terms:
  - DOMException
  - Exposed
  - SecureContext
  - SecurityError
  - double
  - throw

## References

### Normative References

[ACCELEROMETER]
  Anssi Kostiainen. Accelerometer. URL: https://w3c.github.io/accelerometer/
[GENERIC-SENSOR]
  Mikhail Pozdnyakov; Alexander Shalamov; Tobie Langel. Generic Sensor API. URL: https://w3c.github.io/sensors/
[INFRA]
  Anne van Kesteren; Domenic Denicola. Infra Standard. Living Standard. URL: https://infra.spec.whatwg.org/
[PERMISSIONS]
  Mounir Lamouri; Marcos Caceres; Jeffrey Yasskin. Permissions. URL: https://w3c.github.io/permissions/
[RFC2119]
  S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[WEBIDL]
  Boris Zbarsky. Web IDL. URL: https://heycam.github.io/webidl/

### Informative References

[KEYSTROKEDEFENSE]
  Song, Yihang, et al. Two novel defenses against motion-based keystroke inference attacks. 2014. Informational. URL: https://arxiv.org/abs/1410.7746
[SI]
  SI Brochure: The International System of Units (SI), 8th edition. 2014. URL: http://www.bipm.org/en/publications/si-brochure/
[TOUCHSIGNATURES]
  Mehrnezhad, Maryam, et al. Touchsignatures: identification of user touch actions and pins based on mobile sensor data via javascript. 2016. Informational. URL: https://arxiv.org/abs/1602.04115

## IDL Index

    [SecureContext, Exposed=Window]
    interface Gyroscope : Sensor {
      constructor(optional GyroscopeSensorOptions sensorOptions = {});
      readonly attribute double? x;
      readonly attribute double? y;
      readonly attribute double? z;
    };

    enum GyroscopeLocalCoordinateSystem { "device", "screen" };

    dictionary GyroscopeSensorOptions : SensorOptions {
      GyroscopeLocalCoordinateSystem referenceFrame = "device";
    };

    dictionary GyroscopeReadingValues {
      required double? x;
      required double? y;
      required double? z;
    };

#gyroscope-sensor-type Referenced in:

- 5. Model (2) (3)
- 7.1. Construct a Gyroscope object

#angular-velocity Referenced in:

- 5. Model (2)
- 6.1.1. Gyroscope.x
- 6.1.2. Gyroscope.y
- 6.1.3. Gyroscope.z

#gyroscope Referenced in:

- 5. Model
- 5.1. Reference Frame
- 6.1. The Gyroscope Interface (2)
- 6.1.1. Gyroscope.x
- 6.1.2. Gyroscope.y
- 6.1.3. Gyroscope.z
- 7.1. Construct a Gyroscope object (2)
- 8. Automation
- 8.1. Mock Sensor Type

#dom-gyroscope-x Referenced in:

- 6.1.1. Gyroscope.x

#dom-gyroscope-y Referenced in:

- 6.1.2. Gyroscope.y

#dom-gyroscope-z Referenced in:

- 6.1.3. Gyroscope.z

#enumdef-gyroscopelocalcoordinatesystem Referenced in:

- 6.1. The Gyroscope Interface

#dictdef-gyroscopesensoroptions Referenced in:

- 6.1. The Gyroscope Interface
- 7.1. Construct a Gyroscope object

#dom-gyroscopesensoroptions-referenceframe Referenced in:

- 7.1. Construct a Gyroscope object

#construct-a-gyroscope-object Referenced in:

- 6.1. The Gyroscope Interface