@zumer/snapdom 0.9.2 β†’ 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,9 +1,24 @@
1
1
  # snapDOM
2
2
 
3
- **snapDOM** is a high-fidelity DOM capture tool, developed as part of the animation engine I'm building 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`
@@ -47,7 +62,7 @@ import { snapdom } from './snapdom.mjs';
47
62
 
48
63
  ### Script Tag (Type Module)
49
64
 
50
- ```javascript
65
+ ```html
51
66
  <script type="module">
52
67
  import { snapdom } from 'https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.mjs';
53
68
  </script>
@@ -55,53 +70,88 @@ import { snapdom } from './snapdom.mjs';
55
70
 
56
71
  Now you can call `snapdom(el)`, `snapdom.toPng(el)`, etc., directly in your JavaScript.
57
72
 
58
-
59
73
  ## Basic usage
60
74
 
61
75
  ```javascript
62
- // Capture an element as SVG Data URL
63
- 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);
64
79
 
65
- // Insert the captured image into the page
66
- const img = new Image();
67
- img.src = svgDataUrl;
68
80
  document.body.appendChild(img);
69
81
  ```
70
82
 
71
-
72
83
  ## API
73
84
 
74
85
  ### Note: API may evolve until v1.0.0!
75
86
 
76
87
  The main API is exposed as `snapdom` and offers multiple capture methods:
77
88
 
78
- | Method | Description | Returns |
79
- |:----------------------|:---------------------------------------|:-------------------------|
80
- | `snapdom(el, options?)` | Captures as SVG Data URL | `Promise<string>` |
81
- | `snapdom.toImg(el, options?)` | Captures as `HTMLImageElement` (SVG) | `Promise<HTMLImageElement>` |
82
- | `snapdom.toCanvas(el, options?)` | Captures as `HTMLCanvasElement` | `Promise<HTMLCanvasElement>` |
83
- | `snapdom.toPng(el, options?)` | Captures as PNG image (`Image`) | `Promise<HTMLImageElement>` |
84
- | `snapdom.toJpg(el, options?)` | Captures as JPG image (`Image`) | `Promise<HTMLImageElement>` |
85
- | `snapdom.toWebp(el, options?)` | Captures as WebP image (`Image`) | `Promise<HTMLImageElement>` |
86
- | `snapdom.toBlob(el, options?)` | Captures as SVG `Blob` | `Promise<Blob>` |
87
-
88
- **Options:**
89
- - `scale` *(number)*: Scale factor (default is `1`)
90
- - `quality` *(number)*: Compression quality for JPG/WebP (0–1)
91
- - `backgroundColor` *(string)*: Background fill for JPG/WebP exports
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 |
99
+
100
+ ---
101
+
102
+ ### Options
103
+
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`. |
92
114
 
93
115
  ---
94
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
+
95
144
  ## Special features
96
145
 
97
- - **Shadow DOM**: Captures content inside Web Components and `shadowRoot`.
98
- - **Pseudo-elements**: Captures `::before` and `::after`, including background images.
99
- - **Backgrounds and images**: Inlines external images as Data URLs.
100
- - **Fonts**: Replicates applied font families without requiring external font files.
101
- - **Icon fonts**: Captures icon fonts like **Font Awesome** and **Material Icons**.
102
- - **Placeholder and Exclusion**:
103
- - `data-capture="exclude"`: Skips an element while preserving layout space.
104
- - `data-capture="placeholder"` + `data-placeholder-text="Text"`: Replaces an element with placeholder text.
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.
105
155
 
106
156
  **Now with improved fidelity and even faster performance.**
107
157
 
@@ -121,39 +171,42 @@ The main API is exposed as `snapdom` and offers multiple capture methods:
121
171
  const button = document.getElementById('captureBtn');
122
172
  button.addEventListener('click', async () => {
123
173
  const target = document.getElementById('captureMe');
124
- const img = await snapdom.toImg(target);
174
+ const img = await snapdom.toJpg(target, { quality: 0.5});
125
175
  document.body.appendChild(img);
126
176
  });
127
177
  </script>
128
178
  ```
129
179
 
130
-
131
180
  ## Limitations
132
181
 
133
- - External images must be CORS-accessible.
134
- - Fonts must be fully loaded before capturing (`document.fonts.ready` is automatically awaited).
135
- - Iframes are not captured.
182
+ * External images must be CORS-accessible.
183
+ * Iframes are not captured.
136
184
 
185
+ ### Please if you find a bug **open an issue**.
137
186
 
138
187
  ## Benchmarks
139
188
 
140
- `snapDOM` is not only highly accurate β€” it’s **extremely fast**, especially for large elements.
189
+ `snapDOM` is not only highly accurate β€” it’s **extremely fast**.
190
+
141
191
 
142
- Latest benchmarks show **major speed improvements** over existing libraries:
192
+ Latest benchmarks show significant performance improvements against other libraries:
143
193
 
144
- | Element Size | vs. `modern-screenshot` | vs. `html2canvas` |
145
- |:------------------------:|:-----------------------:|:-----------------:|
146
- | **Small (200Γ—100)** | 9.84Γ— faster | 33.19Γ— faster |
147
- | **Modal (400Γ—300)** | 11.72Γ— faster | 36.03Γ— faster |
148
- | **Page view (1200Γ—800)** | 24.40Γ— faster | 55.65Γ— faster |
149
- | **Large (2000Γ—1500)** | 49.85Γ— faster | 90.74Γ— faster |
150
- | **Very large (4000Γ—2000)** | 138.93Γ— faster | 148.41Γ— faster |
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 |
151
206
 
152
- βœ… **snapDOM** also improved fidelity, even on complex layouts:
153
- - Up to **7.82Γ— faster** for complex modal captures.
154
- - Still faster even at very large sizes.
155
207
 
156
208
  ### Run the benchmarks
209
+
157
210
  To run these benchmarks yourself:
158
211
 
159
212
  ```sh
@@ -165,7 +218,7 @@ npm run test:benchmark
165
218
 
166
219
  They execute in **headless Chromium** using real DOM nodes.
167
220
 
168
-
169
221
  ## License
170
222
 
171
223
  MIT Β© Zumerlab
224
+