forked from soulwire/sketch.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.min.js
2 lines (2 loc) · 4.25 KB
/
sketch.min.js
1
2
/* Copyright (C) 2013 Justin Windle, http://soulwire.co.uk */
var Sketch=function(){"use strict";function e(e){return"[object Array]"==Object.prototype.toString.call(e)}function t(e){return"function"==typeof e}function n(e){return"number"==typeof e}function o(e){return"string"==typeof e}function r(e){return E[e]||String.fromCharCode(e)}function i(e,t,n){for(var o in t)!n&&o in e||(e[o]=t[o]);return e}function u(e,t){return function(){e.apply(t,arguments)}}function a(e){var n={};for(var o in e)n[o]=t(e[o])?u(e[o],e):e[o];return n}function c(e){function n(n){t(n)&&n.apply(e,[].splice.call(arguments,1))}function u(e){for(_=0;_<et.length;_++)B=et[_],o(B)?R[(e?"add":"remove")+"EventListener"].call(R,B,k,!1):t(B)?k=B:R=B}function c(){L(T),T=I(c),K||(n(e.setup),K=t(e.setup)),Y||(n(e.resize),Y=t(e.resize)),e.running&&!U&&(e.dt=(z=+new Date)-e.now,e.millis+=e.dt,e.now=z,n(e.update),e.autoclear&&Z&&e.clear(),n(e.draw)),U=++U%e.interval}function l(){R=J?e.style:e.canvas,D=J?"px":"",j=e.width,M=e.height,e.fullscreen&&(M=e.height=x.innerHeight,j=e.width=x.innerWidth),e.retina&&Z&&V&&(R.style.height=M+"px",R.style.width=j+"px",j*=V,M*=V,e.scale(V,V)),R.height!==M&&(R.height=M+D),R.width!==j&&(R.width=j+D),K&&n(e.resize)}function s(e,t){return N=t.getBoundingClientRect(),e.x=e.pageX-N.left-x.scrollX,e.y=e.pageY-N.top-x.scrollY,e}function f(t,n){return s(t,e.element),n=n||{},n.ox=n.x||t.x,n.oy=n.y||t.y,n.x=t.x,n.y=t.y,n.dx=n.x-n.ox,n.dy=n.y-n.oy,n}function g(e){if(e.preventDefault(),G=a(e),G.originalEvent=e,G.touches)for(X.length=G.touches.length,_=0;_<G.touches.length;_++)X[_]=f(G.touches[_],X[_]);else X.length=0,X[0]=f(G,$);return i($,X[0],!0),G}function d(t){for(t=g(t),q=(Q=et.indexOf(W=t.type))-1,e.dragging=/down|start/.test(W)?!0:/up|end/.test(W)?!1:e.dragging;q;)o(et[q])?n(e[et[q--]],t):o(et[Q])?n(e[et[Q++]],t):q=0}function h(t){F=t.keyCode,H="keyup"==t.type,tt[F]=tt[r(F)]=!H,n(e[t.type],t)}function v(t){e.autopause&&("blur"==t.type?b:C)(),n(e[t.type],t)}function C(){e.now=+new Date,e.running=!0}function b(){e.running=!1}function P(){(e.running?b:C)()}function A(){Z&&e.clearRect(0,0,e.width,e.height)}function S(){O=e.element.parentNode,_=w.indexOf(e),O&&O.removeChild(e.element),~_&&w.splice(_,1),u(!1),b()}var T,k,R,O,N,_,D,z,B,G,W,F,H,q,Q,j,M,U=0,X=[],Y=!1,K=!1,V=x.devicePixelRatio,J=e.type==m,Z=e.type==p,$={x:0,y:0,ox:0,oy:0,dx:0,dy:0},et=[e.element,d,"mousedown","touchstart",d,"mousemove","touchmove",d,"mouseup","touchend",d,"click",y,h,"keydown","keyup",x,v,"focus","blur",l,"resize"],tt={};for(F in E)tt[E[F]]=!1;return i(e,{touches:X,mouse:$,keys:tt,dragging:!1,running:!1,millis:0,now:0/0,dt:0/0,destroy:S,toggle:P,clear:A,start:C,stop:b}),w.push(e),e.autostart&&C(),u(!0),l(),c(),e}for(var l,s,f="E LN10 LN2 LOG2E LOG10E PI SQRT1_2 SQRT2 abs acos asin atan ceil cos exp floor log round sin sqrt tan atan2 pow max min".split(" "),g="__hasSketch",d=Math,p="canvas",h="webgl",m="dom",y=document,x=window,w=[],v={fullscreen:!0,autostart:!0,autoclear:!0,autopause:!0,container:y.body,interval:1,globals:!0,retina:!1,type:p},E={8:"BACKSPACE",9:"TAB",13:"ENTER",16:"SHIFT",27:"ESCAPE",32:"SPACE",37:"LEFT",38:"UP",39:"RIGHT",40:"DOWN"},C={CANVAS:p,WEB_GL:h,WEBGL:h,DOM:m,instances:w,install:function(t){if(!t[g]){for(var o=0;o<f.length;o++)t[f[o]]=d[f[o]];i(t,{TWO_PI:2*d.PI,HALF_PI:d.PI/2,QUATER_PI:d.PI/4,random:function(t,o){return e(t)?t[~~(d.random()*t.length)]:(n(o)||(o=t||1,t=0),t+d.random()*(o-t))},lerp:function(e,t,n){return e+n*(t-e)},map:function(e,t,n,o,r){return(e-t)/(n-t)*(r-o)+o}}),t[g]=!0}},create:function(e){return e=i(e||{},v),e.globals&&C.install(self),l=e.element=e.element||y.createElement(e.type===m?"div":"canvas"),s=e.context=e.context||function(){switch(e.type){case p:return l.getContext("2d",e);case h:return l.getContext("webgl",e)||l.getContext("experimental-webgl",e);case m:return l.canvas=l}}(),(e.container||y.body).appendChild(l),C.augment(s,e)},augment:function(e,t){return t=i(t||{},v),t.element=e.canvas||e,t.element.className+=" sketch",i(e,t,!0),c(e)}},b=["ms","moz","webkit","o"],P=self,A=0,S="AnimationFrame",T="request"+S,k="cancel"+S,I=P[T],L=P[k],R=0;R<b.length&&!I;R++)I=P[b[R]+"Request"+S],L=P[b[R]+"Cancel"+S];return P[T]=I=I||function(e){var t=+new Date,n=d.max(0,16-(t-A)),o=setTimeout(function(){e(t+n)},n);return A=t+n,o},P[k]=L=L||function(e){clearTimeout(e)},C}();