CrossBrowdy - Basic tutorial

Device

Motion (accelerometer)

With CrossBrowdy, you can manage physical movements in the device.

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


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

To set an event handler to manage physical movements and their speed (normally, detected by an accelerometer):


	//Checks physical movements and their speed (use "null" as the first parameter to remove them):
	CB_Device.Motion.onChange
	(
		//Motion is obtained successfully:
		function(data) //Data will be normalized automatically.
		{
			CB_console
			(
				"Motion obtained successfully:\n" +
				"Acceleration in x: "  + data.acceleration.x + "\n" +
				"Acceleration in y: "  + data.acceleration.y + "\n" +
				"Acceleration in z: "  + data.acceleration.z + "\n" +
				"Acceleration in x (with gravity): "  + data.accelerationIncludingGravity.x + "\n" +
				"Acceleration in y (with gravity): "  + data.accelerationIncludingGravity.y + "\n" +
				"Acceleration in z (with gravity): "  + data.accelerationIncludingGravity.z + "\n" +
				"Interval: "  + data.interval + "\n" +
				"Rotation Rate Alpha: "  + data.rotationRate.alpha + "\n" +
				"Rotation Rate Beta: "  + data.rotationRate.beta + "\n" +
				"Rotation Rate Gamma: "  + data.rotationRate.gamma
			);
		}
	);

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.Motion static classes.

Go back to Guides & Tutorials

« PrevReturnNext »
CrossBrowdy by Joan Alba Maldonado