@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
1 lines • 4.97 kB
JavaScript
!function(){"use strict";var t=Tiny.Vec3,e=Tiny.Vec2;const a=-1,i=0,n=1,s=2,o=3,h=new t,r=new e,u=new e;Tiny.OrbitControls=function(c,{element:d=document,enabled:p=!0,target:l=new t,ease:m=.25,inertia:v=.85,enableRotate:g=!0,rotateSpeed:f=.1,autoRotate:y=!1,autoRotateSpeed:M=1,enableZoom:w=!0,zoomSpeed:b=1,zoomStyle:x="dolly",enablePan:E=!0,panSpeed:L=.1,minPolarAngle:Y=0,maxPolarAngle:X=Math.PI,minAzimuthAngle:P=-1/0,maxAzimuthAngle:k=1/0,minDistance:A=0,maxDistance:D=1/0}={}){this.enabled=p,this.target=l,this.zoomStyle=x,m=m||1,v=v||0,this.minDistance=A,this.maxDistance=D;const z={radius:1,phi:0,theta:0},S={radius:1,phi:0,theta:0},I={radius:1,phi:0,theta:0},O=new t,B=new t;B.copy(c.position).subVectors(B,this.target),I.radius=S.radius=B.length(),I.theta=S.theta=Math.atan2(B.x,B.z),I.phi=S.phi=Math.acos(Math.min(Math.max(B.y/S.radius,-1),1)),this.offset=B,this.update=()=>{y&&function(){const t=2*Math.PI/60/60*M;z.theta-=t}(),S.radius*=z.radius,S.theta+=z.theta,S.phi+=z.phi,S.theta=Math.max(P,Math.min(k,S.theta)),S.phi=Math.max(Y,Math.min(X,S.phi)),S.radius=Math.max(this.minDistance,Math.min(this.maxDistance,S.radius)),I.phi+=(S.phi-I.phi)*m,I.theta+=(S.theta-I.theta)*m,I.radius+=(S.radius-I.radius)*m,this.target.add(O);let t=I.radius*Math.sin(Math.max(1e-6,I.phi));B.x=t*Math.sin(I.theta),B.y=I.radius*Math.cos(I.phi),B.z=t*Math.cos(I.theta),c.position.copy(this.target).add(B),c.lookAt(this.target),c.updateProjectionMatrix(),z.theta*=v,z.phi*=v,O.multiplyScalar(v),z.radius=1},this.forcePosition=()=>{B.copy(c.position).sub(this.target),I.radius=S.radius=B.distance(),I.theta=S.theta=Math.atan2(B.x,B.z),I.phi=S.phi=Math.acos(Math.min(Math.max(B.y/S.radius,-1),1)),c.lookAt(this.target)};const V=new e,R=new e,T=new e;let H=a;function Z(){return Math.pow(.95,b)}this.mouseButtons={ORBIT:0,ZOOM:1,PAN:2};const q=(t,e)=>{let a=d===document?document.body:d;h.copy(c.position).sub(this.target);let i=h.length();var n,s;i*=Math.tan((c.fov||45)/2*Math.PI/180),n=2*t*i/a.clientHeight,s=c.matrix,h.set(s[0],s[1],s[2]),h.multiply(-n),O.add(h),function(t,e){h.set(e[4],e[5],e[6]),h.multiply(t),O.add(h)}(2*e*i/a.clientHeight,c.matrix)},N=t=>{'dolly'===this.zoomStyle?z.radius/=t:(c.fov/=t,'orthographic'===c.type?c.orthographic():c.perspective())};function j(t,e){r.set(t,e),u.subVectors(r,V).multiplyScalar(f);let a=d===document?document.body:d;z.theta-=2*Math.PI*u.x/a.clientHeight,z.phi-=2*Math.PI*u.y/a.clientHeight,V.copy(r)}function C(t,e){r.set(t,e),u.subVectors(r,R).multiply(L),q(u.x,u.y),R.copy(r)}const F=t=>{if(this.enabled){switch(t.button){case this.mouseButtons.ORBIT:if(!1===g)return;V.set(t.clientX,t.clientY),H=i;break;case this.mouseButtons.ZOOM:if(!1===w)return;T.set(t.clientX,t.clientY),H=n;break;case this.mouseButtons.PAN:if(!1===E)return;R.set(t.clientX,t.clientY),H=s}H!==a&&(window.addEventListener('mousemove',G,!1),window.addEventListener('mouseup',J,!1))}},G=t=>{if(this.enabled)switch(H){case i:if(!1===g)return;j(t.clientX,t.clientY);break;case n:if(!1===w)return;!function(t){r.set(t.clientX,t.clientY),u.subVectors(r,T),u.y>0?N(Z()):u.y<0&&N(1/Z()),T.copy(r)}(t);break;case s:if(!1===E)return;C(t.clientX,t.clientY)}},J=()=>{window.removeEventListener('mousemove',G,!1),window.removeEventListener('mouseup',J,!1),H=a},K=t=>{this.enabled&&w&&(H===a||H===i)&&(t.stopPropagation(),t.preventDefault(),t.deltaY<0?N(1/Z()):t.deltaY>0&&N(Z()))},Q=t=>{if(this.enabled)switch(t.preventDefault(),t.touches.length){case 1:if(!1===g)return;V.set(t.touches[0].pageX,t.touches[0].pageY),H=i;break;case 2:if(!1===w&&!1===E)return;!function(t){if(w){let e=t.touches[0].pageX-t.touches[1].pageX,a=t.touches[0].pageY-t.touches[1].pageY,i=Math.sqrt(e*e+a*a);T.set(0,i)}if(E){let e=.5*(t.touches[0].pageX+t.touches[1].pageX),a=.5*(t.touches[0].pageY+t.touches[1].pageY);R.set(e,a)}}(t),H=o;break;default:H=a}},U=t=>{if(this.enabled)switch(t.preventDefault(),t.stopPropagation(),t.touches.length){case 1:if(!1===g)return;j(t.touches[0].pageX,t.touches[0].pageY);break;case 2:if(!1===w&&!1===E)return;!function(t){if(w){let e=t.touches[0].pageX-t.touches[1].pageX,a=t.touches[0].pageY-t.touches[1].pageY,i=Math.sqrt(e*e+a*a);r.set(0,i),u.set(0,Math.pow(r.y/T.y,b)),N(u.y),T.copy(r)}E&&C(.5*(t.touches[0].pageX+t.touches[1].pageX),.5*(t.touches[0].pageY+t.touches[1].pageY))}(t);break;default:H=a}},W=()=>{this.enabled&&(H=a)},$=t=>{this.enabled&&t.preventDefault()};this.remove=function(){d.removeEventListener('contextmenu',$),d.removeEventListener('mousedown',F),d.removeEventListener('wheel',K),d.removeEventListener('touchstart',Q),d.removeEventListener('touchend',W),d.removeEventListener('touchmove',U),window.removeEventListener('mousemove',G),window.removeEventListener('mouseup',J)},d.addEventListener('contextmenu',$,!1),d.addEventListener('mousedown',F,!1),d.addEventListener('wheel',K,{passive:!1}),d.addEventListener('touchstart',Q,{passive:!1}),d.addEventListener('touchend',W,!1),d.addEventListener('touchmove',U,{passive:!1})}}();