# 1904labs/dom-to-image-more architecture

> Architecture diagram of the GitHub repository 1904labs/dom-to-image-more, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/1904labs/dom-to-image-more
- Repository: https://github.com/1904labs/dom-to-image-more

## Overview

##### Project Type and Purpose
- This is a front-end focused JavaScript library designed to convert a DOM node (or subtree) into an image. It supports vector (SVG) and raster (PNG/JPEG) output formats.

##### File Structure
- Top-Level Files: You have several configuration files (.prettierrc, .eslintrc, Gruntfile.js, package.json, etc.) which indicate a well-maintained project with testing and build/continuous integration support.
- Key Directories:
- src: Contains the core implementation code (e.g. dom-to-image-more.js). This is where the main logic lives.
- spec: Contains a rich set of test resources (HTML, CSS, control images, etc.) that serve as inputs to test various rendering cases.
- test-lib: Contains third-party libraries (like fontawesome and ocrad) used internally or during testing.
- The organizational style suggests a modular design where testing, assets, configuration, and source separation are clearly maintained.

##### README for Architectural Insights
- The README explains that the library works by:
a. Cloning the provided DOM node recursively.
b. Computing and inlining styles (including pseudo-elements).
c. Embedding assets like web fonts and images (inlining them in Base64).
d. Serializing the updated clone into XML, wrapping it within an SVG’s <foreignObject> tag.
e. Optionally rendering the SVG via an off-screen canvas to produce PNG/JPEG or raw pixel data.
- It highlights the multiple “stages” of processing from DOM cloning to final image generation and discusses configuration options such as quality, scaling, and callbacks (onclone, adjustClonedNode).

a. Main Components:
- API Layer: Represent the externally exposed functions (toPng, toJpeg, toSvg, toCanvas, toBlob, toPixelData) that accept a DOM node and options.
- DOM Cloning Module: Shows the process of recursively cloning the DOM tree including the adjustment of nodes based on any provided “adjustClone” hook.
- Style & Asset Processing Module:
- CSS Processing: Computing and inlining computed styles plus handling of pseudo-elements.
- Asset Embedding: Sub-module for web fonts (downloading, base64 conversion, CSS injection) and image inlining.
- Serialization & SVG Wrapping Module: Serializes the processed clone to XML and embeds that XML inside a <foreignObject> within an SVG.
- Canvas Rendering Module: For cases where a raster image is needed, the SVG is rendered onto an off-screen canvas. This module handles drawing the SVG into a canvas, pixel extraction, and quality/scaling settings.
- Error Handling & Promises: Since the API returns promises, include an element for asynchronous processing and error logging.

b. Relationships and Interactions:
- From the API Layer, a direct flow into the DOM Cloning Module.
- Finally, outputs (data URL, Blob, or Image) return through the API Layer back to the caller.

c. Architectural Patterns and Technologies:
- Usage of promises and asynchronous processing for network operations (like asset fetching).
- Inlining technique leveraging SVG’s <foreignObject> for complex HTML rendering.
- That the design uses dependency injection for options (filter, style copying, quality, etc.) which allow for customization.
- External dependencies (if any) like FileSaver.js for blob saving and ocrad.js in tests.

d. Relevant Technologies/Frameworks:
- JavaScript (ES5/ES6) with modular coding practices.
- Browser APIs for canvas, SVG, and DOM manipulation.
- Testing frameworks indicated by Karma and Grunt for build automation.

## Diagram (Mermaid)

```mermaid
graph TD
    A["Caller/Client"]:::client
    B["API Layer (toPng,toJpeg,toSvg,toCanvas,toBlob,toPixelData)"]:::api
    G["Hooks (onclone,adjustClonedNode)"]:::hook
    C["DOM Cloning Module"]:::core
    D["Style & Asset Processing (CSS Inliner & Asset Embedding)"]:::core
    E["Serialization & SVG Wrapper"]:::core
    F["Canvas Rendering Module"]:::core
    H["Output: SVG/XML"]:::output
    I["Output: Raster Image (PNG/JPEG/Blob)"]:::output
    J["Async Handling & Error Logging"]:::async

    subgraph "External and Test Resources"
        K["Test Resources (Rendering Cases)"]:::test
        L["Web Font Resources"]:::external
        M["OCR Functionality (for tests)"]:::external
    end

    %% Main Processing Flow
    A --> B
    B -.-> G
    G -.-> C
    B --> C
    C --> D
    D --> E
    E --> F
    E --> H
    F --> I
    H --> B
    I --> B
    B --> A
    B -.-> J

    %% External Dependencies to Processing Modules (dotted arrows)
    L -.-> D
    M -.-> D
    K -.-> B

    %% Click Events
    click B "https://github.com/1904labs/dom-to-image-more/blob/main/src/dom-to-image-more.js"
    click C "https://github.com/1904labs/dom-to-image-more/blob/main/src/dom-to-image-more.js"
    click D "https://github.com/1904labs/dom-to-image-more/blob/main/src/dom-to-image-more.js"
    click E "https://github.com/1904labs/dom-to-image-more/blob/main/src/dom-to-image-more.js"
    click F "https://github.com/1904labs/dom-to-image-more/blob/main/src/dom-to-image-more.js"
    click K "https://github.com/1904labs/dom-to-image-more/tree/main/spec"
    click L "https://github.com/1904labs/dom-to-image-more/tree/main/test-lib/fontawesome"
    click M "https://github.com/1904labs/dom-to-image-more/tree/main/test-lib/ocrad-bower"

    %% Styles
    classDef client fill:#f8c,stroke:#b300b3,stroke-width:2px;
    classDef api fill:#ffcc00,stroke:#cc9900,stroke-width:2px;
    classDef core fill:#b3e6ff,stroke:#0066cc,stroke-width:2px;
    classDef hook fill:#d1ffd1,stroke:#339933,stroke-width:2px;
    classDef async fill:#ffe6cc,stroke:#ff9933,stroke-width:2px;
    classDef output fill:#cce6ff,stroke:#3399ff,stroke-width:2px;
    classDef test fill:#e6ccff,stroke:#9933ff,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#ffcc00,stroke-width:2px;
```
