fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
2 lines • 4.14 kB
JavaScript
import{_defineProperty as e}from"../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.min.mjs";import{FabricError as t}from"../util/internals/console.min.mjs";import{getEnv as n}from"../env/index.min.mjs";import{createCanvasElementFor as r}from"../util/misc/dom.min.mjs";import{isWebGLPipelineState as i}from"./utils.min.mjs";import{highPsourceCode as a,identityFragmentShader as o,vertexSource as s}from"./shaders/baseFilter.min.mjs";const c=new RegExp(a,`g`);var l=class{get type(){return this.constructor.type}constructor({type:e,...t}={}){Object.assign(this,this.constructor.defaults,t)}getFragmentSource(){return o}getVertexSource(){return s}createProgram(e,r=this.getFragmentSource(),i=this.getVertexSource()){let{WebGLProbe:{GLPrecision:o=`highp`}}=n();o!==`highp`&&(r=r.replace(c,a.replace(`highp`,o)));let s=e.createShader(e.VERTEX_SHADER),l=e.createShader(e.FRAGMENT_SHADER),u=e.createProgram();if(!s||!l||!u)throw new t(`Vertex, fragment shader or program creation error`);if(e.shaderSource(s,i),e.compileShader(s),!e.getShaderParameter(s,e.COMPILE_STATUS))throw new t(`Vertex shader compile error for ${this.type}: ${e.getShaderInfoLog(s)}`);if(e.shaderSource(l,r),e.compileShader(l),!e.getShaderParameter(l,e.COMPILE_STATUS))throw new t(`Fragment shader compile error for ${this.type}: ${e.getShaderInfoLog(l)}`);if(e.attachShader(u,s),e.attachShader(u,l),e.linkProgram(u),!e.getProgramParameter(u,e.LINK_STATUS))throw new t(`Shader link error for "${this.type}" ${e.getProgramInfoLog(u)}`);let d=this.getUniformLocations(e,u)||{};return d.uStepW=e.getUniformLocation(u,`uStepW`),d.uStepH=e.getUniformLocation(u,`uStepH`),{program:u,attributeLocations:this.getAttributeLocations(e,u),uniformLocations:d}}getAttributeLocations(e,t){return{aPosition:e.getAttribLocation(t,`aPosition`)}}getUniformLocations(e,t){let n=this.constructor.uniformLocations,r={};for(let i=0;i<n.length;i++)r[n[i]]=e.getUniformLocation(t,n[i]);return r}sendAttributeData(e,t,n){let r=t.aPosition,i=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,i),e.enableVertexAttribArray(r),e.vertexAttribPointer(r,2,e.FLOAT,!1,0,0),e.bufferData(e.ARRAY_BUFFER,n,e.STATIC_DRAW)}_setupFrameBuffer(e){let t=e.context;if(e.passes>1){let n=e.destinationWidth,r=e.destinationHeight;e.sourceWidth===n&&e.sourceHeight===r||(t.deleteTexture(e.targetTexture),e.targetTexture=e.filterBackend.createTexture(t,n,r)),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,e.targetTexture,0)}else t.bindFramebuffer(t.FRAMEBUFFER,null),t.finish()}_swapTextures(e){e.passes--,e.pass++;let t=e.targetTexture;e.targetTexture=e.sourceTexture,e.sourceTexture=t}isNeutralState(e){return!1}applyTo(e){i(e)?(this._setupFrameBuffer(e),this.applyToWebGL(e),this._swapTextures(e)):this.applyTo2d(e)}applyTo2d(e){}getCacheKey(){return this.type}retrieveShader(e){let t=this.getCacheKey();return e.programCache[t]||(e.programCache[t]=this.createProgram(e.context)),e.programCache[t]}applyToWebGL(e){let t=e.context,n=this.retrieveShader(e);e.pass===0&&e.originalTexture?t.bindTexture(t.TEXTURE_2D,e.originalTexture):t.bindTexture(t.TEXTURE_2D,e.sourceTexture),t.useProgram(n.program),this.sendAttributeData(t,n.attributeLocations,e.aPosition),t.uniform1f(n.uniformLocations.uStepW,1/e.sourceWidth),t.uniform1f(n.uniformLocations.uStepH,1/e.sourceHeight),this.sendUniformData(t,n.uniformLocations),t.viewport(0,0,e.destinationWidth,e.destinationHeight),t.drawArrays(t.TRIANGLE_STRIP,0,4)}bindAdditionalTexture(e,t,n){e.activeTexture(n),e.bindTexture(e.TEXTURE_2D,t),e.activeTexture(e.TEXTURE0)}unbindAdditionalTexture(e,t){e.activeTexture(t),e.bindTexture(e.TEXTURE_2D,null),e.activeTexture(e.TEXTURE0)}sendUniformData(e,t){}createHelpLayer(e){if(!e.helpLayer){let{sourceWidth:t,sourceHeight:n}=e;e.helpLayer=r({width:t,height:n})}}toObject(){let e=Object.keys(this.constructor.defaults||{});return{type:this.type,...e.reduce((e,t)=>(e[t]=this[t],e),{})}}toJSON(){return this.toObject()}static async fromObject({type:e,...t},n){return new this(t)}};e(l,`type`,`BaseFilter`),e(l,`uniformLocations`,[]);export{l as BaseFilter};
//# sourceMappingURL=BaseFilter.min.mjs.map