MENU
Sensor
The Sensor APIs are a set of interfaces built to a common design that consistently expose device sensors to the web platform.
The Sensor interface is the base class for all the other sensor interfaces. It cannot be used directly.
Properties
.activated // Boolean
.hasReading // Boolean
.timestamp // time stamp of the latest reading
Methods
.start()
.stop()
Events
.onerror
.onreading
.onactivate
The Accelerometer interface provides on each reading the acceleration applied to the device.
Properties
.x
.y
.z
The LinearAccelerationSensor interface provides on each reading the acceleration applied to the device, without the contribution of gravity.
Properties
.x
.y
.z
The Gyroscope interface provides on each reading the angular velocity of the device.
Properties
.x
.y
.z
The AmbientLightSensor interface returns the current light level or illuminance of the ambient light around the device.
Property
.illuminance
The Magnetometer provides information about the magnetic field detected by the device.
Properties
.x
.y
.z
The AbsoluteOrientationSensor interface and the RelativeOrientationSensor interface describe the physical orientation of the device in relation to the reference coordinate system of the Earth.
Properties
.quaternion
Methods
.populateMatrix()
Try running the following on a mobile device.
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html><html><head><script>
let acl = new Accelerometer( {frequency: 60, referenceFrame: 'device'}); // or 'screen'
acl.addEventListener('reading', () => {
document.body.innerHTML = "Acceleration along the X-axis " + Math.round(acl.x*10)/10 + "<br/>";
document.body.innerHTML += "Acceleration along the Y-axis " + Math.round(acl.y*10)/10 + "<br/>";
document.body.innerHTML += "Acceleration along the Z-axis " + Math.round(acl.z*10)/10;
});
acl.start();
</script></head><body style="height:100px"></body></html>
<!DOCTYPE html><html><body><script>
const options = { frequency: 60, referenceFrame: 'device' };
const sensor = new RelativeOrientationSensor(options);
sensor.addEventListener('reading', () => { // model is a Three.js object instantiated elsewhere.
model.quaternion.fromArray(sensor.quaternion).inverse();
});
sensor.addEventListener('error', error => {
if (event.error.name == 'NotReadableError') {
console.log("Sensor is not available.");
}
});
sensor.start();
</script></body></html>