mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-15 09:48:14 +08:00
🎨 调整分享图生成参数
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file utils TGShare.ts
|
* @file utils TGShare.ts
|
||||||
* @description 生成分享截图并保存到本地
|
* @description 生成分享截图并保存到本地
|
||||||
* @since Beta v0.3.2
|
* @since Beta v0.3.3
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { dialog, fs, http, path } from "@tauri-apps/api";
|
import { dialog, fs, http, path } from "@tauri-apps/api";
|
||||||
@@ -75,34 +75,35 @@ function getShareImgBgColor(): string {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 生成分享截图
|
* @description 生成分享截图
|
||||||
* @since Alpha v0.2.0
|
* @since Beta v0.3.3
|
||||||
* @param {string} fileName - 文件名
|
* @param {string} fileName - 文件名
|
||||||
* @param {HTMLElement} element - 元素
|
* @param {HTMLElement} element - 元素
|
||||||
* @param {number} scale - 缩放比例
|
* @param {number} scale - 缩放比例
|
||||||
|
* @param {number} offset - 偏移量
|
||||||
* @returns {Promise<void>} 无返回值
|
* @returns {Promise<void>} 无返回值
|
||||||
*/
|
*/
|
||||||
export async function generateShareImg(
|
export async function generateShareImg(
|
||||||
fileName: string,
|
fileName: string,
|
||||||
element: HTMLElement,
|
element: HTMLElement,
|
||||||
scale: number = 1.2,
|
scale: number = 1.0,
|
||||||
|
offset: number = 30,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
const width = element.clientWidth + 50;
|
const width = element.clientWidth + offset;
|
||||||
const height = element.clientHeight + 50;
|
const height = element.clientHeight + offset;
|
||||||
canvas.width = width * scale;
|
canvas.width = width * scale;
|
||||||
canvas.height = height * scale;
|
canvas.height = height * scale;
|
||||||
canvas.style.width = `${width}px`;
|
|
||||||
canvas.style.height = `${height}px`;
|
|
||||||
canvas.getContext("2d")?.scale(scale, scale);
|
|
||||||
const opts = {
|
const opts = {
|
||||||
backgroundColor: getShareImgBgColor(),
|
backgroundColor: getShareImgBgColor(),
|
||||||
windowHeight: height,
|
windowHeight: height,
|
||||||
|
scale,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
useCORS: true,
|
useCORS: true,
|
||||||
canvas,
|
canvas,
|
||||||
x: -15,
|
x: (-offset / 2) * scale,
|
||||||
y: -15,
|
y: (-offset / 2) * scale,
|
||||||
|
dpi: window.devicePixelRatio > 1 ? 300 : 96,
|
||||||
};
|
};
|
||||||
const canvasData = await html2canvas(element, opts);
|
const canvasData = await html2canvas(element, opts);
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user