Skip to content

Instantly share code, notes, and snippets.

@uupaa
Created December 16, 2014 11:20
Show Gist options
  • Select an option

  • Save uupaa/cbc17990ea5590a0a912 to your computer and use it in GitHub Desktop.

Select an option

Save uupaa/cbc17990ea5590a0a912 to your computer and use it in GitHub Desktop.
Canvas.toBlob
function Canvas_toBlob(canvas,   // @arg HTMLCanvasElement
                       callback, // @arg Function - callback(blob:Blob)
                       type) {   // @arg MimeTypeString = "image/png"
    type = type || "image/png";

    if (canvas.toBlob) {
        canvas.toBlob(callback, type);
    } else {
        var dataURL = canvas.toDataURL(type);
        var binary  = atob(dataURL.split(",")[1]);
        var buffer  = new Uint8Array(binary.length);

        for (var i = 0; i < binary.length; ++i) {
            buffer[i] = binary.charCodeAt(i);
        }
        var blob = new Blob([buffer.buffer], { "type": type });

        callback(blob);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment