File "shaderX.min.js"

Full Path: /home/ycoalition/public_html/blog/wp-content/themes/woodmart/js/scripts/shaders/shaderX.min.js
File size: 7.39 KB
MIME-type: text/plain
Charset: utf-8

function ShaderX(t){this.options=jQuery.extend({},{container:null,sizeContainer:null,autoPlay:!0,vertexShader:"",fragmentShader:"",width:0,height:0,mouseMove:!1,distImage:!1},t),this.container=this.options.container,this.pixelRatio=window.devicePixelRatio,this.uniforms={},this.time=0,this.progress=0,this.empty=!0,this.images={},this.texture1=null,this.texture2=null,this.resizing=!1,this.resizingTimeout=0,this.border=0,this.scale=1,this.drawn=!1,this.runned=!1,this.mouseX=0,this.mouseY=0,this.loadedTextures={},this.options.autoPlay&&this.init()}ShaderX.prototype={init:function(){var t=this;window.addEventListener("resize",function(){t.resize()}),this.options.autoPlay&&(this.runned=!0,this.render(),this.raf())},render:function(){this.container.hasClass("wd-with-webgl")||(this.createCanvas(),this.container.append(this.canvas),this.container.addClass("wd-with-webgl")),this.gl&&(0<this.progress&&this.progress<1||!this.drawn)&&(this.renderCanvas(),this.drawn=!0)},createCanvas:function(){var t,i,e,s,r;this.canvas=document.createElement("CANVAS"),this.gl=this.canvas.getContext("webgl"),this.gl?(this.canvas.width=this.options.width*this.pixelRatio,this.canvas.height=this.options.height*this.pixelRatio,e=this.createShader(this.gl.VERTEX_SHADER,this.options.vertexShader),i=this.createShader(this.gl.FRAGMENT_SHADER,this.options.fragmentShader),this.program=this.createProgram(e,i),e=this.gl.getAttribLocation(this.program,"a_position"),i=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,i),i=this.options.width*this.pixelRatio,t=this.options.height*this.pixelRatio,this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([0,0,i,0,0,t,0,t,i,0,i,t]),this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.gl.canvas.width,this.gl.canvas.height),this.gl.clearColor(0,0,0,0),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.useProgram(this.program),i=[2/this.gl.canvas.width,0,0,0,-2/this.gl.canvas.height,0,-1,1,1],this.addUniform("3fv","u_matrix",i),this.addUniform("1f","u_flipY",1),this.addUniform("1f","u_time",0),this.addUniform("2f","u_pixels",[this.options.width*this.pixelRatio,this.options.height*this.pixelRatio]),this.addUniform("1f","u_progress",0),this.addUniform("2f","u_resolution",[this.gl.canvas.width,this.gl.canvas.height]),this.addUniform("2f","u_uvRate",[1,1]),this.addUniform("1f","u_scale",this.scale),this.options.mouseMove&&this.addUniform("2f","u_mouse",[.5,0]),this.gl.enableVertexAttribArray(e),t=this.gl.FLOAT,this.gl.vertexAttribPointer(e,2,t,!1,0,0),i=this.gl.getAttribLocation(this.program,"a_texCoord"),e=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,e),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,0,1,1,0,1,1]),this.gl.STATIC_DRAW),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,0,0),this.texture1&&this.loadImageTexture(this.texture1,0),this.options.distImage&&(s=new Image,this.requestCORSIfNotSameOrigin(s,this.options.distImage),s.src=this.options.distImage,r=this,s.onload=function(){r.loadImageTexture(s,2)})):console.log("WebGL is not supported")},raf:function(){var s;this.canvas&&(s=this,function t(){var i,e;s.time+=.03,s.updateUniform("u_time",s.time),s.options.mouseMove&&(i=(e=s.getUniform("u_mouse"))[0],e=e[1],i=i?i+.05*(s.mouseX-i):s.mouseX,e=e?e+.05*(s.mouseY-e):s.mouseY,s.updateUniform("u_mouse",[i,e])),s.progress<0&&(s.progress=0),1<s.progress&&(s.progress=1),s.updateUniform("u_progress",s.progress),s.updateUniform("u_scale",s.scale),s.render(),s.requestID=window.requestAnimationFrame(t)}())},resize:function(){var e=this;clearTimeout(this.resizingTimeout),this.resizingTimeout=setTimeout(function(){var t,i;e.canvas&&(t=Math.floor(e.options.sizeContainer.outerWidth()*e.pixelRatio),i=Math.floor(e.options.sizeContainer.outerHeight()*e.pixelRatio),e.gl.canvas.width===t&&e.gl.canvas.height===i||(e.gl.canvas.width=t,e.gl.canvas.height=i),e.updateUniform("u_resolution",[t,i]),e.updateUniform("u_pixels",[t,i]),e.updateUniform("u_uvRate",[1,i/t]),e.gl.viewport(0,0,t,i),e.drawn=!1)},500)},run:function(){this.runned||(this.runned=!0,this.render(),this.raf())},stop:function(){this.runned&&(window.cancelAnimationFrame(this.requestID),this.destroyCanvas(),this.container.find("canvas").remove(),this.container.removeClass("wd-with-webgl"),this.runned=!1)},renderCanvas:function(){if(this.empty)return!1;this.gl.drawArrays(this.gl.TRIANGLES,0,6)},destroyCanvas:function(){this.gl&&(this.canvas=null,this.gl.getExtension("WEBGL_lose_context").loseContext(),this.gl=null)},createShader:function(t,i){t=this.gl.createShader(t);if(this.gl.shaderSource(t,i),this.gl.compileShader(t),this.gl.getShaderParameter(t,this.gl.COMPILE_STATUS))return t;console.log(this.gl.getShaderInfoLog(t)),this.gl.deleteShader(t)},createProgram:function(t,i){var e=this.gl.createProgram();if(this.gl.attachShader(e,t),this.gl.attachShader(e,i),this.gl.linkProgram(e),this.gl.getProgramParameter(e,this.gl.LINK_STATUS))return e;console.log(this.gl.getProgramInfoLog(e)),this.gl.deleteProgram(e)},addUniform:function(t,i,e){var s=this.gl.getUniformLocation(this.program,i);!(this.uniforms[i]={location:s,type:t})!==e&&this.updateUniform(i,e)},updateUniform:function(t,i){if(this.gl){var e=this.uniforms[t];switch(e.type){case"1f":this.gl.uniform1f(e.location,i);break;case"2f":this.gl.uniform2f(e.location,i[0],i[1]);break;case"1i":this.gl.uniform1i(e.location,i);break;case"3fv":this.gl.uniformMatrix3fv(e.location,!1,i)}}},getUniform:function(t,i){if(this.gl)return t=this.uniforms[t],this.gl.getUniform(this.program,t.location)},getImageId:function(t){t=t.split("/");return t[t.length-3]+"-"+t[t.length-2]+"-"+t[t.length-1]},loadImage:function(t,i,e,s){var r,a=this.getImageId(t);if(this.images[a])return r=this.images[a],s?void 0:(0===i?this.texture1=r:1===i&&(this.texture2=r),this.loadImageTexture(r,i),this.empty=!1,this.drawn=!1,void(e&&e()));r=new Image,this.requestCORSIfNotSameOrigin(r,t),r.src=t;var h=this;r.onload=function(){h.images[a]=r,s||(0===i?h.texture1=r:h.texture2=r,h.loadImageTexture(r,i),h.empty=!1,h.drawn=!1,e&&e())}},requestCORSIfNotSameOrigin:function(t,i){new URL(i,window.location.href).origin!==window.location.origin&&(t.crossOrigin="")},loadImageTexture:function(t,i){var e,s;this.gl&&(this.loadedTextures[i]?(e=this.loadedTextures[i],s=this.gl.TEXTURE0+i,this.gl.activeTexture(s),this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t),this.addUniform("1i","u_image"+i,i),this.addUniform("2f","u_image"+i+"_size",[t.width,t.height])):(e=this.gl.createTexture(),s=this.gl.TEXTURE0+i,this.gl.activeTexture(s),this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t),this.addUniform("1i","u_image"+i,i),this.addUniform("2f","u_image"+i+"_size",[t.width,t.height]),this.updateUniform("u_flipY",-1)))},replaceImage:function(t){var s=this;this.getImageId(t),this.texture2&&(s.loadImageTexture(this.texture2,0),s.loadImageTexture(this.texture2,1));this.loadImage(t,1,function(){var i,e=0;s.progress=0,function t(){e+=1/78,s.progress=e*(2-e),1<=s.progress?window.cancelAnimationFrame(i):i=window.requestAnimationFrame(t)}()})}};