1
0

preview.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. """Preview module for generating SVG previews of patterns."""
  2. import os
  3. import math
  4. from modules.core.pattern_manager import parse_theta_rho_file, THETA_RHO_DIR
  5. async def generate_preview_svg(pattern_file):
  6. """Generate an SVG preview for a pattern file."""
  7. file_path = os.path.join(THETA_RHO_DIR, pattern_file)
  8. coordinates = parse_theta_rho_file(file_path)
  9. # Convert polar coordinates to SVG path
  10. svg_path = []
  11. for i, (theta, rho) in enumerate(coordinates):
  12. x = 100 - rho * 90 * math.cos(theta)
  13. y = 100 - rho * 90 * math.sin(theta)
  14. if i == 0:
  15. svg_path.append(f"M {x:.2f} {y:.2f}")
  16. else:
  17. svg_path.append(f"L {x:.2f} {y:.2f}")
  18. svg = f'''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  19. <svg width="100%" height="100%" viewBox="0 0 200 200" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg">
  20. <path d="{' '.join(svg_path)}"
  21. fill="none"
  22. stroke="currentColor"
  23. stroke-width="0.5"/>
  24. </svg>'''
  25. return svg