简体   繁体   English

“默默地”将 canvas 保存到 javascript 的图像中

[英]"Silently" save canvas to image with javascript

I have a canvas on which I draw pretty images!我有一个 canvas,我可以在上面画漂亮的图片!

Using.toDataURL('image/gif'), I save those images as image files.使用.toDataURL('image/gif'),我将这些图像保存为图像文件。 So far so good.到目前为止,一切都很好。

Thing is that I want to save a number of those images (I change parameters for each image to be saved.)事情是我想保存一些这样的图像(我为每个要保存的图像更改参数。)

I would like to do it "silently", that is without having to click on "Save" on the modal window that pops up every time.我想“静静地”进行,即不必在每次弹出的模式 window 上单击“保存”。

Is there a way to do that?有没有办法做到这一点?

Thanks.谢谢。

Using the download attribute of an <a> element, you can silently download the image data without needing to deal with any dialog windows. I should warn that this could be potentially dangerous as it is literally downloading a file to your computer without any direct confirmation .使用<a>元素的download属性,您可以静默下载图像数据而无需处理任何对话框 windows。我应该警告这可能有潜在危险,因为它实际上是在没有任何直接确认的情况下将文件下载到您的计算机.

Once you have your data URL from your canvas, the rest is pretty easy.从 canvas 获得数据 URL后,rest 就非常容易了。 You can create a new <a> element, set the href to your data URL , set the download attribute to the filename you wish to use, and then execute the click() event on this newly created element.您可以创建一个新的<a>元素,将href设置为您的数据 URL ,将download属性设置为您希望使用的文件名,然后在这个新创建的元素上执行click()事件。

 let dataURL const _Base64Image = url => { const img = new Image() img.setAttribute('crossOrigin', 'anonymous') img.onload = () => { const canvas = document.createElement("canvas") canvas.width = img.width canvas.height = img.height const ctx = canvas.getContext("2d") ctx.drawImage(img, 0, 0) dataURL = canvas.toDataURL("image/png") _SetBG(document.querySelector("#imgTest"), dataURL, img.width, img.height) } img.src = url } const _SetBG = (el, data, w, h) => { el.style.width = `${w}px` el.style.height = `${h}px` el.style.backgroundImage = `url("${data}")` } const _SaveImage = (data, filename) => { const a = document.createElement("a") a.href = data a.download = filename a.click() } _Base64Image('https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/2048px-Unofficial_JavaScript_logo_2.svg.png') document.querySelector("input[type='button']").addEventListener("click", e => { _SaveImage(dataURL, "test_image.png") })
 .random-background { max-height: 100px; max-width: 100px; background-size: contain; }
 <div id="imgTest" class="random-background"></div> <br> <br> <input type="button" value="Save Image">

NOTE笔记

This snippet will not work on StackOverflow as part of their security settings.作为其安全设置的一部分,此代码段不会StackOverflow上运行。 However I also uploaded this to CodePen so you can see a working version.但是我也将它上传到CodePen ,这样你就可以看到一个工作版本。

For the sake of not forcing downloads on anyone I included a button that must be clicked to save the image.为了不强迫任何人下载,我添加了一个按钮,必须单击该按钮才能保存图像。 However this is not required for the code to work.然而,这不是代码工作所必需的。 Simply calling the _SaveImage() function ( and passing in the data URL and filename ) will work.只需调用_SaveImage() function(并传入数据 URL 和文件名)即可。 But having the snippet do this automatically is somewhat problematic as you can get hit with multiple downloads for the same file just by going to the page.但是让代码片段自动执行此操作有点问题,因为您只需转到该页面就可能会遇到同一文件的多次下载。

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

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