CrossBrowdy - Basic tutorial

Device

Light sensor

It is possible to get information from the ambient light sensor of a device (through the Ambient Light Sensor API or the Ambient Light Sensor Event's "ondevicelight" or "onlightlevel" event or compatible ones). Here is an example:


	//Ambient light sensor management:
	if (CB_Device.AmbientLight.isSupported())
	{
		CB_console("The ambient light detection is supported (through the Ambient Light Sensor API or the Ambient Light Sensor Events ('ondevicelight') or 'onlightlevel' event or compatible ones).");
		
		//Gets the ambient light now and also every time it changes:
		CB_Device.AmbientLight.onChange(function(data) { CB_console("Light detected: " + data.value); }); //Data will be normalized automatically.
	}
	else
	{
		CB_console("The ambient light detection is not supported.");
	}

Note that CrossBrowdy will try to normalize automatically the data of the event object received to minimize the differences across different clients, so you will normally not have to worry about this.

Check the API documentation to read more about the CB_Device and the CB_Device.AmbientLight static classes.

Go back to Guides & Tutorials













Share