Search code examples
javascriptjqueryhtml2canvas

Overwrite only last default argument in function


I am using canvas2image for downloading screenshot of my webpage.I want to override the defaultfile name of the downloaded image.It is the last argument for the function that downloads image.

The function that writes image

return {
        saveAsImage: saveAsImage,
        saveAsPNG: function (canvas, width, height, fileName) {
            return saveAsImage(canvas, width, height, 'png',fileName);
        },

So here I overwritten the default filename with desired fileName as follows

My function to save image

 $("#btnSave").click(function() { 
    html2canvas($("#target"), {
        onrendered: function(canvas) {
            // Convert and download as image 
            Canvas2Image.saveAsPNG(canvas,fileName = 'Calendar.png');
        }
    });
});

But still it downloads the image with default name which is Date.now() according to documentation

if (type == undefined) { type = 'png'; }
            filename = filename == undefined||filename.length === 0 ?Date.now()+'.'+type: filename+'.'+type

Also I am not using the latest library from github page but the library defined at this link Please go over this link to find actual library I am using.I think in latest version they have removed the fileName argument(I was using latest initially and then image was downloading with "download" as name without any extension as well)


Solution

  • When you are calling a function, you need to provide all required paremeters.

    So instead of

    Canvas2Image.saveAsPNG(canvas,fileName = 'Calendar.png');
    

    do:-

    Canvas2Image.saveAsPNG(canvas,100,100,'png','Calendar.png');
    

    Note:- you need to change width and height according to your wish