UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

3 lines (2 loc) 2.06 kB
import{defineProperty as e}from"../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{FabricImage as t}from"../shapes/Image.min.mjs";import{createCanvasElement as a}from"../util/misc/dom.min.mjs";import{BaseFilter as i}from"./BaseFilter.min.mjs";import{classRegistry as r}from"../ClassRegistry.min.mjs";import{fragmentSource as s,vertexSource as m}from"./shaders/blendImage.min.mjs";const n={mode:"multiply",alpha:1};class o extends i{getCacheKey(){return`${this.type}_${this.mode}`}getFragmentSource(){return s[this.mode]}getVertexSource(){return m}applyToWebGL(e){const t=e.context,a=this.createTexture(e.filterBackend,this.image);this.bindAdditionalTexture(t,a,t.TEXTURE1),super.applyToWebGL(e),this.unbindAdditionalTexture(t,t.TEXTURE1)}createTexture(e,t){return e.getCachedTexture(t.cacheKey,t.getElement())}calculateMatrix(){const e=this.image,{width:t,height:a}=e.getElement();return[1/e.scaleX,0,0,0,1/e.scaleY,0,-e.left/t,-e.top/a,1]}applyTo2d(e){let{imageData:{data:t,width:i,height:r},filterBackend:{resources:s}}=e;const m=this.image;s.blendImage||(s.blendImage=a());const n=s.blendImage,o=n.getContext("2d");n.width!==i||n.height!==r?(n.width=i,n.height=r):o.clearRect(0,0,i,r),o.setTransform(m.scaleX,0,0,m.scaleY,m.left,m.top),o.drawImage(m.getElement(),0,0,i,r);const l=o.getImageData(0,0,i,r).data;for(let e=0;e<t.length;e+=4){const a=t[e],i=t[e+1],r=t[e+2],s=t[e+3],m=l[e],n=l[e+1],o=l[e+2],c=l[e+3];switch(this.mode){case"multiply":t[e]=a*m/255,t[e+1]=i*n/255,t[e+2]=r*o/255,t[e+3]=s*c/255;break;case"mask":t[e+3]=c}}}sendUniformData(e,t){const a=this.calculateMatrix();e.uniform1i(t.uImage,1),e.uniformMatrix3fv(t.uTransformMatrix,!1,a)}toObject(){return{...super.toObject(),image:this.image&&this.image.toObject()}}static async fromObject(e,a){let{type:i,image:r,...s}=e;return t.fromObject(r,a).then(e=>new this({...s,image:e}))}}e(o,"type","BlendImage"),e(o,"defaults",n),e(o,"uniformLocations",["uTransformMatrix","uImage"]),r.setClass(o);export{o as BlendImage,n as blendImageDefaultValues}; //# sourceMappingURL=BlendImage.min.mjs.map