mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Fix : Export to GIF : download option
In the GIF export panel, user can now choose between : - export online (previous 'upload' feature) - download GIF Labels have been updated in the PNG export panel to follow the same convention. CanvasToBlob library was modified and moved to dedicated utils to handle not only canvas, but also any base64 dateURI.
This commit is contained in:
51
src/js/utils/Base64.js
Normal file
51
src/js/utils/Base64.js
Normal file
@@ -0,0 +1,51 @@
|
||||
(function () {
|
||||
var ns = $.namespace('pskl.utils');
|
||||
|
||||
var base64_ranks;
|
||||
if (Uint8Array) {
|
||||
base64_ranks = new Uint8Array([
|
||||
62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
|
||||
-1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
||||
-1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
|
||||
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
|
||||
]);
|
||||
}
|
||||
|
||||
ns.Base64 = {
|
||||
decode : function(base64) {
|
||||
var outptr = 0;
|
||||
var last = [0, 0];
|
||||
var state = 0;
|
||||
var save = 0;
|
||||
|
||||
var undef;
|
||||
var len = base64.length, i = 0;
|
||||
var buffer = new Uint8Array(len / 4 * 3 | 0);
|
||||
while (len--) {
|
||||
var code = base64.charCodeAt(i++);
|
||||
var rank = base64_ranks[code-43];
|
||||
if (rank !== 255 && rank !== undef) {
|
||||
last[1] = last[0];
|
||||
last[0] = code;
|
||||
save = (save << 6) | rank;
|
||||
state++;
|
||||
if (state === 4) {
|
||||
buffer[outptr++] = save >>> 16;
|
||||
if (last[1] !== 61 /* padding character */) {
|
||||
buffer[outptr++] = save >>> 8;
|
||||
}
|
||||
if (last[0] !== 61 /* padding character */) {
|
||||
buffer[outptr++] = save;
|
||||
}
|
||||
state = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 2/3 chance there's going to be some null bytes at the end, but that
|
||||
// doesn't really matter with most image formats.
|
||||
// If it somehow matters for you, truncate the buffer up outptr.
|
||||
return buffer.buffer;
|
||||
}
|
||||
};
|
||||
})();
|
||||
38
src/js/utils/ImageToBlob.js
Normal file
38
src/js/utils/ImageToBlob.js
Normal file
@@ -0,0 +1,38 @@
|
||||
(function () {
|
||||
var ns = $.namespace('pskl.utils');
|
||||
|
||||
var BASE64_REGEX = /\s*;\s*base64\s*(?:;|$)/i;
|
||||
|
||||
ns.ImageToBlob = {
|
||||
imageDataToBlob : function(dataURI, type, callback) {
|
||||
var header_end = dataURI.indexOf(","),
|
||||
data = dataURI.substring(header_end + 1),
|
||||
isBase64 = BASE64_REGEX.test(dataURI.substring(0, header_end)),
|
||||
blob;
|
||||
|
||||
if (Blob.fake) {
|
||||
// no reason to decode a data: URI that's just going to become a data URI again
|
||||
blob = new Blob();
|
||||
blob.encoding = isBase64 ? "base64" : "URI";
|
||||
blob.data = data;
|
||||
blob.size = data.length;
|
||||
} else if (Uint8Array) {
|
||||
var blobData = isBase64 ? pskl.utils.Base64.decode(data) : decodeURIComponent(data);
|
||||
blob = new Blob([blobData], {type: type});
|
||||
}
|
||||
callback(blob);
|
||||
},
|
||||
|
||||
canvasToBlob : function(canvas, callback, type /*, ...args*/) {
|
||||
type = type || "image/png";
|
||||
|
||||
if (this.mozGetAsFile) {
|
||||
callback(this.mozGetAsFile("canvas", type));
|
||||
} else {
|
||||
var args = Array.prototype.slice.call(arguments, 2);
|
||||
var dataURI = this.toDataURL.apply(this, args);
|
||||
pskl.utils.ImageToBlob.imageDataToBlob(dataURI, type, callback);
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user