瀏覽代碼

feat: support supplying custom scale when exporting canvas (#2904)

David Luzar 4 年之前
父節點
當前提交
f0f5430313
共有 2 個文件被更改,包括 11 次插入8 次删除
  1. 1 1
      src/packages/utils.ts
  2. 10 7
      src/scene/export.ts

+ 1 - 1
src/packages/utils.ts

@@ -36,7 +36,7 @@ export const exportToCanvas = ({
       canvas.width = ret.width;
       canvas.height = ret.height;
 
-      return canvas;
+      return { canvas, scale: ret.scale };
     },
   );
 };

+ 10 - 7
src/scene/export.ts

@@ -29,14 +29,14 @@ export const exportToCanvas = (
     viewBackgroundColor: string;
     shouldAddWatermark: boolean;
   },
-  createCanvas: (width: number, height: number) => HTMLCanvasElement = (
-    width,
-    height,
-  ) => {
+  createCanvas: (
+    width: number,
+    height: number,
+  ) => { canvas: HTMLCanvasElement; scale: number } = (width, height) => {
     const tempCanvas = document.createElement("canvas");
     tempCanvas.width = width * scale;
     tempCanvas.height = height * scale;
-    return tempCanvas;
+    return { canvas: tempCanvas, scale: 1 };
   },
 ) => {
   const sceneElements = getElementsAndWatermark(elements, shouldAddWatermark);
@@ -47,13 +47,16 @@ export const exportToCanvas = (
     shouldAddWatermark,
   );
 
-  const tempCanvas = createCanvas(width, height);
+  const { canvas: tempCanvas, scale: newScale = scale } = createCanvas(
+    width,
+    height,
+  );
 
   renderScene(
     sceneElements,
     appState,
     null,
-    scale,
+    newScale,
     rough.canvas(tempCanvas),
     tempCanvas,
     {