简体   繁体   English

image.src = canvas.toDataURL(“image/png”); 保存问题

[英]image.src = canvas.toDataURL(“image/png”); Saving issue

How to save image.src = canvas.toDataURL("image/png");如何保存image.src = canvas.toDataURL("image/png"); in within the camera div onclick camera result automatically getting saved in the first image src line.在相机 div onclick 相机结果中自动保存在第一个图像 src 行中。

Working Codepen工作密码笔

You are editing the first img tag.您正在编辑第一个 img 标签。 But you need to edit the second one.但是您需要编辑第二个。

Change let image = document.querySelector("img");改变let image = document.querySelector("img"); to let image = document.getElementById("result"); let image = document.getElementById("result");

and

Change <img src="" alt="" class="image image--hidden" /> to <img src="" id="result" alt="" class="image image--hidden" /><img src="" alt="" class="image image--hidden" />更改为<img src="" id="result" alt="" class="image image--hidden" />

If I understand correctly, you want to download the image on click .如果我理解正确,您想在click上下载图像。 To achieve this you should use an <a /> tag.为此,您应该使用<a />标签。 Try the following:尝试以下操作:

function save(){

    let a = document.createElement('a');

    a.download = '';

    a.href = canvas.toDataURL("image/png");

    a.click();
}

Now you can use the save function whenever you want, even onclick .现在您可以随时使用save function ,甚至onclick

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

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