Last active
October 16, 2020 07:37
-
-
Save Convicted202/7684bc8113b3011b4a6a1b2aa9f7a36f to your computer and use it in GitHub Desktop.
ImageData Polyfill IE11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
try { | |
new window.ImageData(new Uint8ClampedArray([0, 0, 0, 0]), 1, 1); | |
} catch (e) { | |
function ImageDataPolyfill () { | |
let args = [...arguments], data; | |
if (args.length < 2) { | |
throw new TypeError(` | |
Failed to construct 'ImageData': 2 arguments required, but only ${args.length} present. | |
`); | |
} | |
if (args.length > 2) { | |
data = args.shift(); | |
if (!(data instanceof Uint8ClampedArray)) { | |
throw new TypeError(` | |
Failed to construct 'ImageData': parameter 1 is not of type 'Uint8ClampedArray' | |
`); | |
} | |
if (data.length !== 4 * args[0] * args[1]) { | |
throw new Error(` | |
Failed to construct 'ImageData': The input data byte length is not a multiple of (4 * width * height) | |
`); | |
} | |
} | |
const width = args[0], | |
height = args[1], | |
canvas = document.createElement('canvas'), | |
ctx = canvas.getContext('2d'), | |
imageData = ctx.createImageData(width, height); | |
if (data) imageData.data.set(data); | |
return imageData; | |
}; | |
window.ImageData = ImageDataPolyfill; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works. Thx. =D