Created
May 10, 2018 16:37
-
-
Save NatasaPeic/d5d0bf2cdd60eea780a1b6bde2c61fa2 to your computer and use it in GitHub Desktop.
Canvas toBlob() polyfill
This file contains 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
(function(global) { | |
if (!('HTMLCanvasElement' in global)) return; | |
if (!('toDataURL' in global.HTMLCanvasElement.prototype)) return; | |
if ('toBlob' in global.HTMLCanvasElement.prototype) return; | |
Object.defineProperty(global.HTMLCanvasElement.prototype, 'toBlob', { | |
value: function(callback/*, type, encoderOptions*/) { | |
var url = this.toDataURL.apply(this, [].slice.call(arguments, 1)); | |
var m = /^data:(.*?);base64,(.*)$/.exec(url), type = m[1], b64 = m[2]; | |
setTimeout(function() { | |
callback(new Blob([ | |
new Uint8Array(global.atob(b64).split('').map(function(c) { | |
return c.charCodeAt(0); | |
}))], {type: type})); | |
}, 0); | |
}, writable: true, enumerable: true, configurable: true}); | |
}(self)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment