Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hope this isn't coming at you too late, but isn't it frustrating that Safari for iOS 4.2+ will register DeviceOrientationEvent on an iPhone 3GS (or other devices without a gyroscope)?</p> <p>Bottom Line, DeviceOrientation does not work with the iPhone 3GS. But as someone mentioned, DeviceMotionEvent works, but you have to access the event data in a different way than you would with a device with a gyroscope (silly I know!).</p> <p>First Step: I added a variable in the mix to capture whether or not the OrientationEvent was actually firing with any non-null data (as it would if there were a gyroscope).</p> <pre><code>var canHandleOrientation; if (window.DeviceOrientationEvent) { window.addEventListener("deviceorientation", handleOrientation, false); } function handleOrientation(event){ console.log("Orientation:" + event.alpha + ", " + event.beta + ", " + event.gamma); canHandleOrientation = event; // will be either null or with event data } </code></pre> <p>Now you know whether or not the event actually has gyroscope data or not! So if you want to default to something else (ie. window.DeviceMotionEvent) you can use a conditional.</p> <pre><code>if (!canHandleOrientation) { console.log("There is no gyroscope") } </code></pre> <p>I tested this on Mobile Safari for iPhone 3GS (no gyroscope) and iPad 2 (gyroscope) and Chrome on my Macbook Pro (gyroscope). Seems to work.</p> <p>Now, if you want to get the DeviceMotionEvent data as an alternative if the Orientation data is not available, then...</p> <pre><code>if (window.DeviceMotionEvent &amp;&amp; !canHandleOrientation) { window.addEventListener('devicemotion', handleMotion, false); } function handleMotion(event){ if(event.acceleration){ //requires a gyroscope to work. console.log("Motion Acceleration: " + event.acceleration.x + ", " + event.acceleration.y + ", " + event.acceleration.z); } else{ //this is for iPhone 3GS or a device with no gyroscope, and includes gravity. console.log("Motion AccelerationGravity: " + event.accelerationIncludingGravity.x + ", " + event.accelerationIncludingGravity.y + ", " + event.accelerationIncludingGravity.z); } } </code></pre> <p>This should cover your bases for most devices with a webkit browser... I hope. Havent tested it on any Android devices.</p> <p>It should be noted, each event returns different numbers, so you might need to do some work to normalize them. But this is the basics of how you access them.</p> <p>Let me know if this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload