This commit is contained in:
Kiril Vatev 2023-11-29 02:19:28 -05:00
parent cef9b94645
commit 343397f493

View File

@ -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')
};