Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Beyond capturing just simple changes in device orientation, you can also detect and respond to more complex device motion events. You can do this by listening to the DeviceMotion event, and there's also an onDeviceMotion event off of the Window object. So when you register an event handler for DeviceMotion, the event handler function will be called with an event that describes a DeviceMotion event. The DeviceMotion event contains several properties. There is the property for acceleration, which itself contains properties X, Y, and Z that describe acceleration in meters per second squared on three different axes: forward and back, up or down, and left to right.
There is also the accelerationIncludingGravity property, which is the same as the regular acceleration property, but it includes the effects of gravity. The rotationRate contains properties alpha, beta, and gamma that measure the rotation speed for three rotation axes. And then there's the interval, which is the number of milliseconds since the last time a DeviceMotion event came through. Let's see a code example for how this works. And so here I am in my devicemotion_start file, and I've opened this up from the Exercise Files folder. And I've also got my ExampleSnippets file, and let's scroll down to the Complex Motion area.
So what we're going to do in the devicemotion file is put in some code that responds to DeviceMotion events and displays the motion of the iPhone in the X, Y, and Z axes, and also displays the rotation events in the pitch, yaw, and roll. So let's go back to the snippets code and copy that in. So this is the code I'm going to copy over. So select all of this, and I'll explain what it does. So we'll copy that and we'll paste it in.
So now that I've pasted the code, let's go back and take a look what it does. So right down here at the bottom, I have window.addEventListener, and I'm listening for the devicemotion event. And when the devicemotion event occurs, it will call the handleMotion event handler, which I've defined up here. And this is the handleMotion event handler and it takes the event argument. So let's take a walk through the code. I've got a few local variables here that will hold the values for the acceleration values and the rotation values.
And rather than be inundated with device motion events every few milliseconds, I've got a little time to measure here that starts out at the value of zero. And what I'm doing is I'm checking to see if the interval between this device motion and the last one, if the time elapsed is greater than 1.5 seconds, so this will limit the amount the display updates and shows new numbers. So I'm just adding the current interval to the time and if the time is less than a second and a half, I just return; otherwise, we let it pass and we reset the time counter to zero for the next time around, and then we display the information to the user.
So we've got the displayStr here, and it's going to be the Acceleration label. Then what I'm doing is I'm using the Math library here to round up the values of the X, Y, and Z acceleration values, because the accelerometer and the gyroscope in the iPhone are really hypersensitive, and if we didn't do this we would be getting a whole bunch of very long floating-point numbers being displayed on the screen. So just to limit this to integer numbers and numbers that we can easily see changing every one and the half seconds, I'm just rounding the values up here. So then I have the display string, and I'm adding the values for X, Y, and Z in here, and I'm doing the same thing for the Rotation Rate.
Only in this case, rather than X, Y, and Z, I've got the alpha, beta, and gamma values that I'm saving off, and I'm just rounding them, again, to the nearest integer value, just to make the data little more easy to consume. So then I add that to the display string and then down here in the document's output div right here, I simply set the innerHTML of that div to be the displayStr which contains all the values that we just measured. All right, so I'm going to save this, and I'm going to deploy it up to my server. Now I have to demonstrate this on a physical iPhone and the reason for that is because the iPhone Simulator does not provide a simulation value for the accelerometer or the gyroscope, so let's switch over to the physical iPhone, and we can see this work.
So here I'm on the physical iPhone, and you can see that I'm holding the phone, and we're seeing integer values and even though I'm not moving the phone very much at the moment, you can see that I'm getting a little bit of residual rotation rate. And I'm just going to move the phone around and you can see that as I move the phone around in different axes, you see how the numbers are changing and we're getting different values for the acceleration in the X and Y directions, and because the phone is also rotating while this is happening, we're seeing the rotation rate change as well.
So this is actually a pretty cool little feature. You can use this feature to create web applications that are aware of motion and rotation using the iPhone and the accelerometer and the gyroscope.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101182 Viewers
61 Video lessons · 87935 Viewers
71 Video lessons · 71827 Viewers
56 Video lessons · 103675 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.