Skip to content

Instantly share code, notes, and snippets.

@jayantbh
Forked from n1ru4l/offscreen-canvas-polyfill.js
Last active July 28, 2023 01:32
Show Gist options
  • Save jayantbh/059a25368ac9b8ecc3ad478e572db24a to your computer and use it in GitHub Desktop.
Save jayantbh/059a25368ac9b8ecc3ad478e572db24a to your computer and use it in GitHub Desktop.
offscreen-canvas-polyfill.js
if (!window.OffscreenCanvas) {
window.OffscreenCanvas = class OffscreenCanvas {
constructor(width, height) {
this.canvas = document.createElement('canvas');
this.canvas.width = width;
this.canvas.height = height;
this.canvas.transferToImageBitmap = () => {
const ctx = this.canvas.getContext('2d');
if (!ctx) return;
return ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);
};
this.canvas.convertToBlob = () => {
return new Promise((resolve) => {
this.canvas.toBlob(resolve);
});
};
return this.canvas;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment