将画布另存为 jpg 到桌面 [英] Save canvas as jpg to desktop

查看:24
本文介绍了将画布另存为 jpg 到桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 HTML5canvas 将图像 (JPEG) 保存到桌面.我可以在新窗口中打开 window.open(canvas.toDataURL('png'), "");,但是如何将其保存到桌面?谢谢.

I am trying to save an image (JPEG) to the desktop from an HTML5canvas. I can open in a new window window.open(canvas.toDataURL('png'), "");, but how can I save it to the desktop? Thanks.

推荐答案

下载属性

HTML5 中有一个新的下载属性,允许您指定文件名链接.

Download attribute

There is a new download attribute in HTML5 that allow you to specify a file-name for links.

我做这个是为了节省画布.它有一个链接(下载为图像")-

I made this for saving canvas. It has a link ("Download as image") -

<a id="downloadLnk" download="YourFileName.jpg">Download as image</a>

和函数:

function download() {
    var dt = canvas.toDataURL('image/jpeg');
    this.href = dt;
};
downloadLnk.addEventListener('click', download, false);

您甚至可以通过设置属性 downloadLnk.download = 'myFilename.jpg' 来动态更改文件名.

You can even change the file-name dynamically by setting the attribute downloadLnk.download = 'myFilename.jpg'.

演示来自档案.

Demo from the archives.

这篇关于将画布另存为 jpg 到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆