Device orientation on the browser: Difference between revisions
m Andre moved page Browser motion to Device orientation on the browser |
No edit summary |
||
| Line 1: | Line 1: | ||
To detect the | To detect the orientation of devices where the web browser is running, using Javascript, we rely on the | ||
<code> window.addEventListener(" | <code> window.addEventListener("deviceorientation")</code> that listens to the orientation of the device. | ||
=Example= | =Example= | ||
See it running in: http://publicationstation.wdka.hro.nl/ | See it running in: http://publicationstation.wdka.hro.nl/D&T/orientation/ | ||
| Line 9: | Line 9: | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
<!DOCTYPE html> | <!DOCTYPE html> | ||
<html> | <html lang="en"> | ||
<head> | <head> | ||
<meta charset=" | <meta charset="utf-8"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script | <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | |||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> | |||
< | <script defer src="https://code.getmdl.io/1.2.1/material.min.js"></script> | ||
<style> | |||
< | body { | ||
< | margin: 2em; | ||
} | |||
< | </style> | ||
< | <title>Device orientation</title> | ||
<style type="text/css"> | |||
.h5logo { | |||
width: 50%; | |||
display: block; | |||
margin-left: auto; | |||
margin-right: auto; | |||
} | |||
</style> | |||
</head> | |||
<body> | |||
<div role="main" class="container"> | |||
<h1>Device Orientation</h1> | |||
<p>example from <a href="http://googlesamples.github.io/web-fundamentals/fundamentals/native-hardware/device-orientation/dev-orientation.html">http://googlesamples.github.io/web-fundamentals/fundamentals/native-hardware/device-orientation/dev-orientation.html</a></p> | |||
<p> | |||
Device orientation is <b><span id="doeSupported">not</span></b> supported | |||
on your device. | |||
</p> | |||
<img id="h5logo" src="http://publicationstation.wdka.hro.nl/wiki/images/8/85/Guttenberg.jpg" class="h5logo"> | |||
< | <h2>Rotation Data</h2> | ||
<p> | |||
<b>alpha:</b> <span id="alpha"></span><br> | |||
<b>beta:</b> <span id="beta"></span><br> | |||
<b>gamma:</b> <span id="gamma"></span><br> | |||
</p> | |||
<p> | |||
<b>Last updated:</b> <span id="timestamp"></span> | |||
</p> | |||
</div> | |||
<script type="text/javascript"> | |||
var fixed = 2; | |||
var h5logo = $("#h5logo"); | |||
var timestamp = $("#timestamp"); | |||
var alpha = $("#alpha"); | |||
var beta = $("#beta"); | |||
var gamma = $("#gamma"); | |||
/* // [START devori] */ | |||
if (window.DeviceOrientationEvent) { | |||
window.addEventListener('deviceorientation', deviceOrientationHandler, false); | |||
$("#doeSupported").innerText = ""; | |||
} | |||
/* // [END devori] */ | |||
var deviceOrientationData; | |||
</body> | function deviceOrientationHandler(evt) { | ||
deviceOrientationData = evt; | |||
try { | |||
timestamp.text( new Date(evt.timeStamp)); | |||
alpha.text( evt.alpha.toFixed(fixed) ); | |||
beta.text( evt.beta.toFixed(fixed) ); | |||
gamma.text( evt.gamma.toFixed(fixed) ); | |||
var rotation = "rotate("+ evt.alpha +"deg) rotate3d(1,0,0, "+ (evt.gamma * -1)+"deg)"; | |||
h5logo.css('transform', rotation).css('webkitTransform', rotation); | |||
} catch (ex) { | |||
$("EdoeSupported").text("NOT"); | |||
} | |||
} | |||
</script> | |||
</body> | |||
</html> | </html> | ||
</source> | </source> | ||
== Emulate device orientation== | |||
On a desktop computer the device orientation can be emulated by the browser. | |||
In Chrome sensors can be simulated by: | |||
* Opening the DevTools main menu, then | |||
* Under More Tools, clicking on Sensors | |||
https://developers.google.com/web/tools/chrome-devtools/device-mode/imgs/navigate-to-sensors.png | |||
More info at: https://developers.google.com/web/tools/chrome-devtools/device-mode/device-input-and-sensors | |||
==more on Device orientation/motion== | ==more on Device orientation/motion== | ||
http://googlesamples.github.io/web-fundamentals/fundamentals/native-hardware/device-orientation/dev-orientation.html | |||
https://mobiforge.com/design-development/html5-mobile-web-device-orientation-events | https://mobiforge.com/design-development/html5-mobile-web-device-orientation-events | ||
| Line 71: | Line 116: | ||
https://www.html5rocks.com/en/tutorials/device/orientation/ | https://www.html5rocks.com/en/tutorials/device/orientation/ | ||
==debugging mobile browsers== | ==debugging mobile browsers== | ||
[https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Debugging_Firefox_for_Android_over_Wifi debugging Firefox over Wifi] | [https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Debugging_Firefox_for_Android_over_Wifi debugging Firefox over Wifi] | ||
Latest revision as of 19:22, 15 December 2016
To detect the orientation of devices where the web browser is running, using Javascript, we rely on the
window.addEventListener("deviceorientation") that listens to the orientation of the device.
Example
[edit]See it running in: http://publicationstation.wdka.hro.nl/D&T/orientation/
code
[edit]<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script defer src="https://code.getmdl.io/1.2.1/material.min.js"></script>
<style>
body {
margin: 2em;
}
</style>
<title>Device orientation</title>
<style type="text/css">
.h5logo {
width: 50%;
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div role="main" class="container">
<h1>Device Orientation</h1>
<p>example from <a href="http://googlesamples.github.io/web-fundamentals/fundamentals/native-hardware/device-orientation/dev-orientation.html">http://googlesamples.github.io/web-fundamentals/fundamentals/native-hardware/device-orientation/dev-orientation.html</a></p>
<p>
Device orientation is <b><span id="doeSupported">not</span></b> supported
on your device.
</p>
<img id="h5logo" src="http://publicationstation.wdka.hro.nl/wiki/images/8/85/Guttenberg.jpg" class="h5logo">
<h2>Rotation Data</h2>
<p>
<b>alpha:</b> <span id="alpha"></span><br>
<b>beta:</b> <span id="beta"></span><br>
<b>gamma:</b> <span id="gamma"></span><br>
</p>
<p>
<b>Last updated:</b> <span id="timestamp"></span>
</p>
</div>
<script type="text/javascript">
var fixed = 2;
var h5logo = $("#h5logo");
var timestamp = $("#timestamp");
var alpha = $("#alpha");
var beta = $("#beta");
var gamma = $("#gamma");
/* // [START devori] */
if (window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', deviceOrientationHandler, false);
$("#doeSupported").innerText = "";
}
/* // [END devori] */
var deviceOrientationData;
function deviceOrientationHandler(evt) {
deviceOrientationData = evt;
try {
timestamp.text( new Date(evt.timeStamp));
alpha.text( evt.alpha.toFixed(fixed) );
beta.text( evt.beta.toFixed(fixed) );
gamma.text( evt.gamma.toFixed(fixed) );
var rotation = "rotate("+ evt.alpha +"deg) rotate3d(1,0,0, "+ (evt.gamma * -1)+"deg)";
h5logo.css('transform', rotation).css('webkitTransform', rotation);
} catch (ex) {
$("EdoeSupported").text("NOT");
}
}
</script>
</body>
</html>
Emulate device orientation
[edit]On a desktop computer the device orientation can be emulated by the browser.
In Chrome sensors can be simulated by:
- Opening the DevTools main menu, then
- Under More Tools, clicking on Sensors
https://developers.google.com/web/tools/chrome-devtools/device-mode/imgs/navigate-to-sensors.png
More info at: https://developers.google.com/web/tools/chrome-devtools/device-mode/device-input-and-sensors
more on Device orientation/motion
[edit]https://mobiforge.com/design-development/html5-mobile-web-device-orientation-events
https://www.w3.org/TR/orientation-event/
https://www.html5rocks.com/en/tutorials/device/orientation/
