-
Notifications
You must be signed in to change notification settings - Fork 5
/
krpanogyro.min.js
14 lines (11 loc) · 3.3 KB
/
krpanogyro.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
krpano gyroscope script for iOS 4.2
by Aldo Hoeben / fieldofview.com
contributions by Sjeiti / ronvalstar.nl
https://github.com/fieldOfView/krpano_gyro
This software can be used free of charge and the source code is available under a Creative Commons Attribution license:
http://creativecommons.org/licenses/by/3.0/
*/
if(!this.krpanoGyro){var krpanoGyro=function(r){var g=!!window.DeviceOrientationEvent,o,c,p=false,f=false,b=0.5;isTouching=false,hoffset=0,voffset=0,hlookat=0,vlookat=0,degRad=Math.PI/180;if(r){c=(typeof(r)=="object")?r.pid:r}else{c="krpanoSWFObject"}if(g){n()}function n(){o=document.getElementById(c);if(o&&o.get!=undefined){q()}else{setTimeout(n,100)}}function q(){var s={deviceavailable:true,enabled:p,enable:i,disable:d,toggle:m,easing:b,adaptivev:f,setadaptivev:k};s.__defineSetter__("enabled",function(t){if(Boolean(t)){i()}else{d()}});s.__defineGetter__("enabled",function(t){return p});s.__defineSetter__("adaptivev",function(t){k(t)});s.__defineGetter__("adaptivev",function(t){return f});s.__defineSetter__("easing",function(t){b=Math.max(Math.min(t,1),0)});s.__defineGetter__("easing",function(t){return b});o.set("gyro",s);hoffset=o.get("view.hlookat");voffset=o.get("view.vlookat");i()}function i(){if(g&&!p){window.addEventListener("deviceorientation",e,true);o.addEventListener("touchstart",h,true);o.addEventListener("touchend",j,true);o.addEventListener("touchcancel",j,true);p=true}return p}function d(){if(g&&p){window.removeEventListener("deviceorientation",e);o.removeEventListener("touchstart",h);o.removeEventListener("touchend",j);o.removeEventListener("touchcancel",j);p=false}return p}function m(){if(p){return d()}else{return i()}}function k(s){if(s==undefined||s===null||s==""){f=!f}else{f=Boolean(s)}}function h(s){isTouching=true}function j(s){isTouching=false}function e(v){if(!isTouching&&p){var s=a(new Object({yaw:v.alpha*degRad,pitch:v.beta*degRad,roll:v.gamma*degRad})),y=s.yaw/degRad,x=s.pitch/degRad,u=y,t,z=o.get("view.hlookat"),w=o.get("view.vlookat");if(Math.abs(x)>70){u=v.alpha;switch(window.orientation){case 0:if(x>0){u+=180}break;case 90:u+=90;break;case -90:u+=-90;break;case 180:if(x<0){u+=180}break}u=u%360;if(Math.abs(u-y)>180){u+=(u<y)?360:-360}t=Math.min(1,(Math.abs(x)-70)/10);y=y*(1-t)+u*t}hoffset+=z-hlookat;voffset+=w-vlookat;if(Math.abs(x+voffset)>90){voffset=(x+voffset>0)?(90-x):(-90-x)}hlookat=(-y-180+hoffset)%360;vlookat=Math.max(Math.min((x+voffset),90),-90);if(Math.abs(hlookat-z)>180){z+=(hlookat>z)?360:-360}hlookat=(1-b)*hlookat+b*z;vlookat=(1-b)*vlookat+b*w;o.call("lookat("+hlookat+","+vlookat+")");l()}}function l(){if(voffset!=0&&f){voffset*=0.98;if(Math.abs(voffset)<0.1){voffset=0}}}function a(u){var B,y,t,s=Math.cos(u.yaw),x=Math.sin(u.yaw),w=Math.cos(u.pitch),A=Math.sin(u.pitch),v=Math.cos(u.roll),z=Math.sin(u.roll);matrix=new Array(x*z-s*A*v,-s*w,s*A*z+x*v,w*v,-A,-w*z,x*A*v+s*z,x*w,-x*A*z+s*v);if(matrix[3]>0.9999){B=Math.atan2(matrix[2],matrix[8]);t=Math.PI/2;y=0}else{if(matrix[3]<-0.9999){B=Math.atan2(matrix[2],matrix[8]);t=-Math.PI/2;y=0}else{B=Math.atan2(-matrix[6],matrix[0]);y=Math.atan2(-matrix[5],matrix[4]);t=Math.asin(matrix[3])}}return new Object({yaw:B,pitch:t,roll:y})}return{toString:function(){return"[object krpanoGyro]"}}}};
// Comment out or edit the following line if you want to use a custom krpano object or a different object name
krpanoGyro();