What is blueimp-canvas-to-blob?
The blueimp-canvas-to-blob npm package is a JavaScript library that provides a polyfill for the standard HTML5 canvas.toBlob method. This method allows you to convert canvas elements to Blob objects, which can then be used for file uploads, image processing, and other tasks that require binary data.
What are blueimp-canvas-to-blob's main functionalities?
Convert Canvas to Blob
This feature allows you to convert a canvas element to a Blob object. The code sample creates a canvas, draws a red rectangle on it, and then converts the canvas to a Blob object in PNG format.
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 100, 100);
canvas.toBlob(function(blob) {
console.log(blob);
}, 'image/png');
Specify Image Type and Quality
This feature allows you to specify the image type and quality when converting a canvas to a Blob. The code sample creates a canvas, draws a blue rectangle on it, and then converts the canvas to a JPEG Blob with 75% quality.
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'blue';
ctx.fillRect(10, 10, 100, 100);
canvas.toBlob(function(blob) {
console.log(blob);
}, 'image/jpeg', 0.75);
Other packages similar to blueimp-canvas-to-blob
canvas-toBlob
The canvas-toBlob package is another polyfill for the HTML5 canvas.toBlob method. It provides similar functionality to blueimp-canvas-to-blob, allowing you to convert canvas elements to Blob objects. The main difference is that canvas-toBlob is a smaller and more lightweight package.
html2canvas
The html2canvas package allows you to take screenshots of web pages or parts of web pages and convert them to canvas elements. While it offers more comprehensive functionality than blueimp-canvas-to-blob, it can also convert the resulting canvas to a Blob object. This makes it a more versatile but heavier option.
dom-to-image
The dom-to-image package enables you to convert DOM nodes to images, including canvas elements. It provides more flexibility in terms of what can be converted to an image, but it also includes the ability to convert the resulting canvas to a Blob object. This package is useful if you need to capture more than just canvas elements.
JavaScript Canvas to Blob
Contents
Description
Canvas to Blob is a
polyfill for
Browsers that don't support the standard JavaScript
HTMLCanvasElement.toBlob
method.
It can be used to create
Blob objects from an
HTML canvas
element.
Setup
Install via NPM:
npm install blueimp-canvas-to-blob
This will install the JavaScript files inside
./node_modules/blueimp-canvas-to-blob/js/
relative to your current directory,
from where you can copy them into a folder that is served by your web server.
Next include the minified JavaScript Canvas to Blob script in your HTML markup:
<script src="js/canvas-to-blob.min.js"></script>
Or alternatively, include the non-minified version:
<script src="js/canvas-to-blob.js"></script>
Usage
You can use the canvas.toBlob()
method in the same way as the native
implementation:
var canvas = document.createElement('canvas')
if (canvas.toBlob) {
canvas.toBlob(function (blob) {
var formData = new FormData()
formData.append('file', blob, 'image.jpg')
}, 'image/jpeg')
}
Requirements
The JavaScript Canvas to Blob function has zero dependencies.
However, it is a very suitable complement to the
JavaScript Load Image
function.
Browsers
The following browsers have native support for
HTMLCanvasElement.toBlob:
- Chrome 50+
- Firefox 19+
- Safari 11+
- Mobile Chrome 50+ (Android)
- Mobile Firefox 4+ (Android)
- Mobile Safari 11+ (iOS)
- Edge 79+
Browsers which implement the following APIs support canvas.toBlob()
via
polyfill:
This includes the following browsers:
- Chrome 20+
- Firefox 13+
- Safari 8+
- Mobile Chrome 25+ (Android)
- Mobile Firefox 14+ (Android)
- Mobile Safari 8+ (iOS)
- Edge 74+
- Edge Legacy 12+
- Internet Explorer 10+
API
In addition to the canvas.toBlob()
polyfill, the JavaScript Canvas to Blob
script exposes its helper function dataURLtoBlob(url)
:
var b64 = 'R0lGODdhAwACAPEAAAAAAP///yZFySZFySH5BAEAAAIALAAAAAADAAIAAAIDRAJZADs='
var url = 'data:image/gif;base64,' + b64
var blob = dataURLtoBlob(url)
Test
Unit tests
License
The JavaScript Canvas to Blob script is released under the
MIT license.