• Stars
    star
    1,466
  • Rank 30,805 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript Canvas to Blob is a function to convert canvas elements into Blob objects.

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')
// Edit the canvas ...
if (canvas.toBlob) {
  canvas.toBlob(function (blob) {
    // Do something with the blob object,
    // e.g. create multipart form data for file uploads:
    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):

// Uncomment the following line when using a module loader like webpack:
// var dataURLtoBlob = require('blueimp-canvas-to-blob')

// black+white 3x2 GIF, base64 data:
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.

More Repositories

1

jQuery-File-Upload

File Upload widget with multiple file selection, drag&drop support, progress bar, validation and preview images, audio and video for jQuery. Supports cross-domain, chunked and resumable file uploads. Works with any server-side platform (Google App Engine, PHP, Python, Ruby on Rails, Java, etc.) that supports standard HTML form file uploads.
PHP
30,988
star
2

JavaScript-MD5

JavaScript MD5 implementation. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.
JavaScript
4,537
star
3

JavaScript-Load-Image

Load images provided as File or Blob objects or via URL. Retrieve an optionally scaled, cropped or rotated HTML img or canvas element. Use methods to parse image metadata to extract IPTC and Exif tags as well as embedded thumbnail images, to overwrite the Exif Orientation value and to restore the complete image header after resizing.
JavaScript
4,428
star
4

Gallery

blueimp Gallery is a touch-enabled, responsive and customizable image & video gallery, carousel and lightbox, optimized for both mobile and desktop web browsers. It features swipe, mouse and keyboard navigation, transition effects, slideshow functionality, fullscreen support and on-demand content loading.
JavaScript
3,727
star
5

Bootstrap-Image-Gallery

This project is deprecated in favor of blueimp Gallery.
HTML
2,837
star
6

JavaScript-Templates

1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.
JavaScript
1,716
star
7

wdio

Docker setup for WebdriverIO with automatic screenshots, image diffing and screen recording support for containerized versions of Chrome and Firefox on Linux, mobile versions of Chrome and Firefox on Android as well as Safari on iOS, Safari on macOS and Edge on Windows.
JavaScript
295
star
8

aws-lambda

Collection of functions for AWS Lambda
JavaScript
270
star
9

jQuery-Image-Gallery

This project is deprecated in favor of blueimp Gallery.
HTML
175
star
10

shell-scripts

A collection of (mostly POSIX compatible) shell scripts.
Shell
135
star
11

nightwatch

This project is deprecated in favor of blueimp/wdio.
134
star
12

mjpeg-server

MJPEG Server implements MJPEG over HTTP using FFmpeg or any other input source capable of piping a multipart JPEG stream to stdout. Its primary use case is providing Webdriver screen recordings.
Go
98
star
13

aws-smtp-relay

SMTP server to relay emails via Amazon SES or Amazon Pinpoint using IAM roles.
Go
74
star
14

record-screen

Node.js screen recording function for remote servers using ffmpeg.
JavaScript
69
star
15

nightwatch-video-recorder

Record videos of Nightwatch.js test sessions. Uses ffmpeg to capture a (remote) webdriver desktop screen.
JavaScript
42
star
16

mailhog-node

A NodeJS library to interact with the MailHog API. MailHog is a stand-in SMTP server for Web and API based SMTP testing.
JavaScript
34
star
17

ffmpeg-image-diff

An image diffing library using FFmpeg. Creates an image showing perceptual differences and returns SSIM data.
JavaScript
30
star
18

chromedriver

Chromedriver Dockerfile
Dockerfile
21
star
19

basedriver

Base Webdriver Dockerfile
Dockerfile
14
star
20

passphrase

Better passwords by combining random words.
Go
13
star
21

phpbb

phpBB Dockerfile
Shell
12
star
22

nightwatch-browserstack

Nightwatch.js module to update BrowserStack session status for failed tests.
JavaScript
11
star
23

adb-record-screen

Screen recording function using Android Debug Bridge (adb).
JavaScript
9
star
24

wdio-screen-commands

Webdriver.io commands to capture and record browser screens.
JavaScript
7
star
25

playwright-example

Example setup for the Playwright end-to-end testing framework. Includes an example email application with file attachment support and tests to cover its functionality.
TypeScript
5
star
26

geckodriver

Geckodriver Dockerfile
Dockerfile
4
star
27

wildcard-redirect

HTTP service to redirect wildcard subdomains of an origin host to a new target host, e.g. *.example.com to *.example.org.
Go
4
star
28

mocha-chrome

mocha-chrome Dockerfile
Shell
3
star
29

blueimp.github.io

blueimp's GitHub projects
3
star
30

phpbb-s3-backup

phpBB S3 Backup Dockerfile
Shell
2
star