Skip to content

Commit 4e45358

Browse files
committed
Provide a polyfill for canvas.toBlob instead of a custom global function.
Expose dataURLtoBlob as utility function.
1 parent 7b7994e commit 4e45358

File tree

5 files changed

+88
-131
lines changed

5 files changed

+88
-131
lines changed

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,23 @@ Include the (minified) JavaScript Canvas to Blob script in your HTML markup:
77
<script src="canvas-to-blob.min.js"></script>
88
```
99

10-
In your application code, use the **canvasToBlob()** function like this:
10+
The JavaScript Canvas to Blob script is a polyfill for the standard HTML [canvas.toBlob](http://www.w3.org/TR/html5/the-canvas-element.html#dom-canvas-toblob) method:
1111

1212
```js
1313
var canvas = document.createElement('canvas');
1414
/* ... your canvas manipulations ... */
15-
if(!canvasToBlob(
16-
canvas,
17-
function (blob) {
18-
// Do something with the blob object,
19-
// e.g. creating a multipart form for file uploads:
20-
var formData = new FormData();
21-
formData.append('file', blob, fileName);
22-
/* ... */
23-
},
24-
{
25-
type: fileType,
26-
name: fileName // Only used by Mozilla Firefox
27-
}
28-
)) {
29-
/* ... alternative code for unsupported browsers ... */
30-
};
15+
if (canvas.toBlob) {
16+
canvas.toBlob(
17+
function (blob) {
18+
// Do something with the blob object,
19+
// e.g. creating a multipart form for file uploads:
20+
var formData = new FormData();
21+
formData.append('file', blob, fileName);
22+
/* ... */
23+
},
24+
'image/jpeg'
25+
);
26+
}
3127
```
3228

3329
## Requirements
@@ -36,9 +32,16 @@ The JavaScript Canvas to Blob function has zero dependencies.
3632
However, Canvas to Blob is a very suitable complement to the [JavaScript Load Image](https://github.com/blueimp/JavaScript-Load-Image) function.
3733

3834
## API
39-
The **canvasToBlob()** function expects a [canvas](https://developer.mozilla.org/en/HTML/Element/canvas) element as first argument and a callback function as second argument. An options object with the properties name (e.g. "image.png") and image (e.g. "image/png") can be provided as optional third argument.
35+
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:
4036

41-
The function returns **true** if the browser supports canvas to blob conversion. It calls the provided callback function with the created blob as argument.
37+
```js
38+
// 80x60px GIF image (color black, base64 data):
39+
var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
40+
'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
41+
'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7',
42+
imageUrl = 'data:image/gif;base64,' + b64Data,
43+
blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl);
44+
```
4245

4346
## License
4447
The JavaScript Canvas to Blob script is released under the [MIT license](http://www.opensource.org/licenses/MIT).

canvas-to-blob.js

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Canvas to Blob 1.0.1
2+
* JavaScript Canvas to Blob 2.0
33
* https://github.com/blueimp/JavaScript-Canvas-to-Blob
44
*
55
* Copyright 2012, Sebastian Tschan
@@ -15,71 +15,56 @@
1515
/*jslint nomen: true, regexp: true */
1616
/*global window, atob, ArrayBuffer, Uint8Array, define */
1717

18-
(function ($) {
18+
(function (window) {
1919
'use strict';
20-
21-
var BlobBuilder = window.MozBlobBuilder ||
22-
window.WebKitBlobBuilder || window.BlobBuilder,
23-
blobTypes = /^image\/(jpeg|png)$/,
24-
25-
// Converts a canvas element to a Blob or File object:
26-
canvasToBlob = function (canvas, callback, options) {
27-
options = options || {};
28-
if (canvas.toBlob) {
29-
canvas.toBlob(callback, options.type);
30-
return true;
31-
} else if (canvas.mozGetAsFile) {
32-
var name = options.name;
33-
callback(canvas.mozGetAsFile(
34-
(blobTypes.test(options.type) && name) ||
35-
((name && name.replace(/\..+$/, '')) || 'blob') + '.png',
36-
options.type
37-
));
38-
return true;
39-
} else if (canvas.toDataURL && BlobBuilder && window.atob &&
40-
window.ArrayBuffer && window.Uint8Array) {
41-
callback(canvasToBlob.dataURItoBlob(
42-
canvas.toDataURL(options.type)
43-
));
44-
return true;
45-
}
46-
return false;
47-
};
48-
49-
// Converts a dataURI to a Blob:
50-
canvasToBlob.dataURItoBlob = function (dataURI) {
51-
var byteString,
52-
arrayBuffer,
53-
intArray,
54-
i,
55-
bb,
56-
mimeString;
57-
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
58-
// Convert base64 to raw binary data held in a string:
59-
byteString = atob(dataURI.split(',')[1]);
60-
} else {
61-
// Convert base64/URLEncoded data component to raw binary data:
62-
byteString = decodeURIComponent(dataURI.split(',')[1]);
20+
var CanvasPrototype = window.HTMLCanvasElement &&
21+
window.HTMLCanvasElement.prototype,
22+
BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
23+
window.MozBlobBuilder,
24+
dataURLtoBlob = BlobBuilder && window.atob && window.ArrayBuffer &&
25+
window.Uint8Array && function (dataURI) {
26+
var byteString,
27+
arrayBuffer,
28+
intArray,
29+
i,
30+
bb,
31+
mimeString;
32+
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
33+
// Convert base64 to raw binary data held in a string:
34+
byteString = atob(dataURI.split(',')[1]);
35+
} else {
36+
// Convert base64/URLEncoded data component to raw binary data:
37+
byteString = decodeURIComponent(dataURI.split(',')[1]);
38+
}
39+
// Write the bytes of the string to an ArrayBuffer:
40+
arrayBuffer = new ArrayBuffer(byteString.length);
41+
intArray = new Uint8Array(arrayBuffer);
42+
for (i = 0; i < byteString.length; i += 1) {
43+
intArray[i] = byteString.charCodeAt(i);
44+
}
45+
// Write the ArrayBuffer to a blob:
46+
bb = new BlobBuilder();
47+
bb.append(arrayBuffer);
48+
// Separate out the mime component:
49+
mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
50+
return bb.getBlob(mimeString);
51+
};
52+
if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
53+
if (CanvasPrototype.mozGetAsFile) {
54+
CanvasPrototype.toBlob = function (callback, type) {
55+
callback(this.mozGetAsFile('blob', type));
56+
};
57+
} else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
58+
CanvasPrototype.toBlob = function (callback, type) {
59+
callback(dataURLtoBlob(this.toDataURL(type)));
60+
};
6361
}
64-
// Write the bytes of the string to an ArrayBuffer:
65-
arrayBuffer = new ArrayBuffer(byteString.length);
66-
intArray = new Uint8Array(arrayBuffer);
67-
for (i = 0; i < byteString.length; i += 1) {
68-
intArray[i] = byteString.charCodeAt(i);
69-
}
70-
// Write the ArrayBuffer to a blob:
71-
bb = new BlobBuilder();
72-
bb.append(arrayBuffer);
73-
// Separate out the mime component:
74-
mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
75-
return bb.getBlob(mimeString);
76-
};
77-
62+
}
7863
if (typeof define !== 'undefined' && define.amd) {
7964
define(function () {
80-
return canvasToBlob;
65+
return dataURLtoBlob;
8166
});
8267
} else {
83-
$.canvasToBlob = canvasToBlob;
68+
window.dataURLtoBlob = dataURLtoBlob;
8469
}
8570
}(this));

canvas-to-blob.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blueimp-canvas-to-blob",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"title": "JavaScript Canvas to Blob",
55
"description": "JavaScript Canvas to Blob is a function to convert canvas elements into Blob objects.",
66
"keywords": [

test/test.js

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Canvas to Blob Test 1.0
2+
* JavaScript Canvas to Blob Test 2.0
33
* https://github.com/blueimp/JavaScript-Canvas-to-Blob
44
*
55
* Copyright 2012, Sebastian Tschan
@@ -11,98 +11,70 @@
1111

1212
/*global window, describe, it, expect, Blob */
1313

14-
(function (expect, canvasToBlob) {
14+
(function (expect) {
1515
'use strict';
1616

1717
// 80x60px GIF image (color black, base64 data):
1818
var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
1919
'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
2020
'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7',
2121
imageUrl = 'data:image/gif;base64,' + b64Data,
22-
blob = canvasToBlob.dataURItoBlob(imageUrl);
22+
blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl);
2323

24-
describe('canvasToBlob', function () {
25-
26-
it('Returns true when supporting canvas to blob conversions', function (done) {
27-
window.loadImage(blob, function (canvas) {
28-
expect(canvasToBlob(
29-
canvas,
30-
function () {
31-
done();
32-
}
33-
)).to.be.ok();
34-
}, {canvas: true});
35-
});
36-
37-
it('Returns false when not supporting canvas to blob conversions', function () {
38-
expect(canvasToBlob(
39-
{},
40-
function () {}
41-
)).to.not.be.ok();
42-
});
24+
describe('canvas.toBlob', function () {
4325

4426
it('Converts a canvas element to a blob and passes it to the callback function', function (done) {
4527
window.loadImage(blob, function (canvas) {
46-
expect(canvasToBlob(
47-
canvas,
28+
canvas.toBlob(
4829
function (newBlob) {
4930
done();
5031
expect(newBlob).to.be.a(Blob);
51-
},
52-
blob
53-
)).to.be.ok();
32+
}
33+
);
5434
}, {canvas: true});
5535
});
5636

5737
it('Converts a canvas element to a PNG blob', function (done) {
5838
window.loadImage(blob, function (canvas) {
59-
expect(canvasToBlob(
60-
canvas,
39+
canvas.toBlob(
6140
function (newBlob) {
6241
done();
6342
expect(newBlob.type).to.be('image/png');
6443
},
65-
{
66-
type: 'image/png'
67-
}
68-
)).to.be.ok();
44+
'image/png'
45+
);
6946
}, {canvas: true});
7047
});
7148

7249
it('Converts a canvas element to a JPG blob', function (done) {
7350
window.loadImage(blob, function (canvas) {
74-
expect(canvasToBlob(
75-
canvas,
51+
canvas.toBlob(
7652
function (newBlob) {
7753
done();
7854
expect(newBlob.type).to.be('image/jpeg');
7955
},
80-
{
81-
type: 'image/jpeg'
82-
}
83-
)).to.be.ok();
56+
'image/jpeg'
57+
);
8458
}, {canvas: true});
8559
});
8660

8761
it('Keeps the aspect ratio of the canvas image', function (done) {
8862
window.loadImage(blob, function (canvas) {
89-
expect(canvasToBlob(
90-
canvas,
63+
canvas.toBlob(
9164
function (newBlob) {
9265
window.loadImage(newBlob, function (img) {
9366
done();
9467
expect(img.width).to.be(canvas.width);
9568
expect(img.height).to.be(canvas.height);
9669
});
9770
}
98-
)).to.be.ok();
71+
);
9972
}, {canvas: true});
10073
});
10174

10275
it('Keeps the image data of the canvas image', function (done) {
10376
window.loadImage(blob, function (canvas) {
104-
expect(canvasToBlob(
105-
canvas,
77+
canvas.toBlob(
10678
function (newBlob) {
10779
window.loadImage(newBlob, function (newCanvas) {
10880
var canvasData = canvas.getContext('2d')
@@ -114,13 +86,10 @@
11486
expect(canvasData.height).to.be(newCanvasData.height);
11587
}, {canvas: true});
11688
}
117-
)).to.be.ok();
89+
);
11890
}, {canvas: true});
11991
});
12092

12193
});
12294

123-
}(
124-
this.expect,
125-
this.canvasToBlob
126-
));
95+
}(this.expect));

0 commit comments

Comments
 (0)