blueimp-canvas-to-blob
Advanced tools
Comparing version 2.2.0 to 2.2.1
/* | ||
* JavaScript Canvas to Blob 2.0.5 | ||
* JavaScript Canvas to Blob | ||
* https://github.com/blueimp/JavaScript-Canvas-to-Blob | ||
@@ -39,16 +39,31 @@ * | ||
window.MozBlobBuilder || window.MSBlobBuilder, | ||
dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/, | ||
dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob && | ||
window.ArrayBuffer && window.Uint8Array && function (dataURI) { | ||
var byteString, | ||
var matches, | ||
mediaType, | ||
isBase64, | ||
dataString, | ||
byteString, | ||
arrayBuffer, | ||
intArray, | ||
i, | ||
mimeString, | ||
bb; | ||
if (dataURI.split(',')[0].indexOf('base64') >= 0) { | ||
// Parse the dataURI components as per RFC 2397 | ||
matches = dataURI.match(dataURIPattern); | ||
if (!matches) { | ||
throw new Error('invalid data URI'); | ||
} | ||
// Default to text/plain;charset=US-ASCII | ||
mediaType = matches[2] ? | ||
matches[1] : | ||
'text/plain' + (matches[3] || ';charset=US-ASCII'); | ||
isBase64 = !!matches[4]; | ||
dataString = dataURI.slice(matches[0].length); | ||
if (isBase64) { | ||
// Convert base64 to raw binary data held in a string: | ||
byteString = atob(dataURI.split(',')[1]); | ||
byteString = atob(dataString); | ||
} else { | ||
// Convert base64/URLEncoded data component to raw binary data: | ||
byteString = decodeURIComponent(dataURI.split(',')[1]); | ||
// Convert base64/URLEncoded data component to raw binary: | ||
byteString = decodeURIComponent(dataString); | ||
} | ||
@@ -61,4 +76,2 @@ // Write the bytes of the string to an ArrayBuffer: | ||
} | ||
// Separate out the mime component: | ||
mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; | ||
// Write the ArrayBuffer (or ArrayBufferView) to a blob: | ||
@@ -68,3 +81,3 @@ if (hasBlobConstructor) { | ||
[hasArrayBufferViewSupport ? intArray : arrayBuffer], | ||
{type: mimeString} | ||
{type: mediaType} | ||
); | ||
@@ -74,3 +87,3 @@ } | ||
bb.append(arrayBuffer); | ||
return bb.getBlob(mimeString); | ||
return bb.getBlob(mediaType); | ||
}; | ||
@@ -77,0 +90,0 @@ if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) { |
@@ -1,1 +0,1 @@ | ||
!function(a){"use strict";var b=a.HTMLCanvasElement&&a.HTMLCanvasElement.prototype,c=a.Blob&&function(){try{return Boolean(new Blob)}catch(a){return!1}}(),d=c&&a.Uint8Array&&function(){try{return 100===new Blob([new Uint8Array(100)]).size}catch(a){return!1}}(),e=a.BlobBuilder||a.WebKitBlobBuilder||a.MozBlobBuilder||a.MSBlobBuilder,f=(c||e)&&a.atob&&a.ArrayBuffer&&a.Uint8Array&&function(a){var b,f,g,h,i,j;for(b=a.split(",")[0].indexOf("base64")>=0?atob(a.split(",")[1]):decodeURIComponent(a.split(",")[1]),f=new ArrayBuffer(b.length),g=new Uint8Array(f),h=0;h<b.length;h+=1)g[h]=b.charCodeAt(h);return i=a.split(",")[0].split(":")[1].split(";")[0],c?new Blob([d?g:f],{type:i}):(j=new e,j.append(f),j.getBlob(i))};a.HTMLCanvasElement&&!b.toBlob&&(b.mozGetAsFile?b.toBlob=function(a,c,d){a(d&&b.toDataURL&&f?f(this.toDataURL(c,d)):this.mozGetAsFile("blob",c))}:b.toDataURL&&f&&(b.toBlob=function(a,b,c){a(f(this.toDataURL(b,c)))})),"function"==typeof define&&define.amd?define(function(){return f}):a.dataURLtoBlob=f}(window); | ||
!function(t){"use strict";var e=t.HTMLCanvasElement&&t.HTMLCanvasElement.prototype,n=t.Blob&&function(){try{return Boolean(new Blob)}catch(t){return!1}}(),o=n&&t.Uint8Array&&function(){try{return 100===new Blob([new Uint8Array(100)]).size}catch(t){return!1}}(),r=t.BlobBuilder||t.WebKitBlobBuilder||t.MozBlobBuilder||t.MSBlobBuilder,a=/^data:((.*?)(;charset=.*?)?)(;base64)?,/,i=(n||r)&&t.atob&&t.ArrayBuffer&&t.Uint8Array&&function(t){var e,i,l,u,B,b,c,f,d;if(e=t.match(a),!e)throw new Error("invalid data URI");for(i=e[2]?e[1]:"text/plain"+(e[3]||";charset=US-ASCII"),l=!!e[4],u=t.slice(e[0].length),B=l?atob(u):decodeURIComponent(u),b=new ArrayBuffer(B.length),c=new Uint8Array(b),f=0;f<B.length;f+=1)c[f]=B.charCodeAt(f);return n?new Blob([o?c:b],{type:i}):(d=new r,d.append(b),d.getBlob(i))};t.HTMLCanvasElement&&!e.toBlob&&(e.mozGetAsFile?e.toBlob=function(t,n,o){t(o&&e.toDataURL&&i?i(this.toDataURL(n,o)):this.mozGetAsFile("blob",n))}:e.toDataURL&&i&&(e.toBlob=function(t,e,n){t(i(this.toDataURL(e,n)))})),"function"==typeof define&&define.amd?define(function(){return i}):t.dataURLtoBlob=i}(window); |
{ | ||
"name": "blueimp-canvas-to-blob", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"title": "JavaScript Canvas to Blob", | ||
@@ -32,8 +32,9 @@ "description": "JavaScript Canvas to Blob is a function to convert canvas elements into Blob objects.", | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-uglify": "~0.2.7", | ||
"grunt-contrib-jshint": "~0.7.1", | ||
"grunt-bump-build-git": "~1.0.0", | ||
"expect.js": "0.2.0" | ||
"jshint": "2.8.0", | ||
"uglify-js": "2.6.1" | ||
}, | ||
"scripts": { | ||
"test": "jshint js/canvas-to-blob.js test/test.js", | ||
"build": "uglifyjs js/canvas-to-blob.js -c -m > js/canvas-to-blob.min.js" | ||
} | ||
} |
# JavaScript Canvas to Blob | ||
## Description | ||
Canvas to Blob is a polyfill for the standard JavaScript [canvas.toBlob](http://www.w3.org/TR/html5/scripting-1.html#dom-canvas-toblob) method. | ||
Canvas to Blob is a polyfill for the standard JavaScript | ||
[canvas.toBlob](http://www.w3.org/TR/html5/scripting-1.html#dom-canvas-toblob) | ||
method. | ||
It can be used to create [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects from an HTML [canvas](https://developer.mozilla.org/en-US/docs/HTML/Canvas) element. | ||
It can be used to create | ||
[Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) | ||
objects from an HTML | ||
[canvas](https://developer.mozilla.org/en-US/docs/HTML/Canvas) element. | ||
@@ -15,6 +20,7 @@ ## Usage | ||
Then use the *canvas.toBlob()* method in the same way as the native implementation: | ||
Then use the *canvas.toBlob()* method in the same way as the native | ||
implementation: | ||
```js | ||
var canvas = document.createElement('canvas'); | ||
var canvas = document.createElement('canvas'); | ||
/* ... your canvas manipulations ... */ | ||
@@ -38,6 +44,10 @@ if (canvas.toBlob) { | ||
However, Canvas to Blob is a very suitable complement to the [JavaScript Load Image](https://github.com/blueimp/JavaScript-Load-Image) function. | ||
However, Canvas to Blob is a very suitable complement to the | ||
[JavaScript Load Image](https://github.com/blueimp/JavaScript-Load-Image) | ||
function. | ||
## API | ||
In addition to the **canvas.toBlob** polyfill, the JavaScript Canvas to Blob script provides one additional function called **dataURLtoBlob**, which is added to the global window object if no AMD loader is used to load the script: | ||
In addition to the **canvas.toBlob** polyfill, the JavaScript Canvas to Blob | ||
script provides one additional function called **dataURLtoBlob**, which is added | ||
to the global window object if no AMD loader is used to load the script: | ||
@@ -54,3 +64,4 @@ ```js | ||
## Browsers | ||
The following browsers support either the native or the polyfill *canvas.toBlob()* method: | ||
The following browsers support either the native or the polyfill | ||
*canvas.toBlob()* method: | ||
@@ -74,2 +85,3 @@ ### Desktop browsers | ||
## License | ||
The JavaScript Canvas to Blob script is released under the [MIT license](http://www.opensource.org/licenses/MIT). | ||
The JavaScript Canvas to Blob script is released under the | ||
[MIT license](http://www.opensource.org/licenses/MIT). |
/* | ||
* JavaScript Canvas to Blob Test 2.1.0 | ||
* JavaScript Canvas to Blob Test | ||
* https://github.com/blueimp/JavaScript-Canvas-to-Blob | ||
@@ -12,3 +12,3 @@ * | ||
/*global window, describe, it, expect, Blob */ | ||
/*global window, describe, it, Blob */ | ||
@@ -15,0 +15,0 @@ (function (expect) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19274
2
84
8
195