Accelerometer
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
TheAccelerometer interface of theSensor APIs provides on each reading the acceleration applied to the device along all three axes.
To use this sensor, the user must grant permission to the'accelerometer', device sensor through thePermissions API.
This feature may be blocked by aPermissions Policy set on your server.
In this article
Constructor
Accelerometer()ExperimentalCreates a new
Accelerometerobject.
Instance properties
In addition to the properties listed below,Accelerometer inherits properties from its parent interfaces,Sensor andEventTarget.
Accelerometer.xRead onlyExperimentalReturns a double containing the acceleration of the device along the device's x axis.
Accelerometer.yRead onlyExperimentalReturns a double containing the acceleration of the device along the device's y axis.
Accelerometer.zRead onlyExperimentalReturns a double containing the acceleration of the device along the device's z axis.
Instance methods
Accelerometer doesn't have its own methods. However, it inherits methods from its parent interfaces,Sensor andEventTarget.
Events
Accelerometer doesn't have its own events. However, it inherits events from its parent interface,Sensor.
Example
Acceleration is typically read in thereading event callback. In the example below this occurs sixty times a second.
const acl = new Accelerometer({ frequency: 60 });acl.addEventListener("reading", () => { console.log(`Acceleration along the X-axis ${acl.x}`); console.log(`Acceleration along the Y-axis ${acl.y}`); console.log(`Acceleration along the Z-axis ${acl.z}`);});acl.start();Specifications
| Specification |
|---|
| Accelerometer> # accelerometer-interface> |