UNPKG

clay-core

Version:

Provide a more friendly web-side drawing interface!

18 lines (16 loc) 19.9 kB
/*! * clay.js - Provide a more friendly web-side drawing interface! * git+https://github.com/yelloxing/clay-core.git * * author 心叶 * * version 2.1.3 * * build Sun Jul 29 2018 * * Copyright yelloxing * Released under the MIT license * * Date:Wed Feb 20 2019 09:05:09 GMT+0800 (GMT+08:00) */ !function(t,e){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e():t.clay=t.$$=e()}("undefined"!=typeof window?window:this,function(d){"use strict";var a=function(t,e){return new a.prototype.init(t,e)};a.prototype.init=function(t,e){this.context=e=e||document;var r,n=s(t,e);for(r=0;r<n.length;r++)this[r]=n[r];return this.selector=t,this.length=n.length,this.type="clay-object",this},a.prototype.init.prototype=a.prototype;var m,h={svg:"http://www.w3.org/2000/svg",xhtml:"http://www.w3.org/1999/xhtml",xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"},w="[\\x20\\t\\r\\n\\f]",x="(?:\\\\.|[\\w-]|[^\0-\\xa0])+",i=["href","title","show","type","role","actuate"],o=["webgl","experimental-webgl","webkit-3d","moz-webgl"],r={},u=CanvasRenderingContext2D,t=a.prototype,E="Unsupported selector!",F=function(t){return"number"==typeof t},b=function(t){return"function"==typeof t},y=function(t){return"string"==typeof t};function s(t,e){var r,n=[];if(y(t)){if(t=(t+"").trim().replace(/[\n\f\r]/g,""),/^</.test(t))return[T(t)];if(b(m))return m(t,e);if("*"===t)return e.getElementsByTagName("*");var i=w,o=x,a="\\["+i+"{0,}"+o+"(?:"+i+"{0,}="+i+"{0,}(\\'|\\\"){0,1}"+o+"\\1{0,1}){0,1}"+i+"{0,}\\]";if(new RegExp("^(?:"+o+"){0,1}(?:(?:#|\\.)"+o+"|"+a+"){0,}$").test(t)){var h,u=t.match(new RegExp("#"+o,"g")),s=t.match(new RegExp("\\."+o,"g")),c=t.match(new RegExp("^"+o)),f=t.match(new RegExp(a,"g"));if(u){if(1<u.length)return[];h=(h=document.getElementById((u.shift(0)+"").replace(/^#/,"")))?[h]:[]}else h=e.getElementsByClassName&&s?e.getElementsByClassName((s.shift(0)+"").replace(/^\./,"")):c?e.getElementsByTagName(c.shift(0)):e.getElementsByTagName("*");var l,d,g,p,v=new RegExp("^\\["+i+"{0,}("+o+")(?:"+i+"{0,}="+i+"{0,}(?:\\'|\\\"){0,1}("+o+")(?:\\'|\\\"){0,1}){0,1}"+i+"{0,}\\]$");for(r=0;r<h.length;r++)if(p=!0,!(c&&0<c.length&&(c[0]+"").toUpperCase()!==(h[r].tagName+"").toUpperCase())){for(l=" "+h[r].getAttribute("class")+" ",d=0;p&&s&&d<s.length;d++)if(l.search(" "+(s[d]+"").replace(/\./,"")+" ")<0){p=!1;break}for(d=0;p&&f&&d<f.length;d++)if(l=v.exec(f[d]),null===(g=h[r].getAttribute(l[1]))||l[2]&&g!=l[2]){p=!1;break}p&&n.push(h[r])}return n}throw new Error(E)}if(!t||1!==t.nodeType&&11!==t.nodeType&&9!==t.nodeType){if(!t||t.constructor!==Array&&t.constructor!==HTMLCollection&&t.constructor!==NodeList){if(t&&"clay-object"===t.type)return t;if(t)throw new Error(E);return[]}for(r=0;r<t.length;r++)!t[r]||1!==t[r].nodeType&&11!==t[r].nodeType&&9!==t[r].nodeType||n.push(t[r]);return n}return[t]}function T(t){var e=document.createElementNS(h.svg,"svg");new RegExp("^"+x+"$").test(t)&&(t="<"+t+"></"+t+">"),c(e,t);var r,n,i=e.childNodes;for(r=0;r<i.length;r++)if(1===i[r].nodeType||9===i[r].nodeType||11===i[r].nodeType){n=i[r];break}if(!n||"canvas"==n.tagName||/[A-Z]/.test(n.tagName))for((e=document.createElement("div")).innerHTML=t,i=e.childNodes,r=0;r<i.length;r++)if(1===i[r].nodeType||9===i[r].nodeType||11===i[r].nodeType){n=i[r];break}return n}r.$sizzleProvider=function(t){m=t},t.find=function(t){if(this.length<=0)return a();var e,r=a(),n=s(t,this[0]);for(r.selector=t,e=0;e<n.length;e++)r[e]=n[e],r.length+=1;return r},t.eq=function(t){return this.length<=t?new a:new a(this[t])},t.appendTo=function(t){var e,r,n=a(t);for(e=0;e<n.length;e++)for(r=0;r<this.length;r++)n[e].appendChild(this[r]);return this},t.remove=function(){var t;for(t=0;t<this.length;t++)this[t].parentNode.removeChild(this[t]);return this},t.refresh=function(){var t,e=s(this.selector,this.context),r=this.length;for(t=this.length=0;t<e.length;t++)this[t]=e[t],this.length+=1;for(;t<r;t++)delete this[t];return this},t.attr=function(t,e){if(null==e||e==d)return 0<this.length?this[0].getAttribute(t):d;var r,n;for(r=0;r<this.length;r++)n=b(e)?e(this[r]._data,r,this.eq(r)):e,/[A-Z]/.test(this[r].tagName)&&0<=i.indexOf(t)?this[r].setAttributeNS(h.xlink,"xlink:"+t,n):this[r].setAttribute(t,n);return this},t.css=function(t,e){if(arguments.length<=1&&"object"!=typeof t){if(this.length<1)return d;var r=document.defaultView&&document.defaultView.getComputedStyle?document.defaultView.getComputedStyle(this[0],null):this[0].currentStyle;return y(t)?r.getPropertyValue(t):r}var n,i;if(0<this.length)if("object"==typeof t)for(i in t)for(n=0;n<this.length;n++)this[n].style[i]=b(e)?e(this[n]._data,n,i,t[i]):t[i];else for(n=0;n<this.length;n++)this[n].style[t]=b(e)?e(this[n]._data,n):e;return this},t.size=function(t){var e,r;return"content"==(t=t||"border")?(r=this[0].clientWidth-(this.css("padding-left")+"").replace("px","")-(this.css("padding-right")+"").replace("px",""),e=this[0].clientHeight-(this.css("padding-top")+"").replace("px","")-(this.css("padding-bottom")+"").replace("px","")):"padding"==t?(r=this[0].clientWidth,e=this[0].clientHeight):"border"==t?(r=this[0].offsetWidth,e=this[0].offsetHeight):"scroll"==t&&(r=this[0].scrollWidth,e=this[0].scrollHeight),{width:r,height:e}},t.datum=function(t,e){if(null===t||t===d)return 0<this.length?this[0]._data:d;var r;for(r=0;r<this.length;r++)t=b(e)?e(t,r):t,this[r]._data=t;return this},t.data=function(t,e){var r,n=[];if(t){if(t.constructor!==Array){var i=[];for(r in t)i.push(t[r]);t=i}var o=a();for(o.selector=this.selector,r=0;r<t.length&&r<this.length;r++)this[r]._data=b(e)?e(t[r],r):t[r],o[r]=this[r],o.length+=1;for(o._enter=[];r<t.length;r++)o._enter.push(b(e)?e(t[r],r):t[r]);for(o._exit=[];r<this.length;r++)o._exit.push(this[r]);return o}for(r=0;r<this.length;r++)n[r]=this[r]._data;return n},t.enter=function(t){var e,r,n=a();for(n.selector=this.selector,e=0;this._enter&&e<this._enter.length;e++)(r=T(t))._data=this._enter[e],n[e]=r,n.length+=1;return delete this._enter,n},t.exit=function(){var t,e=a();for(e.selector=this.selector,t=0;this._exit&&t<this._exit.length;t++)e[t]=this._exit[t],e.length+=1;return delete this._exit,e},t.loop=function(t){var e;for(e=0;e<this.length;e++)t(this[e]._data,e,this.eq(e));return this},t.bind=function(t,e){var r;if(window.attachEvent)for(r=0;r<this.length;r++)this[r].attachEvent("on"+t,e);else for(r=0;r<this.length;r++)this[r].addEventListener(t,e,!1);return this},t.trigger=function(t){var e,r;if(document.createEventObject)for(r=document.createEventObject(),e=0;e<this.length;e++)this[e].fireEvent("on"+t,r);else for((r=document.createEvent("HTMLEvents")).initEvent(t,!0,!1),e=0;e<this.length;e++)this[e].dispatchEvent(r);return this},t.position=function(t){var e=this[0].getBoundingClientRect();return{x:t.clientX-e.left,y:t.clientY-e.top}};var c=function(t,e){if("innerHTML"in SVGElement.prototype==!1||"innerHTML"in SVGSVGElement.prototype==!1){var r=document.createElement("div");r.innerHTML=e;var o=function(t){var e,r=document.createElementNS(h.svg,(t.tagName+"").toLowerCase()),n=t.attributes,i=a(r);for(e=0;n&&e<n.length;e++)i.attr(n[e].nodeName,t.getAttribute(n[e].nodeName));return r},n=o(r.firstChild);!function t(e,r){var n=e.firstChild;if(n&&3==n.nodeType)r.textContent=e.innerText;else for(;n;){var i=o(n);r.appendChild(i),n.firstChild&&t(n,i),n=n.nextSibling}}(r.firstChild,n),t.appendChild(n)}else t.innerHTML=e},f={timers:[],interval:13,speeds:400,timerId:null};a.animation=function(e,t,r){f.timer(function(t){e(t)},t,r)},f.timer=function(t,e,r){if(!b(t))throw new Error("tick is required!");F(e),(e=e)<0&&(e=-e),f.timers.push({createTime:new Date,tick:t,duration:e,callback:r}),f.start()},f.start=function(){f.timerId||(f.timerId=setInterval(f.tick,f.interval))},f.tick=function(){var t,e,r,n,i,o,a,h=f.timers;for(f.timers=[],e=f.timers.length=0;e<h.length;e++)t=(i=h[e]).createTime,r=i.tick,o=i.duration,n=i.callback,!1,1<=(a=(+new Date-t)/o)&&!0,r(a=1<a?1:a),a<1?f.timers.push(i):n&&n();f.timers.length<=0&&f.stop()},f.stop=function(){f.timerId&&(clearInterval(f.timerId),f.timerId=null)};a.color=function(t){var e=a("head").css("color",t).css("color").replace(/^rgba?\(([^)]+)\)$/,"$1").split(new RegExp("\\,"+w));return[+e[0],+e[1],+e[2],e[3]==d?1:+e[3]]},a.getColors=function(t,e,r){if(!e)return function(t){if(F(t)&&3<t){var e=[],r=0;for(r=1;r<=t;r++)e.push("rgb("+(230*Math.random(1)+20).toFixed(0)+","+(230*Math.random(1)+20).toFixed(0)+","+(230*Math.random(1)+20).toFixed(0)+")");return e}return["red","green","blue"]}(t);var n,i,o,a,h,u,s,c,f,l,d,g,p,v=(e[1]-e[0])/t;if(r){var m=(o=r[0],a=r[1],h=r[2],f=+o/255,l=+a/255,d=+h/255,g=Math.max(f,l,d),p=Math.min(f,l,d),g===p?u=0:g===f?u=60*(l-d)/(g-p):g===l?u=60*(d-f)/(g-p)+120:g===d&&(u=60*(f-l)/(g-p)+240),u<0&&(u+=360),0==(c=(g+p)/2)||g===p?s=0:0<c&&c<=.5?s=(g-p)/(g+p):.5<c&&(s=(g-p)/(2-g-p)),[+u.toFixed(2),+s.toFixed(2),+c.toFixed(2)]);n=m[1],i=m[2]}else n=.78,i=.4;for(var w,x,E,b,y,T,M,R,A,_=[],C=0;C<t;C++)w=v*C+e[0],x=n,E=i,T=y=b=void 0,M=(1-Math.abs(2*E-1))*x,R=M*(1-Math.abs(w/60%2-1)),A=E-M/2,0<=w&&w<60?(b=255*(M+A),y=255*(R+A),T=255*A):60<=w&&w<120?(b=255*(R+A),y=255*(M+A),T=255*A):120<=w&&w<180?(b=255*A,y=255*(M+A),T=255*(R+A)):180<=w&&w<240?(b=255*A,y=255*(R+A),T=255*(M+A)):240<=w&&w<300?(b=255*(R+A),y=255*A,T=255*(M+A)):300<=w&&w<360&&(b=255*(M+A),y=255*A,T=255*(R+A)),r=[+b.toFixed(0),+y.toFixed(0),+T.toFixed(0)],_.push("rgb("+r[0]+","+r[1]+","+r[2]+")");return _},a.loop=function(t,e){var r,n=0;for(r in t)e(t[r],r,n++);return a};var l=function(t,e,r,n){return t.beginPath(),t.fillStyle=e,t.strokeStyle=e,"function"!=typeof r?t:(r(t),n)};function g(t){if(t&&t.constructor===u)return t;var e=a(t);return 0<e.length?e[0].getContext("2d"):void 0}t.region=function(e,r){var i={},t=document.createElement("canvas"),n=[0,0,0],o="r";F(e)||(e=this[0].offsetWidth),F(r)||(r=this[0].offsetHeight),t.setAttribute("width",e),t.setAttribute("height",r);var a=this,h=t.getContext("2d"),u={drawer:function(t,e){return i[t]==d&&(i[t]={r:function(){return n[0]+=1,o="g","rgb("+n[0]+","+n[1]+","+n[2]+")"},g:function(){return n[1]+=1,o="b","rgb("+n[0]+","+n[1]+","+n[2]+")"},b:function(){return n[2]+=1,o="r","rgb("+n[0]+","+n[1]+","+n[2]+")"}}[o]()),l(h,i[t],e,u)},erase:function(t){return b(t)||(t=function(t){t.clearRect(0,0,e,r)}),l(h,"rgb(0,0,0)",t,u)},getRegion:function(t){var e,r=a.position(t);r.x-=a.css("border-left-width").replace("px",""),r.y-=a.css("border-top-width").replace("px","");var n=h.getImageData(r.x-.5,r.y-.5,1,1).data;for(e in i)if("rgb("+n[0]+","+n[1]+","+n[2]+")"==i[e])return[e,r.x,r.y];return d}};return u},t.painter=function(){if(0<this.length&&"CANVAS"!=this[0].nodeName&&"canvas"!=this[0].nodeName)throw new Error("painter is not function");return g(this)},t.layer=function(e,r){if(0<this.length&&"CANVAS"!=this[0].nodeName&&"canvas"!=this[0].nodeName)throw new Error("layer is not function");var n=g(this),i=[],o={};F(e)||(e=this[0].clientWidth),F(r)||(r=this[0].clientHeight);var a={painter:function(t){return o[t]&&o[t].constructor===u||(i.push(document.createElement("canvas")),i[i.length-1].setAttribute("width",e),i[i.length-1].setAttribute("height",r),o[t]=i[i.length-1].getContext("2d")),o[t]},clean:function(t){return t&&(t.constructor!==u&&(t=a.painter(t)),t.clearRect(0,0,e,r)),a},update:function(){if(n&&n.constructor===u){var t;for(n.clearRect(0,0,e,r),n.save(),t=0;t<i.length;t++)n.drawImage(i[t],0,0,e,r,0,0,e,r);n.restore()}return a}};return a};var p=function(t,e){var r,n,i=[];for(r=0;r<4;r++)for(n=0;n<e.length/4;n++)i[4*n+r]=t[r]*e[4*n]+t[r+4]*e[4*n+1]+t[r+8]*e[4*n+2]+t[r+12]*e[4*n+3];return i},v=function(t){if(9==t.length)return t[0]*t[4]*t[8]-t[0]*t[7]*t[5]-t[3]*t[1]*t[8]+t[3]*t[7]*t[2]+t[6]*t[1]*t[5]-t[6]*t[4]*t[2];if(16==t.length)return t[0]*v([t[5],t[6],t[7],t[9],t[10],t[11],t[13],t[14],t[15]])-t[4]*v([t[1],t[2],t[3],t[9],t[10],t[11],t[13],t[14],t[15]])+t[8]*v([t[1],t[2],t[3],t[5],t[6],t[7],t[13],t[14],t[15]])-t[12]*v([t[1],t[2],t[3],t[5],t[6],t[7],t[9],t[10],t[11]]);throw new Error("Unsupported parameter!")},M=function(t){var e,r=[v([(e=t)[5],e[6],e[7],e[9],e[10],e[11],e[13],e[14],e[15]]),-v([e[4],e[6],e[7],e[8],e[10],e[11],e[12],e[14],e[15]]),v([e[4],e[5],e[7],e[8],e[8],e[11],e[12],e[13],e[15]]),-v([e[4],e[5],e[6],e[8],e[9],e[10],e[12],e[13],e[14]]),-v([e[1],e[2],e[3],e[9],e[10],e[11],e[13],e[14],e[15]]),v([e[0],e[2],e[3],e[8],e[10],e[11],e[12],e[14],e[15]]),-v([e[0],e[1],e[3],e[8],e[9],e[11],e[12],e[13],e[15]]),v([e[0],e[1],e[2],e[8],e[9],e[10],e[12],e[13],e[14]]),v([e[1],e[2],e[3],e[5],e[6],e[7],e[13],e[14],e[15]]),-v([e[0],e[2],e[3],e[4],e[6],e[7],e[12],e[14],e[15]]),v([e[0],e[1],e[3],e[4],e[5],e[7],e[12],e[13],e[15]]),-v([e[0],e[1],e[2],e[4],e[5],e[6],e[12],e[13],e[14]]),-v([e[1],e[2],e[3],e[5],e[6],e[7],e[9],e[10],e[11]]),v([e[0],e[2],e[3],e[4],e[6],e[7],e[8],e[10],e[11]]),-v([e[0],e[1],e[3],e[4],e[5],e[7],e[8],e[9],e[11]]),v([e[0],e[1],e[2],e[4],e[5],e[6],e[8],e[9],e[10]])];return[r[0],r[4],r[8],r[12],r[1],r[5],r[9],r[13],r[2],r[6],r[10],r[14],r[3],r[7],r[11],r[15]]};a.Matrix4=function(t){var l=t||[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],d={move:function(t,e,r,n){return l=p(function(t,e,r,n){n=n||0;var i=Math.sqrt(e*e+r*r+n*n);return[1,0,0,0,0,1,0,0,0,0,1,0,e*t/i,r*t/i,n*t/i,1]}(t,e,r,n),l),d},rotate:function(t,e,r,n,i,o,a){var h,u,s,c=function(t,e,r,n,i,o){if(F(t)&&F(e)){if(F(r)?F(n)&&F(i)&&F(o)||(n=t,i=e,o=r,r=e=t=0):(r=0,n=t,i=e,o=1),t==n&&e==i&&r==o)throw new Error("It's not a legitimate ray!");var a=Math.sqrt((n-t)*(n-t)+(i-e)*(i-e)),h=0!=a?(i-e)/a:1,u=0!=a?(n-t)/a:0,s=(n-t)*u+(i-e)*h,c=o-r,f=Math.sqrt(s*s+c*c),l=0!=f?c/f:1,d=0!=f?s/f:0;return[[h,l*u,u*d,0,-u,h*l,h*d,0,0,-d,l,0,e*u-t*h,r*d-t*u*l-e*h*l,-t*u*d-e*h*d-r*l,1],[h,-u,0,0,l*u,l*h,-d,0,u*d,h*d,l,0,t,e,r,1]]}throw new Error("a1 and b1 is required!")}(e,r,n,i,o,a);return l=p(p(p(c[1],(h=t,u=Math.sin(h),[s=Math.cos(h),u,0,0,-u,s,0,0,0,0,1,0,0,0,0,1])),c[0]),l),d},scale:function(t,e,r,n,i,o){var a,h,u,s,c,f;return l=p([a=t,0,0,0,0,h=e,0,0,0,0,u=r,0,(s=(s=n)||0)-s*a,(c=(c=i)||0)-c*h,(f=(f=o)||0)-f*u,1],l),d},multiply:function(t,e){return l=e?p(l,t):p(t,l),d},inverse:function(){return l=function(t){var e,r=M(t),n=v(t),i=[];if(0==n)throw new Error("This matrix is irreversible!");for(e=0;e<16;e++)i[e]=r[e]/n;return i}(l),d},use:function(t,e,r,n){var i=p(l,[t,e,r=r||0,n=n||1]);return i[0]=i[0].toFixed(7),i[1]=i[1].toFixed(7),i[2]=i[2].toFixed(7),i},value:function(){return l}};return d},a.hermite=function(){var u={u:.5},s=function(t){if(u.MR){var e=(t-u.a)/(u.b-u.a),r=e*e;return(e*r*u.MR[0]+r*u.MR[1]+e*u.MR[2]+u.MR[3])*(u.b-u.a)}throw new Error("You shoud first set the position!")};return s.setU=function(t){if(!F(t))throw new Error("Expecting a figure!");return u.u=.5*(1-t),s},s.setP=function(t,e,r,n,i,o){if(!(t<r))throw new Error("The point position should be increamented!");u.a=t,u.b=r;var a=u.u*i,h=u.u*o;return e/=r-t,n/=r-t,u.MR=[2*e-2*n+a+h,3*n-3*e-2*a-h,a,e],s},s},a.cardinal=function(){var e,i={t:0},o=function(t){if(i.hs){for(e=-1;e+1<i.hs.x.length&&(t>i.hs.x[e+1]||-1==e&&t>=i.hs.x[e+1]);)e+=1;if(-1==e||e>=i.hs.h.length)throw new Error("Coordinate crossing!");return i.hs.h[e](t)}throw new Error("You shoud first set the position!")};return o.setU=function(t){if(!F(t))throw new Error("Expecting a figure!");return i.t=t,o},o.setP=function(t){i.hs={x:[],h:[]};var e,r,n=(t[1][1]-t[0][1])/(t[1][0]-t[0][0]);for(i.hs.x[0]=t[0][0],e=1;e<t.length;e++){if(t[e][0]<=t[e-1][0])throw new Error("The point position should be increamented!");i.hs.x[e]=t[e][0],r=e<t.length-1?(t[e+1][1]-t[e-1][1])/(t[e+1][0]-t[e-1][0]):(t[e][1]-t[e-1][1])/(t[e][0]-t[e-1][0]),i.hs.h[e-1]=a.hermite().setU(i.t).setP(t[e-1][0],t[e-1][1],t[e][0],t[e][1],n,r),n=r}return o},o},a.catmullRom=function(){var i={},o=function(t){var e=t*t,r=e*t;return[.5*(i.x[0]*r+i.x[1]*e+i.x[2]*t+i.x[3]),.5*(i.y[0]*r+i.y[1]*e+i.y[2]*t+i.y[3])]};return o.setP=function(t,e,r,n){return i.x=a.Matrix4([-1,2,-1,0,3,-5,0,2,-3,4,1,0,1,-1,0,0]).use(t[0],e[0],r[0],n[0]),i.y=a.Matrix4([-1,2,-1,0,3,-5,0,2,-3,4,1,0,1,-1,0,0]).use(t[1],e[1],r[1],n[1]),o},o};var R=function(t,e,r,n){var i=Math.cos(t),o=Math.sin(t);return[e*i-r*o,e*o+r*i,n]};a.map=function(){var g,p={c:[107,36],s:1},r=function(t,e){var r,n,i,o,a,h,u,s,c,f,l,d;return r=(360-e)/180*Math.PI,n=100*p.s,o=i=0,a=Math.cos(r),h=Math.sin(r),g=[o*h+n*a,i,o*a-n*h],g=R(t/180*Math.PI,g[0],g[1],g[2]),g=R((90-p.c[0])/180*Math.PI,g[0],g[1],g[2]),u=(90-p.c[1])/180*Math.PI,s=g[0],c=g[1],f=g[2],l=Math.cos(u),d=Math.sin(u),[-(g=[s,c*l-f*d,c*d+f*l])[0],g[1],g[2]]};return r.scale=function(t){return F(t)&&(p.s=t),r},r.center=function(t,e){return F(t)&&(F(e),1)&&(p.c=[t,e]),r},r},a.rotate=function(t,e,r,n,i){var o=Math.cos(r),a=Math.sin(r);return[((n-t)*o-(i-e)*a+t).toFixed(7),((n-t)*a+(i-e)*o+e).toFixed(7)]},a.move=function(t,e,r,n,i){var o=Math.sqrt(t*t+e*e);return[(t*r/o+n).toFixed(7),(e*r/o+i).toFixed(7)]},a.scale=function(t,e,r,n,i){return[(r*(n-t)+t).toFixed(7),(r*(i-e)+e).toFixed(7)]};var A=function(t,e,r){var n=t.createShader(e);if(null==n)throw new Error("Unable to create shader!");if(t.shaderSource(n,r),t.compileShader(n),!t.getShaderParameter(n,t.COMPILE_STATUS))throw new Error("Failed to compile shader:"+t.getShaderInfoLog(n));return n};return t.webgl=function(t){var v=function(t,e){var r,n=o,i=null;for(r=0;r<n.length;r++){try{i=t.getContext(n[r],e)}catch(t){}if(i)break}return i}(this[0],t),h={painter:function(){return v},shader:function(t,e){return v.program=function(t,e,r){var n=A(t,t.VERTEX_SHADER,e),i=A(t,t.FRAGMENT_SHADER,r),o=t.createProgram();if(t.attachShader(o,n),t.attachShader(o,i),t.linkProgram(o),!t.getProgramParameter(o,t.LINK_STATUS))throw new Error("Failed to link program: "+t.getProgramInfoLog(o));return t.useProgram(o),o}(v,t,e),h},buffer:function(a){var g,t,e,r,n,i=(e=a,r=(t=v).createBuffer(),n=e?t.ELEMENT_ARRAY_BUFFER:t.ARRAY_BUFFER,t.bindBuffer(n,r),r),p={write:function(t,e){var r,n,i,o;return e=e||v.STATIC_DRAW,r=v,n=t,i=e,o=a?r.ELEMENT_ARRAY_BUFFER:r.ARRAY_BUFFER,r.bufferData(o,n,i),g=t,p},use:function(t,e,r,n,i,o){var a,h,u,s,c,f,l,d=g.BYTES_PER_ELEMENT;return y(t)&&(t=v.getAttribLocation(v.program,t)),r=r||0,n=n||0,i=i||v.FLOAT,h=t,u=e,s=i,c=r*d,f=n*d,l=o,(a=v).vertexAttribPointer(h,u,s,l||!1,c||0,f||0),a.enableVertexAttribArray(h),p},close:function(){var t;return t=i,v.deleteBuffer(t),h}};return p},texture:function(t,s){s=s||v.TEXTURE_2D;var e,r,n,i,o=(r=t,n=s,i=(e=v).createTexture(),e.activeTexture(e["TEXTURE"+r]),e.bindTexture(n,i),i),c={config:function(t){return function(t,e,r){var n;for(n in r)t.texParameteri(e,t[n],t[r[n]])}(v,s,t),c},use:function(t,e,r,n){var i,o,a,h,u;return i=s,o=t,a=e,h=r,u=n,v.texImage2D(i,o,a,a,h,u),c},close:function(){var t;return t=o,v.deleteTexture(t),h}};return c}};return h},a.treeLayout=function(){var f,i,l={e:{}},e=function(t){var o,a,e,h,r,u,s,c,n=(o=t,h={},r=l.e.root(o),a=e=l.e.id(r),h[a]={data:r,pid:null,id:a,children:[]},function t(e,r){var n,i=l.e.child(e,o);for(n=0;i&&n<i.length;n++)a=l.e.id(i[n]),h[r].children.push(a),h[a]={data:i[n],pid:r,id:a,children:[]},t(i[n],a)}(r,a),[e,h]);return u=[],c=s=0,function t(e,r){var n;for(c<r&&(c=r),n=0;n<e.children.length;n++)t(f[e.children[n]],r+1);if(f[e.id].left=r+.5,0==n?(u[r]==d&&(u[r]=-.5),u[r-1]==d&&(u[r-1]=-.5),f[e.id].top=u[r]+1,u[r]+1+.5*(f[e.pid].children.length-1)-1<u[r-1]&&(f[e.id].top=u[r-1]+1-.5*(f[e.pid].children.length-1))):f[e.id].top=.5*(f[e.children[0]].top+f[e.children[n-1]].top),f[e.id].top<=u[r]){var i=u[r]+1-f[e.id].top;!function t(e,r){var n;for(f[e].top+=i,u[r]<f[e].top&&(u[r]=f[e].top),n=0;n<f[e].children.length;n++)t(f[e].children[n],r+1)}(e.id,r)}u[r]=f[e.id].top,f[e.id].top+.5>s&&(s=f[e.id].top+.5)}((f=n[1])[i=n[0]],0),{node:f,root:i,size:s,deep:c+1}};return e.root=function(t){return l.e.root=t,e},e.child=function(t){return l.e.child=t,e},e.id=function(t){return l.e.id=t,e},e},a.config=function(t,e){return r[t](e),a},a});