{"version":3,"file":"Noise.mjs","names":[],"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   */\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"],"mappings":";;;;;AASA,MAAa,qBAAoC,EAC/C,OAAO,GACR;;;;;;;;;;;AAYD,IAAa,QAAb,cAA2B,WAAmC;CAa5D,oBAAoB;AAClB,SAAO;;;;;;;;CAST,UAAU,EAAE,WAAW,EAAE,UAA4B;EACnD,MAAM,QAAQ,KAAK;AACnB,OAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;GACvC,MAAM,QAAQ,KAAM,KAAK,QAAQ,IAAI;AACrC,QAAK,MAAM;AACX,QAAK,IAAI,MAAM;AACf,QAAK,IAAI,MAAM;;;;;;;;;CAUnB,gBACE,IACA,kBACA;AACA,KAAG,UAAU,iBAAiB,QAAQ,KAAK,QAAQ,IAAI;AACvD,KAAG,UAAU,iBAAiB,OAAO,KAAK,QAAQ,CAAC;;CAGrD,iBAAiB;AACf,SAAO,KAAK,UAAU;;;uBAzCjB,QAAO,QAAQ;uBAEf,YAAW,mBAAmB;uBAE9B,oBAAmB,CAAC,UAAU,QAAQ,CAAC;AAyChD,cAAc,SAAS,MAAM"}