28 lines
613 B
TypeScript
28 lines
613 B
TypeScript
|
|
import type Konva from "konva";
|
||
|
|
|
||
|
|
import type { ExportImageFormat } from "@/lib/image-editor-types";
|
||
|
|
|
||
|
|
export function downloadStageImage(
|
||
|
|
stage: Konva.Stage,
|
||
|
|
format: ExportImageFormat,
|
||
|
|
quality: number
|
||
|
|
): void {
|
||
|
|
const mimeType =
|
||
|
|
format === "png"
|
||
|
|
? "image/png"
|
||
|
|
: format === "jpg"
|
||
|
|
? "image/jpeg"
|
||
|
|
: "image/webp";
|
||
|
|
|
||
|
|
const dataUrl = stage.toDataURL({
|
||
|
|
pixelRatio: 2,
|
||
|
|
mimeType,
|
||
|
|
quality: format === "png" ? 1 : quality / 100,
|
||
|
|
});
|
||
|
|
|
||
|
|
const link = document.createElement("a");
|
||
|
|
link.download = `design-${Date.now()}.${format}`;
|
||
|
|
link.href = dataUrl;
|
||
|
|
link.click();
|
||
|
|
}
|