I've been using this code to download images from a canvas:
function fileSave1()
{
var dataURL = "";
var canvasSave = document.getElementById("cSave");
if (document.getElementById("png").checked == true) dataURL = canvasSave.toDataURL(); // PNG is default
if (document.getElementById("jpg").checked == true) dataURL = canvasSave.toDataURL('image/jpeg', Number(document.getElementById("quality").value) / 100); // JPG with Quality Setting requires 2 arguments
//var start = dataURL.indexOf(";"); //start point will vary based on image/png (9 characters) vs image/jpeg (10 characters)
//dataURL = dataURL.slice(start);
//dataURL = "data:application/octet-stream" + dataURL;
if (document.getElementById("jpg").checked == true) document.getElementById("afterCanvas2").innerHTML = '<a download="new_image.jpg" href='+ dataURL +'>Save New JPG with a Quality Setting of ' + document.getElementById("quality").value + ' </a>';
if (document.getElementById("png").checked == true) document.getElementById("afterCanvas2").innerHTML = '<a download="new_image.png" href='+ dataURL +'>Save New Full Quality PNG</a>';
}
However, in Chrome, it is not downloading some PNGs and very high quality JPGs. This post suggests the problem is that the file sizes are too large, and suggests canvas.toBlob() as the workaround. This post has a code sample, but I am having trouble adapting it to my code.
I would deeply appreciate assistance with the proper syntax.
Thank you
EDIT: The code that failed:
function fileSave1()
{
var canvas = document.getElementById('cSave');
canvas.toBlob(function(blob)
{
var url = URL.createObjectURL(blob);
url.onload = function()
{
URL.revokeObjectURL(url);
};
});
document.getElementById("afterCanvas2").innerHTML = '<a download="new_image.png" href='+ url +'>Save New Full Quality PNG</a>';
}
Error: Uncaught ReferenceError: url is not defined at fileSave1.
Thank you