简体   繁体   English

如何将图像从HTML5画布保存到磁盘

[英]How can save as image to disk from HTML5 canvas

I have an image which is drawn on the canvas. 我有一个在画布上绘制的图像。 I have a save as html input button in my page. 我的页面上有一个另存为html输入按钮。 When i click this button it should display a save as dialog and I have to save this image on the disk in jpeg format. 当我单击此按钮时,它将显示一个“另存为”对话框,我必须将此图像以jpeg格式保存在磁盘上。 How can do this functionality using javascript? 如何使用javascript来实现此功能?

Use the base64 URI from canvas.toDataURL as the href for a link, along with the download attribute . 使用canvas.toDataURL的base64 URI作为链接的href ,以及download 属性

It should look something like: 它看起来应该像这样:

<a href="data:image/png;base64,iVBORw0..." download="my-image">Save Image</a>

use eligrey polyfill to save images to disk. 使用eligrey polyfill将图像保存到磁盘。

The W3C File API includes a FileSaver interface, which makes saving generated data as easy as saveAs(data, filename), though unfortunately it will eventually be removed from the spec. W3C File API包含一个FileSaver接口,该接口使保存生成的数据与saveAs(data,filename)一样容易,尽管不幸的是最终会将其从规范中删除。 a JavaScript library called FileSaver.js , which implements FileSaver in all modern browsers. a JavaScript library called FileSaver.js ,该a JavaScript library called FileSaver.js在所有现代浏览器中实现FileSaver。 Now that it's possible to generate any type of file you want right in the browser, document editors can have an instant save button that doesn't rely on an online connection. 既然可以在浏览器中直接生成任何类型的文件,文档编辑器可以具有一个不依赖在线连接的即时保存按钮。 When paired with the standard HTML5 canvas.toBlob() method, FileSaver.js lets you save canvases instantly and give them filenames, which is very useful for HTML5 image editing webapps. 与标准HTML5 canvas.toBlob()方法配合使用时,FileSaver.js可让您立即保存画布并为其提供文件名,这对于HTML5图像编辑Web应用程序非常有用。

For browsers that don't yet support canvas.toBlob(), use canvas-toBlob.js. 对于尚不支持canvas.toBlob()的浏览器,请使用canvas-toBlob.js。

article 文章

DEMO DEMO

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

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