处理 html2canvas 跨域

This commit is contained in:
BTMuli
2023-06-14 22:57:53 +08:00
parent a4b8dfe8c6
commit 0b5fbf5953

View File

@@ -6,7 +6,7 @@
*/
// tauri
import { dialog, fs } from "@tauri-apps/api";
import { dialog, fs, http } from "@tauri-apps/api";
/**
* @description 保存图片-canvas
@@ -15,16 +15,33 @@ import { dialog, fs } from "@tauri-apps/api";
* @param {string} filename - 文件名
* @returns {Promise<void>} 无返回值
*/
export async function saveCanvasImg(canvas: HTMLCanvasElement, filename: string): Promise<void> {
export async function saveCanvasImg (canvas: HTMLCanvasElement, filename: string): Promise<void> {
const buffer = new Uint8Array(atob(canvas.toDataURL("image/png").split(",")[1]).split("").map((item) => item.charCodeAt(0)));
await dialog.save({
defaultPath: filename,
filters: [{ name: "图片", extensions: ["png"] }]
filters: [{ name: "图片", extensions: ["png"] }],
}).then(async (res) => {
if (res === null) return;
await fs.writeBinaryFile({
path: res,
contents: buffer
contents: buffer,
});
});
}
}
/**
* @description 将图片保存到本地
* @since Alpha v0.2.0
* @param {string} url - 图片链接
* @returns {Promise<string>} 图片元素
*/
export async function saveImgLocal (url: string): Promise<string> {
return await http.fetch<ArrayBuffer>(url, {
method: "GET",
responseType: http.ResponseType.Binary,
}).then(async (res) => {
const buffer = new Uint8Array(res.data);
const blob = new Blob([buffer], { type: "image/png" });
return URL.createObjectURL(blob);
});
}