|
| 1 | +/* |
| 2 | + * JavaScript Canvas to Blob |
| 3 | + * https://github.com/blueimp/JavaScript-Canvas-to-Blob |
| 4 | + * |
| 5 | + * Copyright 2012, Sebastian Tschan |
| 6 | + * https://blueimp.net |
| 7 | + * |
| 8 | + * Licensed under the MIT license: |
| 9 | + * https://opensource.org/licenses/MIT |
| 10 | + * |
| 11 | + * Based on stackoverflow user Stoive's code snippet: |
| 12 | + * http://stackoverflow.com/q/4998908 |
| 13 | + */ |
| 14 | + |
| 15 | +/* global define, Uint8Array, ArrayBuffer, module */ |
| 16 | + |
| 17 | +;(function (window) { |
| 18 | + 'use strict' |
| 19 | + |
| 20 | + var CanvasPrototype = |
| 21 | + window.HTMLCanvasElement && window.HTMLCanvasElement.prototype |
| 22 | + var hasBlobConstructor = |
| 23 | + window.Blob && |
| 24 | + (function () { |
| 25 | + try { |
| 26 | + return Boolean(new Blob()) |
| 27 | + } catch (e) { |
| 28 | + return false |
| 29 | + } |
| 30 | + })() |
| 31 | + var hasArrayBufferViewSupport = |
| 32 | + hasBlobConstructor && |
| 33 | + window.Uint8Array && |
| 34 | + (function () { |
| 35 | + try { |
| 36 | + return new Blob([new Uint8Array(100)]).size === 100 |
| 37 | + } catch (e) { |
| 38 | + return false |
| 39 | + } |
| 40 | + })() |
| 41 | + var BlobBuilder = |
| 42 | + window.BlobBuilder || |
| 43 | + window.WebKitBlobBuilder || |
| 44 | + window.MozBlobBuilder || |
| 45 | + window.MSBlobBuilder |
| 46 | + var dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/ |
| 47 | + var dataURLtoBlob = |
| 48 | + (hasBlobConstructor || BlobBuilder) && |
| 49 | + window.atob && |
| 50 | + window.ArrayBuffer && |
| 51 | + window.Uint8Array && |
| 52 | + function (dataURI) { |
| 53 | + var matches, |
| 54 | + mediaType, |
| 55 | + isBase64, |
| 56 | + dataString, |
| 57 | + byteString, |
| 58 | + arrayBuffer, |
| 59 | + intArray, |
| 60 | + i, |
| 61 | + bb |
| 62 | + // Parse the dataURI components as per RFC 2397 |
| 63 | + matches = dataURI.match(dataURIPattern) |
| 64 | + if (!matches) { |
| 65 | + throw new Error('invalid data URI') |
| 66 | + } |
| 67 | + // Default to text/plain;charset=US-ASCII |
| 68 | + mediaType = matches[2] |
| 69 | + ? matches[1] |
| 70 | + : 'text/plain' + (matches[3] || ';charset=US-ASCII') |
| 71 | + isBase64 = !!matches[4] |
| 72 | + dataString = dataURI.slice(matches[0].length) |
| 73 | + if (isBase64) { |
| 74 | + // Convert base64 to raw binary data held in a string: |
| 75 | + byteString = atob(dataString) |
| 76 | + } else { |
| 77 | + // Convert base64/URLEncoded data component to raw binary: |
| 78 | + byteString = decodeURIComponent(dataString) |
| 79 | + } |
| 80 | + // Write the bytes of the string to an ArrayBuffer: |
| 81 | + arrayBuffer = new ArrayBuffer(byteString.length) |
| 82 | + intArray = new Uint8Array(arrayBuffer) |
| 83 | + for (i = 0; i < byteString.length; i += 1) { |
| 84 | + intArray[i] = byteString.charCodeAt(i) |
| 85 | + } |
| 86 | + // Write the ArrayBuffer (or ArrayBufferView) to a blob: |
| 87 | + if (hasBlobConstructor) { |
| 88 | + return new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], { |
| 89 | + type: mediaType |
| 90 | + }) |
| 91 | + } |
| 92 | + bb = new BlobBuilder() |
| 93 | + bb.append(arrayBuffer) |
| 94 | + return bb.getBlob(mediaType) |
| 95 | + } |
| 96 | + if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) { |
| 97 | + if (CanvasPrototype.mozGetAsFile) { |
| 98 | + CanvasPrototype.toBlob = function (callback, type, quality) { |
| 99 | + var self = this |
| 100 | + setTimeout(function () { |
| 101 | + if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) { |
| 102 | + callback(dataURLtoBlob(self.toDataURL(type, quality))) |
| 103 | + } else { |
| 104 | + callback(self.mozGetAsFile('blob', type)) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | + } else if (CanvasPrototype.toDataURL && dataURLtoBlob) { |
| 109 | + CanvasPrototype.toBlob = function (callback, type, quality) { |
| 110 | + var self = this |
| 111 | + setTimeout(function () { |
| 112 | + callback(dataURLtoBlob(self.toDataURL(type, quality))) |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + if (typeof define === 'function' && define.amd) { |
| 118 | + define(function () { |
| 119 | + return dataURLtoBlob |
| 120 | + }) |
| 121 | + } else if (typeof module === 'object' && module.exports) { |
| 122 | + module.exports = dataURLtoBlob |
| 123 | + } else { |
| 124 | + window.dataURLtoBlob = dataURLtoBlob |
| 125 | + } |
| 126 | +})(window) |
0 commit comments