Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: limit exported png size (#4340)

Liu Ji0a67c94223a2
diff --git a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
index 31525c1f1e..8bf47299e9 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
+++ b/frontend/src/components/SchemaDiagram/Canvas/Canvas.vue
@@ -159,7 +159,7 @@ const renderDummy = () => {
const handleScreenshot = async () => {
busy.value = true;
try {
- await dummy.value?.capture(`${database.value.name}.png`, "png");
+ await dummy.value?.capture(`${database.value.name}.png`);
} catch {
pushNotification({
module: "bytebase",
diff --git a/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue b/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue
index ce2bb69053..5f27110886 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue
+++ b/frontend/src/components/SchemaDiagram/Canvas/DummyCanvas.vue
@@ -5,27 +5,29 @@
ref="canvas"
class="relative overflow-hidden bg-gray-200 pointer-events-none z-10"
:style="{
- width: `${width}px`,
- height: `${height}px`,
+ width: `${resizeParams.rect.width}px`,
+ height: `${resizeParams.rect.height}px`,
}"
>
<div
- class="absolute overflow-visible"
+ class="absolute overflow-visible origin-top-left"
:style="{
width: `${width}px`,
height: `${height}px`,
+ transform: `scale(${resizeParams.zoom})`,
}"
>
<DesktopRenderer />
</div>
<img
- class="absolute z-50 opacity-50"
+ class="absolute z-50 opacity-50 origin-bottom-left"
:style="{
width: `${logoWidth}px`,
height: 'auto',
- left: `${LOGO_PADDING}px`,
- bottom: `${LOGO_PADDING}px`,
+ left: `${LOGO_PADDING * resizeParams.zoom}px`,
+ bottom: `${LOGO_PADDING * resizeParams.zoom}px`,
+ transform: `scale(${resizeParams.zoom})`,
}"
src="../../../assets/logo-full.svg"
alt="Bytebase"
@@ -39,6 +41,7 @@ import { computed, defineComponent, nextTick, PropType, ref, VNode } from "vue";
import {
calcBBox,
+ fitBBox,
minmax,
provideSchemaDiagramContext,
useSchemaDiagramContext,
@@ -86,26 +89,48 @@ const height = computed(() => {
return desktopBBox.value.height + desktopBBox.value.y * 2 + paddingBottom;
});
+const resizeParams = computed(() => {
+ // Fit the output image within a size-limited box
+ // and keep W/H ratio.
+ return fitBBox(
+ { width: width.value, height: height.value },
+ {
+ width: 4096,
+ height: 4096,
+ },
+ [0, 1]
+ );
+});
+
const DesktopRenderer = defineComponent({
render: () => {
return props.renderDesktop();
},
});
-const capture = async (filename: string, format: "png" | "jpg") => {
+const capture = async (filename: string) => {
+ if (isCapturing.value) {
+ return;
+ }
+
isCapturing.value = true;
await nextTick();
const node = canvas.value;
- if (!node) return;
+ if (!node) {
+ return;
+ }
const [{ toPng }, { default: download }] = await Promise.all([
import("html-to-image"),
import("downloadjs"),
]);
- const dataUrl = await toPng(node, {});
- download(dataUrl, filename, `image/${format}`);
+ const dataUrl = await toPng(node, {
+ pixelRatio: 1,
+ quality: 0.9,
+ });
+ download(dataUrl, filename, `image/png`);
isCapturing.value = false;
};
diff --git a/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts b/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
index ff0c60fe71..ede4bd6484 100644
--- a/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
+++ b/frontend/src/components/SchemaDiagram/Canvas/libs/fitView.ts
@@ -1,5 +1,5 @@
-import { calcBBox, minmax } from "../../common";
-import { Geometry, Rect } from "../../types";
+import type { Geometry, Rect } from "../../types";
+import { calcBBox, fitBBox } from "../../common";
export const fitView = (
canvas: Element,
@@ -34,39 +34,9 @@ const calcFitRect = (content: Rect, view: Rect, zoomRange: number[]) => {
const contentMarginTop = content.y;
const contentMarginLeft = content.x;
- const contentWHRatio = content.width / content.height;
- const canvasWHRatio = view.width / view.height;
+ const { zoom, rect } = fitBBox(content, view, zoomRange);
+ rect.x -= contentMarginLeft * zoom;
+ rect.y -= contentMarginTop * zoom;
- if (contentWHRatio > canvasWHRatio) {
- // content is wider than canvas, fit horizontally
- const targetWidth = view.width;
- const targetZoom = targetWidth / content.width;
- const zoom = minmax(targetZoom, zoomRange[0], zoomRange[1]);
- const targetSize = {
- width: content.width * zoom,
- height: content.height * zoom,
- };
- const targetPos = {
- x: (view.width - targetSize.width) / 2 - contentMarginLeft * zoom,
- y: (view.height - targetSize.height) / 2 - contentMarginTop * zoom,
- };
-
- const rect = { ...targetSize, ...targetPos };
- return { zoom, rect };
- } else {
- // content is taller than canvas, fit vertically
- const targetHeight = view.height;
- const targetZoom = targetHeight / content.height;
- const zoom = minmax(targetZoom, zoomRange[0], zoomRange[1]);
- const targetSize = {
- width: content.width * zoom,
- height: content.height * zoom,
- };
- const targetPos = {
- x: (view.width - targetSize.width) / 2 - contentMarginLeft * zoom,
- y: (view.height - targetSize.height) / 2 - contentMarginTop * zoom,
- };
- const rect = { ...targetSize, ...targetPos };
- return { zoom, rect };
- }
+ return { zoom, rect };
};
diff --git a/frontend/src/components/SchemaDiagram/common/geometry.ts b/frontend/src/components/SchemaDiagram/common/geometry.ts
index 39abca1776..f7d35164cc 100644
--- a/frontend/src/components/SchemaDiagram/common/geometry.ts
+++ b/frontend/src/components/SchemaDiagram/common/geometry.ts
@@ -1,4 +1,5 @@
-import { Geometry, Path, Point, Rect } from "../types";
+import type { Geometry, Path, Point, Rect, Size } from "../types";
+import { minmax } from "./utils";
export const pointsOfRect = (rect: Rect): Point[] => {
const { x, y, width, height } = rect;
@@ -102,3 +103,37 @@ export const pointsOfGeometry = (g: Geometry): Point[] => {
throw new Error(`'${String(g)}' is not a geometry.`);
};
+
+export const fitBBox = (
+ content: Size,
+ boundary: Size,
+ zoomRange: number[] = [0, Infinity]
+) => {
+ const contentWHRatio = content.width / content.height;
+ const boundaryWHRatio = boundary.width / boundary.height;
+ let zoom = 1;
+
+ if (contentWHRatio > boundaryWHRatio) {
+ // content is wider than boundary, fit horizontally
+ const targetWidth = boundary.width;
+ const targetZoom = targetWidth / content.width;
+ zoom = minmax(targetZoom, zoomRange[0], zoomRange[1]);
+ } else {
+ // content is taller than boundary, fit vertically
+ const targetHeight = boundary.height;
+ const targetZoom = targetHeight / content.height;
+ zoom = minmax(targetZoom, zoomRange[0], zoomRange[1]);
+ }
+
+ const targetSize = {
+ width: content.width * zoom,
+ height: content.height * zoom,
+ };
+ const targetPos = {
+ x: (boundary.width - targetSize.width) / 2,
+ y: (boundary.height - targetSize.height) / 2,
+ };
+
+ const rect = { ...targetSize, ...targetPos };
+ return { zoom, rect };
+};