-
Notifications
You must be signed in to change notification settings - Fork 2
/
sphoords.min.js
1 lines (1 loc) · 2.51 KB
/
sphoords.min.js
1
var Sphoords=function(){var e=function(){var e=navigator.userAgent;return/Gecko\/[0-9.]+/.test(e)?"Gecko":/Chrome\/[0-9.]+/.test(e)?"Blink":/AppleWebKit\/[0-9.]+/.test(e)?"WebKit":/Trident\/[0-9.]+/.test(e)?"Trident":/Opera\/[0-9.]+/.test(e)?"Presto":"Gecko"},t=function(e){return e-360*Math.floor(e/360)};this.start=function(){return Sphoords.isDeviceOrientationSupported?(window.addEventListener("deviceorientation",n,!1),a=!0,!0):(console.log("Device Orientation API not supported"),!1)},this.stop=function(){a&&(window.removeEventListener("deviceorientation",n,!1),a=!1)},this.toggle=function(){a?this.stop():this.start()},this.isEventAttached=function(){return a};var n=function(e){u=Sphoords.getScreenOrientation();var n=0,a=0;switch(u){case"portrait-primary":n=e.alpha+e.gamma,a=e.beta-90;break;case"landscape-primary":if(n=e.alpha+e.beta-90,a=-e.gamma-90,Math.abs(e.beta)>90)switch(p){case"Blink":a+=180;break;case"Gecko":default:a=-a}"WebKit"===p&&window.orientation&&(0>a&&(a=-1*(a+180)),n>=180?n-=180:n+=180);break;case"landscape-secondary":if(n=e.alpha-e.beta+90,a=e.gamma-90,Math.abs(e.beta)>90)switch(p){case"Blink":a+=180;break;case"Gecko":default:a=-a}"WebKit"===p&&window.orientation&&(0>a&&(a=-1*(a+180)),n>=180?n-=180:n+=180);break;case"portrait-secondary":n=e.alpha-e.gamma,a=180-(e.beta-90),a=270-e.beta}a=t(a),a>=180&&(a-=360),o=t(n),r=Math.max(-90,Math.min(90,a)),s=o*d,c=r*d,i()};this.getCoordinates=function(){return{longitude:s,latitude:c}},this.getCoordinatesInDegrees=function(){return{longitude:o,latitude:r}},this.getScreenOrientation=function(){return u},this.addListener=function(e){l.push(e)};var i=function(){if(l.length)for(var e=0,t=l.length;t>e;++e)l[e]({longitude:s,latitude:c})},a=!1,o=0,r=0,s=0,c=0,d=Math.PI/180,u=Sphoords.getScreenOrientation(),p=e(),l=[]};Sphoords.getScreenOrientation=function(){var e=null;if(screen.orientation)e=screen.orientation;else if(screen.mozOrientation)e=screen.mozOrientation;else if(screen.msOrientation)e=screen.msOrientation;else if(window.orientation||0===window.orientation)switch(window.orientation){case 0:e="portrait-primary";break;case 180:e="portrait-secondary";break;case-90:e="landscape-primary";break;case 90:e="landscape-secondary"}return null!==e&&"object"==typeof e?e.type:e},Sphoords.isDeviceOrientationSupported=!1,function(){function e(t){null!==t&&null!==t.alpha&&(Sphoords.isDeviceOrientationSupported=!0,window.removeEventListener("deviceorientation",e))}window.DeviceOrientationEvent&&null!==Sphoords.getScreenOrientation()&&window.addEventListener("deviceorientation",e)}();