zoukankan      html  css  js  c++  java
  • QQ摇一摇

    HTML5提供了访问传感器的API,分别是DeviceOrientationEvent和DeviceMotionEvent

    实现方法:

    监听devicemotion=>

    设备在x,y,z方向上移动的距离和前一次移动的距离之差/两次事件的时间差即设备移动速度=>

    与预速度(可通过多次测试取平均值)相比,如果大于之前的预设定值,则发生抖动

    html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>文档标题</title>
    </head>
    <body>
        <h1>Shake your phone</h1>
    </body>
    </html>

    js

    var shake_speed_threshold = 300;//摇动速度阈值
    var lastTime = 0;//last change time
    var x = y = z = lastX = lastY = lastZ;//x,y,z speed initialization
    function motionHandler(event) {
        var acceleration = event.accelerationIncludingGravity;//取得包含重力加速的位置信息
        var curTime = Date.now();//get the current time
        if ((curTime - lastTime) > 120) {
            var diffTime = curTime - lastTime;//difference of the two times
            lastTime = curTime;//save the time
            x = acceleration.x;
            y = acceleration.y;
            z = acceleration.z;
            var speed = Math.abs(x + y + z - lastX - lasy - lastZ) / diffTime * 1000;//calulate the speed
            if (speed > shake_speed_threshold) {//judge the speed is over the pre_speed or not
                alert("You've shaken the phone!");
            }
            //save the positions
            lastX = x;
            lastY = y;
            lastZ = z;
        }
    }
    if (window.DeviceMotionEvent) {
        window.addEventListener('devicemotion', motionHandler, false);
    } else {
        alert('Your device dose not support the position sensing.');
    }
  • 相关阅读:
    c++ mvc timer的应用
    table 在网页无法顶到头部问题
    vs2008 C++ 没有找到MSVCR90D.dll 问题
    FrametSet中各frame,iframe之间dom的访问
    关于VC中的Timer
    Vc2008中如何为视图类添加消息响应
    C++ map在MFC中的应用
    解决iframe 右边有空白的问题
    poj1125 Stockbroker Grapevine *
    poj1062 昂贵的聘礼 **
  • 原文地址:https://www.cnblogs.com/xxxsans/p/14379796.html
Copyright © 2011-2022 走看看