Created
March 27, 2015 13:33
-
-
Save salzhrani/02a6e807f24785a4d34b 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
if( !HTMLCanvasElement.prototype.toBlob ) { | |
Object.defineProperty( HTMLCanvasElement.prototype, 'toBlob', | |
{ | |
value: function( callback, type, quality ) | |
{ | |
var bin = atob( this.toDataURL( type, quality ).split(',')[1] ), | |
len = bin.length, | |
len32 = len >> 2, | |
a8 = new Uint8Array( len ), | |
a32 = new Uint32Array( a8.buffer, 0, len32 ); | |
for( var i=0, j=0; i < len32; i++ ) | |
{ | |
a32[i] = bin.charCodeAt(j++) | | |
bin.charCodeAt(j++) << 8 | | |
bin.charCodeAt(j++) << 16 | | |
bin.charCodeAt(j++) << 24; | |
} | |
var tailLength = len & 3; | |
while( tailLength-- ) | |
{ | |
a8[ j ] = bin.charCodeAt(j++); | |
} | |
callback( new Blob( [a8], {'type': type || 'image/png'} ) ); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment