Skip to content

Instantly share code, notes, and snippets.

@Convicted202
Last active October 16, 2020 07:37
Show Gist options
  • Save Convicted202/7684bc8113b3011b4a6a1b2aa9f7a36f to your computer and use it in GitHub Desktop.
Save Convicted202/7684bc8113b3011b4a6a1b2aa9f7a36f to your computer and use it in GitHub Desktop.
ImageData Polyfill IE11
(() => {
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;
}
})();
@doouding
Copy link

doouding commented Aug 1, 2018

It works. Thx. =D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment