CrossBrowdy - Basic tutorial

Device

Orientation and inclination (compass / magnetometer, gyroscope)

With CrossBrowdy, you can know the device orientation every moment.

You can detect whether the device supports orientation management easily (through the Device Orientation Event used by the Device Orientation API or compatible ones):


	//Orientation management support detection:
	if (CB_Device.Orientation.isSupported())
	{
		CB_console("The Device Orientation Event (used by the Device Orientation API or compatible ones) is supported.");
		
		//Do things here...
	}
	else
	{
		CB_console("The Device Orientation Event (used by the Device Orientation API or compatible ones) is not supported.");
	}

To set an event handler called each time that the orientation changes (normally, detected by a gyroscope):


	//Checks when the orientation changes (use "null" as the first parameter to remove them):
	CB_Device.Orientation.onChange
	(
		//Orientation is obtained successfully:
		function(data) //Data will be normalized automatically.
		{
			CB_console
			(
				"Orientation obtained successfully:\n" +
				"Alpha: " + data.alpha + "\n" +
				"Beta: " + data.beta + "\n" +
				"Gamma: " + data.gamma + "\n" +
				"Absolute: " + (data.absolute ? "yes" : "no")
			);
		}
	);

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.

Finally, some devices will also allow you to detect whether the compass / magnetometer (not gyroscope) needs calibration:


	//Checks whether the compass / magnetometer needs calibration (use "null" as the first parameter to remove them):
	if (CB_Device.Orientation.isCompassNeedsCalibrationSupported())
	{
		CB_Device.Orientation.onCompassNeedsCalibration(function() { CB_console("Compass needs calibration!"); });
	}

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

Go back to Guides & Tutorials

« PrevReturnNext »
CrossBrowdy by Joan Alba Maldonado