简体   繁体   English

点击动作下载HTML5 Canvas

[英]Download Html5 canvas on click action

I am working on Html5 canvas. 我正在使用HTML5画布。

Currently I am downloading image on right click and selecting an option of save image 目前,我正在右键单击下载图像并选择保存图像的选项

Now i want to include DOWNLOAD button that can save and downloads the Canvas as an Image. 现在,我要包括“ 下载”按钮,该按钮可以将“画布”保存和下载为图像。

My code is : 我的代码是:

 var download = document.getElementById('img-download');
   download.addEventListener('click', prepareDownload, false);

  function prepareDownload() {

    var canvas = document.getElementById("memecanvas");
    document.getElementById("memecanvas").src = canvas.toDataURL();

    Canvas2Image.saveAsPNG(canvas);
  }

This simply enables to download it via right click. 只需通过右键单击即可下载它。 Please help me how to deal with this on Button click action. 请帮助我如何处理“按钮单击”操作。

Thanks in advance! 提前致谢!

Just create a button, give it an ID of, say, "download-button". 只需创建一个按钮,为其指定一个ID,例如“下载按钮”。

Then instead of adding the click event handler to the img-download element, assign it to the button using it's ID. 然后,不要将click事件处理程序添加到img-download元素,而是使用其ID将其分配给按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM