[Enhancement] Add IE, Opera, Safari support for canvas.toBlob()

This is currently being used for exporting plots to PNG/JPG.
This commit is contained in:
David Hudson 2016-09-05 19:40:22 +09:00
parent 35a331f3fd
commit ceb3e8e3dd

View File

@ -101,6 +101,30 @@ define(
return defer.promise;
}
/**
* canvas.toBlob() not supported in IE < 10, Opera, and Safari. This polyfill
* implements the method in browsers that would not otherwise support it.
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
*/
function polyfillToBlob() {
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", {
value: function (callback, type, quality) {
var binStr = atob(this.toDataURL(type, quality).split(',')[1]),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
callback(new Blob([arr], {type: type || "image/png"}));
}
});
}
}
ExportImageService.prototype.exportPDF = function (element, filename) {
return renderElement(element, "jpeg").then(function (img) {
var pdf = new self.jsPDF("l", "px", [element.offsetHeight, element.offsetWidth]);
@ -121,6 +145,8 @@ define(
});
};
polyfillToBlob();
return ExportImageService;
}
);