This commit is contained in:
iqudoo
2026-06-13 04:12:57 +08:00
parent 3b9724beb3
commit e579da5d6b
4 changed files with 325 additions and 81 deletions

View File

@@ -252,6 +252,8 @@ export interface RenderLabelOptions {
/** 输出格式,默认 png */
imageFormat?: 'png' | 'jpeg';
jpegQuality?: number;
/** 为 false 时不裁剪到画布边界(用于拖动浮层完整显示元素) */
clipToCanvas?: boolean;
}
/** 离屏渲染标签为 PNG data URL */
@@ -269,6 +271,7 @@ export async function renderLabelToDataUrl(options: RenderLabelOptions): Promise
scale: scaleOption = 16,
imageFormat = 'png',
jpegQuality = 0.92,
clipToCanvas = true,
} = options;
const scale = scaleOption;
@@ -295,9 +298,11 @@ export async function renderLabelToDataUrl(options: RenderLabelOptions): Promise
}
ctx.save();
ctx.beginPath();
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.clip();
if (clipToCanvas) {
ctx.beginPath();
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.clip();
}
for (const elem of elements) {
const ex = Math.round(elem.x * scale);