HTML5摇一摇
方式一
(function(){
/**
* 摇一摇
* @author rubekid
*/
function Shake(options){
this.init(options);
}
Shake.prototype = {
init:function(options){
this.options = options || {};
if(!window.DeviceMotionEvent || !window.addEventListener){
alert("该浏览器不支持摇一摇,请换个浏览器试试!");
this.uninit = true;
return false;
}
var threshold = options.threshold || 2000;
var afterShake = options.afterShake;
if(typeof afterShake != "function"){
afterShake = function(speed, acceleration){};
}
var _this = this;
var x=null, y=null, z=null,_x=null,_y=null,_z=null;
var t=_t=(new Date()).getTime();
this.deviceMotionHandler = function(event){
t = (new Date()).getTime();
var diffTime = t - _t;
if(diffTime < 100){//取时间大于0.1秒的变化
return false;
}
var acceleration = event.accelerationIncludingGravity;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
if(_x!==null && _y!==null && _z!==null){
var speed = Math.ceil( Math.abs(( x - _x) + ( y - _y) + ( z - _z )) / diffTime * 100 * 100 );
if(speed > threshold){
afterShake(speed, acceleration);
}
}
//保存上一次记录
_x = x;
_y = y;
_z = z;
_t = t;
}
},
start:function(){
if(this.uninit){
return false;
}
window.addEventListener("devicemotion", this.deviceMotionHandler, false);
},
stop:function(){
if(this.uninit){
return false;
}
window.removeEventListener("devicemotion", this.deviceMotionHandler, false);
}
};
window.Shake = Shake;
})();
准确计数
//摇动
var minY = 10000, maxY = 0;
var lastY = 0;
var maxSpeed = 0;
var counter = 0;
var shakeFlag = 0;
var shake = new Shake({
afterShake:function(speed, acc){
if(speed > maxSpeed){
maxSpeed = speed;
}
if(lastY > acc.y){
minY = acc.y;
shakeFlag = 1;
}
else{
if(shakeFlag){
shakeFlag = 0;
if(maxY - minY > 2 && maxSpeed > 2000){
submitFlag = true;
maxSpeed = 0;
counter ++;
submitCount(); try{
shakeSound.pause();
shakeSound.currentTime = 0;
shakeSound.play();
}
catch(e){}
}
}
maxY = acc.y;
}
lastY = acc.y;
}
});
方式二
(function(){
/**
* 摇一摇
* @author rubekid
*/
function Shake(options){
this.init(options);
}
Shake.prototype = {
init:function(options){
this.options = options || {};
if(!window.DeviceMotionEvent || !window.addEventListener){
alert("该浏览器不支持摇一摇,请换个浏览器试试!");
this.uninit = true;
return false;
}
var afterShake = options.afterShake;
if(typeof afterShake != "function"){
afterShake = function(){};
}
var threshold = options.threshold || 0;
var _this = this;
var x=null, y=null, z=null,_x=null,_z=null,_y=null;
var duration = 100;
var t=_t=(new Date()).getTime();
this.deviceMotionHandler = function(event){
t = (new Date()).getTime();
var diffTime = t - _t;
if(diffTime < duration){//取时间大于0.1秒的变化
return false;
}
var acceleration = event.accelerationIncludingGravity;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
if(_x!==null && _y!==null && _z!==null){
var xv = Math.abs( x - _x ) / diffTime * duration * duration,
yv = Math.abs( y - _y ) / diffTime * duration * duration,
zv = Math.abs( z - _z ) / diffTime * duration * duration;
if(xv > threshold && yv > threshold || yv > threshold && zv >threshold || xv > threshold && zv >threshold){
afterShake();
}
}
//保存上一次记录
_x = x;
_y = y;
_z = z;
_t = t;
}
},
start:function(){
if(this.uninit){
return false;
}
window.addEventListener("devicemotion", this.deviceMotionHandler, false);
},
stop:function(){
if(this.uninit){
return false;
}
window.removeEventListener("devicemotion", this.deviceMotionHandler, false);
}
};
window.Shake = Shake;
})();
HTML5摇一摇的更多相关文章
- HTML5实现摇一摇
一.原理: 利用devicemotion获取移动速度,得到device移动时相对之前某个时间的差值比 二.效果图: 三.源码: //先判断设备是否支持HTML5摇一摇功能 if (window.Dev ...
- html5实现摇一摇功能
原理:使用DeviceMotion实现,关于DeviceMotion介绍可以查看 https://developer.mozilla.org/en-US/docs/Web/Reference/Even ...
- html5实现微信摇一摇功能
在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...
- html5摇一摇[转]
写在前面 年底了,有些公司会出一个摇奖的活动,今天在家没事就搜了一下这方面的资料. 原文地址:http://www.cnblogs.com/waitingbar/p/4682215.html 测试 效 ...
- 用HTML5实现手机摇一摇的功能(转)
在百度开发者大会上我介绍过HTML5另外一个重要特性就是DeviceOrientation,它将底层的方向传感器和运动传感器进行了高级封装,提供了DOM事件的支持.这个特性包括两种事件: 1.devi ...
- Adobe Edge Animate --使用HTML5实现手机摇一摇功能
Adobe Edge Animate --使用HTML5实现手机摇一摇功能 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. HTML5的发展日新月异,其功能 ...
- js html5 仿微信摇一摇
看微信摇一摇之后忽然想知道他是怎么写的.于是就在网上查了一些资料,有些是借鉴别人的.大家共同学习啊 先放代码 <body onload="init()"> <p& ...
- HTML5实现“摇一摇”效果
在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...
- 利用HTML5+Socket.io实现摇一摇控制PC端歌曲切换
我比较喜欢听音乐,特别是周末的时候,电脑开着百度随心听fm,随机播放歌曲,躺在床上享受.但碰到了一个烦人的事情,想切掉不喜欢的曲子,还得起床去操作电脑换歌.于是思考能不能用手机控制电脑切换歌曲,经过一 ...
- 利用HTML5的devicemotion事件实现手机摇一摇抽奖,年会抽奖
摇一摇JS脚本逻辑:接下来是移动端JS脚本逻辑的实现,摇一摇的实现需借助html5新增的devicemotion事件,获取设备在位置和方向上的改变速度的相关信息,该事件的基本使用如下: if (win ...
随机推荐
- Java学习----Java数据类型
1.基本数据类型(8种) 数字类型: 整数: byte :-128~+127 short :-32768~+32767 int: -2147483648~+2147483637 long 小数类型: ...
- 小议window.event || ev
以前做项目时就遇到这个问题,但是太懒没有总结,今天来总结一下 onclick="alert(arguments.callee)"这句随便放在某个元素中,试试不同的浏览器会有弹出什么 ...
- [OSGI]Eclipse4.2 OSGI依赖Bundle
Eclipse 4.2 OSGI 依赖的Bundle: org.eclipse.osgiorg.apache.felix.gogo.runtimeorg.apache.felix.gogo.comma ...
- testservice小项目总结
关于自做小项目testservice的总结: 1.Activity与Service的绑定及之间的通信: 1)关于Activity和Service的生命周期的理解: 2)bindService方法中Se ...
- uva 725 Division(除法)暴力法!
uva 725 Division(除法) A - 暴力求解 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- JS之路——数组对象
String字符串对象 concat() 返回一个由两个数组合并组成的新数组 join() 返回一个由数组中的所有元素连接在一起的String对象 pop() 删除数组中最后一个元素 并返回该值 pu ...
- poj 2528Mayor's posters
http://poj.org/problem?id=2528 这个题有个细节,整个区间的长度为10000000,而n最大只有1000,所以我们要进行离散化. #include<cstdio> ...
- Qt一步一步实现插件调用(附源码)
最近手里几个项目都采用插件的方式进行开发工作,这里记录一下实现方法,给需要的同学一个参考, 在linux系统和window系统都能成功编译通过,不废话直接步骤 第一步:建立插件原型 新建一个Qt项目, ...
- Android的5个进程等级(转)
1.foreground process 正处于activity resume状态 正处于bound服务交互的状态 正处于服务在前台运行的状态(StartForeGround( ...
- Linux2.6内核--内存管理(1)--分页机制
在内核里分配内存可不像在其他地方分配内存那么容易.造成这种局面的因素很多.从根本上讲,是因为内核本身不能像用户空间那样奢侈的使用内存.内核与用户空间不同,它不具备这种能力,它不支持简单便捷 ...