Adding native encoders (#71)

* Adding browser png

* Adding native JPEG & file size output

* Removing log

* Fixing blob typing

* Fix timing issue
This commit is contained in:
Jake Archibald
2018-07-02 15:14:09 +01:00
committed by GitHub
parent 3f18c927f1
commit a09ec269b8
11 changed files with 14755 additions and 44 deletions

View File

@@ -50,3 +50,17 @@ export function drawBitmapToCanvas(canvas: HTMLCanvasElement, bitmap: ImageBitma
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(bitmap, 0, 0);
}
export async function canvasEncode(data: ImageData, type: string, quality?: number) {
const canvas = document.createElement('canvas');
canvas.width = data.width;
canvas.height = data.height;
const ctx = canvas.getContext('2d');
if (!ctx) throw Error('Canvas not initialized');
ctx.putImageData(data, 0, 0);
const blob = await new Promise<Blob | null>(r => canvas.toBlob(r, type, quality));
if (!blob) throw Error('Encoding failed');
return blob;
}