UNPKG

fabric

Version:

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

1 lines 4.33 kB
{"version":3,"file":"Canvas2dFilterBackend.mjs","sources":["../../../src/filters/Canvas2dFilterBackend.ts"],"sourcesContent":["/**\n * Canvas 2D filter backend.\n */\nimport type { BaseFilter } from './BaseFilter';\nimport type { T2DPipelineState, TPipelineResources } from './typedefs';\n\nexport class Canvas2dFilterBackend {\n /**\n * Experimental. This object is a sort of repository of help layers used to avoid\n * of recreating them during frequent filtering. If you are previewing a filter with\n * a slider you probably do not want to create help layers every filter step.\n * in this object there will be appended some canvases, created once, resized sometimes\n * cleared never. Clearing is left to the developer.\n **/\n resources: TPipelineResources = {};\n\n /**\n * Apply a set of filters against a source image and draw the filtered output\n * to the provided destination canvas.\n *\n * @param {EnhancedFilter} filters The filter to apply.\n * @param {HTMLImageElement|HTMLCanvasElement} sourceElement The source to be filtered.\n * @param {Number} sourceWidth The width of the source input.\n * @param {Number} sourceHeight The height of the source input.\n * @param {HTMLCanvasElement} targetCanvas The destination for filtered output to be drawn.\n */\n applyFilters(\n filters: BaseFilter<string, Record<string, any>>[],\n sourceElement: CanvasImageSource,\n sourceWidth: number,\n sourceHeight: number,\n targetCanvas: HTMLCanvasElement,\n ): T2DPipelineState | void {\n const ctx = targetCanvas.getContext('2d');\n if (!ctx) {\n return;\n }\n ctx.drawImage(sourceElement, 0, 0, sourceWidth, sourceHeight);\n const imageData = ctx.getImageData(0, 0, sourceWidth, sourceHeight);\n const originalImageData = ctx.getImageData(0, 0, sourceWidth, sourceHeight);\n const pipelineState: T2DPipelineState = {\n sourceWidth,\n sourceHeight,\n imageData,\n originalEl: sourceElement,\n originalImageData,\n canvasEl: targetCanvas,\n ctx,\n filterBackend: this,\n };\n filters.forEach((filter) => {\n filter.applyTo(pipelineState);\n });\n const { imageData: imageDataPostFilter } = pipelineState;\n if (\n imageDataPostFilter.width !== sourceWidth ||\n imageDataPostFilter.height !== sourceHeight\n ) {\n targetCanvas.width = imageDataPostFilter.width;\n targetCanvas.height = imageDataPostFilter.height;\n }\n ctx.putImageData(imageDataPostFilter, 0, 0);\n return pipelineState;\n }\n}\n"],"names":["Canvas2dFilterBackend","constructor","_defineProperty","applyFilters","filters","sourceElement","sourceWidth","sourceHeight","targetCanvas","ctx","getContext","drawImage","imageData","getImageData","originalImageData","pipelineState","originalEl","canvasEl","filterBackend","forEach","filter","applyTo","imageDataPostFilter","width","height","putImageData"],"mappings":";;AAAA;AACA;AACA;;AAIO,MAAMA,qBAAqB,CAAC;EAAAC,WAAA,GAAA;AACjC;AACF;AACA;AACA;AACA;AACA;AACA;IANEC,eAAA,CAAA,IAAA,EAAA,WAAA,EAOgC,EAAE,CAAA,CAAA;AAAA,GAAA;AAElC;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEC,YAAYA,CACVC,OAAkD,EAClDC,aAAgC,EAChCC,WAAmB,EACnBC,YAAoB,EACpBC,YAA+B,EACN;AACzB,IAAA,MAAMC,GAAG,GAAGD,YAAY,CAACE,UAAU,CAAC,IAAI,CAAC,CAAA;IACzC,IAAI,CAACD,GAAG,EAAE;AACR,MAAA,OAAA;AACF,KAAA;AACAA,IAAAA,GAAG,CAACE,SAAS,CAACN,aAAa,EAAE,CAAC,EAAE,CAAC,EAAEC,WAAW,EAAEC,YAAY,CAAC,CAAA;AAC7D,IAAA,MAAMK,SAAS,GAAGH,GAAG,CAACI,YAAY,CAAC,CAAC,EAAE,CAAC,EAAEP,WAAW,EAAEC,YAAY,CAAC,CAAA;AACnE,IAAA,MAAMO,iBAAiB,GAAGL,GAAG,CAACI,YAAY,CAAC,CAAC,EAAE,CAAC,EAAEP,WAAW,EAAEC,YAAY,CAAC,CAAA;AAC3E,IAAA,MAAMQ,aAA+B,GAAG;MACtCT,WAAW;MACXC,YAAY;MACZK,SAAS;AACTI,MAAAA,UAAU,EAAEX,aAAa;MACzBS,iBAAiB;AACjBG,MAAAA,QAAQ,EAAET,YAAY;MACtB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