Skip to content

Instantly share code, notes, and snippets.

@salzhrani
Created March 27, 2015 13:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salzhrani/02a6e807f24785a4d34b to your computer and use it in GitHub Desktop.
Save salzhrani/02a6e807f24785a4d34b to your computer and use it in GitHub Desktop.
canvas.toBlob polyfill
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