import { writeFileSync, mkdirSync, existsSync } from "fs"; import { join } from "path"; import { config } from "./config"; import { type IStorage } from "./storage"; export class CanvasExporter { private storage: IStorage; private exportInterval: NodeJS.Timeout | null = null; constructor(storage: IStorage) { this.storage = storage; } startAutoExport() { // Ensure export directory exists if (!existsSync(config.exportPath)) { mkdirSync(config.exportPath, { recursive: true }); } // Start export interval this.exportInterval = setInterval(() => { this.exportCanvas(); }, config.autoExportIntervalSeconds * 1000); console.log(`Auto-export started: every ${config.autoExportIntervalSeconds} seconds`); } stopAutoExport() { if (this.exportInterval) { clearInterval(this.exportInterval); this.exportInterval = null; console.log("Auto-export stopped"); } } async exportCanvas() { try { const pixels = await this.storage.getAllPixels(); // Create simple SVG export instead of PNG for now const svgContent = this.createSVG(pixels); // Generate filename with timestamp const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); const filename = `canvas-${timestamp}.svg`; const filepath = join(config.exportPath, filename); // Save SVG writeFileSync(filepath, svgContent); console.log(`Canvas exported: ${filename} (${pixels.length} pixels)`); } catch (error) { console.error("Failed to export canvas:", error); } } private createSVG(pixels: any[]): string { const { canvasWidth, canvasHeight } = config; let svgContent = ``; svgContent += ``; pixels.forEach(pixel => { svgContent += ``; }); svgContent += ``; return svgContent; } }