Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
catdad committed Nov 29, 2023
1 parent cef9b94 commit 343397f
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions formats-browser.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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')
};

0 comments on commit 343397f

Please sign in to comment.