1. Introduction
The Ambient Light Sensor extends the Generic Sensor API [GENERIC-SENSOR] to provide information about ambient light levels, as detected by the device’s main light detector, in terms of lux units.
The light-level
media feature [MEDIAQUERIES-4] provides
less granular information about the ambient light level.
Note: it might be worthwhile to provide a high-level Light Level Sensor
which would mirror the light-level
media feature, but in JavaScript.
This sensor would not require additional user permission to be activated in user agents that exposed the light-level
media feature.
2. Examples
let sensor = new AmbientLightSensor(); sensor.start(); sensor.onchange = function(event) { console.log(event.reading.illuminance); }; sensor.onerror = function(event) { console.log(event.error.name, event.error.message); };
3. Security and Privacy Considerations
There are no specific security and privacy considerations beyond those described in the Generic Sensor API [GENERIC-SENSOR].
4. Model
The Ambient Light Sensor’s associated Sensor subclass is the AmbientLightSensor
class.
The Ambient Light Sensor’s associated SensorReading subclass is the AmbientLightSensorReading
class.
The Ambient Light Sensor has a default sensor, which is the device’s main light detector.
The Ambient Light Sensor has a single supported reporting mode which is "auto".
The Ambient Light Sensor’s permission name is "ambient-light"
.
It has no associated PermissionDescriptor.
The Ambient Light Sensor has an associated abstract operation to retrieve the sensor permission which must simply return a permission whose name is "ambient-light".
The Ambient Light Sensor has an associated abstract operation
to construct a SensorReading object which creates a new AmbientLightSensorReading
object and sets its illuminance attribute to
the current light level.
The current light level or illuminance is a value that represents the ambient light levels around the hosting device. Its unit is the lux (lx) [SI].
Note: The precise lux value reported by different devices in the same light can be different, due to differences in detection method, sensor construction, etc.
5. API
5.1. The AmbientLightSensor Interface
[Constructor(optional SensorOptions sensorOptions)] interface AmbientLightSensor : Sensor { readonly attribute AmbientLightSensorReading? reading; };
To Construct an AmbientLightSensor Object the user agent must invoke the construct a Sensor object abstract operation.
5.2. The AmbientLightSensorReading Interface
[Constructor(AmbientLightSensorReadingInit ambientLightSensorReadingInit)] interface AmbientLightSensorReading : SensorReading { readonly attribute unrestricted double illuminance; }; dictionary AmbientLightSensorReadingInit { unrestricted double illuminance; };
5.2.1. The AmbientLightSensorReading constructor
5.2.2. The illuminance attribute
The illuminance attribute of the AmbientLightSensorReading
interface represents the current light level.
6. Acknowledgements
Doug Turner for the initial prototype and Marcos Caceres for the test suite.
Paul Bakaus for the LightLevelSensor idea.
7. 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]