Creating Web Apps - The Device Orientation API |
Written by Mike James | ||||||
Monday, 05 November 2012 | ||||||
Page 2 of 2
Usually feature detection is to be preferred to browser detection but in this case there is no obvious way to detect that the directions have been reversed. You could ask the user to tilt the device down to the floor and read the sign of the angle and perform a correction but this isn't a good thing to have to get the user to do. This isn't a good situation and we can only hope that its fixed soon. So to correct the beta and gamma angles returned by Chrome we use:
Using this we can now move on and make use of the beta and gamma angles, ignoring any differences between Firefox and Chrome. A Gradient ArrowThe first interesting thing to try out is to simply draw a line in the direction of the greatest slope - i.e. the direction a marble would roll if it was on placed on the screen of the device. This is comparatively easy as the two angles should be zero when the device is horizontal - in practice they aren't but its close enough for most applications. The slope down the y axis is proportional to sin(-beta) and the slope down the x axis is proportional to sin(-gamma). Putting these two together gives us a unit vector pointing down in direction of maximum slope (sin(-gamma),sin(-beta)). To draw this vector all we need are some simple canvas instructions but first we need the canvas:
The JavaScript is fairly simple. First a function that draws a line from the center of the canvas to the point x,y:
We pass in the canvas context and its size width and height. The function clears the canvas, moves to the center and draws a line to x,y. Next we need to see a few things up and write the event handler:
Now if you load the page you will see the canvas has a line drawn on it and it "points" down the slope of the device and it gets longer as the slope gets steeper. If it points in some other directions the angles being reported aren't correct. Roll the BallThe arrow is a nice demo but can you resist the temptation to roll a ball using the arrow as the direction of acceleration. This really isn't anything more than the use of the the standard sprite pattern. We start off with a canvas element as before:
And the JavaScript starts off in the same way:
Next we need to define a sprite with position, velocity and acceleration. We could also add shape and some general methods to animate it but let's keep things simple and have just a single update method that moves the sprite on one animation step.
The first line sets up the position, velocity and acceleration of the sprite to be zero. The second line defines its update method. This clears the canvas, computes the new position using the velocity and the new velocity using the acceleration. Next a circle is drawn at the new position. Finally the if statements at the end check to see if the sprite has hit the boundary of the canvas. If it has then the velocity in the appropriate direction is reversed to implement a bounce. Next we need to get the rotation angles in the usual way, but these are going to be used to set the sprite's acceleration.
Notice that you can't determine how or when the acceleration is updated. Finally we need to start an animation timer off and call update at a regular time step:
If you set the time interval too small then the whole process will choke on updates it hasn't the time to carry out. In most cases a 50ms update will produce a smooth animation but if it doesn't try increasing it until things settle down. Now if you run the program you should see a ball appear in the middle of the canvas and you can make the ball roll in any direction you like by tilting the device. As there is no friction it is possible to get the ball to higher and higher speeds by simply tilting the device following a bounce. If you would like to allow users to play on desktop browsers or devices without accelerometers all you have to do is hookup the click event handler - something like:
However, it just isn't as much fun. ConclusionThe device orientation API is just about well enough implemented for you make use of its tilt information to provide user input. However, it is another example of how poorly HTM5 APIs are implemented as soon as you move even a little off the main track. It really is important that browsers not only implement the standards, but manage to do it in a reasonably standard way. If you would like to take the tilt control idea a little further then why not use Box2D to animate things - see Getting Started with Box2D in JavaScript.
If you would like the code for this article register with I Programmer and visit the CodeBin.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter. More Informationhttp://dev.w3.org/geo/api/spec-source-orientationRelated ArticlesThis article is one in a series on creating web apps:
Getting Started with Box2D in JavaScript
Comments
or email your comment to: comments@i-programmer.info
|
||||||
Last Updated ( Monday, 21 January 2013 ) |