UNPKG

fabric

Version:

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

1 lines 3.47 kB
{"version":3,"file":"Pixelate.mjs","names":[],"sources":["../../../src/filters/Pixelate.ts"],"sourcesContent":["import { BaseFilter } from './BaseFilter';\nimport type { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { fragmentSource } from './shaders/pixelate';\n\nexport type PixelateOwnProps = {\n blocksize: number;\n};\n\nexport const pixelateDefaultValues: PixelateOwnProps = {\n blocksize: 4,\n};\n\n/**\n * Pixelate filter class\n * @example\n * const filter = new Pixelate({\n * blocksize: 8\n * });\n * object.filters.push(filter);\n * object.applyFilters();\n */\nexport class Pixelate extends BaseFilter<'Pixelate', PixelateOwnProps> {\n declare blocksize: PixelateOwnProps['blocksize'];\n\n static type = 'Pixelate';\n\n static defaults = pixelateDefaultValues;\n\n static uniformLocations = ['uBlocksize'];\n\n /**\n * Apply the Pixelate 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, width, height } }: T2DPipelineState) {\n for (let i = 0; i < height; i += this.blocksize) {\n for (let j = 0; j < width; j += this.blocksize) {\n const index = i * 4 * width + j * 4;\n const r = data[index];\n const g = data[index + 1];\n const b = data[index + 2];\n const a = data[index + 3];\n\n for (let _i = i; _i < Math.min(i + this.blocksize, height); _i++) {\n for (let _j = j; _j < Math.min(j + this.blocksize, width); _j++) {\n const index = _i * 4 * width + _j * 4;\n data[index] = r;\n data[index + 1] = g;\n data[index + 2] = b;\n data[index + 3] = a;\n }\n }\n }\n }\n }\n\n /**\n * Indicate when the filter is not gonna apply changes to the image\n **/\n isNeutralState() {\n return this.blocksize === 1;\n }\n\n protected getFragmentSource(): string {\n return fragmentSource;\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.uBlocksize, this.blocksize);\n }\n}\n\nclassRegistry.setClass(Pixelate);\n"],"mappings":";;;;;AASA,MAAa,wBAA0C,EACrD,WAAW,GACZ;;;;;;;;;;AAWD,IAAa,WAAb,cAA8B,WAAyC;;;;;;;CAerE,UAAU,EAAE,WAAW,EAAE,MAAM,OAAO,YAA8B;AAClE,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAK,KAAK,UACpC,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK,KAAK,WAAW;GAC9C,MAAM,QAAQ,IAAI,IAAI,QAAQ,IAAI;GAClC,MAAM,IAAI,KAAK;GACf,MAAM,IAAI,KAAK,QAAQ;GACvB,MAAM,IAAI,KAAK,QAAQ;GACvB,MAAM,IAAI,KAAK,QAAQ;AAEvB,QAAK,IAAI,KAAK,GAAG,KAAK,KAAK,IAAI,IAAI,KAAK,WAAW,OAAO,EAAE,KAC1D,MAAK,IAAI,KAAK,GAAG,KAAK,KAAK,IAAI,IAAI,KAAK,WAAW,MAAM,EAAE,MAAM;IAC/D,MAAM,QAAQ,KAAK,IAAI,QAAQ,KAAK;AACpC,SAAK,SAAS;AACd,SAAK,QAAQ,KAAK;AAClB,SAAK,QAAQ,KAAK;AAClB,SAAK,QAAQ,KAAK;;;;;;;CAU5B,iBAAiB;AACf,SAAO,KAAK,cAAc;;CAG5B,oBAAsC;AACpC,SAAO;;;;;;;;CAST,gBACE,IACA,kBACA;AACA,KAAG,UAAU,iBAAiB,YAAY,KAAK,UAAU;;;0BAvDpD,QAAO,WAAW;0BAElB,YAAW,sBAAsB;0BAEjC,oBAAmB,CAAC,aAAa,CAAC;AAuD3C,cAAc,SAAS,SAAS"}