How to give browser “save image as” option to button

后端 未结 2 1822
难免孤独
难免孤独 2020-12-28 11:13

I\'m working on a canvas drawing project. I convert the canvas as an image, then I save that image as \'.png\'. I have to right click on the image and select the \'save imag

2条回答
  •  醉话见心
    2020-12-28 11:37

    First create a button for it
    Download

    Then add the following in javascript

    var button = document.getElementById('btn-download');
    button.addEventListener('click', function (e) {
        var dataURL = canvas.toDataURL('image/png');
        button.href = dataURL;
    });
    

    I have made an example for you Check this out!

提交回复
热议问题