-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorienter.min.js
7 lines (6 loc) · 2.07 KB
/
orienter.min.js
1
2
3
4
5
6
7
/*!
* VERSION: 0.1.0
* DATE: 2016-12-25
* @author: Chason
**/
!function(i){"function"==typeof define&&define.amd?define(["exports"],function(t){window.Orienter=i(t)}):"undefined"!=typeof exports?i(exports):window.Orienter=i({})}(function(i){return i=function(){this.initialize.apply(this,arguments)},i.prototype={lon:0,lat:0,direction:0,fix:0,os:"",initialize:function(i){var t=i||{};switch(this.onOrient=t.onOrient||function(){},this.onChange=t.onChange||function(){},this._orient=this._orient.bind(this),this._change=this._change.bind(this),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(){window.addEventListener("deviceorientation",this._orient,!1),window.addEventListener("orientationchange",this._change,!1)},destroy:function(){window.removeEventListener("deviceorientation",this._orient,!1),window.removeEventListener("orientationchange",this._change,!1)},_change:function(){this.direction=window.orientation,this.onChange(this.direction)},changeDirectionTo:function(i){this.direction=i},_orient: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.onOrient({a:Math.round(i.alpha),b:Math.round(i.beta),g:Math.round(i.gamma),lon:this.lon,lat:this.lat,dir:this.direction})}},i});