wacomink
Version:
2 lines • 1.47 MB
JavaScript
"use strict";function _possibleConstructorReturn(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function t(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,r,i){return r&&t(e.prototype,r),i&&t(e,i),e}}(),RenderingContext,makeQuadFromFlippedRect,makeRectFromFlippedRect,makeMat4FromMatrix2D;!function(){function t(t,e,r){var i=t[Math.max(e-2,0)],a=t[Math.max(e-1,0)],n=t[e],o=t[Math.min(e+1,r-1)];return{a0:a,a1:.5*(-i+n),a2:.5*(2*i-5*a+4*n-o),a3:.5*(-i+3*a-3*n+o)}}function e(t){return{a0:t.a1,a1:2*t.a2,a2:3*t.a3}}function r(t,e){return t.a2*e*e+t.a1*e+t.a0}function i(t,e,r,i){return Math.sqrt(Math.pow(t-r,2)+Math.pow(e-i,2))}function a(t,e,r,i){return new c(t,e,t+r,e+i)}function n(t){if(!(t instanceof c))throw new TypeError("rect must be instance of WRect");return new u(t.left,t.bottom,t.right,t.bottom,t.left,t.top,t.right,t.top)}var o=8,l=1.25,s=new Float32Array([-.5,1.5,-1.5,.5,1,-2.5,2,-.5,-.5,0,.5,0,0,1,0,0]),h=function(){function t(e){_classCallCheck(this,t),Object.defineProperty(this,"data",{value:new Float32Array(e)})}return _createClass(t,[{key:"toFloat32Array",value:function(){return this.data}},{key:"transpose",value:function(){return new t([this.data[0],this.data[4],this.data[8],this.data[12],this.data[1],this.data[5],this.data[9],this.data[13],this.data[2],this.data[6],this.data[10],this.data[14],this.data[3],this.data[7],this.data[11],this.data[15]])}},{key:"applyToVec4",value:function(t){for(var e=new Float32Array(4),r=0;r<4;r++)for(var i=0;i<4;i++)e[r]+=t[i]*this.data[4*r+i];return e}}],[{key:"makeOrtho",value:function(e,r,i,a,n,o){var l=[],s=r+e,h=r-e,u=a+i,c=a-i,f=o+n,g=o-n;return l[0]=2/h,l[4]=0,l[8]=0,l[12]=0,l[1]=0/h,l[5]=2/c,l[9]=0,l[13]=0,l[2]=0/h,l[6]=0,l[10]=-2/g,l[14]=0,l[3]=-s/h,l[7]=-u/c,l[11]=-f/g,l[15]=1,new t(new Float32Array(l))}}]),t}(),u=function(){function t(e,r,i,a,n,o,l,s){_classCallCheck(this,t),Object.defineProperty(this,"data",{value:new Float32Array([e,r,i,a,n,o,l,s])}),Object.defineProperties(this,{x1:{value:this.data[0],enumerable:!0},y1:{value:this.data[1],enumerable:!0},x2:{value:this.data[2],enumerable:!0},y2:{value:this.data[3],enumerable:!0},x3:{value:this.data[4],enumerable:!0},y3:{value:this.data[5],enumerable:!0},x4:{value:this.data[6],enumerable:!0},y4:{value:this.data[7],enumerable:!0}})}return _createClass(t,[{key:"toFloat32Array",value:function(){return this.data}},{key:"transform",value:function(e){if(!(e&&e instanceof h))throw new TypeError("matrix must be instance of WMat4");var r=e.applyToVec4([this.x1,this.y1,0,1]),i=e.applyToVec4([this.x2,this.y2,0,1]),a=e.applyToVec4([this.x3,this.y3,0,1]),n=e.applyToVec4([this.x4,this.y4,0,1]);return new t(r[0],r[1],i[0],i[1],a[0],a[1],n[0],n[1])}}]),t}(),c=function(){function t(e,r,i,a){_classCallCheck(this,t),Object.defineProperties(this,{left:{value:e,enumerable:!0},x:{value:e,enumerable:!0},bottom:{value:r,enumerable:!0},y:{value:r,enumerable:!0},right:{value:i,enumerable:!0},top:{value:a,enumerable:!0},width:{value:i-e,enumerable:!0},height:{value:a-r,enumerable:!0}})}return _createClass(t,[{key:"union",value:function(e){if(e&&!(e instanceof t))throw new TypeError("rect must be instance of WRect");return e?new t(Math.min(this.left,e.left),Math.min(this.bottom,e.bottom),Math.max(this.right,e.right),Math.max(this.top,e.top)):this}},{key:"intersection",value:function(e){if(e&&!(e instanceof t))throw new TypeError("rect must be instance of WRect");if(!e)return null;var r=new t(Math.max(this.left,e.left),Math.max(this.bottom,e.bottom),Math.min(this.right,e.right),Math.min(this.top,e.top));return r.width>0&&r.height>0?r:null}},{key:"ceil",value:function(){return new t(Math.floor(this.left),Math.floor(this.bottom),Math.ceil(this.right),Math.ceil(this.top))}},{key:"floor",value:function(){return new t(Math.ceil(this.left),Math.ceil(this.bottom),Math.floor(this.right),Math.floor(this.top))}}]),t}(),f=function(){function t(){_classCallCheck(this,t)}return _createClass(t,null,[{key:"getXYR",value:function(t,e,r,i){return{x:t[r*e],y:t[r*e+1],r:isNaN(i)?.5*t[r*e+2]:.5*i}}},{key:"getXYRA",value:function(t,e,r,i,a){return{x:t[r*e],y:t[r*e+1],r:isNaN(i)?.5*t[r*e+2]:.5*i,a:isNaN(a)?t[r*e+3]:a}}}]),t}(),g=function(){function t(e){_classCallCheck(this,t),Object.defineProperties(this,{willGLContext:{value:e},gl:{value:e.gl}}),this.program=this.createProgram(this.compileShader(this.constructor.getVertexShader(),this.gl.VERTEX_SHADER),this.compileShader(this.constructor.getFragmentShader(),this.gl.FRAGMENT_SHADER)),this.contextChanged=!0}return _createClass(t,[{key:"init",value:function(){}},{key:"compileShader",value:function(t,e){var r=this.willGLContext.gl,i=r.createShader(e);r.shaderSource(i,t),r.compileShader(i);var a=r.getShaderParameter(i,r.COMPILE_STATUS);if(!a)throw new Error("could not compile shader:"+r.getShaderInfoLog(i));return i}},{key:"createProgram",value:function(t,e){var r=this.willGLContext.gl,i=r.createProgram();r.attachShader(i,t),r.attachShader(i,e),r.linkProgram(i);var a=r.getProgramParameter(i,r.LINK_STATUS);if(!a)throw new Error("program filed to link:"+r.getProgramInfoLog(i));return i}},{key:"onActivate",value:function(){}},{key:"onDeactivate",value:function(){}},{key:"onContextChange",value:function(){}},{key:"activate",value:function(){this.willGLContext.activateProgram(this)}}]),t}(),_=function(t){function e(){return _classCallCheck(this,e),_possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return _inherits(e,t),_createClass(e,[{key:"init",value:function(){this.a_position=this.gl.getAttribLocation(this.program,"a_position"),this.a_color=this.gl.getAttribLocation(this.program,"a_color"),this.u_projectionMatrix=this.gl.getUniformLocation(this.program,"u_projectionMatrix")}},{key:"onContextChange",value:function(){this.gl.uniformMatrix4fv(this.u_projectionMatrix,!1,this.willGLContext._graphicsSpaceToFramebufferSpaceT.toFloat32Array())}},{key:"onDeactivate",value:function(){this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null)}},{key:"drawVertices",value:function(t,e,r){this.activate(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.willGLContext.vertsBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,t,this.gl.DYNAMIC_DRAW),this.gl.enableVertexAttribArray(this.a_position),this.gl.vertexAttribPointer(this.a_position,2,this.gl.FLOAT,!1,0,0),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.willGLContext.colorsBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,e,this.gl.DYNAMIC_DRAW),this.gl.enableVertexAttribArray(this.a_color),this.gl.vertexAttribPointer(this.a_color,4,this.gl.FLOAT,!1,0,0),t.length&&this.willGLContext.drawArrays(this.gl.TRIANGLES,0,t.length/2)}}],[{key:"getVertexShader",value:function(){return"\n precision highp float;\n\n uniform mat4 u_projectionMatrix;\n\n attribute highp vec4 a_position;\n attribute lowp vec4 a_color;\n\n varying lowp vec4 v_color;\n\n void main()\n {\n gl_Position = u_projectionMatrix * a_position;\n v_color = a_color;\n }\n "}},{key:"getFragmentShader",value:function(){return"\n varying lowp vec4 v_color;\n\n void main()\n {\n gl_FragColor = v_color;\n }\n "}}]),e}(g),d=function(t){function e(){return _classCallCheck(this,e),_possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return _inherits(e,t),_createClass(e,[{key:"init",value:function(){this.a_position=this.gl.getAttribLocation(this.program,"a_position"),this.a_srcPosition=this.gl.getAttribLocation(this.program,"a_srcPosition"),this.u_texture=this.gl.getUniformLocation(this.program,"u_texture"),this.u_projectionMatrix=this.gl.getUniformLocation(this.program,"u_projectionMatrix"),this.u_textureMatrix=this.gl.getUniformLocation(this.program,"u_textureMatrix")}},{key:"onDeactivate",value:function(){this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null)}},{key:"drawTexture",value:function(t,e,r,i,a){this.activate();var n=this.willGLContext.gl;n.disable(n.DEPTH_TEST),n.disable(n.STENCIL_TEST),n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,t),n.uniform1i(this.u_texture,0),n.bindBuffer(n.ARRAY_BUFFER,this.willGLContext.destBuffer),n.bufferData(n.ARRAY_BUFFER,e.toFloat32Array(),n.DYNAMIC_DRAW),n.enableVertexAttribArray(this.a_position),n.vertexAttribPointer(this.a_position,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,this.willGLContext.srcBuffer),n.bufferData(n.ARRAY_BUFFER,r.toFloat32Array(),n.DYNAMIC_DRAW),n.enableVertexAttribArray(this.a_srcPosition),n.vertexAttribPointer(this.a_srcPosition,2,n.FLOAT,!1,0,0),n.uniformMatrix4fv(this.u_projectionMatrix,0,i),n.uniformMatrix4fv(this.u_textureMatrix,0,a),this.willGLContext.drawArrays(n.TRIANGLE_STRIP,0,4)}}],[{key:"getVertexShader",value:function(){return"\n \t\t\tprecision highp float;\n\n \t\tuniform mat4 u_projectionMatrix;\n \t\tuniform mat4 u_textureMatrix;\n \t\tattribute highp vec4 a_position;\n \t\tattribute highp vec4 a_srcPosition;\n \t\tvarying highp vec2 v_textureCoordinate;\n\n \t\tvoid main()\n \t\t{\n \t\t\tgl_Position = u_projectionMatrix * a_position;\n \t\t\tv_textureCoordinate = (u_textureMatrix * a_srcPosition).xy;\n \t\t}\n \t\t"}},{key:"getFragmentShader",value:function(){return"\n \t\t\tprecision highp float;\n\n \t\tvarying vec2 v_textureCoordinate;\n \t\tuniform lowp sampler2D u_texture;\n\n \t\tvoid main()\n \t\t{\n \t\t\tgl_FragColor = texture2D(u_texture, v_textureCoordinate);\n \t\t}\n \t\t"}}]),e}(g),v=function(t){function e(){return _classCallCheck(this,e),_possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return _inherits(e,t),_createClass(e,[{key:"init",value:function(){var t=this.willGLContext.gl;this.a_t=t.getAttribLocation(this.program,"a_t"),this.u_xyr=t.getUniformLocation(this.program,"u_xyr"),this.u_count=t.getUniformLocation(this.program,"u_count"),this.u_concatenated_matrix=t.getUniformLocation(this.program,"u_concatenated_matrix"),this.u_color=t.getUniformLocation(this.program,"u_color"),this.u_scale_vector_abs=t.getUniformLocation(this.program,"u_scale_vector_abs")}},{key:"setColor",value:function(t){this.color=t,this.colorChanged=!0}},{key:"onActivate",value:function(){var t=this.willGLContext.gl;this.colorChanged=!0,t.enableVertexAttribArray(this.a_t),t.bindBuffer(t.ARRAY_BUFFER,this.willGLContext.circleBuffer),t.vertexAttribPointer(this.a_t,2,t.FLOAT,!1,0,0)}},{key:"onDeactivate",value:function(){var t=this.willGLContext.gl;t.bindBuffer(t.ARRAY_BUFFER,null),t.disableVertexAttribArray(this.a_t)}},{key:"onContextChange",value:function(){var t=this.willGLContext,e=t.gl;e.uniformMatrix4fv(this.u_concatenated_matrix,!1,t._graphicsSpaceToFramebufferSpaceT.toFloat32Array()),e.uniform2fv(this.u_scale_vector_abs,t._scaleVectorAbs),this.scaleFactor=Math.max(t._scaleVectorAbs[0],t._scaleVectorAbs[1])}},{key:"drawCircle",value:function(t,e,r){this.activate();var i=this.willGLContext.gl;this.colorChanged&&(i.uniform4fv(this.u_color,new Float32Array([this.color.red/255,this.color.green/255,this.color.blue/255,this.color.alpha])),this.colorChanged=!1);var a=null;if(r){var n=new c(t.x-t.r-l,t.y-t.r-l,t.x+t.r+l,t.y+t.r+l);if(a=n.intersection(this.willGLContext._clipRect),!a||a.width<=0||a.height<=0)return}i.uniform3fv(this.u_xyr,new Float32Array([t.x,t.y,t.r]));var o=Math.min(1024,Math.floor(2*this.scaleFactor*Math.PI*t.r/2)+1);return i.uniform1f(this.u_count,o),this.willGLContext.drawArrays(i.TRIANGLE_FAN,0,o+2),e&&this.willGLContext.drawArrays(i.TRIANGLE_STRIP,1026,2*(o+1)),a}}],[{key:"getVertexShader",value:function(){return"\n \t\t\t#define ANTI_ALIAS_SIZE 1.25\n\n \t\t\tprecision highp float;\n\n \t\t\tuniform mat4 u_concatenated_matrix;\n \t\t\tuniform vec2 u_scale_vector_abs;\n \t\t\tuniform lowp vec4 u_color;\n\n \t\t\t//todo: rename to u_xyr and u_count\n \t\t\tuniform vec3 u_xyr;\n \t\t\tuniform float u_count;\n\n \t\t\tattribute highp vec2 a_t;\n\n \t\t\tvarying lowp vec4 v_color;\n\n \t\t\tvoid main()\n \t\t\t{\n \t\t\t\tfloat x;\n \t\t\t\tfloat y;\n\n \t\t\t\tfloat t = a_t.x / u_count;\n \t\t\t\tfloat alpha_mul = a_t.y;\n\n \t\t\t\tif (t < 0.0) //vertex at the center, for the triangle fan\n \t\t\t\t{\n \t\t\t\t\tx = u_xyr.x;\n \t\t\t\t\ty = u_xyr.y;\n \t\t\t\t\talpha_mul = 1.0;\n \t\t\t\t}\n \t\t\t\telse\n \t\t\t\t{\n \t\t\t\t float correction = max(0.0, ANTI_ALIAS_SIZE - u_xyr.z) / ANTI_ALIAS_SIZE;\n \t\t\t\t alpha_mul = alpha_mul * (1.0 - correction * correction);\n \t\t\t\t\n \t\t\t\t\tfloat r_X = max(0.0, u_xyr.z - (alpha_mul-0.5)*ANTI_ALIAS_SIZE / u_scale_vector_abs.x);\n \t\t\t\t\tfloat r_Y = max(0.0, u_xyr.z - (alpha_mul-0.5)*ANTI_ALIAS_SIZE / u_scale_vector_abs.y);\n\n \t\t\t\t\tfloat t_2pi = 6.28318530717959*t;\n\n \t\t\t\t\tx = u_xyr.x + sin(t_2pi) * r_X;\n \t\t\t\t\ty = u_xyr.y + cos(t_2pi) * r_Y;\n \t\t\t\t}\n\n \t\t\t\tv_color = u_color * alpha_mul;\n \t\t\t\tgl_Position = u_concatenated_matrix * vec4(x, y, alpha_mul, 1.0);\n \t\t\t}\n \t\t"}},{key:"getFragmentShader",value:function(){return"\n \t\t\tvarying lowp vec4 v_color;\n\n \t\t\tvoid main()\n \t\t\t{\n \t\t\t\tgl_FragColor = v_color;\n \t\t\t}\n \t\t"}}]),e}(g),m=function(t){function e(){return _classCallCheck(this,e),_possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return _inherits(e,t),_createClass(e,[{key:"init",value:function(){var t=this.willGLContext.gl;this.a_t=t.getAttribLocation(this.program,"a_t"),this.a_tsf=t.getUniformLocation(this.program,"a_tsf"),this.u_concatenated_matrix=t.getUniformLocation(this.program,"u_concatenated_matrix"),this.u_color=t.getUniformLocation(this.program,"u_color"),this.u_scale_vector_abs=t.getUniformLocation(this.program,"u_scale_vector_abs"),this.u_spline_matrix=t.getUniformLocation(this.program,"u_spline_matrix"),this.a_ctrl_pts_x=t.getUniformLocation(this.program,"a_ctrl_pts_x"),this.a_ctrl_pts_y=t.getUniformLocation(this.program,"a_ctrl_pts_y"),this.a_ctrl_pts_r=t.getUniformLocation(this.program,"a_ctrl_pts_r"),this.u_count=t.getUniformLocation(this.program,"u_count")}},{key:"setColor",value:function(t){this.color=t,this.colorChanged=!0}},{key:"onActivate",value:function(){var t=this.willGLContext,e=t.gl;this.colorChanged=!0,e.enableVertexAttribArray(this.a_t),this.scaleFactor=Math.max(t._scaleVectorAbs[0],t._scaleVectorAbs[1]),e.bindBuffer(e.ARRAY_BUFFER,this.willGLContext.strokeSegmentBuffer),e.vertexAttribPointer(this.a_t,3,e.FLOAT,!1,0,0)}},{key:"onContextChange",value:function(){var t=this.willGLContext,e=t.gl;e.uniformMatrix4fv(this.u_concatenated_matrix,!1,t._graphicsSpaceToFramebufferSpaceT.toFloat32Array()),e.uniform2fv(this.u_scale_vector_abs,t._scaleVectorAbs),this.scaleFactor=Math.max(t._scaleVectorAbs[0],t._scaleVectorAbs[1])}},{key:"drawSplineSegments2",value:function(t,e,r,i){this.activate();var a=this.willGLContext,n=a.gl,o=e.length;this.colorChanged&&(n.uniform4fv(this.u_color,new Float32Array([this.color.red/255,this.color.green/255,this.color.blue/255,this.color.alpha])),this.colorChanged=!1),n.uniformMatrix4fv(this.u_spline_matrix,!1,s);for(var l=0;l<o;l++){var h=12;n.uniform4fv(this.a_ctrl_pts_x,new Float32Array(t.slice(l*h,l*h+4))),n.uniform4fv(this.a_ctrl_pts_y,new Float32Array(t.slice(l*h+4,l*h+8))),n.uniform4fv(this.a_ctrl_pts_r,new Float32Array(t.slice(l*h+8,l*h+12)));var u=2;n.uniform2fv(this.a_tsf,new Float32Array(r.slice(l*u,l*u+2)));var c=e[l],f=2*(c+1);n.uniform1f(this.u_count,c),this.willGLContext.drawArrays(n.TRIANGLE_STRIP,0,f),i&&(this.willGLContext.drawArrays(n.TRIANGLE_STRIP,2050,f),this.willGLContext.drawArrays(n.TRIANGLE_STRIP,4100,f))}}},{key:"drawSplineSegments",value:function(t,e,r,i,a,n,o,h){this.activate();var u=this.willGLContext,g=u.gl,_=Math.floor(t.length/e);this.colorChanged&&(g.uniform4fv(this.u_color,new Float32Array([this.color.red/255,this.color.green/255,this.color.blue/255,this.color.alpha])),this.colorChanged=!1);var d=null;g.uniformMatrix4fv(this.u_spline_matrix,!1,s);for(var v=0;v<_-3;v++){var m=f.getXYR(t,e,v,o),x=f.getXYR(t,e,v+1,o),p=f.getXYR(t,e,v+2,o),b=f.getXYR(t,e,v+3,o),E=[m.x,x.x,p.x,b.x],R=[m.y,x.y,p.y,b.y],T=[m.r,x.r,p.r,b.r];if(E[1]!=E[2]||R[1]!=R[2]){var A=.166666666666,y=E[1],F=-A*E[0]+E[1]+A*E[2],w=+A*E[1]+E[2]-A*E[3],C=E[2],M=R[1],L=-A*R[0]+R[1]+A*R[2],B=+A*R[1]+R[2]-A*R[3],S=R[2],N=T[1],P=-A*T[0]+T[1]+A*T[2],U=+A*T[1]+T[2]-A*T[3],k=T[2];if(h){var I=new c(y-N,M-N,y+N,M+N),D=new c(F-P,L-P,F+P,L+P),G=new c(w-U,B-U,w+U,B+U),O=new c(C-k,S-k,C+k,S+k),X=I.union(D).union(G).union(O);X=new c(X.left-l,X.bottom-l,X.right+l,X.top+l);var Y=X.intersection(n);if(!Y)continue;d=Y.union(d)}g.uniform4f(this.a_ctrl_pts_x,E[0],E[1],E[2],E[3]),g.uniform4f(this.a_ctrl_pts_y,R[0],R[1],R[2],R[3]),g.uniform4f(this.a_ctrl_pts_r,T[0],T[1],T[2],T[3]);var V=[0,1];0==v&&(V[0]=i),v==_-1-3&&(V[1]=a),g.uniform2fv(this.a_tsf,new Float32Array(V));var j=[F-y,L-M,P-N],H=[C-w,S-B,k-U],q=Math.sqrt(j[0]*j[0]+j[1]*j[1]+j[2]*j[2]);j[0]=j[0]/q,j[1]=j[1]/q,j[2]=j[2]/q;var z=Math.sqrt(H[0]*H[0]+H[1]*H[1]+H[2]*H[2]);H[0]=H[0]/z,H[1]=H[1]/z,H[2]=H[2]/z;var W=j[0]*H[0]+j[1]*H[1]+j[2]*H[2],Z=1-W;Z=Math.min(2,Z),Z=Math.max(0,Z);var Q=Math.max(N,k),J=Math.sqrt((y-C)*(y-C)+(M-S)*(M-S)+(N-k)*(N-k)),K=void 0,$=.5*Z,tt=Math.pow(16*Math.PI*Q,$*$)-3,et=J*$*16,rt=Math.max(tt,et);K=1+parseInt(Math.floor(rt*this.scaleFactor)),K=Math.min(K,1024),isNaN(K)&&(K=4);var it=2*(K+1);g.uniform1f(this.u_count,K),this.willGLContext.drawArrays(g.TRIANGLE_STRIP,0,it),r&&(this.willGLContext.drawArrays(g.TRIANGLE_STRIP,2050,it),this.willGLContext.drawArrays(g.TRIANGLE_STRIP,4100,it))}}return g.bindBuffer(g.ARRAY_BUFFER,null),d}}],[{key:"getVertexShader",value:function(){return"\n \t\t\t#define ANTI_ALIAS_SIZE 1.25\n\n \t\t\tprecision highp float;\n\n \t\t\tuniform mat4 u_concatenated_matrix;\n \t\t\tuniform vec2 u_scale_vector_abs; //needed for the anti-aliasing\n \t\t\tuniform lowp vec4 u_color;\n\n \t\t\tuniform mat4 u_spline_matrix;\n\n \t\t\tattribute highp vec3 a_t;\n \t\t\tuniform vec4 a_ctrl_pts_x;\n \t\t\tuniform vec4 a_ctrl_pts_y;\n \t\t\tuniform vec4 a_ctrl_pts_r;\n\n \t\t\tuniform vec2 a_tsf;//starting and final t\n\n \t\t\tvarying lowp vec4 v_color;\n\n \t\t\tuniform float u_count;\n\n \t\t\tconst vec3 kDerivator = vec3 (3.0, 2.0, 1.0);\n\n \t\t\tvoid main()\n \t\t\t{\n \t\t\t\tfloat t = a_t.x / u_count;\n\n \t\t\t\tt = clamp(t, a_tsf.x, a_tsf.y);\n\n \t\t\t\tfloat site_sign = a_t.y;\n \t\t\t\tfloat alpha_mul = a_t.z;\n\n \t\t\t\tfloat t_sq = t*t;\n \t\t\t\tfloat t_thrd = t_sq*t;\n \t\t\t\tvec4 tPowersVector = vec4(t_thrd, t_sq, t, 1.0);\n\n \t\t\t\tvec4 fu = a_ctrl_pts_x * u_spline_matrix;\n \t\t\t\tvec4 fv = a_ctrl_pts_y * u_spline_matrix;\n \t\t\t\tvec4 fr = a_ctrl_pts_r * u_spline_matrix;\n\n \t\t\t\tfloat u = dot(fu , tPowersVector);\n \t\t\t\tfloat v = dot(fv , tPowersVector);\n \t\t\t\tfloat r = dot(fr , tPowersVector);\n\n \t\t\t\tvec3 dfu = fu.xyz * kDerivator;\n \t\t\t\tvec3 dfv = fv.xyz * kDerivator;\n \t\t\t\tvec3 dfr = fr.xyz * kDerivator;\n\n \t\t\t\tfloat du = dot(dfu, tPowersVector.yzw);\n \t\t\t\tfloat dv = dot(dfv, tPowersVector.yzw);\n \t\t\t\tfloat dr = dot(dfr, tPowersVector.yzw);\n \t\t\t\t\n \t\t\t\tfloat correction = max(0.0, ANTI_ALIAS_SIZE - r) / ANTI_ALIAS_SIZE;\n \t\t\t\talpha_mul = alpha_mul * (1.0 - correction * correction);\n\n \t\t\t\tfloat r_X = max(0.0, r - (alpha_mul-0.5)*ANTI_ALIAS_SIZE / u_scale_vector_abs.x);\n \t\t\t\tfloat r_Y = max(0.0, r - (alpha_mul-0.5)*ANTI_ALIAS_SIZE / u_scale_vector_abs.y);\n\n \t\t\t\thighp float s = du*du + dv*dv;\n \t\t\t\thighp float d = s - dr*dr;\n\n \t\t\t\t/* Shady moment... */\n \t\t\t\tif(d < 0.0)\n \t\t\t\t{\n \t\t\t\t\tdr = sign(dr) * sqrt(s);\n \t\t\t\t\td = 0.0;\n \t\t\t\t}\n\n \t\t\t\tfloat denom = 1.0 / s;\n \t\t\t\tfloat d_sqrt = sqrt(d);\n \t\t\t\tfloat d_sqrt_denom = d_sqrt*denom;\n \t\t\t\tfloat dr_denom = dr*denom;\n\n \t\t\t\tfloat arg1 = u - du*dr_denom*r_X;\n \t\t\t\tfloat arg2 = dv*d_sqrt_denom*r_X;\n\n \t\t\t\tfloat arg3 = v - dv*dr_denom*r_Y;\n \t\t\t\tfloat arg4 = du*d_sqrt_denom*r_Y;\n\n \t\t\t\tfloat x = arg1 + site_sign*arg2;\n \t\t\t\tfloat y = arg3 - site_sign*arg4;\n\n \t\t\t\tgl_Position = u_concatenated_matrix * vec4(x, y, alpha_mul, 1.0);\n \t\t\t\tv_color = u_color * alpha_mul;\n \t\t\t}\n \t\t"}},{key:"getFragmentShader",value:function(){return"\n \t\t\tvarying lowp vec4 v_color;\n\n \t\t\tvoid main()\n \t\t\t{\n \t\t\t\tgl_FragColor = v_color;\n \t\t\t}\n \t\t"}}]),e}(g),x=function(t){function e(){return _classCallCheck(this,e),_possibleConstructorReturn(this,Object.getPrototypeOf(e).apply(this,arguments))}return _inherits(e,t),_createClass(e,[{key:"init",value:function(){var t=this.willGLContext.gl;this.a_coordinates=t.getAttribLocation(this.program,"a_coordinates"),this.a_xyr=t.getAttribLocation(this.program,"a_xyr"),this.a_transfromParams=t.getAttribLocation(this.program,"a_transfromParams"),this.a_velocity=t.getAttribLocation(this.program,"a_velocity"),this.a_color=t.getAttribLocation(this.program,"a_color"),this.u_projectionMatrix=t.getUniformLocation(this.program,"u_projectionMatrix"),this.u_fillTextureSize=t.getUniformLocation(this.program,"u_fillTextureSize"),this.u_fillTextureOffset=t.getUniformLocation(this.program,"u_fillTextureOffset"),this.u_shapeTexture=t.getUniformLocation(this.program,"u_shapeTexture"),this.u_fillTexture=t.getUniformLocation(this.program,"u_fillTexture")}},{key:"setBrush",value:function(t){this.brush=t,this.brushChanged=!0}},{key:"setColor",value:function(t){this.color=t,this.colorChanged=!0}},{key:"onActivate",value:function(){this.brushChanged=!0,this.colorChanged=!0;var t=this.willGLContext,e=t.gl;e.bindBuffer(e.ARRAY_BUFFER,t.fullScreenQuad2dBuffer),e.vertexAttribPointer(this.a_coordinates,2,e.FLOAT,!1,0,0),e.enableVertexAttribArray(this.a_coordinates),e.disableVertexAttribArray(this.a_velocity),e.disableVertexAttribArray(this.a_transfromParams),e.disableVertexAttribArray(this.a_xyr),e.disableVertexAttribArray(this.a_color)}},{key:"onContextChange",value:function(){var t=this.willGLContext,e=t.gl;e.uniformMatrix4fv(this.u_projectionMatrix,!1,t._graphicsSpaceToFramebufferSpaceT.toFloat32Array())}},{key:"onDeactivate",value:function(){this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null)}},{key:"drawSprite",value:function(t,e){this.activate();var r=this.willGLContext.gl;this.brushChanged&&(r.uniform2fv(this.u_fillTextureSize,new Float32Array([this.brush.fillTextureSize.x,this.brush.fillTextureSize.y])),r.uniform2fv(this.u_fillTextureOffset,new Float32Array([this.brush.fillTextureOffset.x,this.brush.fillTextureOffset.y])),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this.brush.shapeTexture),r.uniform1i(this.u_shapeTexture,0),r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,this.brush.fillTexture),r.uniform1i(this.u_fillTexture,1),this.brushChanged=!1);var i=t.a,a=Object.assign({},this.color);i=i<0?0:i>1?1:i,a.red*=i,a.green*=i,a.blue*=i,a.alpha*=i,r.vertexAttrib4fv(this.a_color,new Float32Array([a.red/255,a.green/255,a.blue/255,a.alpha])),0==e.x&&0==e.y&&(e.x=2*this.willGLContext.random()-1,e.y=2*this.willGLContext.random()-1),r.vertexAttrib2f(this.a_velocity,e.x,e.y);var n=this.brush.rotationMode==Module.RotationMode.TRAJECTORY?1:0,o=this.brush.rotationMode==Module.RotationMode.RANDOM?2*this.willGLContext.random()*Math.PI:0,l=(2*this.willGLContext.random()-1)*this.brush.scattering;r.vertexAttrib3f(this.a_transfromParams,n,o,l),r.vertexAttrib3fv(this.a_xyr,new Float32Array([t.x,t.y,t.r])),this.willGLContext.drawArrays(r.TRIANGLE_STRIP,0,4)}}],[{key:"getVertexShader",value:function(){return"\n \t\t\tprecision highp float;\n\n \t\t\tuniform mat4 u_projectionMatrix;\n \t\t\tuniform vec2 u_fillTextureSize;\n \t\t\tuniform vec2 u_fillTextureOffset;\n\n \t\t\tattribute lowp vec4 a_color;\n \t\t\t/* (x,y) is the center. z is the offset by x, w is the offset by y */\n \t\t\tattribute highp vec2 a_coordinates;\n \t\t\t/* the velocity at that point. It is the derivative of the function */\n \t\t\tattribute vec2 a_velocity;\n \t\t\t/* 1-trajectory rotation coficient, 2-randrom rotation angle, 3-scattering value */\n \t\t\tattribute vec3 a_transfromParams;\n \t\t\tvarying lowp vec4 v_color;\n \t\t\tattribute vec3 a_xyr;\n \t\t\tvarying highp vec2 v_shapeTexturePosition;\n \t\t\tvarying highp vec2 v_fillTexturePosition;\n\n \t\t\tmat4 trajectoryRotate(vec2 vNormal, float value)\n \t\t\t{\n \t\t\t\tvec2 base = vec2(1.0, 0.0);\n\n \t\t\t\tvec2 vNormalR = mix(base, vNormal, value);\n \t\t\t\tvNormalR = normalize(vNormalR);\n\n \t\t\t\tfloat cosfi = dot(vNormalR, base);\n \t\t\t\tfloat sinfi = vNormalR.x * base.y - vNormalR.y * base.x;\n\n \t\t\t\treturn mat4(\n \t\t\t\tcosfi,-sinfi, 0.0, 0.0,\n \t\t\t\tsinfi, cosfi, 0.0, 0.0,\n \t\t\t\t0.0, 0.0, 1.0, 0.0,\n \t\t\t\t0.0, 0.0, 0.0, 1.0);\n \t\t\t}\n\n \t\t\tmat4 angleRotate(float fi)\n \t\t\t{\n \t\t\t\treturn mat4(\n \t\t\t\tcos(fi),-sin(fi), 0.0, 0.0,\n \t\t\t\tsin(fi), cos(fi), 0.0, 0.0,\n \t\t\t\t0.0, 0.0, 1.0, 0.0,\n \t\t\t\t0.0, 0.0, 0.0, 1.0);\n \t\t\t}\n\n \t\t\tvec2 orthoOffset(vec2 vNormal, float value)\n \t\t\t{\n \t\t\t\tvec2 vno = vec2(-vNormal.y*value, vNormal.x*value);\n \t\t\t\treturn vno;\n \t\t\t}\n\n \t\t\tvoid main()\n \t\t\t{\n \t\t\t\tfloat radius = a_xyr.z;\n \t\t\t\tvec2 vNormal = normalize(a_velocity);\n\n \t\t\t\tmat4 m1 = trajectoryRotate(vNormal, a_transfromParams.x);\n \t\t\t\tmat4 m2 = angleRotate(a_transfromParams.y);\n \t\t\t\tmat4 m = m2*m1;\n\n \t\t\t\tvec2 vno = orthoOffset(vNormal, a_transfromParams.z*radius);\n \t\t\t\tm[3][0] += vno.x;\n \t\t\t\tm[3][1] += vno.y;\n\n \t\t\t\tvec4 offset = m * vec4(a_coordinates.x * radius, a_coordinates.y * radius, 0.0, 1.0);\n \t\t\t\tvec4 position = vec4(a_xyr.x + offset.x ,a_xyr.y + offset.y, 0.0, 1.0);\n \t\t\t\tgl_Position = u_projectionMatrix * position;\n\n \t\t\t\tv_color = a_color;\n \t\t\t\tv_fillTexturePosition = position.xy / u_fillTextureSize + u_fillTextureOffset;\n \t\t\t\tv_shapeTexturePosition = (a_coordinates+1.0)*0.5; /* [-1;1] -> [0;1] */\n \t\t\t}\n "}},{key:"getFragmentShader",value:function(){return"\n \t\t\tuniform lowp sampler2D u_shapeTexture;\n \t\t\tuniform lowp sampler2D u_fillTexture;\n\n \t\t\tvarying mediump vec4 v_color;\t/* lowp seems to lead to an overflow on PowerVR SGX540, so use mediump instead :)*/\n \t\t\tvarying highp vec2 v_shapeTexturePosition;\n \t\t\tvarying highp vec2 v_fillTexturePosition;\n\n \t\t\tvoid main()\n \t\t\t{\n \t\t\t\tlowp vec4 c = v_color * texture2D(u_shapeTexture, v_shapeTexturePosition) * texture2D(u_fillTexture, v_fillTexturePosition);\n\n \t\t\t\tgl_FragColor = c;\n \t\t\t}\n "}}]),e}(g),p=function(){function t(e){if(_classCallCheck(this,t),!e instanceof WebGLRenderingContext)throw new Error("gl must be instance of WebGLRenderingContext");this.gl=e,this.currentProgram=null,this.programs=[]}return _createClass(t,[{key:"init",value:function(t,e){this.gl.disable(this.gl.DITHER),this.gl.disable(this.gl.BLEND),this.gl.disable(this.gl.STENCIL_TEST),this.gl.disable(this.gl.DEPTH_TEST),this.gl.disable(this.gl.SCISSOR_TEST),this.gl.activeTexture(this.gl.TEXTURE0),this.bUseStencilBufferForBlending=!1,this.currentBlendMode=Module.BlendMode.NONE,this.resize(t,e),this.scatterMethodRandomSeed=parseInt((new Date).getTime()/1e3),this.blendMinMaxExt=this.gl.getExtension("EXT_blend_minmax"),this.genStaticVBOs(),this.onChange();for(var r=0;r<this.programs.length;r++)this.programs[r].init()}},{key:"random",value:function(){return this.scatterMethodRandomSeed=1103515245*this.scatterMethodRandomSeed+12345&2147483647,this.scatterMethodRandomSeed/2147483647}},{key:"setUniforms",value:function(t){var e=null;this._scaleVectorAbs=new Float32Array([this.bounds.width/this.graphicsBox.width,this.bounds.height/this.graphicsBox.height]),e=t?h.makeOrtho(this.graphicsBox.left,this.graphicsBox.right,this.graphicsBox.top,this.graphicsBox.bottom,1,-1):h.makeOrtho(this.graphicsBox.left,this.graphicsBox.right,this.graphicsBox.bottom,this.graphicsBox.top,1,-1),this._graphicsSpaceToFramebufferSpaceT=e.transpose(),this.onChange()}},{key:"resize",value:function(t,e){this.textureWidth=parseInt(Math.ceil(t.width)),this.textureHeight=parseInt(Math.ceil(t.height)),this.gl.viewport(t.x,t.y,t.width,t.height),this.bounds=t,this.graphicsBox=e,this._clipRect=this.graphicsBox}},{key:"genStaticVBOs",value:function(){this.fullScreenQuad2dBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.fullScreenQuad2dBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),this.gl.STATIC_DRAW);var t;t=[];for(var e=0;e<=1024;e++)t.push(e,1,1),t.push(e,-1,1);for(var r=0;r<=1024;r++)t.push(r,1,1),t.push(r,1,0);for(var i=0;i<=1024;i++)t.push(i,-1,1),t.push(i,-1,0);this.strokeSegmentBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.strokeSegmentBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array(t),this.gl.STATIC_DRAW),t=[-1,1];for(var a=0;a<=1024;a++)t.push(a,1);for(var n=0;n<=1024;n++)t.push(n,1),t.push(n,0);this.circleBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.circleBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array(t),this.gl.STATIC_DRAW),this.destBuffer=this.gl.createBuffer(),this.srcBuffer=this.gl.createBuffer(),this.vertsBuffer=this.gl.createBuffer(),this.colorsBuffer=this.gl.createBuffer()}},{key:"blendMode",value:function(t){if(t!=this.currentBlendMode){switch(t){case Module.BlendMode.NONE:this.gl.disable(this.gl.BLEND);break;case Module.BlendMode.NORMAL:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ONE,this.gl.ONE_MINUS_SRC_ALPHA);break;case Module.BlendMode.NORMAL_REVERSE:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ONE_MINUS_DST_ALPHA,this.gl.ONE);break;case Module.BlendMode.ERASE:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_ALPHA);break;case Module.BlendMode.MAX:this.gl.enable(this.gl.BLEND),this.blendMinMaxExt?this.gl.blendEquation(this.blendMinMaxExt.MAX_EXT):(this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ONE,this.gl.ONE_MINUS_SRC_ALPHA));break;case Module.BlendMode.MIN:this.gl.enable(this.gl.BLEND),this.blendMinMaxExt?this.gl.blendEquation(this.blendMinMaxExt.MIN_EXT):(this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ONE,this.gl.ONE_MINUS_SRC_ALPHA));break;case Module.BlendMode.ADD:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ONE,this.gl.ONE);break;case Module.BlendMode.SUBSTRACT:
this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_SUBTRACT),this.gl.blendFunc(this.gl.ONE,this.gl.ONE);break;case Module.BlendMode.SUBSTRACT_REVERSE:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_REVERSE_SUBTRACT),this.gl.blendFunc(this.gl.ONE,this.gl.ONE);break;case Module.BlendMode.MULTIPLY_NO_ALPHA:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ZERO,this.gl.SRC_COLOR);break;case Module.BlendMode.MULTIPLY_NO_ALPHA_INVERT:this.gl.enable(this.gl.BLEND),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR)}this.currentBlendMode=t}}},{key:"clearColorbuffer",value:function(t){this.gl.clearColor(t.red*t.alpha/255,t.green*t.alpha/255,t.blue*t.alpha/255,t.alpha),this.gl.clear(this.gl.COLOR_BUFFER_BIT)}},{key:"scissors",value:function(t){if(t&&!(t instanceof c))throw new TypeError("rect must be undefined or instanceof WRect");var e=this.gl,r=e.isEnabled(e.SCISSOR_TEST);t?(r||e.enable(e.SCISSOR_TEST),e.scissor(t.x,t.y,t.width,t.height)):r&&e.disable(e.SCISSOR_TEST)}},{key:"isProgramActive",value:function(t){return this.currentProgram==t}},{key:"activateProgram",value:function(t){this.isProgramActive(t)?t.contextChanged&&(t.onContextChange(),t.contextChanged=!1):(this.currentProgram&&this.currentProgram.onDeactivate(),this.gl.useProgram(t.program),this.currentProgram=t,t.onActivate(),t.onContextChange(),t.contextChanged=!1)}},{key:"generateBuffersExt",value:function(t,e,r,i){var a=this.gl,n=a.createFramebuffer();a.bindFramebuffer(a.FRAMEBUFFER,n);var o=a.createTexture();return a.activeTexture(a.TEXTURE0),a.bindTexture(a.TEXTURE_2D,o),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,r),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,r),a.texImage2D(a.TEXTURE_2D,0,a.RGBA,t,e,0,a.RGBA,i,null),a.framebufferTexture2D(a.FRAMEBUFFER,a.COLOR_ATTACHMENT0,a.TEXTURE_2D,o,0),a.clearColor(0,0,0,0),a.clear(a.COLOR_BUFFER_BIT),{framebuffer:n,colorTexture:o}}},{key:"genFrameAndRenders",value:function(t,e,r){var i=this.gl.createFramebuffer(),a=this.gl.createRenderbuffer();return this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,i),this.gl.bindRenderbuffer(this.gl.RENDERBUFFER,a),this.gl.renderbufferStorage(this.gl.RENDERBUFFER,t,e,r),this.gl.framebufferRenderbuffer(this.gl.FRAMEBUFFER,this.gl.COLOR_ATTACHMENT0,this.gl.RENDERBUFFER,a),{framebuffer:i,renderbuffer:a}}},{key:"deleteBuffers",value:function(t,e){e&&this.gl.deleteTexture(e),t&&this.gl.deleteFramebuffer(t)}},{key:"deleteFrameAndRender",value:function(t,e){e&&this.gl.deleteRenderbuffer(e),t&&this.gl.deleteFramebuffer(t)}},{key:"onChange",value:function(){for(var t=0;t<this.programs.length;t++)this.programs[t].contextChanged=!0}},{key:"isStencilBufferAvailable",value:function(){return this.gl.getFramebufferAttachmentParameter(this.gl.FRAMEBUFFER,this.gl.STENCIL_ATTACHMENT,this.gl.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE)==this.gl.RENDERBUFFER}},{key:"enableStencilBufferForBlending",value:function(){this.bUseStencilBufferForBlending=!0,this.gl.enable(this.gl.STENCIL_TEST),this.gl.stencilMask(4294967295),this.gl.clearStencil(0),this.gl.clear(this.gl.STENCIL_BUFFER_BIT)}},{key:"disableStencilBufferForBlending",value:function(){this.bUseStencilBufferForBlending=!1}},{key:"shouldUseStencilBufferForBlending",value:function(){return this.bUseStencilBufferForBlending}},{key:"drawArrays",value:function(t,e,r){this.currentBlendMode!=Module.BlendMode.MAX||this.blendMinMaxExt?this.gl.drawArrays(t,e,r):(this.shouldUseStencilBufferForBlending()&&(this.gl.enable(this.gl.STENCIL_TEST),this.gl.stencilFunc(this.gl.NOTEQUAL,1,1),this.gl.stencilOp(this.gl.REPLACE,this.gl.REPLACE,this.gl.REPLACE)),this.gl.blendFunc(this.gl.ONE,this.gl.ONE),this.gl.blendEquation(this.gl.FUNC_REVERSE_SUBTRACT),this.gl.drawArrays(t,e,r),this.shouldUseStencilBufferForBlending()&&(this.gl.stencilFunc(this.gl.NOTEQUAL,2,1),this.gl.stencilOp(this.gl.REPLACE,this.gl.REPLACE,this.gl.REPLACE)),this.gl.blendFunc(this.gl.ONE,this.gl.ONE),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.drawArrays(t,e,r),this.shouldUseStencilBufferForBlending()&&this.gl.disable(this.gl.STENCIL_TEST))}}]),t}(),b=function(){function t(e,r,i,a){_classCallCheck(this,t),Object.defineProperties(this,{willGLContext:{value:e,enumerable:!0},gl:{value:e.gl,enumerable:!0}}),this.ownGLResources=!1,this.framebuffer=null,this.colorTexture=null,this.renderbuffer=null,this.scaleFactor=a,this.resize(r,i),this.useTextureStorage=!1,this.flipY=!1,this.releaseRenderbuffer=!1,this.deleted=!1}return _createClass(t,[{key:"resize",value:function(t,e){var r=Math.ceil(t*this.scaleFactor),i=Math.ceil(e*this.scaleFactor),o=a(0,0,t,e),l=a(0,0,r,i),s=n(o);Object.defineProperties(this,{width:{value:t,enumerable:!0,configurable:!0},height:{value:e,enumerable:!0,configurable:!0},storageWidth:{value:r,enumerable:!0,configurable:!0},storageHeight:{value:i,enumerable:!0,configurable:!0},bounds:{value:o,enumerable:!0,configurable:!0},storageBounds:{value:l,enumerable:!0,configurable:!0},boundsQuad:{value:s,enumerable:!0,configurable:!0}})}},{key:"init1",value:function(t){if(this.useTextureStorage=t,t){var e=this.willGLContext.generateBuffersExt(this.storageWidth,this.storageHeight,this.gl.LINEAR,this.gl.UNSIGNED_BYTE);this.framebuffer=e.framebuffer,this.colorTexture=e.colorTexture}else{var r=this.willGLContext.genFrameAndRenders(this.gl.RGBA4,this.storageWidth,this.storageHeight);this.framebuffer=r.framebuffer,this.renderbuffer=r.renderbuffer}this.ownGLResources=!0}},{key:"init3",value:function(t,e,r,i){this.framebuffer=t,this.renderbuffer=e,this.colorTexture=r,this.useTextureStorage=!!r,this.ownGLResources=i}},{key:"delete",value:function(){this.ownGLResources&&(this.useTextureStorage?this.willGLContext.deleteBuffers(this.framebuffer,this.colorTexture):this.willGLContext.deleteFrameAndRender(this.framebuffer,this.renderbuffer)),this.releaseRenderbuffer&&this.renderbuffer&&(this.willGLContext.gl.deleteRenderbuffer(this.renderbuffer),this.renderbuffer=null,this.releaseRenderbuffer=!1),this.deleted=!0}},{key:"deleteLater",value:function(){var t=this;setTimeout(function(){t.delete()},0)}},{key:"isDeleted",value:function(){return this.deleted}}]),t}(),E=function(){function s(t,e){_classCallCheck(this,s),this.willGLContext=new p(t.getContext("webgl",e)||t.getContext("experimental-webgl",e)),this.rasterizerHelper=new Module.RasterizerHelper,this.simpleProgram=new _(this.willGLContext),this.circleProgram=new v(this.willGLContext),this.splineProgram=new m(this.willGLContext),this.textureProgram=new d(this.willGLContext),this.spriteProgram=new x(this.willGLContext),this.circleProgram.setColor(Module.Color.BLACK),this.splineProgram.setColor(Module.Color.BLACK),this.spriteProgram.setColor(Module.Color.BLACK),this.willGLContext.programs=[this.simpleProgram,this.circleProgram,this.splineProgram,this.textureProgram,this.spriteProgram],this.currentTarget=null,this.willGLContext.init(new c(0,0,0,0),new c(0,0,0,0))}return _createClass(s,[{key:"createLayer",value:function(t,e,r){return new b(this.willGLContext,t,e,r)}},{key:"createLayerFromGLFramebuffer",value:function(t,e){var r=this.gl.getParameter(GLctx.RENDERBUFFER_BINDING),i=void 0;if(this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,t),this.gl.getError()==this.gl.INVALID_OPERATION)throw new Error("Invalid framebuffer");if(this.gl.checkFramebufferStatus(this.gl.FRAMEBUFFER)!=this.gl.FRAMEBUFFER_COMPLETE)throw new Error("Incomplete framebuffer");var a=this.gl.getFramebufferAttachmentParameter(this.gl.FRAMEBUFFER,this.gl.COLOR_ATTACHMENT0,this.gl.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE);if(a!=this.gl.RENDERBUFFER)throw new Error("Renderbuffer attachment not found");return i=this.gl.getFramebufferAttachmentParameter(this.gl.FRAMEBUFFER,this.gl.COLOR_ATTACHMENT0,this.gl.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,r),this.createLayerFromGLBuffers(t,i,e)}},{key:"createLayerFromGLBuffers",value:function(t,e,r){var i=this.gl.getParameter(GLctx.RENDERBUFFER_BINDING);this.gl.bindRenderbuffer(this.gl.RENDERBUFFER,e);var a=this.gl.getRenderbufferParameter(this.gl.RENDERBUFFER,this.gl.RENDERBUFFER_WIDTH),n=this.gl.getRenderbufferParameter(this.gl.RENDERBUFFER,this.gl.RENDERBUFFER_HEIGHT);GLctx.bindRenderbuffer(this.gl.RENDERBUFFER,i);var o=this.createLayer(a,n,1);return o.init3(t,e,null,r),o.flipY=!0,o}},{key:"drawLayerWithTransform",value:function(t,e,r){if(this.currentTarget){var i=t.boundsQuad,a=e?i.transform(e):i;this.drawLayer(t,i,a,r)}}},{key:"drawLayer",value:function(t,e,r,i){if(this.currentTarget&&t.useTextureStorage){var a=this.currentTarget,n=this.willGLContext,o=a.flipY?h.makeOrtho(0,a.width,a.height,0,1,-1):h.makeOrtho(0,a.width,0,a.height,1,-1),l=t.flipY?h.makeOrtho(-t.width,t.width,2*t.height,0,0,1):h.makeOrtho(-t.width,t.width,-t.height,t.height,0,1);n.blendMode(i),this.textureProgram.drawTexture(t.colorTexture,r,e,o.transpose().toFloat32Array(),l.transpose().toFloat32Array())}}},{key:"setTarget",value:function(t,e){var r=this.willGLContext,i=r.gl;if(t&&!(t instanceof b))throw new TypeError("layer must be unset or instanceof WillGLLayer");if(e&&!(e instanceof c))throw new TypeError("clipRect must be unset or instanceof WRect");if(t){this.currentTarget=t;var a=new c(0,0,t.storageWidth,t.storageHeight),n=new c(0,0,t.width,t.height);r.resize(a,n),r.setUniforms(t.flipY),e?this.setTargetClipRect(e):this.disableTargetClipRect(),i.bindFramebuffer(i.FRAMEBUFFER,t.framebuffer)}}},{key:"clearColor",value:function(t){this.willGLContext.clearColorbuffer(t)}},{key:"setTargetClipRect",value:function(t){var e=this.willGLContext;t=t.floor().intersection(this.currentTarget.bounds),e._clipRect=t;var r=this.currentTarget.scaleFactor;t=this.currentTarget.flipY?a(t.x,this.currentTarget.storageHeight()-t.top,t.width,t.height):a(t.x*r,t.y*r,t.width*r,t.height*r),e.scissors(t)}},{key:"disableTargetClipRect",value:function(){var t=this.willGLContext;t.scissors(null),t._clipRect=t.graphicsBox}},{key:"drawRobustSolidColorStroke",value:function(i,a,n,o,s,h,u,g){if(!this.currentTarget)return null;if(!this.willGLContext.blendMinMaxExt&&(this.willGLContext.enableStencilBufferForBlending(),!this.willGLContext.isStencilBufferAvailable())){var _=this.willGLContext.gl,d=_.getParameter(_.FRAMEBUFFER_BINDING);if(this.currentTarget.framebuffer==d){var v=_.createRenderbuffer();_.bindRenderbuffer(_.RENDERBUFFER,v),_.renderbufferStorage(_.RENDERBUFFER,_.STENCIL_INDEX8,this.currentTarget.width,this.currentTarget.height),_.framebufferRenderbuffer(_.FRAMEBUFFER,_.STENCIL_ATTACHMENT,_.RENDERBUFFER,v),this.currentTarget.renderbuffer=v,this.currentTarget.releaseRenderbuffer=!0}}this.willGLContext.blendMode(Module.BlendMode.MAX);var m=a.length/n;if(m<4)return null;for(var x=null,p=[],b=[],E=[],R=[],T=Math.max(this.willGLContext._scaleVectorAbs[0],this.willGLContext._scaleVectorAbs[1]),A=0;A<m-3;A++){var y=f.getXYR(a,n,A,o),F=f.getXYR(a,n,A+1,o),w=f.getXYR(a,n,A+2,o),C=f.getXYR(a,n,A+3,o),M=[y.x,F.x,w.x,C.x],L=[y.y,F.y,w.y,C.y],B=[y.r,F.r,w.r,C.r];if(M[1]!=M[2]||L[1]!=L[2]){var S=0==A?h:0,N=A==m-1-3?u:1,P=.166666666666,U=M[1],k=-P*M[0]+M[1]+P*M[2],I=+P*M[1]+M[2]-P*M[3],D=M[2],G=L[1],O=-P*L[0]+L[1]+P*L[2],X=+P*L[1]+L[2]-P*L[3],Y=L[2],V=B[1],j=-P*B[0]+B[1]+P*B[2],H=+P*B[1]+B[2]-P*B[3],q=B[2];if(g){var z=new c(U-V,G-V,U+V,G+V),W=new c(k-j,O-j,k+j,O+j),Z=new c(I-H,X-H,I+H,X+H),Q=new c(D-q,Y-q,D+q,Y+q),J=z.union(W).union(Z).union(Q);J=new c(J.left-l,J.bottom-l,J.right+l,J.top+l);var K=J.intersection(this.willGLContext._clipRect);if(!K)continue;x=K.union(x)}var $=!0,tt=t(M,2,4),et=t(L,2,4),rt=t(B,2,4),it=e(tt),at=e(et),nt=e(rt),ot=r(nt,0),lt=r(it,0),st=r(at,0),ht=-1;if(ot*ot>lt*lt+st*st&&($=!1,ot>0?S=Math.max(S,.5):N=Math.min(N,.5),ht=.5),$){var ut=r(nt,1),ct=r(it,1),ft=r(at,1);ut*ut>ct*ct+ft*ft&&($=!1,ot>0?S=Math.max(S,.5):N=Math.min(N,.5),ht=.5)}if(0==A){var gt=h,_t=gt*gt,dt=_t*gt,vt=dt*tt.a3+_t*tt.a2+gt*tt.a1+tt.a0,mt=dt*et.a3+_t*et.a2+gt*et.a1+et.a0,xt=dt*rt.a3+_t*rt.a2+gt*rt.a1+rt.a0;p.push(vt),p.push(mt),p.push(xt)}if(ht>0){var pt=ht,bt=pt*pt,Et=bt*pt,Rt=Et*tt.a3+bt*tt.a2+pt*tt.a1+tt.a0,Tt=Et*et.a3+bt*et.a2+pt*et.a1+et.a0,At=Et*rt.a3+bt*rt.a2+pt*rt.a1+rt.a0;p.push(Rt),p.push(Tt),p.push(At)}var yt=A==m-4?u:1,Ft=yt*yt,wt=Ft*yt,Ct=wt*tt.a3+Ft*tt.a2+yt*tt.a1+tt.a0,Mt=wt*et.a3+Ft*et.a2+yt*et.a1+et.a0,Lt=wt*rt.a3+Ft*rt.a2+yt*rt.a1+rt.a0;p.push(Ct),p.push(Mt),p.push(Lt);var Bt=[k-U,O-G,j-V],St=[D-I,Y-X,q-H],Nt=Math.sqrt(Bt[0]*Bt[0]+Bt[1]*Bt[1]+Bt[2]*Bt[2]);Bt[0]=Bt[0]/Nt,Bt[1]=Bt[1]/Nt,Bt[2]=Bt[2]/Nt;var Pt=Math.sqrt(St[0]*St[0]+St[1]*St[1]+St[2]*St[2]);St[0]=St[0]/Pt,St[1]=St[1]/Pt,St[2]=St[2]/Pt;var Ut=Bt[0]*St[0]+Bt[1]*St[1]+Bt[2]*St[2],kt=1-Ut;kt=Math.min(2,kt),kt=Math.max(0,kt);var It=Math.max(V,q),Dt=Math.sqrt((U-D)*(U-D)+(G-Y)*(G-Y)+(V-q)*(V-q)),Gt=void 0,Ot=.5*kt,Xt=Math.pow(16*Math.PI*It,Ot*Ot)-3,Yt=Dt*Ot*16,Vt=Math.max(Xt,Yt);Gt=1+parseInt(Math.floor(Vt*T)),Gt=Math.min(Gt,1024),b.push(M[0],M[1],M[2],M[3]),b.push(L[0],L[1],L[2],L[3]),b.push(B[0],B[1],B[2],B[3]),R.push(Gt),E.push(S),E.push(N)}else{var jt=Math.max(B[1],B[2]);p.push(M[1]),p.push(L[1]),p.push(jt)}}isNaN(s.red)&&(s.red=255),isNaN(s.green)&&(s.green=255),isNaN(s.blue)&&(s.blue=255),isNaN(s.alpha)&&(s.alpha=1),s=Module.Color.from(s.red*s.alpha,s.green*s.alpha,s.blue*s.alpha,s.alpha),b.length>0&&this.splineProgram.setColor(s),this.splineProgram.drawSplineSegments2(b,R,E,i.gradientAntialiasingEnabled),p.length>0&&this.circleProgram.setColor(s);for(var Ht=0;Ht<p.length;Ht+=3){var qt={x:p[Ht],y:p[Ht+1],r:p[Ht+2]},zt=this.circleProgram.drawCircle(qt,i.gradientAntialiasingEnabled,g);zt&&(x=zt.union(x))}return this.willGLContext.gl.bindBuffer(this.willGLContext.gl.ARRAY_BUFFER,null),this.willGLContext.disableStencilBufferForBlending(),x?x.ceil():null}},{key:"drawStroke",value:function(t,e,r,i,a,n,l,s,h,u,f){var g=this.willGLContext;if(t instanceof Module.SolidColorBrush)return this.drawRobustSolidColorStroke(t,e,r,i,a,s,h,f);if(!this.currentTarget)return null;var _=e.length/r;if(_<4)return null;var d=null,v=void 0;u&&u.seed&&(v=g.scatterMethodRandomSeed,g.scatterMethodRandomSeed=u.seed);var m=g._clipRect;if(g.blendMode(t.blendMode),t instanceof Module.DirectBrush){isNaN(a.red)&&(a.red=255),isNaN(a.green)&&(a.green=255),isNaN(a.blue)&&(a.blue=255),isNaN(a.alpha)&&(a.alpha=1),this.splineProgram.setColor(a);var x=this.splineProgram.drawSplineSegments(e,r,t.gradientAntialiasingEnabled,s,h,m,i,f);if(x&&(d=x.union(d)),(n||l)&&this.circleProgram.setColor(a),n){var p=this.drawCircleAtSplinePoint(t,e,r,i,0,s,f);p&&(d=p.union(d))}if(l){var b=this.drawCircleAtSplinePoint(t,e,r,i,_-1-3,h,f);b&&(d=b.union(d))}}else if(t instanceof Module.ParticleBrush){var E=a.alpha;a=Object.assign({},a),a.alpha=1,this.spriteProgram.setBrush(t),this.spriteProgram.setColor(a);var R=u&&u.lastPoint?u.lastPoint:{x:1/0,y:1/0,r:1/0,a:1/0},T=this.discretizeXYRA(e,r,t.spacing,R,s,h,i,E,o),A=T.outPoints,y=T.outPointsDerivatives;R=T.previousPoint;for(var F=0;F<A.length;F++){var w=A[F].r*(1+t.scattering)+2,C=new c(A[F].x-w,A[F].y-w,A[F].x+w,A[F].y+w),M=C.intersection(m);M&&(this.spriteProgram.drawSprite(A[F],y[F]),d=M.union(d))}A.length&&(u.lastPoint=A[A.length-1])}return u&&u.seed&&(u.seed=g.scatterMethodRandomSeed,g.scatterMethodRandomSeed=v),d?d.ceil():null}},{key:"drawCircleAtSplinePoint",value:function(e,r,i,a,n,o,l){var s=f.getXYR(r,i,n,a),h=f.getXYR(r,i,n+1,a),u=f.getXYR(r,i,n+2,a),c=f.getXYR(r,i,n+3,a),g=[s.x,h.x,u.x,c.x],_=[s.y,h.y,u.y,c.y],d=[s.r,h.r,u.r,c.r],v=t(g,2,4),m=t(_,2,4),x=t(d,2,4),p=o*o,b=p*o,E=b*v.a3+p*v.a2+o*v.a1+v.a0,R=b*m.a3+p*m.a2+o*m.a1+m.a0,T=b*x.a3+p*x.a2+o*x.a1+x.a0;return this.circleProgram.drawCircle({x:E,y:R,r:T},e.gradientAntialiasingEnabled,l)}},{key:"discretizeXYRA",value:function(a,n,o,l,s,h,u,c,g){var _=0,d=a.length/n,v=[],m=[];if(n<3)throw new Error("WacomInk ERROR: Scatter method requires X, Y and R!");if(d>=4)for(var x=0;x<d;x++)if(!(x<3)){var p=f.getXYRA(a,n,x-3,u,c),b=f.getXYRA(a,n,x-2,u,c),E=f.getXYRA(a,n,x-1,u,c),R=f.getXYRA(a,n,x,u,c),T=[p.x,b.x,E.x,R.x],A=[p.y,b.y,E.y,R.y],y=[p.r,b.r,E.r,R.r],F=[p.a,b.a,E.a,R.a],w=t(T,2,4),C=t(A,2,4),M=t(y,2,4),L=t(F,2,4),B=e(w),S=e(C),N=i(T[1],A[1],T[2],A[2]),P=parseInt(Math.floor(g*N)+1),U=void 0,k=void 0,I=void 0,D=void 0,G=void 0,O=void 0;O=0;for(var X=0;X<=P;X++)if(O=X/P,!(3==x&&O<s||x==d-1&&O>h)){var Y=O*O,V=Y*O;U=V*w.a3+Y*w.a2+O*w.a1+w.a0,k=V*C.a3+Y*C.a2+O*C.a1+C.a0,I=V*M.a3+Y*M.a2+O*M.a1+M.a0,D=U-l.x,G=k-l.y;var j=D*D+G*G,H=(I+l.r)*o;if(j>=H*H){var q=V*L.a3+Y*L.a2+O*L.a1+L.a0,z={x:U,y:k,r:I,a:q};v.push(z);var W={x:r(B,O),y:r(S,O),r:0,a:0};m.push(W),l=Object.assign({},z),_++}}}return{outPoints:v,outPointsDerivatives:m,previousPoint:l}}},{key:"readPixels",value:function(t){if(t&&!(t instanceof c))throw new TypeError("box must be instance of WRect");var e=this.currentTarget,r=this.willGLContext.gl;if(t||(t=e.bounds),e.useTextureStorage){var i=this.createLayer(t.width,t.height,e.scaleFactor);i.init1(!1),i.flipY=e.flipY,this.setTarget(i),this.clearColor(Module.Color.TRANSPERENT),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,e.colorTexture);var o=r.getTexParameter(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER),l=r.getTexParameter(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER);r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),this.drawLayer(e,n(t),n(i.bounds),Module.BlendMode.NONE),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,e.colorTexture),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,o),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,l);var s=new Uint8Array(i.storageWidth*i.storageHeight*4);return r.readPixels(0,0,i.storageWidth,i.storageHeight,r.RGBA,r.UNSIGNED_BYTE,s),this.setTarget(e),s}e.flipY&&(t=a(t.x,e.height-t.y-t.height,t.width,t.height)),this.setTarget(e);var h=new Uint8Array(r.drawingBufferWidth*r.drawingBufferHeight*4);return r.readPixels(parseInt(t.x*e.scaleFactor),parseInt(t.y*