/** * Generate SVG Preview for Example Projects * * Creates a visual preview of the circuit layout for example cards */ import type { ExampleProject } from '../data/examples'; export function generateExamplePreviewSVG(example: ExampleProject): string { const width = 400; const height = 250; const padding = 20; // Calculate bounds let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity; example.components.forEach((comp) => { minX = Math.min(minX, comp.x); maxX = Math.max(maxX, comp.x + 100); // Approximate component width minY = Math.min(minY, comp.y); maxY = Math.max(maxY, comp.y + 100); // Approximate component height }); // Calculate scale to fit in preview const contentWidth = maxX - minX; const contentHeight = maxY - minY; const scaleX = (width - padding * 2) / contentWidth; const scaleY = (height - padding * 2) / contentHeight; const scale = Math.min(scaleX, scaleY, 1); // Don't scale up // Center the content const offsetX = (width - contentWidth * scale) / 2 - minX * scale; const offsetY = (height - contentHeight * scale) / 2 - minY * scale; const components = example.components .map((comp) => { const x = comp.x * scale + offsetX; const y = comp.y * scale + offsetY; // Simplified component representations if (comp.type === 'wokwi-arduino-uno') { return ` Arduino `; } else if (comp.type === 'wokwi-led') { const color = comp.properties.color || 'red'; return ` `; } else if (comp.type === 'wokwi-rgb-led') { return ` `; } else if (comp.type === 'wokwi-pushbutton') { return ` `; } else { // Generic component return ` `; } }) .join(''); // Draw simplified wires const wires = example.wires .map((wire) => { const startComp = example.components.find((c) => c.id === wire.start.componentId); const endComp = example.components.find((c) => c.id === wire.end.componentId); if (!startComp || !endComp) return ''; const x1 = startComp.x * scale + offsetX + 35 * scale; const y1 = startComp.y * scale + offsetY + 30 * scale; const x2 = endComp.x * scale + offsetX + 35 * scale; const y2 = endComp.y * scale + offsetY + 30 * scale; return ` `; }) .join(''); return ` ${wires} ${components} `; } export function generateExamplePreviewDataURL(example: ExampleProject): string { const svg = generateExamplePreviewSVG(example); const base64 = btoa(unescape(encodeURIComponent(svg))); return `data:image/svg+xml;base64,${base64}`; }