[js点滴]JavaScript之设备事件详解01

  <div class="entry"><h2>前言</h2>

今天主要介绍一下html5重力感应事件之DeviceMotionEvent设备事件

事件介绍

1、deviceorientation 提供设备的物理方向信息,表示为一系列本地坐标系的旋角。

2、devicemotion 提供设备的加速信息,表示为定义在设备上的坐标系中的卡尔迪坐标。其还提供了设备在坐标系中的自转速率。若可行的话,事件应该提供设备重心处的加速信息。

3、compassneedscalibration 用于通知Web站点使用罗盘信息校准上述事件。

4、orientationchange用于方向改变,主要用于横竖屏检测事件

用法简介

注册一个deviceorientation事件的接收器:

  window.addEventListener("deviceorientation", function(event) {
      // 处理event.alpha、event.beta及event.gamma
  }, true);

将设备放置在水平表面,屏幕顶端指向西方,则其方向信息如下:

  {alpha: 90,
   beta: 0,
   gamma: 0};

为了获得罗盘指向,可以简单的使用360度减去alpha。若设被平行于水平表面,其罗盘指向为(360 - alpha)。 若用户手持设备,屏幕处于一个垂直平面且屏幕顶端指向上方。beta的值为90,alpha和gamma无关。 用户手持设备,面向alpha角度,屏幕处于一个垂直屏幕,屏幕顶端指向右方,则其方向信息如下:

  {alpha: 270 - alpha,
   beta: 0,
   gamma: 90};

只用自定义界面通知用户校准罗盘:

 window.addEventListener("compassneedscalibration", function(event) {
      alert('您的罗盘需要校准,请将设备沿数字8方向移动。');
      event.preventDefault();
  }, true);

注册一个devicemotion时间的接收器:

 window.addEventListener("devicemotion", function(event) {
      // 处理event.acceleration、event.accelerationIncludingGravity、
      // event.rotationRate和event.interval
  }, true);

将设备放置在水平表面,屏幕向上,acceleration为零,则其accelerationIncludingGravity信息如下:

  {x: 0,
   y: 0,
   z: 9.81};

设备做自由落体,屏幕水平向上,accelerationIncludingGravity为零,则其acceleration信息如下:

  {x: 0,
   y: 0,
   z: -9.81};

将设备安置于车辆至上,屏幕处于一个垂直平面,顶端向上,面向车辆后部。车辆行驶速度为v,向右侧进行半径为r的转弯。设备记录acceleration和accelerationIncludingGravity在位置x处的情况,同时设备还会记录rotationRate.gamma的负值:

  {acceleration: {x: v^2/r, y: 0, z: 0},
   accelerationIncludingGravity: {x: v^2/r, y: 0, z: 9.81},
   rotationRate: {alpha: 0, beta: 0, gamma: -v/r*180/pi} };

应用案例

if (window.DeviceMotionEvent) { 
                 window.addEventListener('devicemotion',deviceMotionHandler, false);  
        } 
        var speed = 30;//speed
        var x = y = z = lastX = lastY = lastZ = 0;
        function deviceMotionHandler(eventData) {  
          var acceleration =eventData.accelerationIncludingGravity;
                x = acceleration.x;
                y = acceleration.y;
                z = acceleration.z;
                if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed || Math.abs(z-lastZ) > speed) {
                    //简单的摇一摇触发代码
                    alert(1);
                }
                lastX = x;
                lastY = y;
                lastZ = z;
        }
    <p>
      Tags:
      
      <a href="/tag/html5">html5</a>,
      
      <a href="/tag/重力感应">重力感应</a>,
      
      <a href="/tag/DeviceMotionEvent">DeviceMotionEvent</a>
      
    </p>
    
   //orientationchange横竖屏检测
	window.addEventListener("orientationchange", function () {
	    if (window.orientation == 0) {
			alert("肖像模式");
	    }else if (window.orientation == 90) {
	    	alert("左旋转的横向模式");
	    }else if (window.orientation == -90) {
	    	alert("右旋转的横向模式");
	    }
	    equipment.innerHTML = window.orientation;
	}, false);
  </div>
##请看下一篇 [[js点滴]JavaScript之设备事件之横竖屏判断详解02](http://blog.csdn.net/BaiHuaXiu123/article/details/53270740)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图解AI

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值