10
¿Cómo puedo utilizar las nuevas API de movimiento del dispositivo MobileSafari para capturar un evento de "sacudida"?¿Detectar un batido en iOS Safari con Javascript?
¿Cómo puedo utilizar las nuevas API de movimiento del dispositivo MobileSafari para capturar un evento de "sacudida"?¿Detectar un batido en iOS Safari con Javascript?
Ver esta entrada del blog impresionante: http://www.jeffreyharrell.com/blog/2010/11/creating-a-shake-event-in-mobile-safari/
que ilustra este ejemplo:
if (typeof window.DeviceMotionEvent != 'undefined') {
// Shake sensitivity (a lower number is more)
var sensitivity = 20;
// Position variables
var x1 = 0, y1 = 0, z1 = 0, x2 = 0, y2 = 0, z2 = 0;
// Listen to motion events and update the position
window.addEventListener('devicemotion', function (e) {
x1 = e.accelerationIncludingGravity.x;
y1 = e.accelerationIncludingGravity.y;
z1 = e.accelerationIncludingGravity.z;
}, false);
// Periodically check the position and fire
// if the change is greater than the sensitivity
setInterval(function() {
var change = Math.abs(x1-x2+y1-y2+z1-z2);
if (change > sensitivity) {
alert('Shake!');
}
// Update new position
x2 = x1;
y2 = y1;
z2 = z1;
}, 150);
}
excelente. Gracias por agregar el extracto del código. Debería ser útil! – tbeseda
Solo una advertencia: accelerationIncludingGravity nunca tendrá los tres componentes en cero. Si la gravedad actúa directamente sobre un eje, ese eje tendrá un valor de 9.81. Además, ¿por qué no verificar batidos durante el evento en lugar de hacerlo en un intervalo? – ughoavgfhw
@ughoavgfhw ¿podría proponer su pedazo de código? – andi