How bubkoo/html-to-image works
Project Type and Purpose
• This is an open‐source TypeScript library (“html‐to‐image”) that converts a given DOM node into an image (PNG, JPEG, SVG, Blob, Canvas element, or raw pixel data).
• It is packaged as an NPM module, bundled via Rollup, tested with Karma, and continuously integrated/deployed via GitHub Actions.
File Structure Analysis
• /src: core pipeline modules (cloning nodes, applying styles, embedding fonts/images, serializing to SVG, generating data URLs).
• /test: resource fixtures (HTML/CSS/media) and test specs.
• rollup.config.js & karma.conf.js: build and test configurations.
• package.json, tsconfig.json: package metadata and TypeScript compiler options.
• .github/workflows: CI pipelines for build, tests, coverage, releases.
Key README Insights
• Describes a linear pipeline: clone DOM → compute and inline styles (including pseudo‐elements) → embed fonts & images → serialize to SVG inside a <foreignObject> → render on canvas for raster outputs.
• Exposes multiple top‐level functions (toPng, toSvg, toJpeg, toBlob, toCanvas, toPixelData) returning Promises.
Main Components
a. Input Layer
• “User Code” calling library functions with a DOM node and options.
b. Core Pipeline Modules (in /src)
1. clone-node.ts & clone-pseudos.ts
2. apply-style.ts
3. embed-webfonts.ts
4. embed-images.ts & embed-resources.ts
5. dataurl.ts & util.ts (serialization helpers)
c. Output Generators
• toSvg: wraps serialized XML in SVG → returns SVG data URL.
• toCanvas: creates <img> from SVG URL → draws on off-screen <canvas>.
• toPng/toJpeg/toBlob: reads canvas and exports desired format.
• toPixelData: reads pixel buffer from canvas.
d. External Services/APIs
• Browser DOM & CSSOM
• Fetch API for fonts/images
• Canvas & Image HTML5 APIs
e. Build & CI Infrastructure
• Rollup bundling → npm package
• Karma + Jasmine + ts‐node tests → code coverage
• GitHub Actions workflows for CI, code scanning, and publishing
Relationships & Data Flow
• Within Core Pipeline: Clone → Style → Embed Fonts → Embed Images → Serialize → Output Generator
• Output Generator → return Promise<DataURL|Blob|Canvas|Uint8Array>
• Build/Test: Source modules → Rollup → Bundled JS → Package Registry; Source + Tests → Karma → CI → GitHub Actions
Architectural Patterns & Principles
• Pipeline/Chain of Responsibility: each module performs one transformation step.
• Separation of Concerns: cloning, styling, embedding, serialization handled by distinct modules.
• Promise‐based async flow for network I/O (fonts, images).
• Modular, pluggable via top‐level functions that orchestrate the pipeline.