Bienvenue - Welcome

You are not logged in.

#1 2016-10-30 20:01:16

falsam
Administrator
From: france
Registered: 2016-07-20
Posts: 45
Website

Detecter Accélération et Orientation d'un mobile en Javascript

A tester depuis un terminal Android par exemple.

<!DOCTYPE html>
<html>
<head>

<script>
function motion(event){
  document.getElementById("ax").innerHTML = "x: " + event.accelerationIncludingGravity.x
  document.getElementById("ay").innerHTML = "y: " + event.accelerationIncludingGravity.y
  document.getElementById("az").innerHTML = "z: " + event.accelerationIncludingGravity.z
}

function orientation(event){
  document.getElementById("malpha").innerHTML = "Alpha (Rotate around z) : " + event.alpha;
  document.getElementById("mbeta").innerHTML  = "Beta (Rotate around y): " + event.beta;
  document.getElementById("mgamma").innerHTML = "Gamma (Rotate around x) : " + 	event.gamma;
}

function go(){
	if(window.DeviceMotionEvent){
		window.addEventListener("devicemotion", motion, false);
	}
  
	if(window.DeviceOrientationEvent){
		window.addEventListener("deviceorientation", orientation, false);
	}
}

</script>

</head>

<body onload="go();">
	<p>Device Accelerometer</p>
	<div id="ax"></div>
	<div id="ay"></div>
	<div id="az"></div>
	<br/><br/>
	
	<p>Device Orientation
	<div id="malpha"></div>
	<div id="mbeta"></div>
	<div id="mgamma"></div>
</body>
</html>

■ Lien de test http://falsam.com/device/sensor.html

J'aimerais bien un feedback si vous avez un iphone ou un ipad.

Merci

Offline

Board footer

Powered by FluxBB