-
Notifications
You must be signed in to change notification settings - Fork 0
/
nimble.min.js
2 lines (2 loc) · 11.3 KB
/
nimble.min.js
1
2
/* nimble.min.js - https://github.com/CodeSmith32/nimblejs - version 1.2.15 - MIT licensed: https://opensource.org/licenses/MIT */
var nimble=new function(e){"use strict";var n,t,i,o,a,r=this,l=e.document,c=e.navigator,s=(a=c.userAgent,t="edge",(n=a.match(/(?:Edge\/((\d+)[\d\.]*))/))||(t="chrome",n=a.match(/Chrome\/((\d+)[\d\.]*)/))||(t="firefox",n=a.match(/Firefox\/((\d+)[\d\.]*)/))||(t="ie",n=a.match(/(?:MSIE |Trident.*?rv:?\s*)((\d+)[\d\.]*)/))||(t="opera",n=a.match(/(?:Opera |Opera\/.*?Version )((\d+)[\d\.]*)/))||(t="safari",n=a.match(/(?:Version\/((\d+)[\d\.]*))?.*Safari/))||(t="other",0)?(i=n[2],o=n[1]):(i=null,o=null),{browser:t,version:+i,fullversion:o,valueOf:function(){return t}}),f="requestAnimationFrame"in e?"":"msRequestAnimationFrame"in e?"ms":"mozRequestAnimationFrame"in e?"moz":"webkitRequestAnimationFrame"in e?"webkit":"oRequestAnimationFrame"in e?"o":"",u=f+(f?"R":"r")+"equestAnimationFrame",d=f+(f?"C":"c")+"ancelAnimationFrame",v="pointerLockElement"in l?"":"msPointerLockElement"in l?"ms":"mozPointerLockElement"in l?"moz":"webkitPointerLockElement"in l?"webkit":"",h=v+(v?"R":"r")+"equestPointerLock",m=v+(v?"E":"e")+"xitPointerLock",p=v+(v?"P":"p")+"ointerLockElement",g=v+"pointerlockchange",x=v+"pointerlockerror";function y(n){return n===e?{left:0,top:0}:n.getBoundingClientRect()}function b(e,n){var t={"*":[]},i={};!function(){n=n.split(" ");for(var e=0;e<n.length;e++)t[n[e]]=[]}(),e.hook=e.on=function(n,i){if("function"==typeof i){n=n.split(" ");for(var o=0;o<n.length;o++)n[o]in t&&t[n[o]].push(i)}return e},e.unhook=e.off=function(n,o){if("function"==typeof o){n=n.split(" ");for(var a=0;a<n.length;a++)if(n[a]in t){var r,l=t[n[a]].indexOf(o);~l&&(t[n[a]].splice(l,1),(r=i[n[a]])&&l<=r.i&&(r.i--,r.l--))}}return e},e.unhookAll=e.offAll=function(n){n=n.split(" ");for(var o=0;o<n.length;o++)if(n[o]in t){t[n[o]]=[];var a=i[n[o]];a&&(a.i=a.l=0)}return e};var o={hooks:t,run:function(n){if(!n||!("event"in n))throw"Doing it wrong: Hook event object is missing 'event'";if(n.event in i)throw"Running hook calls itself recursively: "+n.event;var a;for((a=i[n.event]={l:t[n.event].length,i:0}).i=0;a.i<a.l;a.i++)t[n.event][a.i].call(e,n);for(delete i[n.event],(a=i["*"]={l:t["*"].length,i:0}).i=0;a.i<a.l;a.i++)t["*"][a.i].call(e,n);return delete i["*"],o},extendHooks:function(e){e=e.split(" ");for(var n=0;n<e.length;n++)e[n]in t||(t[e[n]]=[]);return o}};return o}function w(e,n,t){var i=!1;e.enable=function(){if(!i){var e;for(var o in t){e=o.split(" ");for(var a=0;a<e.length;a++)n.addEventListener(e[a],t[o],{passive:!1})}i=!0}},e.disable=function(){if(i){var e;for(var o in t){e=o.split(" ");for(var a=0;a<e.length;a++)n.removeEventListener(e[a],t[o])}i=!1}},e.isenabled=function(){return i},r.enableByDefault&&e.enable()}r.enableByDefault=!0;var z=function(n){var t=this;if(!(t instanceof z))throw"Bad instantiation of object nimble.Canvas";n=n||e;var i,o,a=null,r="2d",l=null;function c(e){a&&a.getContext&&(t.width=i=a.width=n.innerWidth||n.offsetWidth,t.height=o=a.height=n.innerHeight||n.offsetHeight,l=a.getContext(r),t.antialias||(l.imageSmoothingEnabled=!1),t.context=l,s({event:"resize",context:l,width:i,height:o,original:e}),t.redraw())}t.context=null,t.width=0,t.height=0,t.antialias=!0,w(t,e,{resize:c}),t.update=function(){c(null)},t.canvas=function(e,n){if(void 0===e)return a;a=e,r=3==n?"webgl":"2d",c()},t.stepclear=function(){t.clear()},t.clear=function(e){if(e||(e=t.back),"webgl"==r){if(e){var n,a,c;n=(e&=16777215)>>16,a=(e&=65535)>>8,c=255&e,l.clearColor(n,a,c,1)}else l.clearColor(0,0,0,1);l.clear(l.COLOR_BUFFER_BIT)}else e?(l.fillStyle=e,l.fillRect(-1,-1,i+2,o+2)):l.clearRect(-1,-1,i+2,o+2)},t.redraw=function(){s({event:"draw",context:l,width:i,height:o})},t.back=null;var s=b(t,"draw resize").run},k=function(n){var t=this;if(!(t instanceof k))throw"Bad instantiation of object nimble.Steps";n=n||e;var i=null,o=null,a=!1,l=null;function c(n){if(a){var t=+l===l?n-l:16.67;l=n,o=1e3/t,s({event:"step",delta:t}),a&&(i=e[u](c))}}t.enable=t.start=function(){null===i&&(i=e[u](c),a=!0)},t.disable=t.stop=function(){null!==i&&(e[d](i),i=null,a=!1,l=o=null)},t.running=t.isenabled=function(){return a},t.fps=function(){return o};var s=b(t,"step").run;r.enableByDefault&&t.enable()},Y=function(n){var t=this;if(!(t instanceof Y))throw"Bad instantiation of object nimble.Keyboard";n=n||e;var i=function(){for(var e={backspace:8,tab:9,numclear:12,enter:13,shift:16,control:17,alt:18,space:32,pageup:33,pagedown:34,end:35,home:36,left:37,up:38,right:39,down:40,pausebreak:19,capslock:20,escape:27,printscreen:44,insert:45,del:46,windows:91,numpad0:96,numpad1:97,numpad2:98,numpad3:99,numpad4:100,numpad5:101,numpad6:102,numpad7:103,numpad8:104,numpad9:105,multiply:106,add:107,subtract:109,decimal:110,divide:111,f1:112,f2:113,f3:114,f4:115,f5:116,f6:117,f7:118,f8:119,f9:120,f10:121,f11:122,f12:123,numlock:144,scrolllock:145,semicolon:186,equals:187,comma:188,period:190,minus:189,slash:191,accent:192,lbracket:219,rbracket:221,quote:222,backslash:220},n="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",t=0;t<n.length;t++)e[n[t]]=n.charCodeAt(t);return e}(),o={};for(var a in i)t[a]=!1,o[i[a]]=a;var r={colon:186,plus:187,lessthan:188,greaterthan:190,underscore:189,question:191,atilda:192,verticalbar:220};for(var l in r)i[l]=r[l];t.codes=Object.create(i),t.icodes=Object.create(o),t.pressed={},t.released={},w(t,n,{keydown:function(e){var n=o[e.which];t[n]||(t[n]=!0,t.steps&&(t.pressed[n]=!0),c({event:"down",code:e.which,key:n,original:e}))},keyup:function(e){var n=o[e.which];t[n]=!1,t.steps&&(t.released[n]=!0),c({event:"up",code:e.which,key:n,original:e})}}),t.stepclear=function(){t.pressed={},t.released={}},t.steps=!1;var c=b(t,"down up").run},M=function(n){var t=this;if(!(t instanceof M))throw"Bad instantiation of object nimble.Mouse";var i=h in(n=n||e)?n:l.documentElement,o={left:1,middle:2,right:3},a={};for(var r in o)t[r]=!1,a[o[r]]=r;t.codes=Object.create(o),t.icodes=Object.create(a),t.pressed={},t.released={},t.wheeldelta={x:0,y:0,d:0},t.x=-1e4,t.y=-1e4,t.xdelta=0,t.ydelta=0,t.xwheel=0,t.ywheel=0,t.wheel=0;var c=!1,f=!1;t.pointerlock=function(e){if(!arguments.length)return c;!(c=!!e)&&f&&t.exitpointerlock()},t.ispointerlocked=function(){return f},t.exitpointerlock=function(){f&&i[m]()};var u,d={},v={};t.enable=function(){d.enable(),v.enable()},t.disable=function(){d.disable(),v.disable()},t.isenabled=function(){return d.isenabled()},(u={})[g]=function(e){f=l[p]==i,z({event:"pointerlock",x:t.x,y:t.y,pointerlocked:f,failed:!1,original:e})},u[x]=function(e){console.log("failed to obtain pointerlock"),z({event:"pointerlock",x:t.x,y:t.y,pointerlocked:f,failed:!0,original:e})},w(d,l,u),w(v,n,{mousedown:function(e){c&&!f&&i[h]();var o=a[e.which];t[o]=!0,t.steps&&(t.pressed[o]=!0);var r=y(n);t.x=e.clientX-r.left,t.y=e.clientY-r.top,z({event:"down",x:t.x,y:t.y,code:e.which,button:o,pointerlocked:f,original:e})},mouseup:function(e){var i=a[e.which];t[i]=!1,t.steps&&(t.released[i]=!0);var o=y(n);t.x=e.clientX-o.left,t.y=e.clientY-o.top,z({event:"up",x:t.x,y:t.y,code:e.which,button:i,pointerlocked:f,original:e})},mousemove:function(e){var i=y(n),o=e.clientX-i.left,a=e.clientY-i.top,r=+e.movementX===e.movementX?e.movementX:+e.mozMovementX===e.mozMovementX?e.mozMovementX:+e.webkitMovementX===e.webkitMovementX?e.webkitMovementX:o-t.x,l=+e.movementY===e.movementY?e.movementY:+e.mozMovementY===e.mozMovementY?e.mozMovementY:+e.webkitMovementY===e.webkitMovementY?e.webkitMovementY:a-t.y;t.xdelta+=r,t.ydelta+=l,t.x=o,t.y=a,z({event:"move",x:t.x,y:t.y,xdelta:r,ydelta:l,pointerlocked:f,original:e})},wheel:function(e){var n,i,o;"firefox"==s?(n=24*-e.deltaX,i=24*-e.deltaY,o=24*-e.deltaY):(n=e.wheelDeltaX,i=e.wheelDeltaY,o=e.wheelDelta),t.xwheel+=n,t.ywheel+=i,t.wheel+=o,t.wheeldelta.x+=n,t.wheeldelta.y+=i,t.wheeldelta.d+=o,z({event:"wheel",x:t.x,y:t.y,wheel:o,xwheel:n,ywheel:i,pointerlocked:f,original:e})}}),t.stepclear=function(){t.pressed={},t.released={},t.wheeldelta={x:0,y:0,d:0},t.xdelta=t.ydelta=0},t.steps=!1;var z=b(t,"wheel down up move pointerlock").run},X=function(n){var t=this;if(!(t instanceof X))throw"Bad instantiation of object nimble.Touch";function i(e,n,i){var o=this;t.fingers[i]=o,t.steps&&t.pressed.push(o),o.x=e,o.y=n,o.identifier=i,o.ended=!1,o._move=function(e,n,t){o.x=e,o.y=n,a({event:"move",x:o.x,y:o.y,original:t})},o._end=function(e,n,r,l){+e===e&&(o.x=e,o.y=n),delete t.fingers[i],o.ended=!0,t.steps&&t.released.push(o),a({event:"end",x:o.x,y:o.y,canceled:r,original:l})};var a=b(o,"move end").run}n=n||e,t.fingers={},t.pressed=[],t.released=[],w(t,n,{touchstart:function(e){e.preventDefault();for(var t=y(n),a=t.left,r=t.top,l=e.changedTouches,c=[],s=l.length,f=0;f<s;f++)c.push(new i(l[f].clientX-a,l[f].clientY-r,l[f].identifier));o({event:"start",fingers:c,original:e})},touchmove:function(e){e.preventDefault();for(var i=y(n),a=i.left,r=i.top,l=e.changedTouches,c=[],s=l.length,f=0;f<s;f++)l[f].identifier in t.fingers&&(c.push(t.fingers[l[f].identifier]),t.fingers[l[f].identifier]._move(l[f].clientX-a,l[f].clientY-r,e));o({event:"move",fingers:c,original:e})},touchend:function(e){e.preventDefault();for(var i=y(n),a=i.left,r=i.top,l=e.changedTouches,c=[],s=l.length,f=0;f<s;f++)l[f].identifier in t.fingers&&(c.push(t.fingers[l[f].identifier]),t.fingers[l[f].identifier]._end(l[f].clientX-a,l[f].clientY-r,!1,e));o({event:"end",fingers:c,canceled:!1,original:e})},touchcancel:function(e){e.preventDefault();for(var i=y(n),a=i.left,r=i.top,l=e.changedTouches,c=[],s=l.length,f=0;f<s;f++)l[f].identifier in t.fingers&&(c.push(t.fingers[l[f].identifier]),t.fingers[l[f].identifier]._end(l[f].clientX-a,l[f].clientY-r,!0,e));o({event:"end",fingers:c,canceled:!0,original:e})}}),t.clear=function(){for(var e in t.fingers)t.fingers[e]._end(null,null,2,null)},t.stepclear=function(){t.pressed=[],t.released=[]},t.steps=!1;var o=b(t,"start move end").run},E=function(n){var t=this;if(!(t instanceof E))throw"Bad instantiation of object nimble.Orientation";n=n||e,t.alpha=0,t.beta=0,t.gamma=0,t.xgrav=0,t.ygrav=0,t.zgrav=0,t.xacc=0,t.yacc=0,t.zacc=0,t.xorient={x:0,y:0,z:0},t.yorient={x:0,y:0,z:0},t.zorient={x:0,y:0,z:0},t.xreal={x:0,y:0,z:0},t.yreal={x:0,y:0,z:0},t.zreal={x:0,y:0,z:0};var i=Math.PI/180,o=Math.cos,a=Math.sin;w(t,n,{deviceorientation:function(e){t.alpha=e.alpha,t.beta=e.beta,t.gamma=e.gamma;var n=t.alpha*i,l=t.beta*i,c=t.gamma*i,s=o(n),f=a(n),u=o(l),d=a(l),v=o(c),h=a(c);t.xorient.x=v*u,t.yorient.x=v*d*f-s*h,t.zorient.x=h*f+v*s*d,t.xorient.y=u*h,t.yorient.y=v*s+h*d*f,t.zorient.y=s*h*d-v*f,t.xorient.z=-d,t.yorient.z=u*f,t.zorient.z=u*s,f=-f,d=-d,h=-h,t.xreal.x=u*v,t.yreal.x=-u*h,t.zreal.x=d,t.xreal.y=s*h+v*f*d,t.yreal.y=s*v-f*d*h,t.zreal.y=-u*f,t.xreal.z=f*h-s*v*d,t.yreal.z=v*f+s*d*h,t.zreal.z=s*u,r({event:"rotate",alpha:t.alpha,beta:t.beta,gamma:t.gamma,original:e})},devicemotion:function(e){var n,i;n=e.accelerationIncludingGravity||{},null===(i=e.acceleration||{}).x&&null===n.x||(null===n.x&&(n={x:i.x+9.81*t.zreal.x,y:i.y+9.81*t.zreal.y,z:i.z+9.81*t.zreal.z}),null===i.x&&(i={x:n.x-9.81*t.zreal.x,y:n.y-9.81*t.zreal.y,z:n.z-9.81*t.zreal.z}),t.xgrav=n.x,t.ygrav=n.y,t.zgrav=n.z,t.xacc=i.x,t.yacc=i.y,t.zacc=i.z,r({event:"move",xacc:t.xacc,yacc:t.yacc,zacc:t.zacc,xgrav:t.xgrav,ygrav:t.ygrav,zgrav:t.zgrav,original:e}))}}),t.stepclear=function(){},t.steps=!1;var r=b(t,"rotate move").run};r.Canvas=z,r.Steps=k,r.Keyboard=Y,r.Mouse=M,r.Touch=X,r.Orientation=E}("undefined"!=typeof window?window:"undefined"!=typeof globalThis?globalThis:this);"object"==typeof module&&module&&module.exports&&(module.exports=nimble);