@zumer/snapdom 0.9.1 β†’ 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,29 +1,51 @@
1
1
  # snapDOM
2
2
 
3
- **snapDOM** is a high-fidelity DOM capture tool, developed as part of the animation engine for Zumly β€” a framework for creating smooth zoom-based view transitions.
3
+
4
+ <p align="center">
5
+ <a href="http://zumerlab.github.io/orbit-docs">
6
+ <img src="https://raw.githubusercontent.com/zumerlab/snapdom/main/docs/assets/hero.png" width="80%">
7
+ </a>
8
+ </p>
9
+
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/@zumer/snapdom"><img src="https://img.shields.io/github/package-json/v/zumerlab/snapdom"></a>
13
+ </p>
14
+
15
+
16
+
17
+ **snapDOM** is a fast and accurate DOM capture tool to images developed for **Zumly**, a WIP framework that enables zoom-based view transitions.
4
18
 
5
19
  It converts any HTML element into a scalable SVG image, preserving styles, fonts, backgrounds, shadow DOM content, pseudo-elements, and more.
6
20
 
21
+
7
22
  - πŸ“Έ Full DOM capture
8
23
  - 🎨 Embedded styles, pseudo-elements, and fonts
9
24
  - πŸ–ΌοΈ Export to SVG, PNG, JPG, WebP, or `canvas`
10
- - ⚑ Lightweight, no dependencies
25
+ - ⚑ Ultra fast, no dependencies
11
26
  - πŸ“¦ 100% based on standard Web APIs
12
27
 
13
- ---
14
28
 
15
29
  ## Installation
16
30
 
17
- You can use **snapDOM** by including it via **CDN**, **script tag**, or by **importing it as a module**.
31
+ You can use **snapDOM** via **NPM**, **CDN**, **script tag**, or by **importing as a module**.
32
+
33
+ ### NPM / Yarn
34
+
35
+ ```sh
36
+ npm i @zumer/snapdom
37
+ ```
38
+
39
+ ```sh
40
+ yarn add @zumer/snapdom
41
+ ```
18
42
 
19
43
  ### CDN
20
44
 
21
45
  ```html
22
- <script src="https://unpkg.com/@zumer/snapdom@latest/snapdom.min.js"></script>
46
+ <script src="https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.min.js"></script>
23
47
  ```
24
48
 
25
- ---
26
-
27
49
  ### Script tag (local)
28
50
 
29
51
  ```html
@@ -32,8 +54,6 @@ You can use **snapDOM** by including it via **CDN**, **script tag**, or by **imp
32
54
 
33
55
  The global object `snapdom` will be available.
34
56
 
35
- ---
36
-
37
57
  ### ES Module
38
58
 
39
59
  ```javascript
@@ -42,63 +62,99 @@ import { snapdom } from './snapdom.mjs';
42
62
 
43
63
  ### Script Tag (Type Module)
44
64
 
45
- ```
65
+ ```html
46
66
  <script type="module">
47
- import { snapdom } from 'https://unpkg.com/@zumer/snapdom@latest/snapdom.mjs';
67
+ import { snapdom } from 'https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs';
48
68
  </script>
49
69
  ```
50
70
 
51
71
  Now you can call `snapdom(el)`, `snapdom.toPng(el)`, etc., directly in your JavaScript.
52
72
 
53
- ---
54
-
55
73
  ## Basic usage
56
74
 
57
75
  ```javascript
58
- // Capture an element as SVG Data URL
59
- const svgDataUrl = await snapdom(document.querySelector("#myElement"));
76
+ // Capture an element as <img> tag
77
+ const el = document.querySelector("#myElement")
78
+ const img = await snapdom.toImg(el);
60
79
 
61
- // Insert the captured image into the page
62
- const img = new Image();
63
- img.src = svgDataUrl;
64
80
  document.body.appendChild(img);
65
81
  ```
66
82
 
67
- ---
68
-
69
83
  ## API
70
84
 
85
+ ### Note: API may evolve until v1.0.0!
86
+
71
87
  The main API is exposed as `snapdom` and offers multiple capture methods:
72
88
 
73
- | Method | Description | Returns |
74
- |:-------|:------------|:--------|
75
- | `snapdom(el, scale?)` | Captures as SVG Data URL | `Promise<string>` |
76
- | `snapdom.toImg(el, scale?)` | Captures as `HTMLImageElement` (SVG) | `Promise<HTMLImageElement>` |
77
- | `snapdom.toCanvas(el, scale?)` | Captures as `HTMLCanvasElement` | `Promise<HTMLCanvasElement>` |
78
- | `snapdom.toPng(el, scale?)` | Captures as PNG image (`Image`) | `Promise<HTMLImageElement>` |
79
- | `snapdom.toJpg(el, scale?, quality?)` | Captures as JPG image (`Image`) | `Promise<HTMLImageElement>` |
80
- | `snapdom.toWebp(el, scale?, quality?)` | Captures as WebP image (`Image`) | `Promise<HTMLImageElement>` |
81
- | `snapdom.toBlob(el, scale?)` | Captures as SVG `Blob` | `Promise<Blob>` |
82
-
83
- **Parameters:**
84
- - `el`: DOM element to capture.
85
- - `scale`: Scale factor (default is `1`).
86
- - `quality`: Compression quality for JPG/WebP (range `0`–`1`).
89
+ | Method | Description |
90
+ | :------------------------------- | :------------------------------------------------------------- |
91
+ | `snapdom(el, options?)` | Captures as SVG Data URL |
92
+ | `snapdom.toImg(el, options?)` | Captures as `HTMLImageElement` (SVG) |
93
+ | `snapdom.toCanvas(el, options?)` | Captures as `HTMLCanvasElement` |
94
+ | `snapdom.toPng(el, options?)` | Captures as PNG image (`Image`) |
95
+ | `snapdom.toJpg(el, options?)` | Captures as JPG image (`Image`) |
96
+ | `snapdom.toWebp(el, options?)` | Captures as WebP image (`Image`) |
97
+ | `snapdom.toBlob(el, options?)` | Captures as SVG `Blob` |
98
+ | `preCache(root?, options?)` | Preload resources for faster and more accurate captures |
87
99
 
88
100
  ---
89
101
 
90
- ## Special features
102
+ ### Options
91
103
 
92
- - **Shadow DOM**: Captures content inside Web Components and `shadowRoot`.
93
- - **Pseudo-elements**: Captures `::before` and `::after`, including background images.
94
- - **Backgrounds and images**: Inlines external images as Data URLs.
95
- - **Fonts**: Replicates applied font families without needing external font files.
96
- - **Placeholder and Exclusion**:
97
- - `data-capture="exclude"`: Skips an element while preserving layout space.
98
- - `data-capture="placeholder"` + `data-placeholder-text="Text"`: Replaces an element with decorative placeholder text.
104
+ You can pass an `options` object to control the behavior of all capture methods:
105
+
106
+ | Option | Type | Default | Description |
107
+ | ----------------- | --------- | -------- | ----------------------------------------------------------------------------------- |
108
+ | `compress` | `boolean` | `true` | Removes default styles to reduce size and deduplicate CSS where possible. |
109
+ | `fast` | `boolean` | `true` | Skips `requestIdleCallback` delays and executes preparation immediately. |
110
+ | `embedFonts` | `boolean` | `false` | Inlines external fonts as Data URLs (except icon fonts, which are always embedded). |
111
+ | `scale` | `number` | `1` | Multiplies the output resolution without affecting layout size. |
112
+ | `backgroundColor` | `string` | `"#fff"` | Used when exporting to JPG or WebP formats (which don't support transparency). |
113
+ | `quality` | `number` | `1` | Compression quality for JPG/WebP, between `0` and `1`. |
99
114
 
100
115
  ---
101
116
 
117
+ ### `preCache()` – Optional helper
118
+
119
+ The `preCache()` function can be used to load external resources (like images and fonts) in advance.
120
+
121
+ ```js
122
+ import { preCache } from '@zumer/snapdom';
123
+
124
+ await preCache(document.body, {
125
+ embedFonts: true,
126
+ preWarm: true
127
+ });
128
+ ```
129
+
130
+ ```js
131
+ import { snapdom, preCache } from './snapdom.mjs';
132
+ window.addEventListener('load', async () => {
133
+ await preCache();
134
+ console.log('πŸ“¦ Resources preloaded');
135
+ });
136
+ ```
137
+
138
+ **Options for `preCache()`:**
139
+
140
+ * `embedFonts` *(boolean, default: true)* β€” Inlines non-icon fonts during preload.
141
+ * `reset` *(boolean, default: false)* β€” Clears all existing internal caches.
142
+ * `preWarm` *(boolean, default: true)* β€” Runs a fake capture in memory to warm up styles, fonts and layout detection.
143
+
144
+ ## Special features
145
+
146
+ * **Shadow DOM**: Captures content inside Web Components and `shadowRoot`.
147
+ * **Pseudo-elements**: Captures `::before` and `::after`, including background images.
148
+ * **Backgrounds and images**: Inlines external images as Data URLs.
149
+ * **Fonts**: Replicates applied font families without requiring external font files.
150
+ * **Icon fonts**: Captures icon fonts like **Font Awesome** and **Material Icons**.
151
+ * **Placeholder and Exclusion**:
152
+
153
+ * `data-capture="exclude"`: Skips an element while preserving layout space.
154
+ * `data-capture="placeholder"` + `data-placeholder-text="Text"`: Replaces an element with placeholder text.
155
+
156
+ **Now with improved fidelity and even faster performance.**
157
+
102
158
  ## Full example
103
159
 
104
160
  ```html
@@ -107,53 +163,62 @@ The main API is exposed as `snapdom` and offers multiple capture methods:
107
163
  <p>This content will be captured.</p>
108
164
  </div>
109
165
 
166
+ <button id="captureBtn">Capture as img</button>
167
+
110
168
  <script type="module">
111
- import { snapdom } from './snapdom.esm.js';
169
+ import { snapdom } from './snapdom.mjs';
112
170
 
113
- const button = document.createElement('button');
114
- button.textContent = "Capture";
115
- button.onclick = async () => {
116
- const img = await snapdom.toPng(document.getElementById('captureMe'), 2);
171
+ const button = document.getElementById('captureBtn');
172
+ button.addEventListener('click', async () => {
173
+ const target = document.getElementById('captureMe');
174
+ const img = await snapdom.toJpg(target, { quality: 0.5});
117
175
  document.body.appendChild(img);
118
- };
119
- document.body.appendChild(button);
176
+ });
120
177
  </script>
121
178
  ```
122
179
 
123
- ---
124
-
125
180
  ## Limitations
126
181
 
127
- - External images must be CORS-accessible.
128
- - Fonts must be fully loaded before capturing (`document.fonts.ready` is automatically awaited).
129
- - Iframes are not captured.
182
+ * External images must be CORS-accessible.
183
+ * Iframes are not captured.
130
184
 
131
- ---
185
+ ### Please if you find a bug **open an issue**.
132
186
 
133
- ## Benchmark
187
+ ## Benchmarks
134
188
 
135
- `snapDOM` is not only highly accurate β€” it's also **extremely fast** at capturing large or complex DOM structures.
189
+ `snapDOM` is not only highly accurate β€” it’s **extremely fast**.
136
190
 
137
- In benchmark tests against popular libraries:
138
191
 
139
- | Element Size | Winner | Compared to `modern-screenshot` | Compared to `html2canvas` |
140
- |:------------:|:------:|:-------------------------------:|:-------------------------:|
141
- | 200Γ—100 (Small) | `modern-screenshot` | 1.18Γ— faster | 4.46Γ— faster |
142
- | 400Γ—300 (Modal) | `snapDOM` | 1.04Γ— faster | 4.07Γ— faster |
143
- | 1200Γ—800 (Page view) | `snapDOM` | 2.43Γ— faster | 5.74Γ— faster |
144
- | 2000Γ—1500 (Large scroll area) | `snapDOM` | 5.02Γ— faster | 9.35Γ— faster |
145
- | 4000Γ—2000 (Very large) | `snapDOM` | 11.35Γ— faster | 15.98Γ— faster |
192
+ Latest benchmarks show significant performance improvements against other libraries:
146
193
 
147
- βœ… **Key insight**:
148
- While `modern-screenshot` is yet slightly faster for very small elements, **snapDOM dramatically outperforms all others as the DOM size grows**.
194
+ | Scenario | vs. `modern-screenshot` | vs. `html2canvas` |
195
+ | -------------------------------- | :---------------------: | :---------------: |
196
+ | Small element (200Γ—100) | 6.46Γ— faster | 32.27Γ— faster |
197
+ | Modal size (400Γ—300) | 7.28Γ— faster | 32.66Γ— faster |
198
+ | Page view (1200Γ—800) | 13.17Γ— faster | 35.29Γ— faster |
199
+ | Large scroll area (2000Γ—1500) | 38.23Γ— faster | 68.85Γ— faster |
200
+ | Very large element (4000Γ—2000) | 93.31Γ— faster | 133.12Γ— faster |
201
+ | Complex small element (200Γ—100) | 3.97Γ— faster | 15.23Γ— faster |
202
+ | Complex modal (400Γ—300) | 2.32Γ— faster | 5.33Γ— faster |
203
+ | Complex page (1200Γ—800) | 1.62Γ— faster | 1.65Γ— faster |
204
+ | Complex large scroll (2000Γ—1500) | 1.66Γ— faster | 1.24Γ— faster |
205
+ | Complex very large (4000Γ—2000) | 1.52Γ— faster | 1.28Γ— faster |
149
206
 
150
- βœ… **Perfect for:**
151
- - Capturing full-page views
152
- - Capturing modal windows
153
- - Complex layouts with custom fonts, backgrounds, or shadow DOM
154
207
 
155
- ---
208
+ ### Run the benchmarks
209
+
210
+ To run these benchmarks yourself:
211
+
212
+ ```sh
213
+ git clone https://github.com/zumerlab/snapdom.git
214
+ cd snapdom
215
+ npm install
216
+ npm run test:benchmark
217
+ ```
218
+
219
+ They execute in **headless Chromium** using real DOM nodes.
156
220
 
157
221
  ## License
158
222
 
159
- MIT Β© Juan MartΓ­n Muda - Zumerlab
223
+ MIT Β© Zumerlab
224
+