Skip to content

Commit 548522d

Browse files
committed
refactor(canvas): enhance canvas resizing logic
1 parent 4d610fc commit 548522d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/canvas.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface CanvasOptions<
1414
pixelRatio?: number;
1515
/** 根据内容调整画布大小 */
1616
fitContent?: boolean;
17+
updateStyles?: boolean;
1718
// 图像平滑选项
1819
imageSmoothingEnabled?: boolean;
1920
imageSmoothingQuality?: "low" | "medium" | "high";
@@ -45,6 +46,16 @@ export function createCanvas<T extends HTMLCanvasElement | OffscreenCanvas | Can
4546

4647
const canvas = options.canvas ?? createRawCanvas(width * pixelRatio, height * pixelRatio);
4748

49+
if (options.canvas) {
50+
canvas.width = width * pixelRatio;
51+
canvas.height = height * pixelRatio;
52+
53+
if ("style" in canvas && options.updateStyles !== false) {
54+
canvas.style.width = `${width}px`;
55+
canvas.style.height = `${height}px`;
56+
}
57+
}
58+
4859
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
4960
if (!ctx) {
5061
throw new Error("Failed to get 2d context");
@@ -85,7 +96,7 @@ export function createCanvas<T extends HTMLCanvasElement | OffscreenCanvas | Can
8596
if (canvas.width !== contentWidth * pixelRatio || canvas.height !== contentHeight * pixelRatio) {
8697
canvas.width = contentWidth * pixelRatio;
8798
canvas.height = contentHeight * pixelRatio;
88-
if ("style" in canvas) {
99+
if ("style" in canvas && options.updateStyles !== false) {
89100
canvas.style.width = `${contentWidth}px`;
90101
canvas.style.height = `${contentHeight}px`;
91102
}

0 commit comments

Comments
 (0)