[FE] Canvas 转图片并下载的方式

通过llama.cpp与羊驼聊天的网页界面- 详解 Serge 的启动使用

 

先获取 canvas 节点,使用 toDataURL 转为 image 数据,最后使用 a 链接下载。

// Trans to image
const canvas = document.getElementsByTagName('canvas')[0]
const link = canvas.toDataURL('image/png')
const filename = 'xxx'

// Download
let oA = document.createElement('a');
oA.download = filename // 设置下载的文件名,默认是'下载'
oA.href = link;
document.body.appendChild(oA);
oA.click();
oA.remove();

以上代码可以直接使用。 

 

Other:什么是Canvas

Refer:在线Qrcode

Link:https://www.cnblogs.com/farwish/p/15426593.html

posted on 2021-10-19 21:49  ercom  阅读(229)  评论(0编辑  收藏  举报