How niklasvh/html2canvas works
html2canvas is a client-side JavaScript renderer that reconstructs a DOM element as a canvas rather than taking a browser screenshot. The primary workflow is a caller’s element entering the public API, being cloned and parsed into styled containers, then organized into paint order and drawn to a canvas returned by Promise. CSS parsing, resource loading, and the alternate foreign-object renderer are also represented. Relationships reflect the README and shown or indexed source references; several implementation details are unsampled, so uncertain links are omitted.
Main entry points: src/index.ts html2canvas API · node-parser.ts DOM tree parser · element-container.ts Element and text containers · css/index.ts CSS declarations
Components 19
API and context
- html2canvas API
src/index.ts - Render context
src/core/context.ts
DOM preparation
- Document cloner
src/dom/document-cloner.ts - DOM tree parser
src/dom/node-parser.ts - Element and text containers
src/dom/element-container.ts - Replaced elements
src/dom/replaced-elements
CSS interpretation
- CSS declarations
src/css/index.ts - CSS value parser
src/css/syntax/parser.ts - CSS tokenizer
src/css/syntax/tokenizer.ts - CSS value types
src/css/types - Layout bounds
src/css/layout/bounds.ts
Rendering
- Stacking contexts
src/render/stacking-context.ts - Canvas renderer
src/render/canvas/canvas-renderer.ts - Foreign-object renderer
src/render/canvas/foreignobject-renderer.ts
Resources
- Image resource cache
src/core/cache-storage.ts - Browser image loading
- Configured proxy
Other components
- Application caller
- Canvas result
Connections 21
- Application caller → html2canvas API: calls
- html2canvas API → Render context: creates
src/index.ts - html2canvas API → Document cloner: clones DOM
src/index.ts - html2canvas API → DOM tree parser: parses DOM
src/index.ts - html2canvas API → Canvas renderer: renders canvas
src/index.ts - html2canvas API → Foreign-object renderer: selects renderer
src/index.ts - Document cloner → DOM tree parser: parses cloned nodes
src/dom/document-cloner.ts - DOM tree parser → Element and text containers: builds containers
src/dom/node-parser.ts - DOM tree parser → Replaced elements: dispatches elements
src/dom/node-parser.ts - Element and text containers → CSS declarations: parses styles
src/dom/element-container.ts - Element and text containers → Layout bounds: measures bounds
src/dom/element-container.ts - CSS declarations → CSS value parser: parses values
src/css/index.ts - CSS value parser → CSS tokenizer: tokenizes input
src/css/syntax/parser.ts - CSS declarations → CSS value types: uses value types
src/css/index.ts - Stacking contexts → Element and text containers: orders elements
src/render/stacking-context.ts - Stacking contexts → Layout bounds: computes paint paths
src/render/stacking-context.ts - Canvas renderer → Stacking contexts: paints contexts
src/render/canvas/canvas-renderer.ts - html2canvas API → Image resource cache: loads resources
src/index.ts - Image resource cache → Browser image loading: loads images
src/core/cache-storage.ts - Image resource cache → Configured proxy: fetches through
src/core/cache-storage.ts - html2canvas API → Canvas result: returns canvas