forked from shrekshrek/orienter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorienter.min.js
More file actions
9 lines (8 loc) · 2.17 KB
/
Copy pathorienter.min.js
File metadata and controls
9 lines (8 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
/*!
* VERSION: 0.1.0
* DATE: 2015-12-20
* GIT:https://github.com/shrekshrek/orienter
*
* @author: Shrek.wang, shrekshrek@gmail.com
**/
!function(i){var t="object"==typeof self&&self.self==self&&self||"object"==typeof global&&global.global==global&&global;"function"==typeof define&&define.amd?define(["exports"],function(a){t.Orienter=i(t,a)}):"undefined"!=typeof exports?i(t,exports):t.Orienter=i(t,{})}(function(i,t){function a(i,t){for(var a in t)i[a]=t[a]}return t=function(){this.initialize.apply(this,arguments)},a(t.prototype,{lon:0,lat:0,direction:0,fix:0,os:"",initialize:function(){switch(this.lon=0,this.lat=0,this.direction=window.orientation||0,this.direction){case 0:this.fix=0;break;case 90:this.fix=-270;break;case-90:this.fix=-90}this.os=navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)?"ios":navigator.userAgent.indexOf("Android")>-1||navigator.userAgent.indexOf("Linux")?"android":""},init:function(){this._orient=this.orientHandler.bind(this),window.addEventListener("deviceorientation",this._orient,!1),this._change=this.changeHandler.bind(this),window.addEventListener("orientationchange",this._change,!1)},destroy:function(){window.removeEventListener("deviceorientation",this._orient,!1),window.removeEventListener("orientationchange",this._change,!1)},changeHandler:function(){this.direction=window.orientation},orientHandler:function(i){switch(this.os){case"ios":switch(this.direction){case 0:this.lon=i.alpha+i.gamma,i.beta>0&&(this.lat=i.beta-90);break;case 90:this.lon=i.gamma<0?i.alpha-90:i.alpha-270,this.lat=i.gamma>0?90-i.gamma:-90-i.gamma;break;case-90:this.lon=i.gamma<0?i.alpha-90:i.alpha-270,this.lat=i.gamma<0?90+i.gamma:-90+i.gamma}break;case"android":switch(this.direction){case 0:this.lon=i.alpha+i.gamma+30,this.lat=i.gamma>90?90-i.beta:i.beta-90;break;case 90:this.lon=i.alpha-230,this.lat=i.gamma>0?270-i.gamma:-90-i.gamma;break;case-90:this.lon=i.alpha-180,this.lat=-90+i.gamma}}this.lon+=this.fix,this.lon%=360,this.lon<0&&(this.lon+=360),this.lon=Math.round(this.lon),this.lat=Math.round(this.lat),this.handler&&this.handler.apply(this,[{a:Math.round(i.alpha),b:Math.round(i.beta),g:Math.round(i.gamma),lon:this.lon,lat:this.lat,dir:this.direction}])}}),t});