-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(TS): Converting the filtering backend to ts + es6 (#8403)
- Loading branch information
Showing
8 changed files
with
935 additions
and
848 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,64 @@ | ||
//@ts-nocheck | ||
(function (global) { | ||
var fabric = global.fabric, | ||
noop = function () {}; | ||
/** | ||
* Canvas 2D filter backend. | ||
*/ | ||
import { T2DPipelineState } from './typedefs'; | ||
|
||
fabric.Canvas2dFilterBackend = Canvas2dFilterBackend; | ||
export class Canvas2dFilterBackend { | ||
/** | ||
* Experimental. This object is a sort of repository of help layers used to avoid | ||
* of recreating them during frequent filtering. If you are previewing a filter with | ||
* a slider you probably do not want to create help layers every filter step. | ||
* in this object there will be appended some canvases, created once, resized sometimes | ||
* cleared never. Clearing is left to the developer. | ||
**/ | ||
resources = {}; | ||
|
||
/** | ||
* Canvas 2D filter backend. | ||
* Apply a set of filters against a source image and draw the filtered output | ||
* to the provided destination canvas. | ||
* | ||
* @param {EnhancedFilter} filters The filter to apply. | ||
* @param {HTMLImageElement|HTMLCanvasElement} sourceElement The source to be filtered. | ||
* @param {Number} sourceWidth The width of the source input. | ||
* @param {Number} sourceHeight The height of the source input. | ||
* @param {HTMLCanvasElement} targetCanvas The destination for filtered output to be drawn. | ||
*/ | ||
function Canvas2dFilterBackend() {} | ||
|
||
Canvas2dFilterBackend.prototype = | ||
/** @lends fabric.Canvas2dFilterBackend.prototype */ { | ||
evictCachesForKey: noop, | ||
dispose: noop, | ||
clearWebGLCaches: noop, | ||
|
||
/** | ||
* Experimental. This object is a sort of repository of help layers used to avoid | ||
* of recreating them during frequent filtering. If you are previewing a filter with | ||
* a slider you probably do not want to create help layers every filter step. | ||
* in this object there will be appended some canvases, created once, resized sometimes | ||
* cleared never. Clearing is left to the developer. | ||
**/ | ||
resources: {}, | ||
|
||
/** | ||
* Apply a set of filters against a source image and draw the filtered output | ||
* to the provided destination canvas. | ||
* | ||
* @param {EnhancedFilter} filters The filter to apply. | ||
* @param {HTMLImageElement|HTMLCanvasElement} sourceElement The source to be filtered. | ||
* @param {Number} sourceWidth The width of the source input. | ||
* @param {Number} sourceHeight The height of the source input. | ||
* @param {HTMLCanvasElement} targetCanvas The destination for filtered output to be drawn. | ||
*/ | ||
applyFilters: function ( | ||
filters, | ||
sourceElement, | ||
sourceWidth, | ||
sourceHeight, | ||
targetCanvas | ||
) { | ||
var ctx = targetCanvas.getContext('2d'); | ||
ctx.drawImage(sourceElement, 0, 0, sourceWidth, sourceHeight); | ||
var imageData = ctx.getImageData(0, 0, sourceWidth, sourceHeight); | ||
var originalImageData = ctx.getImageData( | ||
0, | ||
0, | ||
sourceWidth, | ||
sourceHeight | ||
); | ||
var pipelineState = { | ||
sourceWidth: sourceWidth, | ||
sourceHeight: sourceHeight, | ||
imageData: imageData, | ||
originalEl: sourceElement, | ||
originalImageData: originalImageData, | ||
canvasEl: targetCanvas, | ||
ctx: ctx, | ||
filterBackend: this, | ||
}; | ||
filters.forEach(function (filter) { | ||
filter.applyTo(pipelineState); | ||
}); | ||
if ( | ||
pipelineState.imageData.width !== sourceWidth || | ||
pipelineState.imageData.height !== sourceHeight | ||
) { | ||
targetCanvas.width = pipelineState.imageData.width; | ||
targetCanvas.height = pipelineState.imageData.height; | ||
} | ||
ctx.putImageData(pipelineState.imageData, 0, 0); | ||
return pipelineState; | ||
}, | ||
applyFilters( | ||
filters: any[], | ||
sourceElement: HTMLImageElement | HTMLCanvasElement, | ||
sourceWidth: number, | ||
sourceHeight: number, | ||
targetCanvas: HTMLCanvasElement | ||
): T2DPipelineState | void { | ||
const ctx = targetCanvas.getContext('2d'); | ||
if (!ctx) { | ||
return; | ||
} | ||
ctx.drawImage(sourceElement, 0, 0, sourceWidth, sourceHeight); | ||
const imageData = ctx.getImageData(0, 0, sourceWidth, sourceHeight); | ||
const originalImageData = ctx.getImageData(0, 0, sourceWidth, sourceHeight); | ||
const pipelineState: T2DPipelineState = { | ||
sourceWidth, | ||
sourceHeight, | ||
imageData, | ||
originalEl: sourceElement, | ||
originalImageData, | ||
canvasEl: targetCanvas, | ||
ctx, | ||
filterBackend: this, | ||
}; | ||
})(typeof exports !== 'undefined' ? exports : window); | ||
filters.forEach(function (filter) { | ||
filter.applyTo(pipelineState); | ||
}); | ||
const { imageData: imageDataPostFilter } = pipelineState; | ||
if ( | ||
imageDataPostFilter.width !== sourceWidth || | ||
imageDataPostFilter.height !== sourceHeight | ||
) { | ||
targetCanvas.width = imageDataPostFilter.width; | ||
targetCanvas.height = imageDataPostFilter.height; | ||
} | ||
ctx.putImageData(imageDataPostFilter, 0, 0); | ||
return pipelineState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.