微信頁面監聽搖一搖事件,並伴有音效

原文出處:http://i.jakeyu.top/2016/05/0...

最近要寫一個微信網頁,須要監聽手機搖動事件,而且伴隨有聲音html

在HTML5,devicemotion事件deviceorientation特性的運動傳感器的封裝時間裝置,你能夠經過改變運動時間獲取設備的狀態,加速和其餘數據(有另外一個角度deviceorientation事件提供設備,定位等信息)。微信

<!--more-->code

而經過DeviceMotion對設備運動狀態的判斷,則能夠幫助咱們在網頁上就實現「搖一搖」的交互效果。htm

把監聽事件綁定給 deviceMotionHandler事件

if (window.DeviceMotionEvent) {
        window.addEventListener('devicemotion', deviceMotionHandler, false);
    } else {
        alert('本設備不支持devicemotion事件');
    }
    獲取設備加速度信息 accelerationIncludingGravity
    
    function deviceMotionHandler(eventData) {
        var acceleration = eventData.accelerationIncludingGravity,
        x, y, z;
        x = acceleration.x;
        y = acceleration.y;
        z = acceleration.z;
        document.getElementById("status").innerHTML = "x:"+x+"<br />y:"+y+"<br />z:"+z;
    }

「搖一搖」的動做既「必定時間內設備了必定距離」,所以經過監聽上一步獲取到的x, y, z 值在必定時間範圍內 的變化率,便可進行設備是否有進行晃動的判斷。而爲了防止正常移動的誤判,須要給該變化率設置一個合適的臨界 值。ip

var SHAKE_THRESHOLD = 800;
    var last_update = 0;
    var x = y = z = last_x = last_y = last_z = 0;
    
    if (window.DeviceMotionEvent) {
        window.addEventListener('devicemotion', deviceMotionHandler, false);
    } else {
    alert('本設備不支持devicemotion事件');
    }
    
    function deviceMotionHandler(eventData) {
        var acceleration = eventData.accelerationIncludingGravity;
        var curTime = new Date().getTime();
        
        if ((curTime - last_update) > 100) {
            var diffTime = curTime - last_update;
            last_update = curTime;
            x = acceleration.x;
            y = acceleration.y;
            z = acceleration.z;
            var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
            var status = document.getElementById("status");
            
            if (speed > SHAKE_THRESHOLD) {
                doResult();
            }
            last_x = x;
            last_y = y;
            last_z = z;
        }
    }

100毫秒進行一次位置判斷,若先後x, y, z間的差值的絕對值和時間比率超過了預設的閾值,則判斷設備進行 了搖晃操做。get

下面是我改寫的代碼

<audio style="display: none;" src="http://xunlei.sc.chinaz.com/files/download/sound1/201410/5018.mp3" id="musicBox" preload="preload"  controls></audio>
<script>
        var SHAKE_THRESHOLD = 3000;    
        var last_update = 0;    
        var x=y=z=last_x=last_y=last_z=0;   
       var  media; 
       media= document.getElementById("musicBox");
       function init(){
              last_update=new Date().getTime();
               if (window.DeviceMotionEvent) { 
                   window.addEventListener('devicemotion',deviceMotionHandler, false);  
               } else{    
          alert('not support mobile event');
          }
       }
        function deviceMotionHandler(eventData) {    
           var acceleration =eventData.accelerationIncludingGravity; 
           var curTime = new Date().getTime(); 
           if ((curTime - last_update)> 100) {  
                   var diffTime = curTime -last_update;    
                   last_update = curTime;        
                   x = acceleration.x; 
                   y = acceleration.y;   
                   z = acceleration.z;   
                   var speed = Math.abs(x +y + z - last_x - last_y - last_z) / 
           diffTime * 10000;     
                   if (speed > SHAKE_THRESHOLD) {    
                       media.play();
                   }    
                   last_x = x;    
                   last_y = y;    
                   last_z = z;    
               }
        }
        window.onload = function(){
               init();
        }
    </script>
相關文章
相關標籤/搜索