diff --git a/formats-browser.js b/formats-browser.js index 3092ac0..170be9f 100644 --- a/formats-browser.js +++ b/formats-browser.js @@ -1,20 +1,16 @@ -module.exports = {}; - -const initializeCanvas = ({ data, width, height }) => { +const initializeCanvas = ({ width, height }) => { const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; - const ctx = canvas.getContext('2d'); - - const imageData = new ImageData(data, width, height); - - ctx.putImageData(imageData, 0, 0); return canvas; }; const convert = async ({ data, width, height }, ...blobArgs) => { - const canvas = initializeCanvas({ data, width, height }); + const canvas = initializeCanvas({ width, height }); + + const ctx = canvas.getContext('2d'); + ctx.putImageData(new ImageData(data, width, height), 0, 0); const blob = await new Promise((resolve, reject) => { canvas.toBlob(blob => { @@ -31,10 +27,7 @@ const convert = async ({ data, width, height }, ...blobArgs) => { return new Uint8Array(arrayBuffer); }; -module.exports.JPEG = async ({ data, width, height, quality }) => { - return convert({ data, width, height }, 'image/jpeg', quality); -}; - -module.exports.PNG = async ({ data, width, height }) => { - return convert({ data, width, height }, 'image/png'); +module.exports = { + JPEG: async ({ data, width, height, quality }) => await convert({ data, width, height }, 'image/jpeg', quality), + PNG: async ({ data, width, height }) => await convert({ data, width, height }, 'image/png') };