UNPKG

3.61 kBJavaScriptView Raw
1/* canvas-toBlob.js
2 * A canvas.toBlob() implementation.
3 * 2016-05-26
4 *
5 * By Eli Grey, http://eligrey.com and Devin Samarin, https://github.com/eboyjr
6 * License: MIT
7 * See https://github.com/eligrey/canvas-toBlob.js/blob/master/LICENSE.md
8 */
9
10/*global self */
11/*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
12 plusplus: true */
13
14/*! @source http://purl.eligrey.com/github/canvas-toBlob.js/blob/master/canvas-toBlob.js */
15
16(function(view) {
17"use strict";
18var
19 Uint8Array = view.Uint8Array
20 , HTMLCanvasElement = view.HTMLCanvasElement
21 , canvas_proto = HTMLCanvasElement && HTMLCanvasElement.prototype
22 , is_base64_regex = /\s*;\s*base64\s*(?:;|$)/i
23 , to_data_url = "toDataURL"
24 , base64_ranks
25 , decode_base64 = function(base64) {
26 var
27 len = base64.length
28 , buffer = new Uint8Array(len / 4 * 3 | 0)
29 , i = 0
30 , outptr = 0
31 , last = [0, 0]
32 , state = 0
33 , save = 0
34 , rank
35 , code
36 , undef
37 ;
38 while (len--) {
39 code = base64.charCodeAt(i++);
40 rank = base64_ranks[code-43];
41 if (rank !== 255 && rank !== undef) {
42 last[1] = last[0];
43 last[0] = code;
44 save = (save << 6) | rank;
45 state++;
46 if (state === 4) {
47 buffer[outptr++] = save >>> 16;
48 if (last[1] !== 61 /* padding character */) {
49 buffer[outptr++] = save >>> 8;
50 }
51 if (last[0] !== 61 /* padding character */) {
52 buffer[outptr++] = save;
53 }
54 state = 0;
55 }
56 }
57 }
58 // 2/3 chance there's going to be some null bytes at the end, but that
59 // doesn't really matter with most image formats.
60 // If it somehow matters for you, truncate the buffer up outptr.
61 return buffer;
62 }
63;
64if (Uint8Array) {
65 base64_ranks = new Uint8Array([
66 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1
67 , -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
68 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
69 , -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
70 , 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
71 ]);
72}
73if (HTMLCanvasElement && (!canvas_proto.toBlob || !canvas_proto.toBlobHD)) {
74 if (!canvas_proto.toBlob)
75 canvas_proto.toBlob = function(callback, type /*, ...args*/) {
76 if (!type) {
77 type = "image/png";
78 } if (this.mozGetAsFile) {
79 callback(this.mozGetAsFile("canvas", type));
80 return;
81 } if (this.msToBlob && /^\s*image\/png\s*(?:$|;)/i.test(type)) {
82 callback(this.msToBlob());
83 return;
84 }
85
86 var
87 args = Array.prototype.slice.call(arguments, 1)
88 , dataURI = this[to_data_url].apply(this, args)
89 , header_end = dataURI.indexOf(",")
90 , data = dataURI.substring(header_end + 1)
91 , is_base64 = is_base64_regex.test(dataURI.substring(0, header_end))
92 , blob
93 ;
94 if (Blob.fake) {
95 // no reason to decode a data: URI that's just going to become a data URI again
96 blob = new Blob
97 if (is_base64) {
98 blob.encoding = "base64";
99 } else {
100 blob.encoding = "URI";
101 }
102 blob.data = data;
103 blob.size = data.length;
104 } else if (Uint8Array) {
105 if (is_base64) {
106 blob = new Blob([decode_base64(data)], {type: type});
107 } else {
108 blob = new Blob([decodeURIComponent(data)], {type: type});
109 }
110 }
111 callback(blob);
112 };
113
114 if (!canvas_proto.toBlobHD && canvas_proto.toDataURLHD) {
115 canvas_proto.toBlobHD = function() {
116 to_data_url = "toDataURLHD";
117 var blob = this.toBlob();
118 to_data_url = "toDataURL";
119 return blob;
120 }
121 } else {
122 canvas_proto.toBlobHD = canvas_proto.toBlob;
123 }
124}
125}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));