From 6f94ca1a4afe960c460dc06e728d395f94ea91b1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 18 Nov 2020 16:28:48 +0100 Subject: [PATCH] make toBlob work on IE11 --- src/platform/web/dom/ImageHandle.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/platform/web/dom/ImageHandle.js b/src/platform/web/dom/ImageHandle.js index dcead27c..c8b08e04 100644 --- a/src/platform/web/dom/ImageHandle.js +++ b/src/platform/web/dom/ImageHandle.js @@ -53,10 +53,16 @@ export class ImageHandle { const ctx = canvas.getContext("2d"); const img = await this._getImgElement(); ctx.drawImage(img, 0, 0, scaledWidth, scaledHeight); - const mimeType = this.blob.mimeType === "image/jpeg" ? "image/jpeg" : "image/png"; - const nativeBlob = await new Promise(resolve => { - canvas.toBlob(resolve, mimeType); - }); + let mimeType = this.blob.mimeType === "image/jpeg" ? "image/jpeg" : "image/png"; + let nativeBlob; + if (canvas.toBlob) { + nativeBlob = await new Promise(resolve => canvas.toBlob(resolve, mimeType)); + } else if (canvas.msToBlob) { + mimeType = "image/png"; + nativeBlob = canvas.msToBlob(); + } else { + throw new Error("canvas can't be turned into blob"); + } const blob = BlobHandle.fromBlob(nativeBlob); return new ImageHandle(blob, scaledWidth, scaledHeight, null); }