UNPKG

fabric

Version:

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

1 lines 3.04 kB
{"version":3,"file":"Noise.min.mjs","sources":["../../../src/filters/Noise.ts"],"sourcesContent":["import { BaseFilter } from './BaseFilter';\nimport type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/noise';\n\nexport type NoiseOwnProps = {\n noise: number;\n};\n\nexport const noiseDefaultValues: NoiseOwnProps = {\n noise: 0,\n};\n\n/**\n * Noise filter class\n * @example\n * const filter = new Noise({\n * noise: 700\n * });\n * object.filters.push(filter);\n * object.applyFilters();\n * canvas.renderAll();\n */\nexport class Noise extends BaseFilter<'Noise', NoiseOwnProps> {\n /**\n * Noise value, from\n * @param {Number} noise\n * @default\n */\n declare noise: NoiseOwnProps['noise'];\n\n static type = 'Noise';\n\n static defaults = noiseDefaultValues;\n\n static uniformLocations = ['uNoise', 'uSeed'];\n\n getFragmentSource() {\n return fragmentSource;\n }\n\n /**\n * Apply the Brightness operation to a Uint8ClampedArray representing the pixels of an image.\n *\n * @param {Object} options\n * @param {ImageData} options.imageData The Uint8ClampedArray to be filtered.\n */\n applyTo2d({ imageData: { data } }: T2DPipelineState) {\n const noise = this.noise;\n for (let i = 0; i < data.length; i += 4) {\n const rand = (0.5 - Math.random()) * noise;\n data[i] += rand;\n data[i + 1] += rand;\n data[i + 2] += rand;\n }\n }\n\n /**\n * Send data from this filter to its shader program's uniforms.\n *\n * @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader.\n * @param {Object} uniformLocations A map of string uniform names to WebGLUniformLocation objects\n */\n sendUniformData(\n gl: WebGLRenderingContext,\n uniformLocations: TWebGLUniformLocationMap,\n ) {\n gl.uniform1f(uniformLocations.uNoise, this.noise / 255);\n gl.uniform1f(uniformLocations.uSeed, Math.random());\n }\n\n isNeutralState() {\n return this.noise === 0;\n }\n}\n\nclassRegistry.setClass(Noise);\n"],"names":["noiseDefaultValues","noise","Noise","BaseFilter","getFragmentSource","fragmentSource","applyTo2d","_ref","imageData","data","this","i","length","rand","Math","random","sendUniformData","gl","uniformLocations","uniform1f","uNoise","uSeed","isNeutralState","_defineProperty","classRegistry","setClass"],"mappings":"sPASO,MAAMA,EAAoC,CAC/CC,MAAO,GAaF,MAAMC,UAAcC,EAczBC,iBAAAA,GACE,OAAOC,CACT,CAQAC,SAAAA,CAASC,GAA4C,IAAzCC,WAAWC,KAAEA,IAA0BF,EACjD,MAAMN,EAAQS,KAAKT,MACnB,IAAK,IAAIU,EAAI,EAAGA,EAAIF,EAAKG,OAAQD,GAAK,EAAG,CACvC,MAAME,GAAQ,GAAMC,KAAKC,UAAYd,EACrCQ,EAAKE,IAAME,EACXJ,EAAKE,EAAI,IAAME,EACfJ,EAAKE,EAAI,IAAME,CACjB,CACF,CAQAG,eAAAA,CACEC,EACAC,GAEAD,EAAGE,UAAUD,EAAiBE,OAAQV,KAAKT,MAAQ,KACnDgB,EAAGE,UAAUD,EAAiBG,MAAOP,KAAKC,SAC5C,CAEAO,cAAAA,GACE,OAAsB,IAAfZ,KAAKT,KACd,EAjDAsB,EADWrB,EAAK,OAQF,SAAOqB,EARVrB,EAAK,WAUEF,GAAkBuB,EAVzBrB,EAAK,mBAYU,CAAC,SAAU,UAyCvCsB,EAAcC,SAASvB"}