The demo above uses the deviceorientation event listener. Part of the geo api. The deviceorientation gives three readings - alpha, beta and gamma. Alpha represents compass heading, sort of - to get compass heading, take alpha away from 360. Beta is to do with the orientation of the long edge of the device, and gamma is to do with the orientation of the short edge of the device.
Here are some live alpha beta and gamma readings - if you're looking at this page on a mobile device with a browser that can display this stuff, they'll update automatically as you wave it around:
As you can see, they're pretty noisy values, so some kind of smoothing or filter might be appropriate.