# niklasvh/html2canvas architecture

> Architecture diagram of the GitHub repository niklasvh/html2canvas, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/niklasvh/html2canvas
- Repository: https://github.com/niklasvh/html2canvas

## Overview

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`](https://github.com/niklasvh/html2canvas/blob/master/src/index.ts) (html2canvas API), [`src/dom/node-parser.ts`](https://github.com/niklasvh/html2canvas/blob/master/src/dom/node-parser.ts) (DOM tree parser), [`src/dom/element-container.ts`](https://github.com/niklasvh/html2canvas/blob/master/src/dom/element-container.ts) (Element and text containers), [`src/css/index.ts`](https://github.com/niklasvh/html2canvas/blob/master/src/css/index.ts) (CSS declarations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["API and context"]
  node_api["html2canvas API<br/>[index.ts]"]
  node_context["Render context<br/>[context.ts]"]
end

subgraph group_dom["DOM preparation"]
  node_clone["Document cloner<br/>[document-cloner.ts]"]
  node_tree["DOM tree parser<br/>[node-parser.ts]"]
  node_containers["Element and text containers"]
  node_replaced["Replaced elements"]
end

subgraph group_css["CSS interpretation"]
  node_cssstyles["CSS declarations<br/>[index.ts]"]
  node_cssparser["CSS value parser<br/>[parser.ts]"]
  node_tokenizer["CSS tokenizer<br/>[tokenizer.ts]"]
  node_types["CSS value types"]
  node_bounds["Layout bounds<br/>[bounds.ts]"]
end

subgraph group_render["Rendering"]
  node_stacking["Stacking contexts"]
  node_canvas["Canvas renderer<br/>[canvas-renderer.ts]"]
  node_foreign["Foreign-object renderer"]
end

subgraph group_resources["Resources"]
  node_cache["Image resource cache<br/>[cache-storage.ts]"]
  node_image["Browser image loading"]
  node_proxy["Configured proxy"]
end

node_caller(("Application caller"))
node_result["Canvas result"]

node_caller -->|"calls"| node_api
node_api -->|"creates"| node_context
node_api -->|"clones DOM"| node_clone
node_api -->|"parses DOM"| node_tree
node_api -->|"renders canvas"| node_canvas
node_api -->|"selects renderer"| node_foreign
node_clone -->|"parses cloned nodes"| node_tree
node_tree -->|"builds containers"| node_containers
node_tree -->|"dispatches elements"| node_replaced
node_containers -->|"parses styles"| node_cssstyles
node_containers -->|"measures bounds"| node_bounds
node_cssstyles -->|"parses values"| node_cssparser
node_cssparser -->|"tokenizes input"| node_tokenizer
node_cssstyles -->|"uses value types"| node_types
node_stacking -->|"orders elements"| node_containers
node_stacking -->|"computes paint paths"| node_bounds
node_canvas -->|"paints contexts"| node_stacking
node_api -->|"loads resources"| node_cache
node_cache -->|"loads images"| node_image
node_cache -.->|"fetches through"| node_proxy
node_api -->|"returns canvas"| node_result

click node_api "https://github.com/niklasvh/html2canvas/blob/master/src/index.ts"
click node_context "https://github.com/niklasvh/html2canvas/blob/master/src/core/context.ts"
click node_clone "https://github.com/niklasvh/html2canvas/blob/master/src/dom/document-cloner.ts"
click node_tree "https://github.com/niklasvh/html2canvas/blob/master/src/dom/node-parser.ts"
click node_containers "https://github.com/niklasvh/html2canvas/blob/master/src/dom/element-container.ts"
click node_replaced "https://github.com/niklasvh/html2canvas/tree/master/src/dom/replaced-elements"
click node_cssstyles "https://github.com/niklasvh/html2canvas/blob/master/src/css/index.ts"
click node_cssparser "https://github.com/niklasvh/html2canvas/blob/master/src/css/syntax/parser.ts"
click node_tokenizer "https://github.com/niklasvh/html2canvas/blob/master/src/css/syntax/tokenizer.ts"
click node_types "https://github.com/niklasvh/html2canvas/tree/master/src/css/types"
click node_bounds "https://github.com/niklasvh/html2canvas/blob/master/src/css/layout/bounds.ts"
click node_stacking "https://github.com/niklasvh/html2canvas/blob/master/src/render/stacking-context.ts"
click node_canvas "https://github.com/niklasvh/html2canvas/blob/master/src/render/canvas/canvas-renderer.ts"
click node_foreign "https://github.com/niklasvh/html2canvas/blob/master/src/render/canvas/foreignobject-renderer.ts"
click node_cache "https://github.com/niklasvh/html2canvas/blob/master/src/core/cache-storage.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_context toneBlue
class node_clone,node_tree,node_containers,node_replaced toneAmber
class node_cssstyles,node_cssparser,node_tokenizer,node_types,node_bounds toneMint
class node_stacking,node_canvas,node_foreign toneRose
class node_cache,node_image,node_proxy,node_caller,node_result toneIndigo
```

## Components

### API and context

- [html2canvas API](https://github.com/niklasvh/html2canvas/blob/master/src/index.ts): `src/index.ts`
- [Render context](https://github.com/niklasvh/html2canvas/blob/master/src/core/context.ts): `src/core/context.ts`

### DOM preparation

- [Document cloner](https://github.com/niklasvh/html2canvas/blob/master/src/dom/document-cloner.ts): `src/dom/document-cloner.ts`
- [DOM tree parser](https://github.com/niklasvh/html2canvas/blob/master/src/dom/node-parser.ts): `src/dom/node-parser.ts`
- [Element and text containers](https://github.com/niklasvh/html2canvas/blob/master/src/dom/element-container.ts): `src/dom/element-container.ts`
- [Replaced elements](https://github.com/niklasvh/html2canvas/tree/master/src/dom/replaced-elements): `src/dom/replaced-elements`

### CSS interpretation

- [CSS declarations](https://github.com/niklasvh/html2canvas/blob/master/src/css/index.ts): `src/css/index.ts`
- [CSS value parser](https://github.com/niklasvh/html2canvas/blob/master/src/css/syntax/parser.ts): `src/css/syntax/parser.ts`
- [CSS tokenizer](https://github.com/niklasvh/html2canvas/blob/master/src/css/syntax/tokenizer.ts): `src/css/syntax/tokenizer.ts`
- [CSS value types](https://github.com/niklasvh/html2canvas/tree/master/src/css/types): `src/css/types`
- [Layout bounds](https://github.com/niklasvh/html2canvas/blob/master/src/css/layout/bounds.ts): `src/css/layout/bounds.ts`

### Rendering

- [Stacking contexts](https://github.com/niklasvh/html2canvas/blob/master/src/render/stacking-context.ts): `src/render/stacking-context.ts`
- [Canvas renderer](https://github.com/niklasvh/html2canvas/blob/master/src/render/canvas/canvas-renderer.ts): `src/render/canvas/canvas-renderer.ts`
- [Foreign-object renderer](https://github.com/niklasvh/html2canvas/blob/master/src/render/canvas/foreignobject-renderer.ts): `src/render/canvas/foreignobject-renderer.ts`

### Resources

- [Image resource cache](https://github.com/niklasvh/html2canvas/blob/master/src/core/cache-storage.ts): `src/core/cache-storage.ts`
- **Browser image loading**
- **Configured proxy**

### Other components

- **Application caller**
- **Canvas result**

## Connections

- Application caller → html2canvas API: calls
- html2canvas API → Render context: creates (evidence: `src/index.ts`)
- html2canvas API → Document cloner: clones DOM (evidence: `src/index.ts`)
- html2canvas API → DOM tree parser: parses DOM (evidence: `src/index.ts`)
- html2canvas API → Canvas renderer: renders canvas (evidence: `src/index.ts`)
- html2canvas API → Foreign-object renderer: selects renderer (evidence: `src/index.ts`)
- Document cloner → DOM tree parser: parses cloned nodes (evidence: `src/dom/document-cloner.ts`)
- DOM tree parser → Element and text containers: builds containers (evidence: `src/dom/node-parser.ts`)
- DOM tree parser → Replaced elements: dispatches elements (evidence: `src/dom/node-parser.ts`)
- Element and text containers → CSS declarations: parses styles (evidence: `src/dom/element-container.ts`)
- Element and text containers → Layout bounds: measures bounds (evidence: `src/dom/element-container.ts`)
- CSS declarations → CSS value parser: parses values (evidence: `src/css/index.ts`)
- CSS value parser → CSS tokenizer: tokenizes input (evidence: `src/css/syntax/parser.ts`)
- CSS declarations → CSS value types: uses value types (evidence: `src/css/index.ts`)
- Stacking contexts → Element and text containers: orders elements (evidence: `src/render/stacking-context.ts`)
- Stacking contexts → Layout bounds: computes paint paths (evidence: `src/render/stacking-context.ts`)
- Canvas renderer → Stacking contexts: paints contexts (evidence: `src/render/canvas/canvas-renderer.ts`)
- html2canvas API → Image resource cache: loads resources (evidence: `src/index.ts`)
- Image resource cache → Browser image loading: loads images (evidence: `src/core/cache-storage.ts`)
- Image resource cache → Configured proxy: fetches through (evidence: `src/core/cache-storage.ts`)
- html2canvas API → Canvas result: returns canvas
