asciiblaster- draw irc art in your web browser |
git clone git://git.acid.vegas/asciiblaster.git |
Log | Files | Refs | Archive | README |
dataUriToBlob.js (1116B)
1 var dataUriToUint8Array = function(uri){ 2 var data = uri.split(',')[1]; 3 var bytes = atob(data); 4 var buf = new ArrayBuffer(bytes.length); 5 var u8 = new Uint8Array(buf); 6 for (var i = 0; i < bytes.length; i++) { 7 u8[i] = bytes.charCodeAt(i); 8 } 9 return u8 10 } 11 12 window.dataUriToBlob = (function(){ 13 /** 14 * Blob constructor. 15 */ 16 17 var Blob = window.Blob; 18 19 /** 20 * ArrayBufferView support. 21 */ 22 23 var hasArrayBufferView = new Blob([new Uint8Array(100)]).size == 100; 24 25 /** 26 * Return a `Blob` for the given data `uri`. 27 * 28 * @param {String} uri 29 * @return {Blob} 30 * @api public 31 */ 32 33 var dataUriToBlob = function(uri){ 34 var data = uri.split(',')[1]; 35 var bytes = atob(data); 36 var buf = new ArrayBuffer(bytes.length); 37 var arr = new Uint8Array(buf); 38 for (var i = 0; i < bytes.length; i++) { 39 arr[i] = bytes.charCodeAt(i); 40 } 41 42 if (!hasArrayBufferView) arr = buf; 43 var blob = new Blob([arr], { type: mime(uri) }); 44 blob.slice = blob.slice || blob.webkitSlice; 45 return blob; 46 }; 47 48 /** 49 * Return data uri mime type. 50 */ 51 52 function mime(uri) { 53 return uri.split(';')[0].slice(5); 54 } 55 56 return dataUriToBlob; 57 58 })()