import { UpploadEffect } from "../../effect"; import { IHandlersParams, ITemplateParams, IUpploadFile, } from "../../helpers/interfaces"; import { fitImageToContainer, safeListen, canvasToBlob, } from "../../helpers/elements"; export default class Flip extends UpploadEffect { name = "flip"; originalfileURL = ""; originalFile: IUpploadFile = { blob: new Blob() }; icon = ``; canvas: HTMLCanvasElement = document.createElement("canvas"); template = ({ file, translate }: ITemplateParams) => { const image = URL.createObjectURL(file.blob); this.originalfileURL = image; this.originalFile = file; return `
`; }; imageToCanvasBlob( params: IHandlersParams, flipH = false, flipV = false ): Promise { return new Promise((resolve) => { params.uppload.emitter.emit("processing"); const scaleH = flipH ? -1 : 1; const scaleV = flipV ? -1 : 1; this.canvas = document.createElement("canvas"); const image = document.createElement("img"); image.src = this.originalfileURL; image.onload = () => { this.canvas.width = image.width; this.canvas.height = image.height; const posX = flipH ? image.width * -1 : 0; const posY = flipV ? image.height * -1 : 0; const context = this.canvas.getContext("2d"); if (!context) return; context.clearRect(0, 0, this.canvas.width, this.canvas.height); context.scale(scaleH, scaleV); context.drawImage(image, posX, posY); canvasToBlob(this.canvas).then((blob) => { const image = URL.createObjectURL(blob); this.originalfileURL = image; params.uppload.emitter.emit("process"); return resolve(blob); }); }; }); } update(params: IHandlersParams, x: boolean, y: boolean) { const img = params.uppload.container.querySelector( ".uppload-flip img" ) as HTMLImageElement | null; if (!img) return; this.imageToCanvasBlob(params, x, y).then((blob) => { if (!blob) return; let file = this.originalFile; file.blob = blob; params.next(file); const image = URL.createObjectURL(blob); img.setAttribute("src", image); }); } handlers = (params: IHandlersParams) => { const img = params.uppload.container.querySelector( ".uppload-flip img" ) as HTMLImageElement | null; if (img) { fitImageToContainer(params, img).then(() => { const horizontal = params.uppload.container.querySelector( ".settings button.flip-btn-horizontal" ) as HTMLButtonElement | null; if (horizontal) safeListen( horizontal, "click", this.update.bind(this, params, true, false) ); const vertical = params.uppload.container.querySelector( ".settings button.flip-btn-vertical" ) as HTMLButtonElement | null; if (vertical) safeListen( vertical, "click", this.update.bind(this, params, false, true) ); }); } }; }