Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/web/src/editor/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ function captureLabel(
return canvas;
}

function downloadCanvasAsPng(canvas: HTMLCanvasElement, filename: string): void {
canvas.toBlob((blob) => {
if (!blob) return;

const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
}, "image/png");
}

function rotateCanvas90CW(src: HTMLCanvasElement): HTMLCanvasElement {
const dst = document.createElement("canvas");
dst.width = src.height;
Expand Down Expand Up @@ -74,6 +87,26 @@ export function Editor() {
return () => window.removeEventListener("beforeunload", h);
}, []);

const exportPng = useCallback(async (): Promise<void> => {
const stage = stageRef.current;
if (!stage) return;

const { label, currentLabelName } = useEditorV2Store.getState();

// Deselect to avoid selection handles in the exported image.
useEditorV2Store.getState().clearSelection();

// Wait a frame for Konva to re-render without selection handles.
await new Promise((r) => requestAnimationFrame(r));

const canvas = captureLabel(stage, label.widthPx, label.heightPx);

const filename =
(currentLabelName || "label").replace(/[^a-zA-Z0-9._-]+/g, "_") + ".png";

downloadCanvasAsPng(canvas, filename);
}, []);

const print = useCallback(async (copies: number): Promise<boolean> => {
const printer = getPrinter();
const stage = stageRef.current;
Expand Down Expand Up @@ -131,7 +164,7 @@ export function Editor() {

return (
<div className="w-screen h-screen flex flex-col overflow-hidden bg-ink-950 text-ink-100">
<TopChrome onPrint={print} />
<TopChrome onPrint={print} onExportPng={exportPng} />
<div className="relative flex-1 min-h-0 flex flex-col">
<Canvas ref={stageRef} />
<Inspector />
Expand Down
11 changes: 10 additions & 1 deletion packages/web/src/editor/top-chrome/top-chrome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Grid3x3,
Ruler,
Heart,
Download,
} from "lucide-react";
import { useEditorV2Store } from "../../store/editor-store.ts";
import { PrintButton } from "./print-button.tsx";
Expand All @@ -18,9 +19,10 @@ import logoSvg from "../../assets/logo.svg";

interface TopChromeProps {
onPrint: (copies: number) => Promise<boolean>;
onExportPng: () => Promise<void>;
}

export function TopChrome({ onPrint }: TopChromeProps) {
export function TopChrome({ onPrint, onExportPng }: TopChromeProps) {
const zoom = useEditorV2Store((s) => s.zoom);
const label = useEditorV2Store((s) => s.label);
const gridVisible = useEditorV2Store((s) => s.gridVisible);
Expand Down Expand Up @@ -81,6 +83,13 @@ export function TopChrome({ onPrint }: TopChromeProps) {
<a href="https://github.com/sponsors/tomLadder" target="_blank" rel="noopener noreferrer" className="w-8 h-8 flex items-center justify-center rounded-md text-pink-400 hover:bg-pink-500/10 hover:text-pink-300 hover-fade" title="Sponsor this project">
<Heart size={16} />
</a>
<button
onClick={onExportPng}
className="w-8 h-8 flex items-center justify-center rounded-md text-ink-300 hover:bg-ink-800 hover:text-ink-100"
title="Export PNG"
>
<Download size={16} />
</button>
<PrintButton onPrint={onPrint} />
</div>

Expand Down