How lucidedit/x-image works
Project type and purpose
• Type: Standalone client-side NPM library
• Purpose: Convert arbitrary HTML + text into a styled PNG image (download or data URL)
• Audience: Frontend applications (browser) that import beautiful-text-images
Top-level components
A. Consumer (Browser app / React / Vanilla JS)
B. x-image Package Entry
1. API Layer (src/index.ts → createBeautifulTextImage)
C. Core Services (inside src/lib/)
• get-theme-by-name
• load-fonts
• inject-custom-css
• apply-styles-to-html
D. Themes Module (src/themes/ + manifest in src/themes/index.ts)
E. External Dependencies
• html-to-image (renders styled DOM → Canvas → PNG)
• Google Fonts / external font URLs
F. Browser Environment
• DOM (temporary container)
• Canvas API
Data flow & interactions
1. Consumer calls createBeautifulTextImage(html, options)
2. API Layer consults Themes Module → getThemeByName(themeName)
3. Core Services orchestrate:
a. loadFonts(theme.fontLinks) → network fetch to Google Fonts → inject <link>
b. Create hidden DOM container → apply wrapperStyle + elementStyles via applyStylesToHtml
c. injectCustomCss(theme.customCSS) → <style> tag in <head>
4. Hand off styled DOM node to html-to-image → renders to Canvas → PNG blob or dataURL
5. Return to consumer (download trigger or data URL)
6. Cleanup: remove temporary DOM elements
Architectural patterns & principles
• Modular separation of concerns (theme lookup vs styling vs rendering)
• Plug-in style theming (themes are simple JS objects, strategy pattern)
• Client-side rendering pipeline (DOM → Canvas → PNG)
• Async operations with Promises (font loading, rendering delay)